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
|
|
12 extern void task_init(void);
|
|
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
|
|
70 run_start(char *filename)
|
|
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 }
|
|
167
|
|
168 return filename;
|
|
169 }
|
|
170
|
|
171 int
|
|
172 TMmain(int argc, char *argv[])
|
|
173 {
|
|
174
|
394
|
175 char *filename = 0;
|
|
176 filename = init(argc, argv);
|
393
|
177
|
|
178 if (filename < 0) {
|
|
179 return -1;
|
|
180 }
|
|
181
|
|
182 task_init();
|
|
183 run_start(filename);
|
|
184
|
|
185 return 0;
|
|
186 }
|