view example/bm_search/ppe/Exec.cc @ 2064:f786ffb2a198 draft

add bm.h
author Nozomi
date Wed, 17 Feb 2016 17:37:06 +0900
parents a8f4227d6a21
children
line wrap: on
line source

#include <stdio.h>
#include <string.h>
#include "Exec.h"
#include "Func.h"
#include "SchedTask.h"
#include "FileMapReduce.h"
#include "bm.h"
#define max(a,b)((a)>(b)?a:b) 

/* これは必須 */
SchedDefineTask1(Exec,task_exec);

//ボイヤームーア法による文字列検索アルゴリズム
static int BM_method(unsigned char *text,int text_len,
              unsigned char *pattern,int sw_len,int *skip)
{

//    int text_pointer = 0;
//    int pattern_pointer = 0;
//    int match_counter = 0;
//
//    while (text_pointer < text_len && pattern_pointer < sw_len) {
//        if (text[text_pointer] == pattern[pattern_pointer]) {
//            text_pointer++; pattern_pointer++;
//            if (pattern_pointer == sw_len) {
//                match_counter++;
//                text_pointer = text_pointer - pattern_pointer + 1;
//                pattern_pointer = 0;
//            }
//        } else {
//            text_pointer = text_pointer - pattern_pointer + 1;
//            pattern_pointer = 0;
//        }
//    }

    int i = sw_len - 1;
    int match_counter = 0;

    while ( i < text_len){
        int j = sw_len - 1;
        while (text[i] == pattern[j]){
            if (j == 0){
                match_counter++;
            }
            --i;
            --j;
        }
        i = i + max(skip[text[i]],sw_len - j);
    }
    return match_counter;
}

static int
task_exec(SchedTask *s, void *rbuf, void *wbuf)
{
    //get_input
    unsigned char *i_data = (unsigned char *)s->get_input(0);
    int length = (int)s->get_inputSize(0);
    MapReduce *w = (MapReduce*)s->get_param(4);
    BMPtr bm = (BMPtr)w->global;

    unsigned long long *o_data = (unsigned long long*)s->get_output(0);

    o_data[0] = BM_method(i_data,length,bm->search_word,bm->search_word_len,bm->skip_table);

    //printf("match count : %llu\n",o_data[0]);
    //long task_count = (long)s->get_param(0);  //何番目のtaskか
    //s->printf("[start exec No: %lld]\n",task_count+1);

    return 0;
}