Mercurial > hg > Game > Cerium
annotate example/word_count/main.cc @ 783:76781837ff3a draft
wordcount fix?
author | yutaka@localhost.localdomain |
---|---|
date | Tue, 20 Apr 2010 15:37:42 +0900 |
parents | 55ccf5f6d9af |
children | 083c4a270809 |
rev | line source |
---|---|
393 | 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" | |
10 #include "Func.h" | |
11 | |
400 | 12 extern void task_init(); |
393 | 13 |
394 | 14 const char *usr_help_str = "Usage: ./word_count [-cpu spe_num] [-file filename]\n"; |
393 | 15 |
16 typedef struct { | |
17 caddr_t file_mmap; | |
18 off_t size; | |
19 } st_mmap_t; | |
20 | |
21 | |
22 /*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/ | |
23 int | |
24 fix_byte(int size,int fix_byte_size) | |
25 { | |
26 size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size; | |
27 | |
28 return size; | |
29 } | |
30 | |
31 | |
32 st_mmap_t | |
33 my_mmap(char *filename) | |
34 { | |
35 | |
36 /*マッピングだよ!*/ | |
37 int fd = -1; | |
38 int map = MAP_PRIVATE; | |
39 st_mmap_t st_mmap; | |
40 struct stat sb; | |
41 | |
42 if ((fd=open(filename,O_RDONLY,0666))==0) { | |
43 fprintf(stderr,"can't open %s\n",filename); | |
44 } | |
45 | |
46 if (fstat(fd,&sb)) { | |
47 fprintf(stderr,"can't fstat %s\n",filename); | |
48 } | |
49 | |
50 printf("file size %d\n",(int)sb.st_size); | |
51 | |
52 /*sizeをページングサイズの倍数にあわせる*/ | |
53 st_mmap.size = fix_byte(sb.st_size,4096); | |
54 | |
394 | 55 printf("fix 4096byte file size %d\n",(int)st_mmap.size); |
393 | 56 |
57 st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0); | |
58 if (st_mmap.file_mmap == (caddr_t)-1) { | |
59 fprintf(stderr,"Can't mmap file\n"); | |
60 perror(NULL); | |
61 exit(0); | |
62 } | |
63 | |
64 return st_mmap; | |
65 | |
66 } | |
67 | |
68 | |
69 void | |
400 | 70 run_start(TaskManager *manager, char *filename) |
393 | 71 { |
72 HTaskPtr t_exec; | |
73 HTaskPtr t_print; | |
74 | |
75 st_mmap_t st_mmap; | |
76 st_mmap = my_mmap(filename); | |
77 | |
78 | |
79 /*sizeはdivision_sizeの倍数にしている。*/ | |
80 int size = st_mmap.size; | |
81 char *file_mmap = st_mmap.file_mmap; | |
82 | |
83 /* 1task分のデータサイズ(byte) */ | |
84 int division_size; | |
783 | 85 if (size >= 1024*16) { |
86 division_size = 1024 * 16;/*16kbyte*/ | |
393 | 87 } |
88 else { | |
89 division_size = size; | |
90 } | |
91 | |
92 printf("dvision_size %d\n",division_size); | |
93 | |
94 /* "word num" and "line num" */ | |
95 int status_num = 2; | |
96 /* taskの数 */ | |
97 int task_num = size / division_size; | |
394 | 98 int out_task_num = task_num + (division_size*task_num < size); |
99 | |
393 | 100 printf("task_num %d\n",task_num); |
394 | 101 |
393 | 102 /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */ |
103 int division_out_size = 16; | |
104 /* out用のデータのサイズ。*/ | |
394 | 105 int out_size = division_out_size*out_task_num; |
393 | 106 unsigned long long *o_data = (unsigned long long*)manager->allocate(out_size); |
107 | |
108 | |
109 printf("out size %d\n",out_size); | |
110 | |
111 /*各SPEの結果を合計して出力するタスク*/ | |
112 | |
113 t_print = manager->create_task(TASK_PRINT); | |
114 t_print->add_inData(o_data, out_size); | |
394 | 115 t_print->add_param(out_task_num); |
393 | 116 t_print->add_param(status_num); |
117 | |
118 /*各SPEに処理するデータを割り振る。*/ | |
119 | |
120 /*渡すデータの最後が文字かどうか。(スペース、改行以外)*/ | |
121 int word_flag = 0; | |
783 | 122 int index = 1; |
123 int array_task_num = 1; | |
124 for (int i = 0; i < task_num; i += array_task_num) { | |
125 HTask *task_main = manager->create_task_array(TASK_EXEC,array_task_num,2,1,1); | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
126 int j ; |
743 | 127 Task *t_exec = 0; |
783 | 128 for(j = 0; j < array_task_num; j++) { |
743 | 129 t_exec = task_main->next_task_array(TASK_EXEC,t_exec); |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
130 t_print->wait_for(task_main); |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
131 |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
132 // t_exec = manager->create_task(TASK_EXEC); |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
133 t_exec->set_param(0,division_size); |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
134 t_exec->set_param(1,word_flag); |
783 | 135 |
136 //t_exec->set_inData(0,file_mmap + index*division_size, division_size); | |
137 //t_exec->set_outData(0,o_data + index*status_num, division_out_size); | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
138 |
783 | 139 printf("%c",file_mmap[index*division_size]); |
140 | |
141 t_exec->set_inData(0,&file_mmap[index*division_size], division_size); | |
142 t_exec->set_outData(0,&o_data[index*status_num], division_out_size); | |
143 | |
144 word_flag = ((file_mmap[(index+1)*division_size-1] != 0x20) && (file_mmap[(index+1)*division_size-1] != 0x0A)); | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
145 |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
146 size -= division_size; |
783 | 147 index++; |
148 | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
149 } |
783 | 150 |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
151 task_main->spawn_task_array(t_exec->next()); |
783 | 152 task_main->set_cpu(SPE_ANY); |
153 task_main->spawn(); | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
154 |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
155 } |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
156 |
783 | 157 |
158 | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
159 while (size>0) { |
393 | 160 |
161 t_exec = manager->create_task(TASK_EXEC); | |
742
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
162 t_exec->set_param(0,size); |
fdf78db1530b
Task Arrayed Word Count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
163 t_exec->set_param(1,word_flag); |
783 | 164 t_exec->set_inData(0,file_mmap + index*division_size, size); |
165 t_exec->set_outData(0,o_data + index*status_num, division_out_size); | |
393 | 166 t_exec->set_cpu(SPE_ANY); |
167 t_print->wait_for(t_exec); | |
168 t_exec->spawn(); | |
783 | 169 index++; |
394 | 170 size -= division_size; |
171 } | |
172 | |
393 | 173 t_print->spawn(); |
174 } | |
175 | |
176 char* | |
177 init(int argc, char **argv) | |
178 { | |
179 | |
394 | 180 char *filename = 0; |
393 | 181 |
394 | 182 for (int i = 1; argv[i]; ++i) { |
393 | 183 if (strcmp(argv[i], "-file") == 0) { |
184 filename = argv[i+1]; | |
185 } | |
186 } | |
400 | 187 if (filename==0) { |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
188 printf("usage: %s [-count 10] -file filename\n",argv[0]); |
400 | 189 exit(1); |
190 } | |
393 | 191 |
192 return filename; | |
193 } | |
194 | |
195 int | |
400 | 196 TMmain(TaskManager *manager, int argc, char *argv[]) |
393 | 197 { |
198 | |
394 | 199 char *filename = 0; |
200 filename = init(argc, argv); | |
393 | 201 |
202 if (filename < 0) { | |
203 return -1; | |
204 } | |
205 | |
206 task_init(); | |
400 | 207 run_start(manager, filename); |
393 | 208 |
209 return 0; | |
210 } |