Mercurial > hg > Game > Cerium
annotate example/many_task/ppe/QuickSort.cc @ 1508:0e1318e7caed draft
create sort test
author | Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 18 Sep 2012 18:59:54 +0900 |
parents | 704b9e320f1e |
children | 18b63e697c61 |
rev | line source |
---|---|
109 | 1 #include "QuickSort.h" |
2 #include <stdio.h> | |
3 #include <string.h> | |
4 | |
5 SchedDefineTask(QuickSort); | |
6 | |
467
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
220
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 |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
9 static void |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
10 swap( Data *data, int left, int right ) |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
11 { |
1508 | 12 Data tmp = data[left]; |
934
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; |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
15 } |
467
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
220
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 |
467
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
220
diff
changeset
|
19 static int |
839e34d0cc3c
fix all examples. test_render is not working now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
220
diff
changeset
|
20 run(SchedTask *s, void* rbuff, void* wbuff) { |
109 | 21 // copy value |
1508 | 22 int begin = 0; |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
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:
625
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 |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
27 Data *w_data = (Data*)wbuff; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
28 #endif |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
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:
625
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:
625
diff
changeset
|
35 #endif |
109 | 36 |
938
20beb83a5a22
dead lock still remains. zombi problem?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
936
diff
changeset
|
37 // printf("[PPE] Quick: length:%d addr->%x \n",end, (int)rbuff); |
1508 | 38 // printf("[PPE] Quick: data[0]: %ld addr->%lx\n",sizeof(r_data),(long)r_data); |
109 | 39 |
40 quick_sort(r_data, begin, end-1); | |
1508 | 41 |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
42 #ifdef USE_MEMCPY |
109 | 43 memcpy(w_data, r_data, sizeof(Data)*end); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
44 #else |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
45 s->swap(); |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
46 #endif |
109 | 47 |
48 return 0; | |
49 } | |
50 | |
1508 | 51 void |
52 qsort_test(Data *data, int begin, int end ) { | |
53 quick_sort(data, begin, end); | |
54 printf("end is %d\n",end); | |
55 } | |
56 | |
57 static void | |
58 quick_sort(Data *data, int begin, int end ) { | |
109 | 59 |
60 if (begin < end) { | |
1508 | 61 int where = (begin + end) / 2; |
62 int pivot = data[where].index; | |
63 data[where].index = data[begin].index; | |
64 int p = begin; | |
65 int i; | |
66 for (i=begin+1; i<end; i++) { | |
67 if (data[i].index < pivot) { | |
68 p++; | |
69 swap(data, p, i); | |
70 } | |
71 } | |
72 data[begin].index = data[p].index; | |
73 data[p].index = pivot; | |
74 | |
75 quick_sort(data, begin, p-1); | |
76 quick_sort(data, p+1, end); // tail call | |
109 | 77 } |
78 } | |
79 | |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
80 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
625
diff
changeset
|
81 /* end */ |