annotate example/many_task/ppe/QuickSort.cc @ 434:852e3f6a6357 draft

merge overlay
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 24 Sep 2009 21:09:27 +0900
parents 305ac1897c50
children 839e34d0cc3c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
109
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
1 #include "QuickSort.h"
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
2 #include <stdio.h>
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
3 #include <string.h>
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
4
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
5 SchedDefineTask(QuickSort);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
6
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
7 int
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
8 QuickSort::run(void* rbuff, void* wbuff) {
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
9 // copy value
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
10 int begin = 0;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
11 int end = get_param(0);
220
gongo@localhost.localdomain
parents: 109
diff changeset
12
109
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
13 Data *r_data = (Data*)get_input(rbuff, 0);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
14 Data *w_data = (Data*)get_output(wbuff, 0);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
15
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
16 //printf("[PPE] Quick: length:%d addr->%x \n",end, (int*)rbuff);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
17 //printf("[PPE] Quick: data[0]: %d addr->%x\n",sizeof(r_data),r_data);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
18
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
19 //show_data(r_data, end);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
20 quick_sort(r_data, begin, end-1);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
21 memcpy(w_data, r_data, sizeof(Data)*end);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
22
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
23 return 0;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
24 }
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
25
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
26 void
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
27 QuickSort::quick_sort( Data *data, int begin, int end ) {
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
28
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
29 if (begin < end) {
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
30 int where = (begin + end) / 2;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
31 int pivot = data[where].index;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
32 data[where].index = data[begin].index;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
33 int p = begin;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
34 int i;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
35 for (i=begin+1; i<=end; i++) {
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
36 if (data[i].index < pivot) {
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
37 p++;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
38 swap(data, p, i);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
39 }
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
40 }
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
41 data[begin].index = data[p].index;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
42 data[p].index = pivot;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
43
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
44 quick_sort(data, begin, p-1);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
45 quick_sort(data, p+1, end);
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
46 }
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
47 }
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
48
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
49 void
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
50 QuickSort::swap( Data *data, int left, int right )
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
51 {
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
52 int tmp = data[left].index;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
53 data[left].index = data[right].index;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
54 data[right].index = tmp;
028ffc9c0375 Cerium cvs version
gongo@gendarme.local
parents:
diff changeset
55 }