view example/regex_mas/main.cc @ 1950:1f297d3eacb1 draft

fix bugs
author masa
date Thu, 06 Feb 2014 17:55:06 +0900
parents f3e9f811c387
children 30b95f372a0d
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
#include <vector>
#include "TaskManager.h"
#include "SchedTask.h"
#include "Func.h"
#include "WordCount.h"

/* ;TODO
 * PS3でCPU数が2以上の時に、あまりが計算されてない
 */

extern void task_init();
void TMend(TaskManager *);
static double st_time;
static double ed_time;
int all = 0;
int use_task_array = 1;
int use_task_creater = 0;
int use_compat = 0;
int div_read = 0;
int array_task_num = 8;
int spe_num = 1;
int divide_read_flag = 0;
int DIVISION_SIZE = 4*4096;
int READ_DIVISION_SIZE = 4*4096 * 1024;

unsigned char *sword = 0;
int task_count = 0;

static int division = 16; // in Kbyte
CPU_TYPE spe_cpu = SPE_ANY;
CPU_TYPE read_spe_cpu = IO_0;

const char *usr_help_str = "Usage: ./word_count [-a -c -s] [-cpu spe_num] [-sw search_word] [-file filename]\n       Required filename & search_word\n";

static double
getTime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + (double)tv.tv_usec*1e-6;
}

typedef struct {
    caddr_t file_mmap;
    off_t size;
} st_mmap_t;

/*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/
static unsigned long long
fix_byte(unsigned long long size,int fix_byte_size)
{
    size = (size/fix_byte_size)*fix_byte_size  + ((size%fix_byte_size)!= 0)*fix_byte_size;

    return size;
}


SchedDefineTask1(MMAP,my_mmap);

static int
my_mmap(SchedTask *s, void *in, void *out)
{
    WordCountPtr w = (WordCountPtr)in;
    /*マッピングだよ!*/

    int map = MAP_PRIVATE;
    st_mmap_t st_mmap;

    int fd = w->fd;
    st_mmap.size = fix_byte(w->read_filesize,4096);
    w->file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0);
    if (st_mmap.file_mmap == (caddr_t)-1) {
        fprintf(stderr,"Can't mmap file\n");

        perror(NULL);
        exit(0);
    }

    return 0;
}

static void
run_tasks(SchedTask *manager, WordCount *w, int task_count,HTaskPtr t_read, HTaskPtr t_next, int size)
{

    if (task_count < array_task_num) {
        array_task_num = task_count;
        if (task_count<=0) return;
    }
    for (int i = 0; i < task_count; i += array_task_num) {
        HTask *task_array;
        if (use_task_array) {
            int task_num = (w->size+size-1)/size;
            if (task_num>array_task_num) task_num = array_task_num;
            task_array = manager->create_task_array(TASK_EXEC,task_num,1,3,1);
            if (t_read != 0) task_array->wait_for(t_read);
            if (!all) {
                t_next->wait_for(task_array);
            } else {
                w->t_print->wait_for(task_array);
            }
        }

        Task *t_exec = 0;
        HTask *h_exec = 0;
        for (int j = 0; j < array_task_num; j++) {
            long i = w->task_spawned++;
            if (w->size < size) size = w->size;
            if (size==0) break;
            if (use_task_array) {
                t_exec = task_array->next_task_array(TASK_EXEC,t_exec);
                t_exec->set_inData(0,w->file_mmap + i*w->division_size, size);
                t_exec->set_inData(1,w->search_word, w->search_word_len);
                t_exec->set_inData(2,w->BMskip_table, 256);

                t_exec->set_param(0,(long)i);

                t_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size);
            } else if (use_compat) {
                h_exec = manager->create_task(TASK_EXEC);
                if (t_read != 0) h_exec->wait_for(t_read);
                h_exec->set_inData(0,w->file_mmap + i*w->division_size, size);
                h_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size);

                t_next->wait_for(h_exec);

                h_exec->set_cpu(spe_cpu);
                h_exec->spawn();
            } else {
                h_exec = manager->create_task(TASK_EXEC,
                                              (memaddr)(w->file_mmap + i*w->division_size), size,
                                              (memaddr)(w->o_data + i*w->out_size), w->division_out_size);
                if (t_read != 0) h_exec->wait_for(t_read);
                t_next->wait_for(h_exec);
                h_exec->set_cpu(spe_cpu);
                h_exec->spawn();
            }
            w->size -= w->division_size;
            w->task_num--;
        }
        if (use_task_array) {
            task_array->spawn_task_array(t_exec->next());
            task_array->set_cpu(spe_cpu);
            task_array->spawn();
        } else {
            //if (!all) t_next->wait_for(h_exec);
        }
    }
}

