view example/fileread/main.cc @ 1740:5a294d8e5643 draft

fix
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Tue, 12 Nov 2013 16:56:43 +0900
parents 264a36d30f7f
children e08f6133f5d7
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 ONE_TASK_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";

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

    int filesize = sb->st_size;
    int task_num = filesize / ONE_TASK_SIZE; 
    task_num += ((filesize % ONE_TASK_SIZE) != 0);

    printf("filesize     : %d\n",filesize);
    printf("one_task_size: %d\n",ONE_TASK_SIZE);
    printf("task_num     : %d\n",task_num);

    read = manager->create_task(READ_TASK);
    read->set_cpu(spe_cpu);

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

    read->set_param(0,ONE_TASK_SIZE);  //1つのタスクが読み込む量
    read->iterate(task_num);           //タスク数分イテレートする
}


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