comparison example/word_count/main.cc @ 394:e08d0aa94367

debug word_count
author e065725@localhost.localdomain
date Wed, 16 Sep 2009 22:33:40 +0900
parents 72b73beb3325
children 00fe05184a02
comparison
equal deleted inserted replaced
393:72b73beb3325 394:e08d0aa94367
9 #include "TaskManager.h" 9 #include "TaskManager.h"
10 #include "Func.h" 10 #include "Func.h"
11 11
12 extern void task_init(void); 12 extern void task_init(void);
13 13
14 const char *usr_help_str = "Usage: ./dependency [-cpu spe_num] [-nodepend]\n\ 14 const char *usr_help_str = "Usage: ./word_count [-cpu spe_num] [-file filename]\n";
15 -nodepend No Depenendcy";
16 15
17 typedef struct { 16 typedef struct {
18 caddr_t file_mmap; 17 caddr_t file_mmap;
19 off_t size; 18 off_t size;
20 } st_mmap_t; 19 } st_mmap_t;
51 printf("file size %d\n",(int)sb.st_size); 50 printf("file size %d\n",(int)sb.st_size);
52 51
53 /*sizeをページングサイズの倍数にあわせる*/ 52 /*sizeをページングサイズの倍数にあわせる*/
54 st_mmap.size = fix_byte(sb.st_size,4096); 53 st_mmap.size = fix_byte(sb.st_size,4096);
55 54
56 printf("fix 4096byte file size %d\n",st_mmap.size); 55 printf("fix 4096byte file size %d\n",(int)st_mmap.size);
57 56
58 st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0); 57 st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0);
59 if (st_mmap.file_mmap == (caddr_t)-1) { 58 if (st_mmap.file_mmap == (caddr_t)-1) {
60 fprintf(stderr,"Can't mmap file\n"); 59 fprintf(stderr,"Can't mmap file\n");
61 perror(NULL); 60 perror(NULL);
88 } 87 }
89 else { 88 else {
90 division_size = size; 89 division_size = size;
91 } 90 }
92 91
93 division_size = 4096;
94
95 printf("dvision_size %d\n",division_size); 92 printf("dvision_size %d\n",division_size);
96 93
97 /* "word num" and "line num" */ 94 /* "word num" and "line num" */
98 int status_num = 2; 95 int status_num = 2;
99 /* taskの数 */ 96 /* taskの数 */
100 int task_num = size / division_size; 97 int task_num = size / division_size;
98 int out_task_num = task_num + (division_size*task_num < size);
99
101 printf("task_num %d\n",task_num); 100 printf("task_num %d\n",task_num);
101
102 /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */ 102 /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */
103 int division_out_size = 16; 103 int division_out_size = 16;
104 /* out用のデータのサイズ。*/ 104 /* out用のデータのサイズ。*/
105 int out_size = division_out_size*task_num; 105 int out_size = division_out_size*out_task_num;
106 unsigned long long *o_data = (unsigned long long*)manager->allocate(out_size); 106 unsigned long long *o_data = (unsigned long long*)manager->allocate(out_size);
107 107
108 108
109 printf("out size %d\n",out_size); 109 printf("out size %d\n",out_size);
110 110
111 /*各SPEの結果を合計して出力するタスク*/ 111 /*各SPEの結果を合計して出力するタスク*/
112 112
113 t_print = manager->create_task(TASK_PRINT); 113 t_print = manager->create_task(TASK_PRINT);
114 t_print->add_inData(o_data, out_size); 114 t_print->add_inData(o_data, out_size);
115 t_print->add_param(task_num); 115 t_print->add_param(out_task_num);
116 t_print->add_param(status_num); 116 t_print->add_param(status_num);
117
118 int length = division_size;
119
120 printf("length %d\n",length);
121 117
122 /*各SPEに処理するデータを割り振る。*/ 118 /*各SPEに処理するデータを割り振る。*/
123 119
124 /*渡すデータの最後が文字かどうか。(スペース、改行以外)*/ 120 /*渡すデータの最後が文字かどうか。(スペース、改行以外)*/
125 int word_flag = 0; 121 int word_flag = 0;
127 for (i = 0; i < task_num; i++) { 123 for (i = 0; i < task_num; i++) {
128 124
129 t_exec = manager->create_task(TASK_EXEC); 125 t_exec = manager->create_task(TASK_EXEC);
130 t_exec->add_inData(file_mmap + i*division_size, division_size); 126 t_exec->add_inData(file_mmap + i*division_size, division_size);
131 t_exec->add_outData(o_data + i*status_num, division_out_size); 127 t_exec->add_outData(o_data + i*status_num, division_out_size);
132 t_exec->add_param(length); 128 t_exec->add_param(division_size);
133 t_exec->add_param(word_flag); 129 t_exec->add_param(word_flag);
134 t_exec->set_cpu(SPE_ANY); 130 t_exec->set_cpu(SPE_ANY);
135 t_print->wait_for(t_exec); 131 t_print->wait_for(t_exec);
136 t_exec->spawn(); 132 t_exec->spawn();
137 133
138 word_flag = ((file_mmap[(i+1)*division_size-1] != 0x20) && (file_mmap[(i+1)*division_size-1] != 0x0A)); 134 word_flag = ((file_mmap[(i+1)*division_size-1] != 0x20) && (file_mmap[(i+1)*division_size-1] != 0x0A));
139 135
136 size -= division_size;
137
140 } 138 }
139
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
141 152
142 t_print->spawn(); 153 t_print->spawn();
143 } 154 }
144 155
145 char* 156 char*
146 init(int argc, char **argv) 157 init(int argc, char **argv)
147 { 158 {
148 159
149 char *filename; 160 char *filename = 0;
150 161
151 for (int i = 1; argv[i]; ++i) { 162 for (int i = 1; argv[i]; ++i) {
152
153 if (strcmp(argv[i], "-file") == 0) { 163 if (strcmp(argv[i], "-file") == 0) {
154 filename = argv[i+1]; 164 filename = argv[i+1];
155 } 165 }
156
157 } 166 }
158 167
159 return filename; 168 return filename;
160 } 169 }
161 170
162 int 171 int
163 TMmain(int argc, char *argv[]) 172 TMmain(int argc, char *argv[])
164 { 173 {
165 174
166 char *filename = init(argc, argv); 175 char *filename = 0;
167 176 filename = init(argc, argv);
168 177
169 if (filename < 0) { 178 if (filename < 0) {
170 return -1; 179 return -1;
171 } 180 }
172 181