annotate example/many_task/sort.cc @ 674:bde5f13adf10

fix many task example (sort). Dummy task is now system supported.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 06 Dec 2009 00:54:10 +0900
parents c13bbb7d70b3
children f4d7cf77ec3d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
1 #include "TaskManager.h"
573
2f4b5ce2a515 example fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 567
diff changeset
2 #include "SchedTask.h"
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
3 #include "sort.h"
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
4 #include "Func.h"
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
5
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
6 DataPtr data; // sort array
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
7 int data_length;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
8 static int sort_count; // sort 完了に必要な回数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
9 static int split_num; // data の分割数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
10 static int half_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
11 static int block_num; // 一つのタスクで sort する data 数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
12 static int last_block_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
13 static int half_block_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
14 static int last_half_block_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
15
573
2f4b5ce2a515 example fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 567
diff changeset
16 static void sort_restart(SchedTask *, void *, void *);
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
17 static void sort_start(SchedTask *);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
18
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
19 /**
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
20 * 一つの block にある data の数が MAX_BLOCK_SIZE 超えないような
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
21 * len の分割数を返す
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
22 *
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
23 * @param len sort する data の総数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
24 * @param num 使用する SPE の数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
25 *
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
26 * @return data の分割数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
27 *
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
28 * TODO:
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
29 * len が num 以下とか考えてません
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
30 */
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
31 static int
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
32 get_split_num(int len, int num)
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
33 {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
34 if (len / num < MAX_BLOCK_SIZE) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
35 return num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
36 } else {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
37 // 切り上げ
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
38 return (len + MAX_BLOCK_SIZE - 1) / MAX_BLOCK_SIZE;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
39 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
40 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
41
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
42 /**
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
43 * btask が全て終了したら、再び sort_start を実行する
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
44 * @param d 生成された btask の数
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
45 */
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
46 static void
573
2f4b5ce2a515 example fix
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 567
diff changeset
47 sort_restart(SchedTask *s, void *d, void *e)
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
48 {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
49 static int cnt = 0;
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
50 // static int ccc = 0;
625
60aa3f241b10 64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 573
diff changeset
51 long max = (long)d;
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
52
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
53 if (++cnt == max) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
54 cnt = 0;
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
55 // printf("restarted %d\n",ccc++);
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
56 sort_start(s);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
57 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
58 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
59
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
60 static void
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
61 sort_start(SchedTask *manager)
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
62 {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
63 if (--sort_count < 0) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
64 return;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
65 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
66
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
67 HTaskPtr fsort[split_num];
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
68
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
69 for (int i = 0; i < split_num-1; i++) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
70 fsort[i] = manager->create_task(QUICK_SORT);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
71 fsort[i]->add_inData(&data[i*block_num], sizeof(Data)*block_num);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
72 fsort[i]->add_outData(&data[i*block_num], sizeof(Data)*block_num);
625
60aa3f241b10 64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 573
diff changeset
73 fsort[i]->set_param(0,(memaddr)block_num);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
74 fsort[i]->set_cpu(SPE_ANY);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
75 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
76
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
77 // 最後の block は端数なので last_block_num を使う
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
78 {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
79 int i = split_num-1;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
80
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
81 fsort[i] = manager->create_task(QUICK_SORT);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
82 fsort[i]->add_inData(&data[i*block_num], sizeof(Data)*last_block_num);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
83 fsort[i]->add_outData(&data[i*block_num], sizeof(Data)*last_block_num);
625
60aa3f241b10 64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 573
diff changeset
84 fsort[i]->set_param(0,(memaddr)last_block_num);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
85 fsort[i]->set_cpu(SPE_ANY);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
86 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
87
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
88 if (split_num > 1) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
89 HTaskPtr bsort[half_num];
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
90
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
91 for (int i = 0; i < half_num-1; i++) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
92 bsort[i] = manager->create_task(QUICK_SORT);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
93 bsort[i]->add_inData(&data[i*block_num+half_block_num],
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
94 sizeof(Data)*block_num);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
95 bsort[i]->add_outData(&data[i*block_num+half_block_num],
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
96 sizeof(Data)*block_num);
625
60aa3f241b10 64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 573
diff changeset
97 bsort[i]->set_param(0,(memaddr)block_num);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
98 bsort[i]->set_cpu(SPE_ANY);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
99 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
100
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
101 {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
102 int i = half_num-1;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
103
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
104 bsort[i] = manager->create_task(QUICK_SORT);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
105 bsort[i]->add_inData(&data[i*block_num+half_block_num],
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
106 sizeof(Data)*last_half_block_num);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
107 bsort[i]->add_outData(&data[i*block_num+half_block_num],
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
108 sizeof(Data)*last_half_block_num);
625
60aa3f241b10 64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 573
diff changeset
109 bsort[i]->set_param(0,(memaddr)last_half_block_num);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
110 bsort[i]->set_cpu(SPE_ANY);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
111 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
112
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
113 for (int i = 0; i < half_num; i++) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
114 bsort[i]->wait_for(fsort[i]);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
115 bsort[i]->wait_for(fsort[i+1]);
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
116 bsort[i]->set_post(sort_restart, (void*)(half_num),(void*)manager);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
117 bsort[i]->spawn();
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
118 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
119 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
120
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
121 for (int i = 0; i < split_num; i++) {
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
122 fsort[i]->spawn();
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
123 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
124 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
125
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
126 void
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
127 sort_init(SchedTask *manager, void *a, void *b)
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
128 {
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
129 int cpuNum = (int)a;
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
130 int length = (int)b;
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
131
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
132 data = (DataPtr)manager->allocate(sizeof(Data)*length);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
133 data_length = length;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
134
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
135 for (int i = 0; i < length; i++) {
230
2b114977852d fix Random
gongo@localhost.localdomain
parents: 227
diff changeset
136 data[i].index = manager->get_random()%10000;
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
137 data[i].ptr = 0;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
138 }
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
139
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
140 split_num = get_split_num(length, cpuNum);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
141 half_num = split_num-1;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
142 sort_count = split_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
143
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
144 block_num = (length + split_num -1)/split_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
145 half_block_num = block_num/2;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
146
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
147 last_block_num = length - (split_num-1)*block_num;
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
148 last_half_block_num = half_block_num+(last_block_num/2);
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
149
674
bde5f13adf10 fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
150 sort_start(manager);
227
d54cbfafcb82 add sort
gongo@localhost.localdomain
parents:
diff changeset
151 }
400
00fe05184a02 Fix examples.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
152
651
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 625
diff changeset
153 /* end */