| 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 16 |
|---|
| 9 | #define SIZE 500 //8000 |
|---|
| 10 | #define SORT_TYPE 1 |
|---|
| 11 | |
|---|
| 12 | volatile int lock=0; |
|---|
| 13 | volatile int compteur=NPROCS; |
|---|
| 14 | volatile int nprocs=NPROCS; |
|---|
| 15 | |
|---|
| 16 | volatile int write_boost[16*120]; |
|---|
| 17 | |
|---|
| 18 | unsigned int SortArr0[16*(SIZE+200)]; |
|---|
| 19 | |
|---|
| 20 | void SORT(int *base, int n, int size, int (*compar) (),int type); |
|---|
| 21 | void QSORT(int *base, int n, int size, int (*compar) ()); |
|---|
| 22 | void simple_sort(int *base, int n, int size, int (*compar) ()); |
|---|
| 23 | void insertion_sort(int *base, int n, int size, int (*compar) ()); |
|---|
| 24 | void bubble_sort(int *base, int n, int size, int (*compar) ()); |
|---|
| 25 | int compare(int *n1, int *n2) |
|---|
| 26 | { |
|---|
| 27 | return (*((unsigned int *) n1) - *((unsigned int *) n2)); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | int main() |
|---|
| 31 | { |
|---|
| 32 | register int p; |
|---|
| 33 | |
|---|
| 34 | p=procnum(); |
|---|
| 35 | |
|---|
| 36 | puts("Hello from processor "); |
|---|
| 37 | putchar(p+'0'); |
|---|
| 38 | putchar('\n'); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | int i; |
|---|
| 42 | int j; |
|---|
| 43 | int temp; |
|---|
| 44 | |
|---|
| 45 | if(p+1 <= nprocs){ |
|---|
| 46 | // for(i=1 ; i <= 8 ; i++ ){ |
|---|
| 47 | puts("Memory copy \n"); |
|---|
| 48 | memcpy(SortArr0+p*(SIZE+200), gQSortNum0 + ( (4*(p+1)*i) % 32) ,SIZE); |
|---|
| 49 | |
|---|
| 50 | puts("Sort... \n"); |
|---|
| 51 | SORT((int *) (SortArr0+p*(SIZE+200)), (int) SIZE, sizeof(unsigned int), compare,SORT_TYPE); |
|---|
| 52 | for (j = 1; j < SIZE; j++) |
|---|
| 53 | { |
|---|
| 54 | if (SortArr0[p*(SIZE+200)+j] < SortArr0[p*(SIZE+200) + j - 1]) |
|---|
| 55 | { |
|---|
| 56 | puts("ucbqsort: failed\n"); |
|---|
| 57 | while(1); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | } |
|---|
| 61 | puts("ucbqsort: success\n"); |
|---|
| 62 | // } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | if(p+1 <= nprocs){ |
|---|
| 67 | lock_lock(&lock); |
|---|
| 68 | compteur--; |
|---|
| 69 | if(!compteur){ |
|---|
| 70 | putchar('\n'); |
|---|
| 71 | puti(temp); |
|---|
| 72 | putchar('\n'); |
|---|
| 73 | } |
|---|
| 74 | lock_unlock(&lock); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | while(1); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | static int (*qcmp) (); |
|---|
| 81 | static int qsz; |
|---|
| 82 | static int thresh; |
|---|
| 83 | static int mthresh; |
|---|
| 84 | static void qst(int *, int *); |
|---|
| 85 | void QSORT(base, n, size, compar) |
|---|
| 86 | int *base; |
|---|
| 87 | int n; |
|---|
| 88 | int size; |
|---|
| 89 | int (*compar) (); |
|---|
| 90 | { |
|---|
| 91 | register int c, *i, *j, *lo, *hi; |
|---|
| 92 | int *min, *max; |
|---|
| 93 | if (n <= 1) |
|---|
| 94 | return; |
|---|
| 95 | qsz = size; |
|---|
| 96 | qcmp = compar; |
|---|
| 97 | thresh = 4; |
|---|
| 98 | mthresh = 6; |
|---|
| 99 | max = base + n; |
|---|
| 100 | if (n >= 4) |
|---|
| 101 | { |
|---|
| 102 | qst(base, max); |
|---|
| 103 | hi = base + thresh; |
|---|
| 104 | } |
|---|
| 105 | else |
|---|
| 106 | { |
|---|
| 107 | hi = max; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | puts("Quick sort done... \n"); |
|---|
| 111 | |
|---|
| 112 | for (j = lo = base; (lo ++) < hi;) |
|---|
| 113 | if ((*qcmp) (j, lo) > 0) |
|---|
| 114 | j = lo; |
|---|
| 115 | if (j != base) |
|---|
| 116 | { |
|---|
| 117 | for (i = base, hi = base + 1; i < hi;) |
|---|
| 118 | { |
|---|
| 119 | c = *j; |
|---|
| 120 | *j++ = *i; |
|---|
| 121 | *i++ = c; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | for (min = base; (hi = min ++) < max;) |
|---|
| 126 | { |
|---|
| 127 | while ((*qcmp) (hi --, min) > 0) |
|---|
| 128 | ; |
|---|
| 129 | if ((hi ++) != min) |
|---|
| 130 | { |
|---|
| 131 | for (lo = min + 1; --lo >= min;) |
|---|
| 132 | { |
|---|
| 133 | c = *lo; |
|---|
| 134 | for (i = j = lo; (j --) >= hi; i = j) |
|---|
| 135 | *i = *j; |
|---|
| 136 | *i = c; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | void simple_sort(base, n, size, compar) |
|---|
| 146 | int *base; |
|---|
| 147 | int n; |
|---|
| 148 | int size; |
|---|
| 149 | int (*compar) (); |
|---|
| 150 | { |
|---|
| 151 | register int c, *i, *j, *lo, p, *k; |
|---|
| 152 | int *max; |
|---|
| 153 | k=write_boost; |
|---|
| 154 | if (n <= 1) |
|---|
| 155 | return; |
|---|
| 156 | qsz = size; |
|---|
| 157 | qcmp = compar; |
|---|
| 158 | max = base + n; |
|---|
| 159 | puts("Selection Sort\n"); |
|---|
| 160 | p=procnum(); |
|---|
| 161 | // k=4; |
|---|
| 162 | for(j = base; j < max ; j++) |
|---|
| 163 | { |
|---|
| 164 | putchar('-'); // added for debug |
|---|
| 165 | |
|---|
| 166 | lo=j; |
|---|
| 167 | c=(*lo); |
|---|
| 168 | |
|---|
| 169 | for(i = j; i < max ; i++) |
|---|
| 170 | { |
|---|
| 171 | *(k+(p * 70)+20)=p; |
|---|
| 172 | |
|---|
| 173 | asm(" nop \n"); |
|---|
| 174 | |
|---|
| 175 | if( c-(*i) > 0) |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | lo=i; |
|---|
| 179 | c=(*lo); |
|---|
| 180 | |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | asm(" nop \n"); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | *lo = *j; |
|---|
| 187 | *j = c; |
|---|
| 188 | |
|---|
| 189 | putchar('+'); |
|---|
| 190 | |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void insertion_sort(base, n, size, compar) |
|---|
| 197 | int *base; |
|---|
| 198 | int n; |
|---|
| 199 | int size; |
|---|
| 200 | int (*compar) (); |
|---|
| 201 | { |
|---|
| 202 | register int c, *i, *j,p; |
|---|
| 203 | int *max; |
|---|
| 204 | if (n <= 1) |
|---|
| 205 | return; |
|---|
| 206 | qsz = size; |
|---|
| 207 | qcmp = compar; |
|---|
| 208 | max = base + n; |
|---|
| 209 | puts("Insertion Sort\n"); |
|---|
| 210 | p=procnum(); |
|---|
| 211 | for(j = base; j < max-1 ; j++){ |
|---|
| 212 | for(i = j; i < max-1 ; i++){ |
|---|
| 213 | if( (*i)-(*(i+1)) > 0){ |
|---|
| 214 | // if( (*qcmp) (i,i+1) > 0){ |
|---|
| 215 | c = *i; |
|---|
| 216 | *i = *(i+1); |
|---|
| 217 | *(i+1)=c; |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | void bubble_sort(base, n, size, compar) |
|---|
| 225 | int *base; |
|---|
| 226 | int n; |
|---|
| 227 | int size; |
|---|
| 228 | int (*compar) (); |
|---|
| 229 | { |
|---|
| 230 | register int c, *i; |
|---|
| 231 | register int b_swap=1; |
|---|
| 232 | int *max; |
|---|
| 233 | if (n <= 1) |
|---|
| 234 | return; |
|---|
| 235 | qsz = size; |
|---|
| 236 | qcmp = compar; |
|---|
| 237 | max = base + n; |
|---|
| 238 | puts("Bubble Sort\n"); |
|---|
| 239 | while(b_swap){ |
|---|
| 240 | b_swap=0; |
|---|
| 241 | for(i = base; i < max-1 ; i++){ |
|---|
| 242 | if( (*i)-(*(i+1)) > 0){ |
|---|
| 243 | // if( (*qcmp) (i,i+1) > 0){ |
|---|
| 244 | b_swap=1; |
|---|
| 245 | c = *i; |
|---|
| 246 | *i = *(i+1); |
|---|
| 247 | *(i+1)=c; |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | static |
|---|
| 254 | void qst(base, max) |
|---|
| 255 | int *base, *max; |
|---|
| 256 | { |
|---|
| 257 | register int c, *i, *j, *jj; |
|---|
| 258 | register int ii; |
|---|
| 259 | int *mid, *tmp; |
|---|
| 260 | int lo, hi; |
|---|
| 261 | lo = max - base; |
|---|
| 262 | do |
|---|
| 263 | { |
|---|
| 264 | mid = i = base + 1 * ((lo / qsz) >> 1); |
|---|
| 265 | if (lo >= mthresh) |
|---|
| 266 | { |
|---|
| 267 | j = ((*qcmp) ((jj = base), i) > 0 ? jj : i); |
|---|
| 268 | if ((*qcmp) (j, (tmp = max - 1)) > 0) |
|---|
| 269 | { |
|---|
| 270 | j = (j == jj ? i : jj); |
|---|
| 271 | if ((*qcmp) (j, tmp) < 0) |
|---|
| 272 | j = tmp; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | if (j != i) |
|---|
| 276 | { |
|---|
| 277 | ii = 1; |
|---|
| 278 | do |
|---|
| 279 | { |
|---|
| 280 | c = *i; |
|---|
| 281 | *i++ = *j; |
|---|
| 282 | *j++ = c; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | while (--ii); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | for (i = base, j = max - 1;;) |
|---|
| 290 | { |
|---|
| 291 | while (i < mid && (*qcmp) (i, mid) <= 0) |
|---|
| 292 | i ++; |
|---|
| 293 | while (j > mid) |
|---|
| 294 | { |
|---|
| 295 | if ((*qcmp) (mid, j) <= 0) |
|---|
| 296 | { |
|---|
| 297 | j --; |
|---|
| 298 | continue; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | tmp = i + 1; |
|---|
| 302 | if (i == mid) |
|---|
| 303 | { |
|---|
| 304 | mid = jj = j; |
|---|
| 305 | } |
|---|
| 306 | else |
|---|
| 307 | { |
|---|
| 308 | jj = j; |
|---|
| 309 | j --; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | goto swap; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | if (i == mid) |
|---|
| 316 | { |
|---|
| 317 | break; |
|---|
| 318 | } |
|---|
| 319 | else |
|---|
| 320 | { |
|---|
| 321 | jj = mid; |
|---|
| 322 | tmp = mid = i; |
|---|
| 323 | j --; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | swap: |
|---|
| 327 | ii = 1; |
|---|
| 328 | do |
|---|
| 329 | { |
|---|
| 330 | c = *i; |
|---|
| 331 | *i++ = *jj; |
|---|
| 332 | *jj++ = c; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | while (--ii); |
|---|
| 336 | i = tmp; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | i = (j = mid) + 1; |
|---|
| 340 | if ((lo = j - base) <= (hi = max - i)) |
|---|
| 341 | { |
|---|
| 342 | if (lo >= thresh) |
|---|
| 343 | qst(base, j); |
|---|
| 344 | base = i; |
|---|
| 345 | lo = hi; |
|---|
| 346 | } |
|---|
| 347 | else |
|---|
| 348 | { |
|---|
| 349 | if (hi >= thresh) |
|---|
| 350 | qst(i, max); |
|---|
| 351 | max = j; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | } |
|---|
| 355 | while (lo >= thresh); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | void SORT(int *base, int n, int size, int (*compar) (),int type){ |
|---|
| 359 | switch(type){ |
|---|
| 360 | case 0: |
|---|
| 361 | QSORT(base, n, size, compar); |
|---|
| 362 | break; |
|---|
| 363 | case 1: |
|---|
| 364 | simple_sort(base, n, size, compar); |
|---|
| 365 | break; |
|---|
| 366 | case 2: |
|---|
| 367 | insertion_sort(base, n, size, compar); |
|---|
| 368 | break; |
|---|
| 369 | case 3: |
|---|
| 370 | bubble_sort(base, n, size, compar); |
|---|
| 371 | break; |
|---|
| 372 | default: |
|---|
| 373 | break; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | |
|---|