Mercurial > hg > Game > Cerium
diff example/fileread/main.cc @ 1721:797e3ec1ca74 draft
add fileread only example
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 28 Oct 2013 13:35:17 +0900 |
parents | |
children | 264a36d30f7f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/fileread/main.cc Mon Oct 28 13:35:17 2013 +0900 @@ -0,0 +1,90 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include "TaskManager.h" +#include "Func.h" + +extern void task_init(void); + +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"; + +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(); + run_start(manager, filename); + + return 0; +}