/**
 *   このTaskは、PPE上で実行されるので、並列に実行されることはない
 *   二つ実行されていて、Task が足りなくなることがないようにしている。
 */

SchedDefineTask1(RUN_TASK_BLOCKS,task_run16);

static int
task_run16(SchedTask *manager, void *in, void *out)
{
    WordCount *w = *(WordCount **)in;

    HTaskPtr t_read = manager->create_task(READ_TASK);
    w->t_print->wait_for(t_read);
    t_read->set_cpu(read_spe_cpu);
    t_read->set_param(0,w->fd);

    if (w->task_num < w->task_blocks) {
        t_read->set_param(1,w->task_spawned*w->division_size);
        t_read->set_outData(0, w->file_mmap + w->task_spawned * w->division_size, w->task_blocks * w->division_size);

        // last case
        while (w->size >= w->division_size)
            run_tasks(manager,w,w->task_num,t_read,w->t_print, w->division_size + w->extra_len);
        // remaining data
        while (w->size>0)
            run_tasks(manager,w,1,t_read,w->t_print, w->size);

        t_read->set_param(2,w->task_spawned*w->division_size);
        t_read->spawn();
        // printf("run16 last %d\n",w->task_num);
    } else {
        HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS,
                                               (memaddr)&w->self,sizeof(memaddr),0,0);
        w->t_print->wait_for(t_next);

        t_read->set_param(1,w->task_spawned*w->division_size);
        t_read->set_outData(0, w->file_mmap + w->task_spawned * w->division_size, w->task_blocks * w->division_size);

        run_tasks(manager,w, w->task_blocks, t_read, t_next, w->division_size + w->extra_len);

        t_read->set_param(2,w->task_spawned*w->division_size);

        t_read->spawn();
        t_next->spawn();
        // printf("run16 next %d\n",w->task_num);
    }
    return 0;
}


static int blocks = 48;
//static int blocks = 31 * 6 * 24;

//Boyer Moore法に使用するテーブルを作成
static int*
create_BMskiptable(unsigned char *search_word,int search_word_len,int *skip_table)
{
    for(int i = 0; i < 256; ++i){
        skip_table[i] = search_word_len;
    }

    for(int j = 0; j < search_word_len - 1; ++j){
        skip_table[(int)search_word[j]] = search_word_len - j - 1;
    }
    return skip_table;
}


