comparison example/word_count/main.cc @ 1911:f842ea419307 draft

running mmap mode ( cannot running divide read )
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Sun, 19 Jan 2014 23:36:02 +0900
parents b7b528e9ec5e
children 4a716f35980a
comparison
equal deleted inserted replaced
1910:b7b528e9ec5e 1911:f842ea419307
29 int array_task_num = 8; 29 int array_task_num = 8;
30 int spe_num = 1; 30 int spe_num = 1;
31 int divide_read_flag = 0; 31 int divide_read_flag = 0;
32 int READ_DIVISION_SIZE = 16 * 1024; 32 int READ_DIVISION_SIZE = 16 * 1024;
33 CPU_TYPE spe_cpu = SPE_ANY; 33 CPU_TYPE spe_cpu = SPE_ANY;
34 CPU_TYPE read_spe_cpu = IO_0;
34 const char *usr_help_str = "Usage: ./word_count [-a -c -s] [-cpu spe_num] [-g] [-file filename]\n"; 35 const char *usr_help_str = "Usage: ./word_count [-a -c -s] [-cpu spe_num] [-g] [-file filename]\n";
35 36
36 static double 37 static double
37 getTime() { 38 getTime() {
38 struct timeval tv; 39 struct timeval tv;
110 111
111 int map = MAP_PRIVATE; 112 int map = MAP_PRIVATE;
112 st_mmap_t st_mmap; 113 st_mmap_t st_mmap;
113 114
114 int fd = w->fd; 115 int fd = w->fd;
115 st_mmap.size = w->read_filesize; 116 st_mmap.size = fix_byte(w->read_filesize,4096);
116 w->file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0); 117 w->file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0);
117 if (st_mmap.file_mmap == (caddr_t)-1) { 118 if (st_mmap.file_mmap == (caddr_t)-1) {
118 fprintf(stderr,"Can't mmap file\n"); 119 fprintf(stderr,"Can't mmap file\n");
119 120
120 perror(NULL); 121 perror(NULL);
162 //} 163 //}
163 164
164 static void 165 static void
165 run_tasks(SchedTask *manager, WordCount *w, int task_count, HTaskPtr t_next, int size) 166 run_tasks(SchedTask *manager, WordCount *w, int task_count, HTaskPtr t_next, int size)
166 { 167 {
167 168
168 if (task_count < array_task_num) { 169 if (task_count < array_task_num) {
169 array_task_num = task_count; 170 array_task_num = task_count;
170 if (task_count<=0) return; 171 if (task_count<=0) return;
171 } 172 }
172 173
173 for (int i = 0; i < task_count; i += array_task_num) { 174 for (int i = 0; i < task_count; i += array_task_num) {
174 HTask *task_array; 175 HTask *task_array;
175 if (use_task_array) { 176 if (use_task_array) {
176 int task_num = (w->size+size-1)/size; 177 int task_num = (w->size+size-1)/size;
177 if (task_num>array_task_num) task_num = array_task_num; 178 if (task_num>array_task_num) task_num = array_task_num;
180 t_next->wait_for(task_array); 181 t_next->wait_for(task_array);
181 } else { 182 } else {
182 w->t_print->wait_for(task_array); 183 w->t_print->wait_for(task_array);
183 } 184 }
184 } 185 }
185 186
186 Task *t_exec = 0; 187 Task *t_exec = 0;
187 HTask *h_exec = 0; 188 HTask *h_exec = 0;
188 for (int j = 0; j < array_task_num; j++) { 189 for (int j = 0; j < array_task_num; j++) {
189 int i = w->task_spwaned++; 190 int i = w->task_spwaned++;
190 if (w->size < size) size = w->size; 191 if (w->size < size) size = w->size;
196 t_exec->set_param(0,(long)size); 197 t_exec->set_param(0,(long)size);
197 } else if (use_compat) { 198 } else if (use_compat) {
198 h_exec = manager->create_task(TASK_EXEC); 199 h_exec = manager->create_task(TASK_EXEC);
199 h_exec->set_inData(0,w->file_mmap + i*w->division_size, size); 200 h_exec->set_inData(0,w->file_mmap + i*w->division_size, size);
200 h_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size); 201 h_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size);
201 202
202 t_next->wait_for(h_exec); 203 t_next->wait_for(h_exec);
203 204
204 h_exec->set_cpu(spe_cpu); 205 h_exec->set_cpu(spe_cpu);
205 h_exec->spawn(); 206 h_exec->spawn();
206 } else if (use_iterate) { 207 } else if (use_iterate) {
216 h_exec->set_outData(1,w->o_data,w->out_size_); 217 h_exec->set_outData(1,w->o_data,w->out_size_);
217 h_exec->set_param(0,(long)i); 218 h_exec->set_param(0,(long)i);
218 h_exec->set_param(1,(long)w->division_size); 219 h_exec->set_param(1,(long)w->division_size);
219 h_exec->set_param(2,(long)size); 220 h_exec->set_param(2,(long)size);
220 h_exec->set_param(3,(long)w->out_size); 221 h_exec->set_param(3,(long)w->out_size);
221 222
222 t_next->wait_for(h_exec); 223 t_next->wait_for(h_exec);
223 h_exec->set_cpu(spe_cpu); 224 h_exec->set_cpu(spe_cpu);
224 h_exec->iterate(array_task_num); 225 h_exec->iterate(array_task_num);
225 226
226 w->task_num -= array_task_num; 227 w->task_num -= array_task_num;
227 w->task_spwaned += array_task_num-1; 228 w->task_spwaned += array_task_num-1;
228 229
229 if(w->size < 0) { 230 if(w->size < 0) {
230 h_exec = manager->create_task(TASK_EXEC_DATA_PARALLEL); 231 h_exec = manager->create_task(TASK_EXEC_DATA_PARALLEL);
231 h_exec->flip(); 232 h_exec->flip();
232 h_exec->set_inData(0,w->file_mmap,w->file_size); 233 h_exec->set_inData(0,w->file_mmap,w->file_size);
233 h_exec->set_inData(1,w->o_data,w->out_size_); 234 h_exec->set_inData(1,w->o_data,w->out_size_);
235 h_exec->set_outData(1,w->o_data,w->out_size_); 236 h_exec->set_outData(1,w->o_data,w->out_size_);
236 h_exec->set_param(0,(long)w->task_spwaned); 237 h_exec->set_param(0,(long)w->task_spwaned);
237 h_exec->set_param(1,(long)w->division_size); 238 h_exec->set_param(1,(long)w->division_size);
238 h_exec->set_param(2,(long)(size+w->size)); 239 h_exec->set_param(2,(long)(size+w->size));
239 h_exec->set_param(3,(long)w->out_size); 240 h_exec->set_param(3,(long)w->out_size);
240 241
241 t_next->wait_for(h_exec); 242 t_next->wait_for(h_exec);
242 h_exec->set_cpu(spe_cpu); 243 h_exec->set_cpu(spe_cpu);
243 h_exec->iterate(1); 244 h_exec->iterate(1);
244 245
245 w->task_num -= 1; 246 w->task_num -= 1;
292 // printf("run16 last %d\n",w->task_num); 293 // printf("run16 last %d\n",w->task_num);
293 } else { 294 } else {
294 HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS, 295 HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS,
295 (memaddr)&w->self,sizeof(memaddr),0,0); 296 (memaddr)&w->self,sizeof(memaddr),0,0);
296 w->t_print->wait_for(t_next); 297 w->t_print->wait_for(t_next);
297 298
298 run_tasks(manager,w, w->task_blocks, t_next, w->division_size); 299 run_tasks(manager,w, w->task_blocks, t_next, w->division_size);
299 300
300 t_next->spawn(); 301 t_next->spawn();
301 // printf("run16 next %d\n",w->task_num); 302 // printf("run16 next %d\n",w->task_num);
302 } 303 }
326 //WordCount *w = (WordCount*)manager->allocate(sizeof(WordCount)); 327 //WordCount *w = (WordCount*)manager->allocate(sizeof(WordCount));
327 WordCountPtr w = (WordCountPtr)manager->allocate(sizeof(WordCount)); 328 WordCountPtr w = (WordCountPtr)manager->allocate(sizeof(WordCount));
328 329
329 w->self = w; 330 w->self = w;
330 w->fd = fd; 331 w->fd = fd;
331 w->read_cpu = spe_cpu; 332 w->read_cpu = read_spe_cpu;
332 w->read_task_blocks = 16; 333 w->read_task_blocks = 16;
333 w->read_filesize = sb->st_size; 334 w->read_filesize = sb->st_size;
334 w->read_left_size = w->read_filesize; 335 w->read_left_size = w->read_filesize;
335 w->read_division_size = READ_DIVISION_SIZE; 336 w->read_division_size = READ_DIVISION_SIZE;
336 w->read_task_num = w->read_filesize / READ_DIVISION_SIZE; 337 w->read_task_num = w->read_filesize / READ_DIVISION_SIZE;
338 339
339 printf("filesize : %d\n",w->read_filesize); 340 printf("filesize : %d\n",w->read_filesize);
340 printf("one_task_size: %d\n",w->read_division_size); 341 printf("one_task_size: %d\n",w->read_division_size);
341 printf("task_num : %d\n",w->read_task_num); 342 printf("task_num : %d\n",w->read_task_num);
342 343
343 HTaskPtr run = NULL; 344 HTaskPtr r_run = NULL;
344 345
345 if (divide_read_flag != 0) { 346 if (divide_read_flag != 0) {
346 char *read_text = (char*)manager->allocate(w->read_filesize); 347 char *read_text = (char*)manager->allocate(w->read_filesize);
347 run = manager->create_task(RUN_READ_BLOCKS, (memaddr)&w->self, sizeof(memaddr),read_text,w->read_filesize); 348 r_run = manager->create_task(RUN_READ_BLOCKS, (memaddr)&w->self, sizeof(memaddr),read_text,w->read_filesize);
348 w->read_text = read_text; 349 w->read_text = read_text;
349 }else { 350 }else {
350 //my_mmap(filename, fd, fr); 351 //my_mmap(filename, fd, fr);
351 run = manager->create_task(MMAP , (memaddr)&w->self, sizeof(memaddr),0,0); 352 r_run = manager->create_task(MMAP , (memaddr)&w->self, sizeof(memaddr),0,0);
352 } 353 }
353 354
354 run->spawn();
355 355
356 /* original */ 356 /* original */
357 HTaskPtr t_print; 357 HTaskPtr t_print;
358 358
359 st_mmap_t st_mmap; 359 st_mmap_t st_mmap;
360 //st_mmap = my_mmap(filename); 360 //st_mmap = my_mmap(filename);
361 // bzero(w,sizeof(WordCount));
362 361
363 //w->task_blocks = blocks; 362 //w->task_blocks = blocks;
364 w->self = w; 363 w->self = w;
365 w->task_spwaned = 0; 364 w->task_spwaned = 0;
366 365
367 /*sizeはdivision_sizeの倍数にしている。*/ 366 /*sizeはdivision_sizeの倍数にしている。*/
368 w->size = w->file_size = st_mmap.size; 367 w->size = w->file_size = w->read_filesize;
369 w->file_mmap = st_mmap.file_mmap; 368 //w->file_mmap = st_mmap.file_mmap;
370 printf("w %lx\n",(long)w); 369 printf("w %lx\n",(long)w);
371 370
372 /* 1task分のデータサイズ(byte) */ 371 /* 1task分のデータサイズ(byte) */
373 if (w->size >= 1024*division) { 372 if (w->size >= 1024*division) {
374 w->division_size = 1024 * division;/*16kbyte*/ 373 w->division_size = 1024 * division;/*16kbyte*/
406 405
407 /*各SPEの結果を合計して出力するタスク*/ 406 /*各SPEの結果を合計して出力するタスク*/
408 407
409 t_print = manager->create_task(TASK_PRINT, 408 t_print = manager->create_task(TASK_PRINT,
410 (memaddr)&w->self,sizeof(memaddr),0,0); 409 (memaddr)&w->self,sizeof(memaddr),0,0);
411 w->t_print = t_print; 410 w->t_print = t_print;
412 for(int i=0;i<1;i++) { 411 for(int i=0;i<1;i++) {
413 /* Task を task_blocks ずつ起動する Task */ 412 /* Task を task_blocks ずつ起動する Task */
414 /* serialize されていると仮定する... */ 413 /* serialize されていると仮定する... */
415 HTaskPtr t_exec = manager->create_task(RUN_TASK_BLOCKS, 414 HTaskPtr t_exec = manager->create_task(RUN_TASK_BLOCKS,
416 (memaddr)&w->self,sizeof(memaddr),0,0); 415 (memaddr)&w->self,sizeof(memaddr),0,0);
416 t_exec->wait_for(r_run);
417 t_print->wait_for(t_exec); 417 t_print->wait_for(t_exec);
418 // t_exec->iterate(4); 418 // t_exec->iterate(4);
419 t_exec->spawn(); 419 t_exec->spawn();
420 } 420 }
421 t_print->wait_for(r_run);
422 r_run->spawn();
421 t_print->spawn(); 423 t_print->spawn();
422 } 424 }
423 425
424 static char* 426 static char*
425 init(int argc, char **argv) 427 init(int argc, char **argv)