1 | #include "system.h" |
---|
2 | #include "stdio.h" |
---|
3 | #include "stdlib.h" |
---|
4 | #include "matrice.h" |
---|
5 | |
---|
6 | #include "../segmentation.h" |
---|
7 | |
---|
8 | #define NPROCS 4 |
---|
9 | #define SIZE 500 |
---|
10 | #define SORT_TYPE 2 |
---|
11 | |
---|
12 | volatile int nprocs=NPROCS; |
---|
13 | |
---|
14 | unsigned int SortArr0[NPROCS*(SIZE+200)]; |
---|
15 | //unsigned int SortArr0[4*4*SIZE]; |
---|
16 | |
---|
17 | void SORT(unsigned int *base, unsigned int n, int type); |
---|
18 | void insertion_sort(unsigned int *base, unsigned int n); // type 2 |
---|
19 | void selection_sort(unsigned int *base, unsigned int n); // type 1 |
---|
20 | void bubble_sort(unsigned int *base, unsigned int n); // type 3 |
---|
21 | void shellSortPhase(unsigned int a[],unsigned int length, int gap); |
---|
22 | void shellSort(unsigned int *base, unsigned int n); // type 0 |
---|
23 | |
---|
24 | int main() |
---|
25 | { |
---|
26 | register int p; |
---|
27 | |
---|
28 | int beg_cycle, end_cycle; |
---|
29 | |
---|
30 | beg_cycle = cpu_cycles(); |
---|
31 | |
---|
32 | p=procnum(); |
---|
33 | |
---|
34 | puts("Hello from processor "); |
---|
35 | putchar(p+'0'); |
---|
36 | putchar('\n'); |
---|
37 | |
---|
38 | int i; |
---|
39 | int j; |
---|
40 | unsigned int* SortArray; |
---|
41 | |
---|
42 | if(p+1 <= nprocs) |
---|
43 | { |
---|
44 | i=1; |
---|
45 | puts("Memory copy \n"); |
---|
46 | SortArray = SortArr0 + p*(SIZE+200); |
---|
47 | memcpy(SortArray, gQSortNum0 + p*SIZE,SIZE*4); |
---|
48 | puts("Sort... \n"); |
---|
49 | SORT((unsigned int *) (SortArray), (unsigned int) SIZE, SORT_TYPE); |
---|
50 | |
---|
51 | for (j = 1; j < SIZE; j++) |
---|
52 | { |
---|
53 | if (SortArray[j] < SortArray[j-1]) |
---|
54 | { |
---|
55 | puts("ucbqsort: failed\n"); |
---|
56 | while(1); |
---|
57 | } |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | puts("ucbqsort: success\n"); |
---|
62 | end_cycle = cpu_cycles(); |
---|
63 | printf( "nombre cycles cpu : %i\n", end_cycle-beg_cycle); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | // puts("Display the sorted array : \n"); |
---|
68 | // for(j = 0; j < SIZE; j++) |
---|
69 | // { |
---|
70 | // puti(SortArray[j]); |
---|
71 | // putchar('\n'); |
---|
72 | // } |
---|
73 | |
---|
74 | // printf( "------------------------------ \n"); |
---|
75 | // printf( "nombre cycles cpu : %i\n", end_cycle-beg_cycle); |
---|
76 | // printf( "------------------------------ \n"); |
---|
77 | |
---|
78 | |
---|
79 | while(1); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | //---- insertion sort : non adapté pour tableaux de grande taille (> 100) -- |
---|
84 | void insertion_sort(unsigned int *base, unsigned int n) |
---|
85 | { |
---|
86 | /* Spécifications externes : Tri du tableau base par insertion séquentielle */ |
---|
87 | int i,p,j; |
---|
88 | int x; |
---|
89 | |
---|
90 | puts("Insertion Sort\n"); |
---|
91 | |
---|
92 | for (i = 1; i < n; i++) |
---|
93 | { |
---|
94 | |
---|
95 | putchar('-'); // added for debug |
---|
96 | |
---|
97 | /* stockage de la valeur en i */ |
---|
98 | x = base[i]; |
---|
99 | |
---|
100 | /* recherche du plus petit indice p inférieur à i tel que base[p] >= base[i] */ |
---|
101 | for(p = 0; base[p] < x; p++); |
---|
102 | /* p pointe une valeur de base supérieure à celle en i */ |
---|
103 | |
---|
104 | /* décalage avant des valeurs de base entre p et i */ |
---|
105 | for (j = i-1; j >= p; j--) { |
---|
106 | base[j+1] = base[j]; |
---|
107 | } |
---|
108 | |
---|
109 | base[p] = x; /* insertion de la valeur stockée à la place vacante */ |
---|
110 | |
---|
111 | putchar('+'); // added for debug |
---|
112 | |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | //------ simple_sort ------------------------------- |
---|
117 | void selection_sort(unsigned int *base, unsigned int n) |
---|
118 | { |
---|
119 | int i, min, j , x; |
---|
120 | puts("Selection Sort\n"); |
---|
121 | |
---|
122 | for(i = 0 ; i < n - 1 ; i++) |
---|
123 | { |
---|
124 | |
---|
125 | putchar('-'); // added for debug |
---|
126 | |
---|
127 | min = i; |
---|
128 | |
---|
129 | |
---|
130 | for(j = i+1 ; j < n ; j++) |
---|
131 | { |
---|
132 | |
---|
133 | if(base[j] < base[min]) |
---|
134 | min = j; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | if(min != i) |
---|
139 | { |
---|
140 | x = base[i]; |
---|
141 | base[i] = base[min]; |
---|
142 | base[min] = x; |
---|
143 | } |
---|
144 | |
---|
145 | putchar('+'); // added for debug |
---|
146 | |
---|
147 | } |
---|
148 | } |
---|
149 | //------------------------------- |
---|
150 | void bubble_sort(unsigned int *base, unsigned int n) |
---|
151 | { |
---|
152 | int i = 0; /* Indice de répétition du tri */ |
---|
153 | int j = 0; /* Variable de boucle */ |
---|
154 | int tmp = 0; /* Variable de stockage temporaire */ |
---|
155 | int en_desordre = 1; /* Booléen marquant l'arrêt du tri si le tableau est ordonné */ |
---|
156 | |
---|
157 | puts("Bubble Sort\n"); |
---|
158 | |
---|
159 | /* Boucle de répétition du tri et le test qui arrête le tri dès que le tableau est ordonné */ |
---|
160 | for(i = 0 ; (i < n) && en_desordre; i++) |
---|
161 | { |
---|
162 | putchar('-'); // added for debug |
---|
163 | |
---|
164 | /* Supposons le tableau ordonné */ |
---|
165 | en_desordre = 0; |
---|
166 | /* Vérification des éléments des places j et j-1 */ |
---|
167 | for(j = 1 ; j < n - i ; j++) |
---|
168 | { |
---|
169 | /* Si les 2 éléments sont mal triés */ |
---|
170 | if(base[j] < base[j-1]) |
---|
171 | { |
---|
172 | /* Inversion des 2 éléments */ |
---|
173 | tmp = base[j-1]; |
---|
174 | base[j-1] = base[j]; |
---|
175 | base[j] = tmp; |
---|
176 | |
---|
177 | /* Le tableau n'est toujours pas trié */ |
---|
178 | en_desordre = 1; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | putchar('+'); // added for debug |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | //------------------------------------------------------ |
---|
187 | /* |
---|
188 | * Exécute un tri par insertion avec la séparation donnée |
---|
189 | * If gap == 1, on fait un tri ordinaire. |
---|
190 | * If gap >= length, on ne fait rien. |
---|
191 | */ |
---|
192 | void shellSortPhase(unsigned int a[],unsigned int length, int gap) { |
---|
193 | int i; |
---|
194 | |
---|
195 | puti(gap); |
---|
196 | for (i = gap; i < length; ++i) { |
---|
197 | unsigned int value = a[i]; |
---|
198 | int j; |
---|
199 | for (j = i - gap; j >= 0 && a[j] > value; j -= gap) { |
---|
200 | putchar('+'); |
---|
201 | a[j + gap] = a[j]; |
---|
202 | putchar('-'); |
---|
203 | } |
---|
204 | a[j + gap] = value; |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | void shellSort(unsigned int *base, unsigned int n) { |
---|
209 | /* |
---|
210 | * gaps[] doit approximer une Série géométrique. |
---|
211 | * La sequence suivante est la meilleure connue en terme |
---|
212 | * de nombre moyen de comparaisons. voir: |
---|
213 | * http://www.research.att.com/~njas/sequences/A102549 |
---|
214 | */ |
---|
215 | static const int gaps[] = { |
---|
216 | 1, 4, 10, 23, 57, 132, 301, 701 |
---|
217 | }; |
---|
218 | int sizeIndex; |
---|
219 | |
---|
220 | puts("Shell Sort\n"); |
---|
221 | for (sizeIndex = sizeof(gaps)/sizeof(gaps[0]) - 1; |
---|
222 | sizeIndex >= 0; |
---|
223 | --sizeIndex) |
---|
224 | shellSortPhase(base, n, gaps[sizeIndex]); |
---|
225 | } |
---|
226 | |
---|
227 | //-------------------------------------*/ |
---|
228 | void SORT(unsigned int *base, unsigned int n, int type) |
---|
229 | { |
---|
230 | switch(type) |
---|
231 | { |
---|
232 | case 0: |
---|
233 | shellSort(base, n); |
---|
234 | break; |
---|
235 | case 1: |
---|
236 | selection_sort(base, n); |
---|
237 | break; |
---|
238 | case 2: |
---|
239 | insertion_sort(base, n); |
---|
240 | break; |
---|
241 | case 3: |
---|
242 | bubble_sort(base, n); |
---|
243 | break; |
---|
244 | default: |
---|
245 | break; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|