static void
run_start(TaskManager *manager, char *filename,unsigned char *search_word, int search_word_len)
{
    long   fd = (long)manager->allocate(sizeof(long));
    struct stat *sb = (struct stat*)manager->allocate(sizeof(struct stat));
    HTaskPtr t_exec;

    if ((fd=open(filename,O_RDONLY,0666))==0) {
        fprintf(stderr,"can't open %s\n",filename);
        return ;
    }

    if (fstat(fd,sb)) {
        fprintf(stderr,"can't fstat %s\n",filename);
        return ;
    }

    WordCountPtr w = (WordCountPtr)manager->allocate(sizeof(WordCount));

    w->self = w;
    w->fd = fd;
    w->read_filesize = sb->st_size;

    HTaskPtr r_read = NULL;

    if (divide_read_flag != 0) {
        printf("[divide read mode]\n");
        w->file_mmap = (char*)manager->allocate(w->read_filesize);
    }else {
        printf("[mmap mode]\n");
        r_read = manager->create_task(MMAP , (memaddr)&w->self, sizeof(memaddr),0,0);
        r_read->spawn();
    }


    /* prepare BMSearch*/
    int *skip = (int*)manager->allocate(256 * sizeof(int));  // 文字列に対応した table を用意
    w->search_word = search_word;
    w->search_word_len = search_word_len;
    w->BMskip_table = create_BMskiptable(w->search_word, w->search_word_len, skip);
    w->extra_len = w->search_word_len - 1;

    /* original */
    HTaskPtr t_print;

    //w->task_blocks = blocks;
    w->self = w;
    w->task_spawned = 0;

    /*sizeはdivision_sizeの倍数にしている。*/
    w->size = w->file_size = w->read_filesize;
    //w->file_mmap = st_mmap.file_mmap;
    printf("w %lx\n",(long)w);

    /* 1task分のデータサイズ(byte) */
    if (w->size >= 1024*division) {
        w->division_size = 1024 * division;/*16kbyte*/
    } else {
        w->division_size = w->size;
    }

    printf("division_size %ld\n",w->division_size);

    /* "word num" and "line num" */
    w->status_num = 2;
    /* taskの数 */
    w->task_num = w->size / w->division_size;
    w->task_num = w->task_num + (w->division_size*w->task_num < w->size);
    int out_task_num = w->task_num;

    if(!all) {
        w->task_blocks = blocks;
    } else {
        w->task_blocks = w->task_num;
    }

    w->out_task_num = out_task_num;
    printf("task_num %ld\n",w->task_num);
    printf("out_task_num %ld\n",w->out_task_num);

    /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(4byte)を使用 */

    w->division_out_size = sizeof(unsigned long long)*1;
    int out_size = w->division_out_size*out_task_num;
    w->o_data = (unsigned long long *)manager->allocate(out_size);
    w->out_size_ = out_size;
    w->out_size = 1;
    printf("out size %d\n",out_size);

    /*各SPEの結果を合計して出力するタスク*/

    t_print = manager->create_task(PRINT_TASK,
                                   (memaddr)&w->self,sizeof(memaddr),0,0);
    w->t_print = t_print;

    for(int i=0;i<1;i++) {
        /* Task を task_blocks ずつ起動する Task */
        /* serialize されていると仮定する... */
        t_exec = manager->create_task(RUN_TASK_BLOCKS,
                                               (memaddr)&w->self,sizeof(memaddr),0,0);
        t_print->wait_for(t_exec);
        //    t_exec->iterate(4);
        t_exec->spawn();
    }
    w->t_exec = t_exec;

    t_print->spawn();
}

static char*
init(int argc, char **argv)
{

    char *filename = 0;

    for (int i = 1; argv[i]; ++i) {
        if (strcmp(argv[i], "-file") == 0) {
            filename = argv[i+1];
        } else if (strcmp(argv[i], "-division") == 0) {
            division = atoi(argv[i+1]);
        } else if (strcmp(argv[i], "-block") == 0) {
            blocks = atoi(argv[i+1]);
        } else if (strcmp(argv[i], "-a") == 0) {
            // create task all at once
            all = 1;
        } else if (strcmp(argv[i], "-c") == 0) {
            use_task_array = 0;
            use_compat = 1;
        } else if (strcmp(argv[i], "-s") == 0) {
            use_task_array = 0;
            use_compat = 0;
        } else if (strcmp(argv[i], "-t") == 0) {
            use_task_creater = 1;
            use_task_array = 0;
            use_compat = 0;
        } else if (strcmp(argv[i], "-anum") == 0) {
            array_task_num = atoi(argv[i+1]);
        } else if (strcmp(argv[i], "-g") == 0 ) {
            spe_cpu = GPU_0;
        } else if (strcmp(argv[i], "-cpu") == 0) {
            spe_num = atoi(argv[i+1]);
            if (spe_num==0) spe_num = 1;
        } else if (strcmp(argv[i], "-sw") == 0) {
            sword = (unsigned char*)argv[i+1];
        } else if (strcmp(argv[i], "-dr") == 0) {
            divide_read_flag = 1;
        }
    }
    if ((filename==0) || (sword==0)) {
        puts(usr_help_str);
        exit(1);
    }

    return filename;
}


int
TMmain(TaskManager *manager, int argc, char *argv[])
{

    char *filename = init(argc, argv);
    int sw_len = strlen((const char *)sword);

    unsigned char *search_word = (unsigned char*)manager->allocate(sw_len + 1);
    memcpy(search_word, sword, sw_len + 1);

    task_init();
    st_time = getTime();
    run_start(manager, filename, search_word, sw_len);
    manager->set_TMend(TMend);
    return 0;
}

void
TMend(TaskManager *manager)
{
    ed_time = getTime();
    printf("Time: %0.6f\n",ed_time-st_time);
}

/* end */