Mercurial > hg > Members > kono > Cerium
annotate example/word_count_test/main.cc @ 665:4470ff271c5a
word_count_test
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 03 Dec 2009 01:16:34 +0900 |
parents | 7405d92733a8 |
children | 4615489c5faa |
rev | line source |
---|---|
661 | 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 "TaskManager.h" | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
10 #include "SchedTask.h" |
661 | 11 #include "Func.h" |
12 | |
13 extern void task_init(); | |
14 | |
15 const char *usr_help_str = "Usage: ./word_count [-cpu spe_num] [-file filename]\n"; | |
16 | |
17 typedef struct { | |
18 caddr_t file_mmap; | |
19 off_t size; | |
20 } st_mmap_t; | |
21 | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
22 typedef struct { |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
23 int size; // remaining file size |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
24 int division_size; // for each word count task |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
25 int division_out_size; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
26 int task_num; // remaining task count |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
27 int task_blocks; // spawn task one at a time |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
28 int status_num; |
664 | 29 int task_spwaned; |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
30 unsigned long long *o_data; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
31 unsigned long long *head_tail_flag; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
32 int pad; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
33 caddr_t file_mmap; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
34 HTaskPtr t_print; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
35 } WordCount; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
36 |
661 | 37 |
38 /*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/ | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
39 static int |
661 | 40 fix_byte(int size,int fix_byte_size) |
41 { | |
42 size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size; | |
43 | |
44 return size; | |
45 } | |
46 | |
47 | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
48 static st_mmap_t |
661 | 49 my_mmap(char *filename) |
50 { | |
51 | |
52 /*マッピングだよ!*/ | |
53 int fd = -1; | |
54 int map = MAP_PRIVATE; | |
55 st_mmap_t st_mmap; | |
56 struct stat sb; | |
57 | |
58 if ((fd=open(filename,O_RDONLY,0666))==0) { | |
59 fprintf(stderr,"can't open %s\n",filename); | |
60 } | |
61 | |
62 if (fstat(fd,&sb)) { | |
63 fprintf(stderr,"can't fstat %s\n",filename); | |
64 } | |
65 | |
66 printf("file size %d\n",(int)sb.st_size); | |
67 | |
68 /*sizeをページングサイズの倍数にあわせる*/ | |
69 st_mmap.size = fix_byte(sb.st_size,4096); | |
70 | |
71 printf("fix 4096byte file size %d\n",(int)st_mmap.size); | |
72 | |
73 st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0); | |
74 if (st_mmap.file_mmap == (caddr_t)-1) { | |
75 fprintf(stderr,"Can't mmap file\n"); | |
76 perror(NULL); | |
77 exit(0); | |
78 } | |
79 | |
80 return st_mmap; | |
81 | |
82 } | |
83 | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
84 static void |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
85 run_tasks(SchedTask *manager, WordCount *w, int task_count, HTaskPtr t_next, int size) |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
86 { |
664 | 87 for (int j = 0; j < task_count && w->size>0; j++) { |
88 int i = w->task_spwaned++; | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
89 HTaskPtr t_exec = manager->create_task(TASK_EXEC); |
664 | 90 if (size>w->size) size = w->size; |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
91 t_exec->add_inData(w->file_mmap + i*w->division_size, size); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
92 t_exec->add_outData(w->o_data + i*w->status_num, w->division_out_size); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
93 t_exec->add_outData(w->head_tail_flag + i*w->pad, w->division_out_size); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
94 t_exec->add_param(size); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
95 t_exec->set_cpu(SPE_ANY); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
96 t_next->wait_for(t_exec); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
97 t_exec->spawn(); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
98 w->size -= size; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
99 w->task_num--; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
100 } |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
101 } |
661 | 102 |
664 | 103 SchedDefineTask1(RUN_TASK_BLOCKS,run16); |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
104 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
105 static int |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
106 run16(SchedTask *manager, void *in, void *out) |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
107 { |
664 | 108 WordCount *w = (WordCount *)manager->get_param(0); |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
109 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
110 if (w->task_num < w->task_blocks) { |
664 | 111 if (w->size >= w->division_size) |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
112 run_tasks(manager,w,w->task_blocks, w->t_print, w->division_size); |
664 | 113 while (w->size>0) |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
114 run_tasks(manager,w,1, w->t_print, w->size); |
664 | 115 // printf("run16 last %d\n",w->task_num); |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
116 } else { |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
117 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
118 HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
119 t_next->set_param(0,(void*)w); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
120 w->t_print->wait_for(t_next); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
121 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
122 run_tasks(manager,w, w->task_blocks, t_next, w->division_size); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
123 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
124 t_next->spawn(); |
664 | 125 // printf("run16 next %d\n",w->task_num); |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
126 } |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
127 return 0; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
128 } |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
129 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
130 |
665 | 131 static int blocks = 192; |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
132 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
133 static void |
661 | 134 run_start(TaskManager *manager, char *filename) |
135 { | |
136 HTaskPtr t_print; | |
137 | |
138 st_mmap_t st_mmap; | |
139 st_mmap = my_mmap(filename); | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
140 WordCount *w = (WordCount*)manager->allocate(sizeof(WordCount)); |
664 | 141 // bzero(w,sizeof(WordCount)); |
661 | 142 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
143 w->task_blocks = blocks; |
664 | 144 w->task_spwaned = 0; |
661 | 145 |
146 /*sizeはdivision_sizeの倍数にしている。*/ | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
147 w->size = st_mmap.size; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
148 w->file_mmap = st_mmap.file_mmap; |
661 | 149 |
150 /* 1task分のデータサイズ(byte) */ | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
151 if (w->size >= 4096*4) { |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
152 w->division_size = 4096 * 4;/*16kbyte*/ |
661 | 153 } |
154 else { | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
155 w->division_size = w->size; |
661 | 156 } |
157 | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
158 printf("dvision_size %d\n",w->division_size); |
661 | 159 |
160 /* "word num" and "line num" */ | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
161 w-> status_num = 2; |
661 | 162 /* taskの数 */ |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
163 w-> task_num = w->size / w->division_size; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
164 int out_task_num = w->task_num + (w->division_size*w->task_num < w->size); |
661 | 165 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
166 printf("task_num %d\n",w->task_num); |
661 | 167 |
168 /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */ | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
169 w-> division_out_size = 16; |
661 | 170 /* out用のデータのサイズ。*/ |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
171 int out_size = w->division_out_size*out_task_num; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
172 w->o_data = (unsigned long long*)manager->allocate(out_size); |
664 | 173 //bzero(w->o_data,out_size); |
661 | 174 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
175 w-> pad = 2; |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
176 w->head_tail_flag = (unsigned long long*)manager->allocate(out_size); |
664 | 177 // bzero(w->head_tail_flag,out_size); |
661 | 178 |
179 printf("out size %d\n",out_size); | |
180 | |
181 /*各SPEの結果を合計して出力するタスク*/ | |
182 | |
183 t_print = manager->create_task(TASK_PRINT); | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
184 t_print->add_inData(w->o_data, out_size); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
185 t_print->add_inData(w->head_tail_flag, out_size); |
661 | 186 t_print->add_param(out_task_num); |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
187 t_print->add_param(w->status_num); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
188 t_print->add_param(out_task_num); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
189 t_print->add_param(w->pad); |
661 | 190 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
191 w->t_print = t_print; |
661 | 192 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
193 /* Task を task_blocks ずつ起動する Task */ |
661 | 194 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
195 HTaskPtr t_exec = manager->create_task(RUN_TASK_BLOCKS); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
196 t_exec->set_param(0,(void*)w); |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
197 t_exec->spawn(); |
661 | 198 |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
199 t_print->wait_for(t_exec); |
661 | 200 t_print->spawn(); |
201 } | |
202 | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
203 static char* |
661 | 204 init(int argc, char **argv) |
205 { | |
206 | |
207 char *filename = 0; | |
208 | |
209 for (int i = 1; argv[i]; ++i) { | |
210 if (strcmp(argv[i], "-file") == 0) { | |
211 filename = argv[i+1]; | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
212 } else if (strcmp(argv[i], "-block") == 0) { |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
213 blocks = atoi(argv[i+1]); |
661 | 214 } |
215 } | |
216 if (filename==0) { | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
217 printf("usage: %s [-block 10] -file filename\n",argv[0]); |
661 | 218 exit(1); |
219 } | |
220 | |
221 return filename; | |
222 } | |
223 | |
224 int | |
225 TMmain(TaskManager *manager, int argc, char *argv[]) | |
226 { | |
227 | |
228 char *filename = 0; | |
229 filename = init(argc, argv); | |
230 | |
231 if (filename < 0) { | |
232 return -1; | |
233 } | |
234 | |
235 task_init(); | |
236 run_start(manager, filename); | |
237 | |
238 return 0; | |
239 } | |
663
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
240 |
8a807e2f64f8
incremental task creation on word count_test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
661
diff
changeset
|
241 /* end */ |