Mercurial > hg > Game > Cerium
annotate example/many_task/main.cc @ 1854:c21bd32e20b9 draft
fix sort with task array ( wrong result )
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 22 Dec 2013 11:59:03 +0900 |
parents | f800f61a0311 |
children |
rev | line source |
---|---|
109 | 1 #include <stdio.h> |
2 #include <string.h> | |
3 #include <stdlib.h> | |
4 #include <sys/time.h> | |
5 #include "TaskManager.h" | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
6 #include "SchedTask.h" |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
7 |
109 | 8 #include "Func.h" |
220 | 9 #include "sort.h" |
109 | 10 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
11 extern void task_init(); |
1779 | 12 #ifdef GPU |
1738
893353c014f5
run sort by ANY_ANY
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
1555
diff
changeset
|
13 extern void gpu_task_init(); |
1779 | 14 #endif |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
15 extern int get_split_num(int len, int num); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
16 |
109 | 17 |
220 | 18 // sort.cc |
19 extern int data_length; | |
20 extern DataPtr data; | |
935 | 21 extern int all; |
22 int all = 0; | |
220 | 23 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
24 static int sort_task = SortSimple; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
25 |
220 | 26 // 計測用 |
27 static double st_time; | |
28 static double ed_time; | |
29 | |
1771 | 30 static long length = 1200; |
1519 | 31 CPU_TYPE spe_cpu = SPE_ANY; |
1552
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
32 int use_task_array=0; |
220 | 33 // prototype |
400 | 34 void TMend(TaskManager *); |
220 | 35 |
36 static double | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
37 getTime() |
220 | 38 { |
39 struct timeval tv; | |
40 gettimeofday(&tv, NULL); | |
41 return tv.tv_sec + (double)tv.tv_usec*1e-6; | |
42 } | |
1514
99ea7b932470
create OpenCL test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1508
diff
changeset
|
43 |
256 | 44 const char *usr_help_str = "Usage: ./sort [option]\n \ |
217 | 45 options\n\ |
46 -cpu Number of SPE used (default 1)\n\ | |
47 -l, --length Sorted number of data (default 1200)\n\ | |
48 -h, --help Print this message"; | |
109 | 49 |
50 int | |
51 init(int argc, char **argv) | |
52 { | |
53 for (int i = 1; argv[i]; ++i) { | |
217 | 54 if (strcmp(argv[i], "--length") == 0 || strcmp(argv[i], "-l") == 0) { |
1771 | 55 length = (int)atoi(argv[++i]); |
109 | 56 } |
935 | 57 if (strcmp(argv[i], "-a") == 0 ) { |
58 all = 1; | |
59 } | |
1519 | 60 if (strcmp(argv[i], "-g") == 0 ) { |
61 spe_cpu = GPU_0; | |
62 } | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
63 if (strcmp(argv[i], "-c") == 0 ) { |
1508 | 64 sort_task = SortCompat; |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
65 } |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
66 if (strcmp(argv[i], "-s") == 0 ) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
67 sort_task = SortSimple; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
68 } |
1541
99c9ed2932a0
change task_init WordCount example
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1530
diff
changeset
|
69 if (strcmp(argv[i], "-ta") == 0 ) { |
1552
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
70 use_task_array = 1; |
1854
c21bd32e20b9
fix sort with task array ( wrong result )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1853
diff
changeset
|
71 sort_task = SortTaskArray; |
1541
99c9ed2932a0
change task_init WordCount example
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1530
diff
changeset
|
72 } |
1738
893353c014f5
run sort by ANY_ANY
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
1555
diff
changeset
|
73 if (strcmp(argv[i], "-any") == 0 ) { |
893353c014f5
run sort by ANY_ANY
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
1555
diff
changeset
|
74 spe_cpu = ANY_ANY; |
893353c014f5
run sort by ANY_ANY
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
1555
diff
changeset
|
75 } |
109 | 76 } |
77 | |
78 return 0; | |
79 } | |
80 | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
81 Sort sorter; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
82 |
1508 | 83 static void |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
84 show_data(void) |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
85 { |
1555
096412ad80fb
add opencl example
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1553
diff
changeset
|
86 /* |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
87 puts("-----------------------------------------------"); |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
88 for(int i = 0; i < sorter.data_length; i++) { |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
89 printf("data[%02d].index = %d\n", i, sorter.data[i].index); |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
90 } |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
91 puts("-----------------------------------------------"); |
1530 | 92 */ |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
93 } |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
94 |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
95 static void |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
96 check_data() |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
97 { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
98 for(int i=0; i< sorter.data_length-1;i++) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
99 if (sorter.data[i].index>sorter.data[i+1].index) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
100 printf("Data are not sorted at %d. %d > %d \n",i, sorter.data[i].index,sorter.data[i+1].index); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
101 return; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
102 } |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
103 } |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
104 printf("Data are sorted\n"); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
105 } |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
106 |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
107 |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
108 static void |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
109 sort_init(SchedTask *manager, void *a, void *b) |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
110 { |
1079 | 111 sorter.cpuNum = (long)a; |
112 long length = (long)b; | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
113 |
1853
f800f61a0311
to prevent overrun +1 alloc in many_task
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1779
diff
changeset
|
114 sorter.data = (DataPtr)manager->allocate(sizeof(Data)*(length+1)); |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
115 sorter.data_length = length; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
116 |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
117 sorter.split_num = get_split_num(sorter.data_length, sorter.cpuNum); // data の分割数 |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
118 int half_num = sorter.split_num-1; |
1552
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
119 if (use_task_array==1) { |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
120 sorter.fsort_task = (Task**)manager->allocate(sizeof(Task*)*sorter.split_num); |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
121 sorter.bsort_task = (Task**)manager->allocate(sizeof(Task*)*half_num); |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
122 } else { |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
123 sorter.fsort = (HTaskPtr*)manager->allocate(sizeof(Task*)*sorter.split_num); |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
124 sorter.bsort = (HTaskPtr*)manager->allocate(sizeof(Task*)*half_num); |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
125 } |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
126 if (use_task_array==1) { |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
127 memset((void*)sorter.bsort_task,0, sizeof(HTaskPtr)*half_num); |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
128 } else { |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
129 memset((void*)sorter.bsort,0, sizeof(HTaskPtr)*half_num); |
40a554d45fc6
select task array or task list exec option
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1544
diff
changeset
|
130 } |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
131 for (int i = 0; i < length; i++) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
132 sorter.data[i].index = manager->get_random()%10000; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
133 sorter.data[i].ptr = i; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
134 } |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
135 |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
136 // show_data(); |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
137 HTaskPtr restart = manager->create_task(sort_task,0,0,0,0); |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
138 // default ではSortSimpleがsetされている。SortSimpleはsort.ccに |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
139 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
140 restart->set_param(0,(memaddr)&sorter); |
1514
99ea7b932470
create OpenCL test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1508
diff
changeset
|
141 // set flip flag |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
142 restart->spawn(); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
143 } |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
935
diff
changeset
|
144 |
220 | 145 unsigned int ts, te; |
146 | |
109 | 147 int |
400 | 148 TMmain(TaskManager *manager, int argc, char *argv[]) |
109 | 149 { |
150 if (init(argc, argv) < 0) { | |
1508 | 151 return -1; |
109 | 152 } |
153 | |
154 task_init(); | |
1779 | 155 #ifdef GPU |
1738
893353c014f5
run sort by ANY_ANY
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
1555
diff
changeset
|
156 gpu_task_init(); |
1779 | 157 #endif |
1771 | 158 long cpu = manager->get_cpuNum(); |
1508 | 159 // in case of -cpu 0 |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
160 if (cpu==0) cpu = 1; |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
675
diff
changeset
|
161 if (1) { |
1508 | 162 HTask *dummy = manager->create_task(Dummy); |
163 dummy->set_post(sort_init, (void*)cpu, (void*)length); | |
164 dummy->spawn(); | |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
675
diff
changeset
|
165 } else { |
1508 | 166 sort_init(manager->get_schedTask(),(void*)cpu, (void*)length); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
675
diff
changeset
|
167 } |
109 | 168 |
169 st_time = getTime(); | |
170 | |
227 | 171 // 全ての Task が終了した後に実行する関数をセット |
220 | 172 manager->set_TMend(TMend); |
109 | 173 |
174 return 0; | |
175 } | |
176 | |
220 | 177 void |
400 | 178 TMend(TaskManager *manager) |
109 | 179 { |
220 | 180 ed_time = getTime(); |
1516
e544f9747169
fix gpu kernel source
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1515
diff
changeset
|
181 show_data(); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
182 check_data(); |
220 | 183 printf("Time: %0.6f\n",ed_time-st_time); |
109 | 184 } |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
185 |
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
186 /* end */ |