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