Mercurial > hg > Game > Cerium
annotate 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 |
rev | line source |
---|---|
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h> |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdlib.h> |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include <string.h> |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include <sys/stat.h> |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
5 #include <sys/time.h> |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 #include "TaskManager.h" |
1827 | 7 #include "SchedTask.h" |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 #include "Func.h" |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 extern void task_init(void); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
12 static double st_time; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
13 static double ed_time; |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 static int spe_num = 1; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 static CPU_TYPE spe_cpu = SPE_ANY; |
1808
c25aa7edd1ba
fix conditional branch in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1802
diff
changeset
|
16 static int DIVISION_SIZE = 4096 * 4; |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 extern TaskManager *manager; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 const char *usr_help_str = "Usage: ./fileread [-cpu cpu_num] [-file filename]\n\ |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 -cpu Number of SPE (default 1) \n\ |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
21 -file Opne file\n"; |
1824 | 22 typedef struct fileread { |
23 struct filesize *self; | |
1827 | 24 int *fd; |
1824 | 25 long division_size; |
26 long task_number; | |
27 long task_num; | |
28 long filesize; | |
29 long left_size; | |
30 int task_blocks; | |
31 CPU_TYPE cpu; | |
32 } Fileread, *FilereadPtr; | |
33 | |
34 SchedDefineTask1(RUN_BLOCKS,run16); | |
35 | |
36 static int | |
1827 | 37 run16(SchedTask *manager, void *in, void *out) { |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
38 |
1824 | 39 FilereadPtr fr = (FilereadPtr)in; |
40 HTaskPtr wait; | |
41 | |
1827 | 42 for (int i = 0; (fr->left_size > 0) && (i < fr->task_blocks); i++) { |
1824 | 43 HTaskPtr read = manager->create_task(Read_task); |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
44 read->set_cpu(fr->cpu); |
1824 | 45 |
1827 | 46 if (fr->task_number == fr->task_blocks / 2) wait = read; |
1824 | 47 |
48 //ファイルディスクリプタをそのままタスクに渡してあげる | |
1827 | 49 read->set_inData(0,fr->fd,sizeof(int*)); |
1824 | 50 |
51 read->set_param(0,(long)fr->task_number); //生成するTaskが何番目か | |
52 read->set_param(1,(long)fr->division_size); //1つのタスクが読み込む量 | |
53 if(fr->left_size <= fr->division_size){ | |
54 read->set_param(2,(long)fr->left_size); | |
55 }else{ | |
56 read->set_param(2,(long)fr->division_size); | |
57 } | |
58 read->spawn(); | |
59 | |
60 fr->left_size -= fr->division_size; | |
1827 | 61 fr->task_number++; |
1824 | 62 } |
63 | |
64 if (fr->left_size > 0) { | |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
65 HTaskPtr next = manager->create_task(RUN_BLOCKS, (memaddr)&fr->self, sizeof(memaddr),0,0); |
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
66 next->wait_for(wait); |
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
67 next->spawn(); |
1824 | 68 } |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
69 return 0; |
1824 | 70 } |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
71 |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
72 static double |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
73 getTime(){ |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
74 struct timeval tv; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
75 gettimeofday(&tv,NULL); |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
76 return tv.tv_sec + (double)tv.tv_usec*1e-6; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
77 } |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
78 |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
79 void |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
80 TMend(TaskManager *manager) |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
81 { |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
82 ed_time = getTime(); |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
83 printf("Time: %0.6f\n",ed_time-st_time); |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
84 } |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
85 |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 static char* |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 init(int argc, char **argv) |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 char *filename = 0; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 for (int i = 1; argv[i]; ++i) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 if (strcmp(argv[i], "-cpu") == 0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95 spe_num = atoi(argv[i+1]); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 } else if (strcmp(argv[i], "-file") == 0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 filename = argv[i+1]; |
1740 | 98 } |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101 if (filename==0){ |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 puts(usr_help_str); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 exit(1); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 return filename; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 static void |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 run_start(TaskManager *manager,char *filename) |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111 { |
1827 | 112 int *fd = (int*)manager->allocate(sizeof(int)); |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113 struct stat *sb = (struct stat*)manager->allocate(sizeof(struct stat)); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 |
1827 | 115 if ((*fd=open(filename,O_RDONLY,0666))==0) { |
116 fprintf(stderr,"can't open %s\n",filename); | |
1740 | 117 } |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 |
1827 | 119 if (fstat(*fd,sb)) { |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 fprintf(stderr,"can't fstat %s\n",filename); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
121 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 |
1824 | 123 FilereadPtr fr = (FilereadPtr)manager->allocate(sizeof(Fileread)); |
1802
e08f6133f5d7
change iterator to spawn in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1740
diff
changeset
|
124 |
1827 | 125 fr->fd = fd; |
126 fr->cpu = spe_cpu; | |
1824 | 127 fr->task_blocks = 16; |
128 fr->filesize = sb->st_size; | |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
129 fr->left_size = fr->filesize; |
1824 | 130 fr->division_size = DIVISION_SIZE; |
131 fr->task_num = fr->filesize / DIVISION_SIZE; | |
132 fr->task_num += ((fr->filesize % DIVISION_SIZE) != 0); | |
1802
e08f6133f5d7
change iterator to spawn in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1740
diff
changeset
|
133 |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
134 printf("filesize : %ld\n",fr->filesize); |
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
135 printf("one_task_size: %ld\n",fr->division_size); |
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
136 printf("task_num : %ld\n",fr->task_num); |
1740 | 137 |
1824 | 138 HTaskPtr run = manager->create_task(RUN_BLOCKS, (memaddr)&fr->self, sizeof(memaddr),0,0); |
1825
82c2b9eec625
remove error and warning fileread(but cannot running)
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1824
diff
changeset
|
139 run->spawn(); |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
140 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 int |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
144 TMmain(TaskManager *manager, int argc, char *argv[]) |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 char *filename = 0; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147 filename = init(argc,argv); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 if (filename < 0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150 return -1; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
151 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 task_init(); |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
154 st_time = getTime(); |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 run_start(manager, filename); |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
156 manager->set_TMend(TMend); |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 return 0; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 } |