annotate example/word_count/main.cc @ 967:3e1c218da048

add anum option in WordCount
author Yutaka Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
date Sun, 08 Aug 2010 14:00:12 +0900
parents e0908a8955cd
children 0a6c5ee89a4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
1 #include <stdio.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
2 #include <stdlib.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
3 #include <string.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
4 #include <sys/mman.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
5 #include <sys/types.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
6 #include <sys/stat.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
7 #include <fcntl.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
8 #include <unistd.h>
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
9 #include "TaskManager.h"
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
10 #include "SchedTask.h"
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
11 #include "Func.h"
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
12 #include "WordCount.h"
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
13
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
14 extern void task_init();
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
15
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
16 int all = 0;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
17 int use_task_array = 1;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
18 int use_compat = 0;
967
3e1c218da048 add anum option in WordCount
Yutaka Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents: 950
diff changeset
19 int array_task_num = 8;
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
20
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
21 const char *usr_help_str = "Usage: ./word_count [-a -c -s] [-cpu spe_num] [-file filename]\n";
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
22
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
23 typedef struct {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
24 caddr_t file_mmap;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
25 off_t size;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
26 } st_mmap_t;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
27
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
28
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
29
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
30 /*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
31 static int
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
32 fix_byte(int size,int fix_byte_size)
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
33 {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
34 size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
35
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
36 return size;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
37 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
38
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
39
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
40 static st_mmap_t
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
41 my_mmap(char *filename)
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
42 {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
43
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
44 /*マッピングだよ!*/
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
45 int fd = -1;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
46 int map = MAP_PRIVATE;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
47 st_mmap_t st_mmap;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
48 struct stat sb;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
49
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
50 if ((fd=open(filename,O_RDONLY,0666))==0) {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
51 fprintf(stderr,"can't open %s\n",filename);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
52 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
53
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
54 if (fstat(fd,&sb)) {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
55 fprintf(stderr,"can't fstat %s\n",filename);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
56 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
57
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
58 printf("file size %d\n",(int)sb.st_size);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
59
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
60 /*sizeをページングサイズの倍数にあわせる*/
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
61 st_mmap.size = fix_byte(sb.st_size,4096);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
62
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
63 printf("fix 4096byte file size %d\n",(int)st_mmap.size);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
64
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
65 st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
66 if (st_mmap.file_mmap == (caddr_t)-1) {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
67 fprintf(stderr,"Can't mmap file\n");
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
68 perror(NULL);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
69 exit(0);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
70 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
71
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
72 return st_mmap;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
73
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
74 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
75
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
76 static void
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
77 run_tasks(SchedTask *manager, WordCount *w, int task_count, HTaskPtr t_next, int size)
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
78 {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
79
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
80 if (task_count < array_task_num) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
81 array_task_num = task_count;
948
6a8941ee8294 fix word count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 946
diff changeset
82 if (task_count<=0) return;
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
83 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
84
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
85 for (int i = 0; i < task_count; i += array_task_num) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
86
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
87 HTask *task_array;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
88 if (use_task_array) {
950
e0908a8955cd word count size fix for task array
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 949
diff changeset
89 int task_num = (w->size+size-1)/size;
e0908a8955cd word count size fix for task array
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 949
diff changeset
90 if (task_num>array_task_num) task_num = array_task_num;
e0908a8955cd word count size fix for task array
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 949
diff changeset
91 task_array = manager->create_task_array(TASK_EXEC,task_num,0,1,1);
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
92 if (!all) t_next->wait_for(task_array);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
93 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
94
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
95 Task *t_exec = 0;
948
6a8941ee8294 fix word count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 946
diff changeset
96 HTask *h_exec = 0;
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
97 for (int j = 0; j < array_task_num; j++) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
98 int i = w->task_spwaned++;
949
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
99 if (w->size < size) size = w->size;
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
100 if (size==0) break;
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
101 if (use_task_array) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
102 t_exec = task_array->next_task_array(TASK_EXEC,t_exec);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
103 t_exec->set_inData(0,w->file_mmap + i*w->division_size, size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
104 t_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
105 } else if (use_compat) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
106 h_exec = manager->create_task(TASK_EXEC);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
107 h_exec->set_inData(0,w->file_mmap + i*w->division_size, size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
108 h_exec->set_outData(0,w->o_data + i*w->out_size, w->division_out_size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
109 h_exec->set_cpu(SPE_ANY);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
110 h_exec->spawn();
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
111 } else {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
112 h_exec = manager->create_task(TASK_EXEC,
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
113 (memaddr)(w->file_mmap + i*w->division_size), size,
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
114 (memaddr)(w->o_data + i*w->out_size), w->division_out_size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
115 h_exec->set_cpu(SPE_ANY);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
116 h_exec->spawn();
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
117 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
118 w->size -= size;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
119 w->task_num--;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
120 }
948
6a8941ee8294 fix word count
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 946
diff changeset
121 if (use_task_array) {
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
122 task_array->spawn_task_array(t_exec->next());
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
123 task_array->set_cpu(SPE_ANY);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
124 task_array->spawn();
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
125 } else {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
126 if (!all) t_next->wait_for(h_exec);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
127 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
128 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
129 }
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
130
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
131 /**
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
132 * このTaskは、PPE上で実行されるので、並列に実行されることはない
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
133 * 二つ実行されていて、Task が足りなくなることがないようにしている。
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
134 */
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
135
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
136 SchedDefineTask1(RUN_TASK_BLOCKS,run16);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
137
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
138 static int
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
139 run16(SchedTask *manager, void *in, void *out)
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
140 {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
141 WordCount *w = *(WordCount **)in;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
142
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
143 if (w->task_num < w->task_blocks) {
949
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
144 // last case
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
145 if (w->size >= w->division_size)
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
146 run_tasks(manager,w,w->task_num, w->t_print, w->division_size);
949
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
147 // remaining data
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
148 while (w->size>0)
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
149 run_tasks(manager,w,1, w->t_print, w->size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
150 // printf("run16 last %d\n",w->task_num);
949
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
151 } else {
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
152 HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS,
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
153 (memaddr)&w->self,sizeof(memaddr),0,0);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
154 w->t_print->wait_for(t_next);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
155
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
156 run_tasks(manager,w, w->task_blocks, t_next, w->division_size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
157
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
158 t_next->spawn();
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
159 // printf("run16 next %d\n",w->task_num);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
160 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
161 return 0;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
162 }
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
163
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
164
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
165 static int blocks = 48;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
166 static int division = 16; // in Kbyte
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
167
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
168 static void
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
169 run_start(TaskManager *manager, char *filename)
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
170 {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
171 HTaskPtr t_print;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
172
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
173 st_mmap_t st_mmap;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
174 st_mmap = my_mmap(filename);
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
175 WordCount *w = (WordCount*)manager->allocate(sizeof(WordCount));
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
176 // bzero(w,sizeof(WordCount));
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
177
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
178 w->self = w;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
179 w->task_blocks = blocks;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
180 w->task_spwaned = 0;
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
181
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
182 /*sizeはdivision_sizeの倍数にしている。*/
949
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
183 w->size = w->file_size = st_mmap.size;
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
184 w->file_mmap = st_mmap.file_mmap;
949
86aea6affe6c word count size fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 948
diff changeset
185 printf("w %lx\n",(long)w);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
186
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
187 /* 1task分のデータサイズ(byte) */
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
188 if (w->size >= 1024*division) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
189 w->division_size = 1024 * division;/*16kbyte*/
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
190 } else {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
191 w->division_size = w->size;
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
192 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
193
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
194 printf("dvision_size %d\n",w->division_size);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
195
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
196 /* "word num" and "line num" */
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
197 w->status_num = 2;
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
198 /* taskの数 */
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
199 w->task_num = w->size / w->division_size;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
200 w->task_num = w->task_num + (w->division_size*w->task_num < w->size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
201 int out_task_num = w->task_num;
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
202
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
203 w->out_task_num = out_task_num;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
204 printf("task_num %d\n",w->task_num);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
205 printf("out_task_num %d\n",w->out_task_num);
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
206
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
207 /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
208
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
209 w->division_out_size = sizeof(unsigned long long)*4;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
210 int out_size = w->division_out_size*out_task_num;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
211 w->o_data = (unsigned long long *)manager->allocate(out_size);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
212 w->out_size = 4;
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
213 printf("out size %d\n",out_size);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
214
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
215 /*各SPEの結果を合計して出力するタスク*/
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
216
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
217 t_print = manager->create_task(TASK_PRINT,
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
218 (memaddr)&w->self,sizeof(memaddr),0,0);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
219
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
220 w->t_print = t_print;
783
fc9c3b23d875 wordcount fix?
yutaka@localhost.localdomain
parents: 742
diff changeset
221
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
222 for(int i = 0;i<2;i++) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
223 /* Task を task_blocks ずつ起動する Task */
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
224 /* serialize されていると仮定する... */
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
225 HTaskPtr t_exec = manager->create_task(RUN_TASK_BLOCKS,
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
226 (memaddr)&w->self,sizeof(memaddr),0,0);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
227 t_print->wait_for(t_exec);
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
228 t_exec->spawn();
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
229 }
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
230
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
231 t_print->spawn();
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
232 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
233
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
234 static char*
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
235 init(int argc, char **argv)
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
236 {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
237
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
238 char *filename = 0;
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
239
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
240 for (int i = 1; argv[i]; ++i) {
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
241 if (strcmp(argv[i], "-file") == 0) {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
242 filename = argv[i+1];
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
243 } else if (strcmp(argv[i], "-division") == 0) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
244 division = atoi(argv[i+1]);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
245 } else if (strcmp(argv[i], "-block") == 0) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
246 blocks = atoi(argv[i+1]);
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
247 } else if (strcmp(argv[i], "-a") == 0) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
248 // create task all at once
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
249 all = 1;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
250 } else if (strcmp(argv[i], "-c") == 0) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
251 use_task_array = 0;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
252 use_compat = 1;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
253 } else if (strcmp(argv[i], "-s") == 0) {
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
254 use_task_array = 0;
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
255 use_compat = 0;
967
3e1c218da048 add anum option in WordCount
Yutaka Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents: 950
diff changeset
256 } else if (strcmp(argv[i], "-anum") == 0) {
3e1c218da048 add anum option in WordCount
Yutaka Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents: 950
diff changeset
257 array_task_num = atoi(argv[i+1]);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
258 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
259 }
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
260 if (filename==0) {
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
261 puts(usr_help_str);
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
262 exit(1);
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
263 }
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
264
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
265 return filename;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
266 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
267
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
268
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
269 int
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
270 TMmain(TaskManager *manager, int argc, char *argv[])
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
271 {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
272
394
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
273 char *filename = 0;
e08d0aa94367 debug word_count
e065725@localhost.localdomain
parents: 393
diff changeset
274 filename = init(argc, argv);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
275
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
276 if (filename < 0) {
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
277 return -1;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
278 }
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
279
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
280 task_init();
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 394
diff changeset
281 run_start(manager, filename);
393
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
282
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
283 return 0;
72b73beb3325 add word_count
e065725@localhost.localdomain
parents:
diff changeset
284 }
946
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
285
a8b6ee80c108 unify word count examples....
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 797
diff changeset
286 /* end */