view example/fileread/main.cc @ 1827:d1212026e2a0 draft

some fix
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Tue, 17 Dec 2013 13:57:34 +0900
parents 82c2b9eec625
children f0dab5fffe7a
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "TaskManager.h"
#include "SchedTask.h"
#include "Func.h"

extern void task_init(void);

static double st_time;
static double ed_time;
static int spe_num = 1;
static CPU_TYPE spe_cpu = SPE_ANY;
static int DIVISION_SIZE = 4096 * 4;

extern TaskManager *manager;
const char *usr_help_str = "Usage: ./fileread [-cpu cpu_num] [-file filename]\n\
                            -cpu    Number of SPE (default 1) \n\
                            -file   Opne file\n";
typedef struct fileread {
   struct filesize *self;
   int *fd;
   long division_size;
   long task_number;
   long task_num;
   long filesize;
   long left_size;
   int task_blocks;
   CPU_TYPE cpu;
} Fileread, *FilereadPtr;

SchedDefineTask1(RUN_BLOCKS,run16);

static int
run16(SchedTask *manager, void *in, void *out) {

    FilereadPtr fr = (FilereadPtr)in;
    HTaskPtr wait;

    for (int i = 0; (fr->left_size > 0) && (i < fr->task_blocks); i++) {
        HTaskPtr read = manager->create_task(Read_task);
        read->set_cpu(fr->cpu);

        if (fr->task_number == fr->task_blocks / 2) wait = read;

        //ファイルディスクリプタをそのままタスクに渡してあげる
        read->set_inData(0,fr->fd,sizeof(int*));

        read->set_param(0,(long)fr->task_number);  //生成するTaskが何番目か
        read->set_param(1,(long)fr->division_size);  //1つのタスクが読み込む量
        if(fr->left_size <= fr->division_size){
            read->set_param(2,(long)fr->left_size);
        }else{
            read->set_param(2,(long)fr->division_size);
        }
        read->spawn();

        fr->left_size -= fr->division_size;
        fr->task_number++;
    }

    if (fr->left_size > 0) {
        HTaskPtr next = manager->create_task(RUN_BLOCKS, (memaddr)&fr->self, sizeof(memaddr),0,0);
        next->wait_for(wait);
        next->spawn();
    }
    return 0;
}

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

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


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

    char *filename = 0;

    for (int i = 1; argv[i]; ++i) {
        if (strcmp(argv[i], "-cpu") == 0) {
            spe_num = atoi(argv[i+1]);
        } else if (strcmp(argv[i], "-file") == 0) {
            filename = argv[i+1];
        }
    }

    if (filename==0){
        puts(usr_help_str);
        exit(1);
    }

    return filename;
}

static void
run_start(TaskManager *manager,char *filename)
{
    int *fd = (int*)manager->allocate(sizeof(int));
    struct stat *sb = (struct stat*)manager->allocate(sizeof(struct stat));

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

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

    FilereadPtr fr = (FilereadPtr)manager->allocate(sizeof(Fileread));

    fr->fd = fd;
    fr->cpu = spe_cpu;
    fr->task_blocks = 16;
    fr->filesize = sb->st_size;
    fr->left_size = fr->filesize;
    fr->division_size = DIVISION_SIZE;
    fr->task_num = fr->filesize / DIVISION_SIZE;
    fr->task_num += ((fr->filesize % DIVISION_SIZE) != 0);

    printf("filesize     : %ld\n",fr->filesize);
    printf("one_task_size: %ld\n",fr->division_size);
    printf("task_num     : %ld\n",fr->task_num);

    HTaskPtr run = manager->create_task(RUN_BLOCKS, (memaddr)&fr->self, sizeof(memaddr),0,0);
    run->spawn();
}


int
TMmain(TaskManager *manager, int argc, char *argv[])
{
    char *filename = 0;
    filename = init(argc,argv);

    if (filename < 0) {
        return -1;
    }

    task_init();
    st_time = getTime();
    run_start(manager, filename);
    manager->set_TMend(TMend);

    return 0;
}