Mercurial > hg > Game > Cerium
view example/fileread/main.cc @ 1871:c3f7ba33222d draft
fix
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 27 Dec 2013 03:27:15 +0900 |
parents | 5e06a8089625 |
children | 5e17ab506299 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/types.h> #include <sys/mman.h> #include "TaskManager.h" #include "SchedTask.h" #include "Func.h" #include "FileRead.h" extern void task_init(void); static double st_time; static double ed_time; static int spe_num = 1; int divide_read_flag = 0; 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 { caddr_t file_mmap; off_t size; } st_mmap_t; SchedDefineTask1(RUN_BLOCKS,run16); static int run16(SchedTask *manager, void *in, void *out) { FileReadPtr fr = (FileReadPtr)in; char* read_text = (char*)out; 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 (i == 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); //fdの番号の受け渡し read->set_outData(0,read_text + fr->task_number*fr->division_size, fr->division_size); fr->t_print->wait_for(read); 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),read_text,fr->filesize); fr->t_print->wait_for(next); 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]; } else if (strcmp(argv[i], "-dr") == 0) { divide_read_flag = 1; } } if (filename==0){ puts(usr_help_str); exit(1); } return filename; } SchedDefineTask1(MMAP,my_mmap); static int my_mmap(SchedTask *s, void *in, void *out) { FileReadPtr fr = (FileReadPtr)in; /*マッピングだよ!*/ int map = MAP_PRIVATE; st_mmap_t st_mmap; int fd = fr->fd; st_mmap.size = fr->filesize; fr->read_text = (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); } printf("in mmap mode\n"); return 0; } static void run_start(TaskManager *manager,char *filename) { long fd = (long)manager->allocate(sizeof(long)); 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->self = fr; 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 = NULL; HTaskPtr t_print = manager->create_task(Print_task, (memaddr)&fr->self, sizeof(memaddr),0,0); fr->t_print = t_print; if (divide_read_flag != 0) { char *read_text = (char*)manager->allocate(fr->filesize); run = manager->create_task(RUN_BLOCKS, (memaddr)&fr->self, sizeof(memaddr),read_text,fr->filesize); fr->read_text = read_text; }else { //my_mmap(filename, fd, fr); run = manager->create_task(MMAP , (memaddr)&fr->self, sizeof(memaddr),0,0); } t_print->wait_for(run); run->spawn(); t_print->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; }