Mercurial > hg > Game > Cerium
annotate example/many_task/spe/QuickSort.cc @ 2069:26aa08c9a1de draft default tip
cuda example fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 12 Feb 2017 10:04:55 +0900 |
parents | 637fa9a4105b |
children |
rev | line source |
---|---|
109 | 1 #include "QuickSort.h" |
2 #include <stdio.h> | |
3 #include <string.h> | |
4 | |
5 SchedDefineTask(QuickSort); | |
6 | |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
7 static void quick_sort( Data *data, int begin, int end ) ; |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
8 |
467
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
356
diff
changeset
|
9 static void |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
10 swap( Data *data, int left, int right ) |
109 | 11 { |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
12 Data tmp = data[left]; |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
13 data[left] = data[right]; |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
14 data[right] = tmp; |
109 | 15 } |
467
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
356
diff
changeset
|
16 |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
17 // #define USE_MEMCPY |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
18 |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
19 static int |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
20 run(SchedTask *s, void* rbuff, void* wbuff) { |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
21 // copy value |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
22 int begin = 0; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
23 #if USE_SIMPLE_TASK |
936
178fbcc81fda
dead lock on spu/ppu mail
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
934
diff
changeset
|
24 int end = s->read_size()/sizeof(Data); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
25 Data *r_data = (Data*)rbuff; |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
26 #ifdef USE_MEMCPY |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
27 Data *w_data = (Data*)wbuff; |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
28 #endif |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
29 #else |
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:
940
diff
changeset
|
30 int end = s->get_inputSize(0)/sizeof(Data); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
31 DataPtr r_data = (DataPtr)s->get_input(0); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
32 #ifdef USE_MEMCPY |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
33 DataPtr w_data = (DataPtr)s->get_output(0); |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
34 #endif |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
35 #endif |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
36 |
938
20beb83a5a22
dead lock still remains. zombi problem?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
936
diff
changeset
|
37 //printf("[SPE] Quick: length:%d addr->%x \n",end, (int)rbuff); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
38 //printf("[PPE] Quick: data[0]: %d addr->%x\n",sizeof(r_data),r_data); |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
39 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
40 //show_data(r_data, end); |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
41 quick_sort(r_data, begin, end-1); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
42 #ifdef USE_MEMCPY |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
43 memcpy(w_data, r_data, sizeof(Data)*end); |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
44 #else |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
45 s->swap(); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
46 #endif |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
47 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
48 return 0; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
49 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
50 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
51 static void |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
52 quick_sort( Data *data, int begin, int end ) { |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
53 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
54 if (begin < end) { |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
55 int where = (begin + end) / 2; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
56 int pivot = data[where].index; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
57 data[where].index = data[begin].index; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
58 int p = begin; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
59 int i; |
1851
637fa9a4105b
fix many_task ( sort )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
945
diff
changeset
|
60 for (i=begin+1; i<end; i++) { |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
61 if (data[i].index < pivot) { |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
62 p++; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
63 swap(data, p, i); |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
64 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
65 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
66 data[begin].index = data[p].index; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
67 data[p].index = pivot; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
68 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
69 quick_sort(data, begin, p-1); |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
70 quick_sort(data, p+1, end); // tail call |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
71 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
72 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
73 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
626
diff
changeset
|
74 |
467
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
356
diff
changeset
|
75 /* end */ |