Mercurial > hg > Game > Cerium
annotate example/many_task/main.cc @ 1516:e544f9747169 draft
fix gpu kernel source
author | Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 08 Nov 2012 17:20:10 +0900 |
parents | b3644b73d2cf |
children | 9a5f87f4b60f |
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(); |
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
|
12 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
|
13 |
109 | 14 |
220 | 15 // sort.cc |
16 extern int data_length; | |
17 extern DataPtr data; | |
935 | 18 extern int all; |
19 int all = 0; | |
220 | 20 |
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
|
21 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
|
22 |
220 | 23 // 計測用 |
24 static double st_time; | |
25 static double ed_time; | |
26 | |
27 static int length = 1200; | |
28 | |
29 // prototype | |
400 | 30 void TMend(TaskManager *); |
220 | 31 |
32 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
|
33 getTime() |
220 | 34 { |
35 struct timeval tv; | |
36 gettimeofday(&tv, NULL); | |
37 return tv.tv_sec + (double)tv.tv_usec*1e-6; | |
38 } | |
1514
99ea7b932470
create OpenCL test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1508
diff
changeset
|
39 |
256 | 40 const char *usr_help_str = "Usage: ./sort [option]\n \ |
217 | 41 options\n\ |
42 -cpu Number of SPE used (default 1)\n\ | |
43 -l, --length Sorted number of data (default 1200)\n\ | |
44 -h, --help Print this message"; | |
109 | 45 |
46 int | |
47 init(int argc, char **argv) | |
48 { | |
49 for (int i = 1; argv[i]; ++i) { | |
217 | 50 if (strcmp(argv[i], "--length") == 0 || strcmp(argv[i], "-l") == 0) { |
220 | 51 length = atoi(argv[++i]); |
109 | 52 } |
935 | 53 if (strcmp(argv[i], "-a") == 0 ) { |
54 all = 1; | |
55 } | |
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
|
56 if (strcmp(argv[i], "-c") == 0 ) { |
1508 | 57 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
|
58 } |
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
|
59 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
|
60 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
|
61 } |
256 | 62 |
109 | 63 } |
64 | |
65 return 0; | |
66 } | |
67 | |
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
|
68 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
|
69 |
1508 | 70 static void |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
71 show_data(void) |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
72 { |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
73 puts("-----------------------------------------------"); |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
74 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
|
75 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
|
76 } |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
77 puts("-----------------------------------------------"); |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
78 } |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
79 |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
80 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
|
81 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
|
82 { |
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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 } |
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
|
88 } |
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
|
89 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
|
90 } |
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
|
91 |
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
|
92 |
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
|
93 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
|
94 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
|
95 { |
1079 | 96 sorter.cpuNum = (long)a; |
97 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
|
98 |
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 sorter.data = (DataPtr)manager->allocate(sizeof(Data)*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
|
100 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
|
101 |
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 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
|
103 int half_num = sorter.split_num-1; |
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 sorter.fsort = (HTaskPtr*)manager->allocate(sizeof(HTaskPtr)*sorter.split_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
|
105 sorter.bsort = (HTaskPtr*)manager->allocate(sizeof(HTaskPtr)*half_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
|
106 memset((void*)sorter.bsort,0, sizeof(HTaskPtr)*half_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
|
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 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
|
109 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
|
110 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
|
111 } |
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
|
112 |
1515
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
113 // 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
|
114 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
|
115 // default ではSortSimpleがsetされている。SortSimpleはsort.ccに |
b3644b73d2cf
add flip flag test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1514
diff
changeset
|
116 |
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
|
117 restart->set_param(0,(memaddr)&sorter); |
1514
99ea7b932470
create OpenCL test
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1508
diff
changeset
|
118 // 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
|
119 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
|
120 } |
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
|
121 |
220 | 122 unsigned int ts, te; |
123 | |
109 | 124 int |
400 | 125 TMmain(TaskManager *manager, int argc, char *argv[]) |
109 | 126 { |
127 if (init(argc, argv) < 0) { | |
1508 | 128 return -1; |
109 | 129 } |
130 | |
131 task_init(); | |
132 | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
133 int cpu = manager->get_cpuNum(); |
1508 | 134 // in case of -cpu 0 |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
135 if (cpu==0) cpu = 1; |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
675
diff
changeset
|
136 if (1) { |
1508 | 137 HTask *dummy = manager->create_task(Dummy); |
138 dummy->set_post(sort_init, (void*)cpu, (void*)length); | |
139 dummy->spawn(); | |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
675
diff
changeset
|
140 } else { |
1508 | 141 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
|
142 } |
109 | 143 |
144 st_time = getTime(); | |
145 | |
227 | 146 // 全ての Task が終了した後に実行する関数をセット |
220 | 147 manager->set_TMend(TMend); |
109 | 148 |
149 return 0; | |
150 } | |
151 | |
220 | 152 void |
400 | 153 TMend(TaskManager *manager) |
109 | 154 { |
220 | 155 ed_time = getTime(); |
1516
e544f9747169
fix gpu kernel source
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
1515
diff
changeset
|
156 show_data(); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
157 check_data(); |
220 | 158 printf("Time: %0.6f\n",ed_time-st_time); |
109 | 159 } |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
160 |
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
400
diff
changeset
|
161 /* end */ |