Mercurial > hg > Game > Cerium
annotate example/word_count/main.cc @ 625:94d82f2c842f draft
64bit mode worked on Mac OS X.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 16 Nov 2009 10:59:55 +0900 |
parents | 984e7890db0c |
children | fdf78db1530b |
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; | |
85 if (size >= 4096*4) { | |
86 division_size = 4096 * 4;/*16kbyte*/ | |
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; | |
122 int i; | |
123 for (i = 0; i < task_num; i++) { | |
124 | |
125 t_exec = manager->create_task(TASK_EXEC); | |
126 t_exec->add_inData(file_mmap + i*division_size, division_size); | |
127 t_exec->add_outData(o_data + i*status_num, division_out_size); | |
394 | 128 t_exec->add_param(division_size); |
393 | 129 t_exec->add_param(word_flag); |
130 t_exec->set_cpu(SPE_ANY); | |
131 t_print->wait_for(t_exec); | |
132 t_exec->spawn(); | |
133 | |
134 word_flag = ((file_mmap[(i+1)*division_size-1] != 0x20) && (file_mmap[(i+1)*division_size-1] != 0x0A)); | |
135 | |
394 | 136 size -= division_size; |
137 | |
393 | 138 } |
139 | |
394 | 140 if (size) { |
141 | |
142 t_exec = manager->create_task(TASK_EXEC); | |
143 t_exec->add_inData(file_mmap + i*division_size, size); | |
144 t_exec->add_outData(o_data + i*status_num, division_out_size); | |
145 t_exec->add_param(size); | |
146 t_exec->add_param(word_flag); | |
147 t_exec->set_cpu(SPE_ANY); | |
148 t_print->wait_for(t_exec); | |
149 t_exec->spawn(); | |
150 } | |
151 | |
152 | |
393 | 153 t_print->spawn(); |
154 } | |
155 | |
156 char* | |
157 init(int argc, char **argv) | |
158 { | |
159 | |
394 | 160 char *filename = 0; |
393 | 161 |
394 | 162 for (int i = 1; argv[i]; ++i) { |
393 | 163 if (strcmp(argv[i], "-file") == 0) { |
164 filename = argv[i+1]; | |
165 } | |
166 } | |
400 | 167 if (filename==0) { |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
168 printf("usage: %s [-count 10] -file filename\n",argv[0]); |
400 | 169 exit(1); |
170 } | |
393 | 171 |
172 return filename; | |
173 } | |
174 | |
175 int | |
400 | 176 TMmain(TaskManager *manager, int argc, char *argv[]) |
393 | 177 { |
178 | |
394 | 179 char *filename = 0; |
180 filename = init(argc, argv); | |
393 | 181 |
182 if (filename < 0) { | |
183 return -1; | |
184 } | |
185 | |
186 task_init(); | |
400 | 187 run_start(manager, filename); |
393 | 188 |
189 return 0; | |
190 } |