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