view example/fileread/main.cc @ 1824:016746241fc7 draft

fix fileread
author masakoha
date Sat, 14 Dec 2013 18:31:15 +0900
parents ff4e06001518
children 82c2b9eec625
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 "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 *m, void *in, void *out){
    FilereadPtr fr = (FilereadPtr)in;
    HTaskPtr wait;

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

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

        //ファイルディスクリプタをそのままタスクに渡してあげる

        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->set_param(3,(long)fr->fd);
        read->spawn();

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

    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();
    }
}

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)
{
    HTask *read;
    int fd;

    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->task_blocks = 16;
    fr->filesize = sb->st_size;
    fr->left_size = filesize;
    fr->division_size = DIVISION_SIZE;
    fr->task_num = fr->filesize / DIVISION_SIZE;
    fr->task_num += ((fr->filesize % DIVISION_SIZE) != 0);

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

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


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;
}