Mercurial > hg > Game > Cerium
annotate example/regex_mas/main.cc @ 1723:d54e287eeef3 draft
change time measurement point in regex_mas
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 28 Oct 2013 15:26:37 +0900 (2013-10-28) |
parents | 64bb17ddc3e6 |
children | 5a294d8e5643 |
rev | line source |
---|---|
1598 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include <sys/mman.h> | |
5 #include <sys/types.h> | |
6 #include <sys/stat.h> | |
7 #include <fcntl.h> | |
8 #include <unistd.h> | |
9 #include <sys/time.h> | |
1690 | 10 #include <iostream> |
11 #include <vector> | |
1598 | 12 #include "TaskManager.h" |
13 #include "SchedTask.h" | |
14 #include "Func.h" | |
15 #include "WordCount.h" | |
16 | |
1680
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
17 #define EXTRA_LENGTH 4 //多く取ってくる文字数(search word length - 1) |
1630 | 18 |
1598 | 19 /* ;TODO |
20 * PS3でCPU数が2以上の時に、あまりが計算されてない | |
21 */ | |
22 | |
23 extern void task_init(); | |
24 void TMend(TaskManager *); | |
1645
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
25 static double st_time; |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
26 static double ed_time; |
1598 | 27 int all = 0; |
28 int use_task_array = 1; | |
29 int use_task_creater = 0; | |
30 int use_compat = 0; | |
31 int array_task_num = 8; | |
32 int spe_num = 1; | |
1662 | 33 unsigned char *search_word; |
1598 | 34 CPU_TYPE spe_cpu = SPE_ANY; |
35 const char *usr_help_str = "Usage: ./word_count [-a -c -s] [-cpu spe_num] [-file filename]\n"; | |
36 | |
1645
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
37 static double |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
38 getTime() { |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
39 struct timeval tv; |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
40 gettimeofday(&tv, NULL); |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
41 return tv.tv_sec + (double)tv.tv_usec*1e-6; |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
42 } |
1598 | 43 |
44 typedef struct { | |
45 caddr_t file_mmap; | |
46 off_t size; | |
47 } st_mmap_t; | |
48 | |
49 /*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/ | |
50 static int | |
51 fix_byte(int size,int fix_byte_size) | |
52 { | |
53 size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size; | |
54 | |
55 return size; | |
56 } | |
57 | |
58 | |
59 static st_mmap_t | |
1707
64bb17ddc3e6
change "fread" to "read" in regex_mas
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1692
diff
changeset
|
60 my_read(TaskManager *manager,char *filename) |
1598 | 61 { |
62 /*マッピングだよ!*/ | |
63 int fd = -1; | |
64 st_mmap_t st_mmap; | |
65 struct stat sb; | |
66 | |
67 if ((fd=open(filename,O_RDONLY,0666))==0) { | |
68 fprintf(stderr,"can't open %s\n",filename); | |
69 } | |
70 | |
71 if (fstat(fd,&sb)) { | |
72 fprintf(stderr,"can't fstat %s\n",filename); | |
73 } | |
74 | |
75 /*sizeをページングサイズの倍数にあわせる*/ | |
1647 | 76 st_mmap.size = fix_byte(sb.st_size,4096); |
1707
64bb17ddc3e6
change "fread" to "read" in regex_mas
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1692
diff
changeset
|
77 st_mmap.file_mmap = (char*)manager->allocate(st_mmap.size); |
1598 | 78 |
1707
64bb17ddc3e6
change "fread" to "read" in regex_mas
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1692
diff
changeset
|
79 read(fd,st_mmap.file_mmap,st_mmap.size); |
1691
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
80 |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
81 if (st_mmap.file_mmap == (caddr_t)-1) { |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
82 fprintf(stderr,"Can't mmap file\n"); |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
83 perror(NULL); |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
84 exit(0); |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
85 } |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
86 |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
87 return st_mmap; |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
88 |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
89 } |
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
90 |
1598 | 91 static void |
92 run_tasks(SchedTask *manager, WordCount *w, int task_count, HTaskPtr t_next, int size) | |
93 { | |
94 | |
95 if (task_count < array_task_num) { | |
96 array_task_num = task_count; | |
97 if (task_count<=0) return; | |
98 } | |
99 | |
100 if (use_task_array) { | |
101 | |
102 int spl = spe_num * array_task_num; | |
103 int loop = (task_count + spl - 1) / spl; | |
104 | |
105 for (int i = 0; i < loop; i += 1) { | |
106 | |
1676 | 107 if (w->task_num < spe_num) spe_num = w->task_num; |
1598 | 108 |
109 // ここから | |
110 HTask **task_array = (HTask**)manager->allocate(sizeof(HTask*)*spe_num); | |
111 Task **t_exec = (Task**)manager->allocate(sizeof(Task*)*spe_num); | |
112 | |
1690 | 113 for (int k = 0; k < spe_num; k++) { |
1598 | 114 task_array[k] = manager->create_task_array(TASK_EXEC,array_task_num,1,1,1); |
115 t_exec[k] = 0; | |
116 if (all) { | |
117 w->t_print->wait_for(task_array[k]); | |
118 } else { | |
119 t_next->wait_for(task_array[k]); | |
120 } | |
121 } | |
122 | |
123 for (int j = 0; j < array_task_num; j++) { | |
124 for (int k = 0; k < spe_num; k++) { | |
125 | |
126 int a = w->task_spwaned++; | |
127 | |
128 if (w->size < size) size = w->size; | |
1680
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
129 |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
130 int set_one_task_length = size/sizeof(char); |
1634
c0841aa109a8
add number of match position.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1630
diff
changeset
|
131 const int ONE_TASK_LENGTH = w->division_size; |
c0841aa109a8
add number of match position.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1630
diff
changeset
|
132 const int ONE_LOOP_LENGTH = array_task_num*spe_num*ONE_TASK_LENGTH; |
c0841aa109a8
add number of match position.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1630
diff
changeset
|
133 const int ARRAY_LENGTH_SIZE = spe_num * ONE_TASK_LENGTH; |
c0841aa109a8
add number of match position.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1630
diff
changeset
|
134 int offset = ONE_LOOP_LENGTH * i + ARRAY_LENGTH_SIZE*j + ONE_TASK_LENGTH*k; |
1680
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
135 |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
136 t_exec[k] = task_array[k]->next_task_array(TASK_EXEC,t_exec[k]); |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
137 t_exec[k]->set_param(0,(memaddr)offset); |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
138 |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
139 if(size != w->size){ //最後のタスクかどうかの判定 |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
140 t_exec[k]->set_param(1,(memaddr)set_one_task_length + EXTRA_LENGTH); |
1662 | 141 t_exec[k]->set_inData(0,w->file_mmap + a*w->division_size, size+EXTRA_LENGTH); |
1647 | 142 }else{ |
1680
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
143 t_exec[k]->set_param(1,(memaddr)set_one_task_length); |
1662 | 144 t_exec[k]->set_inData(0,w->file_mmap + a*w->division_size, size); |
1647 | 145 } |
1690 | 146 |
1662 | 147 t_exec[k]->set_outData(0,w->o_data + a*w->out_size, w->division_out_size); |
148 | |
1598 | 149 w->size -= size; |
150 w->task_num--; | |
151 } | |
152 } | |
153 for (int k = 0; k < spe_num; k++) { | |
154 task_array[k]->spawn_task_array(t_exec[k]->next()); | |
155 task_array[k]->set_cpu(spe_cpu); | |
156 task_array[k]->spawn(); | |
157 } | |
158 | |
159 } | |
160 | |
161 return; | |
162 | |
163 } | |
164 | |
165 for (int i = 0; i < task_count; i += array_task_num) { | |
166 | |
167 HTask *h_exec = 0; | |
168 for (int j = 0; j < array_task_num; j++) { | |
169 int i = w->task_spwaned++; | |
170 if (w->size < size) size = w->size; | |
171 int length = size/sizeof(char); | |
172 if (size==0) break; | |
173 | |
174 if (use_compat) { | |
175 h_exec = manager->create_task(TASK_EXEC); | |
176 h_exec->set_param(0,(memaddr)length); | |
177 h_exec->set_inData(0,w->file_mmap + i*w->division_size, size); | |
178 h_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size); | |
179 | |
180 | |
181 if (all) { | |
182 w->t_print->wait_for(h_exec); | |
183 } else { | |
184 t_next->wait_for(h_exec); | |
185 } | |
186 | |
187 h_exec->set_cpu(spe_cpu); | |
188 h_exec->spawn(); | |
189 | |
190 } else { | |
191 h_exec = manager->create_task(TASK_EXEC, | |
192 (memaddr)(w->file_mmap + i*w->division_size), size, | |
193 (memaddr)(w->o_data + i*w->out_size), w->division_out_size); | |
194 | |
195 if (all) { | |
196 w->t_print->wait_for(h_exec); | |
197 } else { | |
198 t_next->wait_for(h_exec); | |
199 } | |
200 | |
201 h_exec->set_cpu(spe_cpu); | |
202 h_exec->spawn(); | |
203 } | |
204 w->size -= size; | |
205 w->task_num--; | |
206 } | |
207 | |
208 } | |
209 | |
210 } | |
211 | |
212 /** | |
213 * このTaskは、PPE上で実行されるので、並列に実行されることはない | |
214 * 二つ実行されていて、Task が足りなくなることがないようにしている。 | |
215 */ | |
216 | |
217 SchedDefineTask1(RUN_TASK_BLOCKS,run16); | |
218 | |
219 static int | |
220 run16(SchedTask *manager, void *in, void *out) | |
221 { | |
222 WordCount *w = *(WordCount **)in; | |
223 | |
224 if (w->task_num < w->task_blocks) { | |
225 // last case | |
226 while (w->size >= w->division_size) | |
227 run_tasks(manager,w,w->task_num, w->t_print, w->division_size); | |
228 // remaining data | |
229 while (w->size>0) | |
230 run_tasks(manager,w,1, w->t_print, w->size); | |
231 // printf("run16 last %d\n",w->task_num); | |
232 } else { | |
233 HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS, | |
234 (memaddr)&w->self,sizeof(memaddr),0,0); | |
235 w->t_print->wait_for(t_next); | |
236 | |
237 run_tasks(manager,w, w->task_blocks, t_next, w->division_size); | |
238 | |
239 t_next->spawn(); | |
240 // printf("run16 next %d\n",w->task_num); | |
241 } | |
242 return 0; | |
243 } | |
244 | |
245 | |
246 static int blocks = 48; | |
247 //static int blocks = 31 * 6 * 24; | |
248 static int division = 16; // in Kbyte | |
249 | |
250 static void | |
251 run_start(TaskManager *manager, char *filename) | |
252 { | |
253 HTaskPtr t_print; | |
254 | |
255 st_mmap_t st_mmap; | |
1691
6b3991ac3f07
changed mmap to fread.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1690
diff
changeset
|
256 //st_mmap = my_mmap(filename); |
1707
64bb17ddc3e6
change "fread" to "read" in regex_mas
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1692
diff
changeset
|
257 st_mmap = my_read(manager,filename); |
1598 | 258 WordCount *w = (WordCount*)manager->allocate(sizeof(WordCount)); |
259 // bzero(w,sizeof(WordCount)); | |
260 | |
261 //w->task_blocks = blocks; | |
262 w->self = w; | |
263 w->task_spwaned = 0; | |
264 | |
265 /*sizeはdivision_sizeの倍数にしている。*/ | |
266 w->size = w->file_size = st_mmap.size; | |
267 w->file_mmap = st_mmap.file_mmap; | |
268 /* 1task分のデータサイズ(byte) */ | |
269 if (w->size >= 1024*division) { | |
270 w->division_size = 1024 * division;/*16kbyte*/ | |
271 } else { | |
272 w->division_size = w->size; | |
273 } | |
274 | |
1680
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
275 /* exec output only "match_num" */ |
e44e5a18392c
fix regex_mas.
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1676
diff
changeset
|
276 w->status_num = 1; |
1598 | 277 /* taskの数 */ |
278 w->task_num = w->size / w->division_size; | |
279 w->task_num = w->task_num + (w->division_size*w->task_num < w->size); | |
280 int out_task_num = w->task_num; | |
281 | |
282 if(!all) { | |
283 w->task_blocks = blocks; | |
284 } else { | |
285 w->task_blocks = w->task_num; | |
286 } | |
287 | |
288 w->out_task_num = out_task_num; | |
289 | |
290 /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(4byte)を使用 */ | |
291 | |
1660
8b50b1ee068e
change result position to match_num.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1655
diff
changeset
|
292 w->division_out_size = sizeof(unsigned long long)*1; |
1598 | 293 int out_size = w->division_out_size*out_task_num; |
294 w->o_data = (unsigned long long *)manager->allocate(out_size); | |
1660
8b50b1ee068e
change result position to match_num.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1655
diff
changeset
|
295 w->out_size = 1; |
1598 | 296 |
297 /*各SPEの結果を合計して出力するタスク*/ | |
298 | |
299 t_print = manager->create_task(TASK_PRINT, | |
300 (memaddr)&w->self,sizeof(memaddr),0,0); | |
301 w->t_print = t_print; | |
302 | |
1690 | 303 for(int i = 0;i<2;i++) { |
1598 | 304 /* Task を task_blocks ずつ起動する Task */ |
305 /* serialize されていると仮定する... */ | |
306 HTaskPtr t_exec = manager->create_task(RUN_TASK_BLOCKS, | |
307 (memaddr)&w->self,sizeof(memaddr),0,0); | |
308 t_print->wait_for(t_exec); | |
309 t_exec->spawn(); | |
310 } | |
311 | |
312 t_print->spawn(); | |
313 } | |
314 | |
315 static char* | |
316 init(int argc, char **argv) | |
317 { | |
318 | |
319 char *filename = 0; | |
320 | |
321 for (int i = 1; argv[i]; ++i) { | |
322 if (strcmp(argv[i], "-file") == 0) { | |
323 filename = argv[i+1]; | |
324 } else if (strcmp(argv[i], "-division") == 0) { | |
325 division = atoi(argv[i+1]); | |
326 } else if (strcmp(argv[i], "-block") == 0) { | |
327 blocks = atoi(argv[i+1]); | |
328 } else if (strcmp(argv[i], "-a") == 0) { | |
329 // create task all at once | |
330 all = 1; | |
331 } else if (strcmp(argv[i], "-c") == 0) { | |
332 use_task_array = 0; | |
333 use_compat = 1; | |
334 } else if (strcmp(argv[i], "-s") == 0) { | |
335 use_task_array = 0; | |
336 use_compat = 0; | |
337 } else if (strcmp(argv[i], "-t") == 0) { | |
338 use_task_creater = 1; | |
339 use_task_array = 0; | |
340 use_compat = 0; | |
341 } else if (strcmp(argv[i], "-anum") == 0) { | |
342 array_task_num = atoi(argv[i+1]); | |
343 } else if (strcmp(argv[i], "-g") == 0 ) { | |
344 spe_cpu = GPU_0; | |
345 } else if (strcmp(argv[i], "-cpu") == 0) { | |
346 spe_num = atoi(argv[i+1]); | |
347 if (spe_num==0) spe_num = 1; | |
1645
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
348 } else if (strcmp(argv[i], "-sw") == 0) { |
1651 | 349 search_word = (unsigned char*)argv[i+1]; |
1598 | 350 } |
351 } | |
352 if (filename==0) { | |
353 puts(usr_help_str); | |
354 exit(1); | |
355 } | |
356 | |
357 return filename; | |
358 } | |
359 | |
360 | |
361 int | |
362 TMmain(TaskManager *manager, int argc, char *argv[]) | |
363 { | |
364 | |
365 char *filename = 0; | |
366 filename = init(argc, argv); | |
367 | |
368 if (filename < 0) { | |
369 return -1; | |
370 } | |
371 | |
372 task_init(); | |
1723
d54e287eeef3
change time measurement point in regex_mas
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1707
diff
changeset
|
373 st_time = getTime(); |
1598 | 374 run_start(manager, filename); |
1645
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
375 manager->set_TMend(TMend); |
1598 | 376 return 0; |
377 } | |
378 | |
1645
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
379 void |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
380 TMend(TaskManager *manager) |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
381 { |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
382 ed_time = getTime(); |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
383 printf("Time: %0.6f\n",ed_time-st_time); |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
384 } |
4698c92bf2ca
Regex_mas implement time measurement.
Masa <e085726@ie.u-ryukyu.ac.jp>
parents:
1642
diff
changeset
|
385 |
1662 | 386 /* end */ |