Changeset 623 for trunk/user/sort
- Timestamp:
- Mar 6, 2019, 4:37:15 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/user/sort/sort.c
r619 r623 29 29 #include <hal_macros.h> 30 30 31 #define ARRAY_LENGTH 256 // number of values31 #define ARRAY_LENGTH 1024 // number of items 32 32 #define MAX_THREADS 1024 // 16 * 16 * 4 33 #define USE_DQT_BARRIER 1 34 #define DISPLAY_ARRAY 0 35 #define INTERACTIVE_MODE 0 33 34 #define USE_DQT_BARRIER 1 // use DQT barrier if non zero 35 #define DISPLAY_ARRAY 0 // display items values before and after 36 #define VERBOSE 0 // for debug 37 #define INTERACTIVE_MODE 0 // for debug 38 #define CHECK_RESULT 0 // for debug 39 #define INSTRUMENTATION 1 // register computation times on file 36 40 37 41 ///////////////////////////////////////////////////////////// … … 84 88 85 89 /////////////////////////////////// 86 static void merge( const int * src, 87 int * dst, 88 int length, 89 int init_pos_src_a, 90 int init_pos_src_b, 91 int init_pos_dst ) 90 static void merge( const int * src, // source array 91 int * dst, // destination array 92 int length, // number of items in a subset 93 int init_pos_src_a, // index first item in src subset A 94 int init_pos_src_b, // index first item in src subset B 95 int init_pos_dst ) // index first item in destination 92 96 { 93 97 int i; … … 135 139 unsigned int lid; 136 140 137 int * src_array = NULL;138 int * dst_array = NULL;141 int * src_array = NULL; 142 int * dst_array = NULL; 139 143 140 144 // get core coordinates an date … … 146 150 unsigned int main_uid = ptr->main_uid; 147 151 152 #if DISPLAY_ARRAY 153 unsigned int n; 154 if( thread_uid == main_uid ) 155 { 156 printf("\n*** array before sort\n"); 157 for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , array0[n] ); 158 } 159 #endif 160 161 ///////////////////////////////// 162 pthread_barrier_wait( &barrier ); 163 164 #if VERBOSE 165 printf("\n[sort] thread[%d] exit barrier 0\n", thread_uid ); 166 #endif 167 148 168 unsigned int items = ARRAY_LENGTH / threads; 149 169 unsigned int stages = __builtin_ctz( threads ) + 1; 150 170 151 printf("\n[SORT] thread[%d] : start\n", thread_uid ); 171 #if VERBOSE 172 printf("\n[sort] thread[%d] : start\n", thread_uid ); 173 #endif 152 174 153 175 bubbleSort( array0, items, items * thread_uid ); 154 176 155 printf("\n[SORT] thread[%d] : stage 0 completed\n", thread_uid ); 177 #if VERBOSE 178 printf("\n[sort] thread[%d] : stage 0 completed\n", thread_uid ); 179 #endif 156 180 157 181 ///////////////////////////////// 158 182 pthread_barrier_wait( &barrier ); 159 printf("\n[SORT] thread[%d] exit barrier 0\n", thread_uid ); 160 161 // the number of threads contributing to sort 162 // is divided by 2 at each next stage 183 184 #if VERBOSE 185 printf("\n[sort] thread[%d] exit barrier 0\n", thread_uid ); 186 #endif 187 188 #if DISPLAY_ARRAY 189 if( thread_uid == main_uid ) 190 { 191 printf("\n*** array after bubble sort\n"); 192 for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , array0[n] ); 193 } 194 #endif 195 196 // the number of threads contributing to sort is divided by 2 197 // and the number of items is multiplied by 2 at each next stage 163 198 for ( i = 1 ; i < stages ; i++ ) 164 199 { 165 pthread_barrier_wait( &barrier ); 200 if((i % 2) == 1) // odd stage 201 { 202 src_array = array0; 203 dst_array = array1; 204 } 205 else // even stage 206 { 207 src_array = array1; 208 dst_array = array0; 209 } 166 210 167 211 if( (thread_uid & ((1<<i)-1)) == 0 ) 168 212 { 169 printf("\n[SORT] thread[%d] : stage %d start\n", thread_uid , i ); 170 171 if((i % 2) == 1) // odd stage 172 { 173 src_array = array0; 174 dst_array = array1; 175 } 176 else // even stage 177 { 178 src_array = array1; 179 dst_array = array0; 180 } 181 213 214 #if VERBOSE 215 printf("\n[sort] thread[%d] : stage %d start\n", thread_uid , i ); 216 #endif 182 217 merge( src_array, 183 218 dst_array, 184 items << i,219 items << (i-1), 185 220 items * thread_uid, 186 221 items * (thread_uid + (1 << (i-1))), 187 222 items * thread_uid ); 188 223 189 printf("\n[SORT] thread[%d] : stage %d completed\n", thread_uid , i ); 224 #if VERBOSE 225 printf("\n[sort] thread[%d] : stage %d completed\n", thread_uid , i ); 226 #endif 190 227 } 191 228 192 229 ///////////////////////////////// 193 230 pthread_barrier_wait( &barrier ); 194 printf("\n[SORT] thread[%d] exit barrier %d\n", thread_uid , i ); 195 196 } 231 232 #if VERBOSE 233 printf("\n[sort] thread[%d] exit barrier %d\n", thread_uid , i ); 234 #endif 235 236 #if DISPLAY_ARRAY 237 if( thread_uid == main_uid ) 238 { 239 printf("\n*** array after merge %d\n", i ); 240 for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , dst_array[n] ); 241 } 242 #endif 243 244 } // en for stages 197 245 198 246 // all threads but the main thread exit … … 220 268 unsigned int lid; // core local index for a thread 221 269 unsigned int n; // index in array to sort 222 unsigned long long cycle; // current date for log223 270 pthread_t trdid; // kernel allocated thread index (unused) 224 271 pthread_barrierattr_t barrier_attr; // barrier attributes 225 272 273 unsigned long long start_cycle; 274 unsigned long long seq_end_cycle; 275 unsigned long long para_end_cycle; 276 277 ///////////////////////// 278 get_cycle( &start_cycle ); 279 226 280 // compute number of threads (one thread per core) 227 281 get_config( &x_size , &y_size , &ncores ); … … 240 294 (total_threads != 512) && (total_threads != 1024) ) 241 295 { 242 printf("\n[ SORT ERROR] number of cores must be power of 2\n");296 printf("\n[sort error] number of cores must be power of 2\n"); 243 297 exit( 0 ); 244 298 } … … 247 301 if ( ARRAY_LENGTH % total_threads) 248 302 { 249 printf("\n[ SORT ERROR] array size must be multiple of number of threads\n");303 printf("\n[sort error] array size must be multiple of number of threads\n"); 250 304 exit( 0 ); 251 305 } 252 306 253 printf("\n\n[ SORT] main starts on core[%x,%d] / %d threads / %d values / PID %x\n",254 main_cxy, main_lid, total_threads, ARRAY_LENGTH, getpid());307 printf("\n\n[sort] main starts / %d threads / %d items / pid %x / cycle %d\n", 308 total_threads, ARRAY_LENGTH, getpid(), (unsigned int)start_cycle ); 255 309 256 310 // initialize barrier … … 269 323 if( error ) 270 324 { 271 printf("\n[ SORT ERROR] cannot initialise barrier\n" );325 printf("\n[sort error] cannot initialise barrier\n" ); 272 326 exit( 0 ); 273 327 } 274 328 275 printf("\n[SORT] main completes barrier init\n"); 329 #if VERBOSE 330 printf("\n[sort] main completes barrier init\n"); 331 #endif 276 332 277 333 // Array to sort initialization … … 281 337 } 282 338 283 #if DISPLAY_ARRAY 284 printf("\n*** array before sort\n"); 285 for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , array0[n] ); 286 #endif 287 288 printf("\n[SORT] main completes array init\n"); 339 #if VERBOSE 340 printf("\n[sort] main completes array init\n"); 341 #endif 289 342 290 343 // launch other threads to execute sort() function … … 317 370 &arg[thread_uid] ) ) // sort arguments 318 371 { 319 printf("\n[ SORT ERROR] main cannot create thread %x \n", thread_uid );372 printf("\n[sort error] main cannot create thread %x \n", thread_uid ); 320 373 exit( 0 ); 321 374 } 322 375 else 323 376 { 324 printf("\n[SORT] main created thread %x \n", thread_uid ); 377 #if VERBOSE 378 printf("\n[sort] main created thread %x \n", thread_uid ); 379 #endif 325 380 } 326 381 } … … 329 384 } 330 385 331 get_cycle( &cycle ); 332 printf("\n[SORT] main completes threads create at cycle %d\n", (unsigned int)cycle ); 386 /////////////////////////// 387 get_cycle( &seq_end_cycle ); 388 389 #if VERBOSE 390 printf("\n[sort] main completes sequencial init at cycle %d\n", 391 (unsigned int)seq_end_cycle ); 392 #endif 333 393 334 394 #if INTERACTIVE_MODE … … 339 399 sort( &arg[main_uid] ); 340 400 401 //////////////////////////// 402 get_cycle( ¶_end_cycle ); 403 404 printf("\n[sort] main completes parallel sort at cycle %d\n", 405 (unsigned int)para_end_cycle ); 406 407 // destroy barrier 408 pthread_barrier_destroy( &barrier ); 409 341 410 #if INTERACTIVE_MODE 342 411 idbg(); 343 412 #endif 344 413 345 // destroy barrier 346 pthread_barrier_destroy( &barrier ); 347 348 #if INTERACTIVE_MODE 349 idbg(); 350 #endif 351 352 // Check result 353 int success = 1; 354 int* res_array = ( (total_threads == 2) || 355 (total_threads == 8) || 356 (total_threads == 32) || 357 (total_threads == 128) || 358 (total_threads == 512) ) ? array1 : array0; 359 360 for( n=0 ; n<(ARRAY_LENGTH-2) ; n++ ) 361 { 362 if ( res_array[n] > res_array[n+1] ) 363 { 364 printf("\n[SORT] array[%d] = %d > array[%d] = %d\n", 365 n , res_array[n] , n+1 , res_array[n+1] ); 366 success = 0; 367 break; 368 } 369 } 370 371 #if DISPLAY_ARRAY 372 printf("\n*** array after sort\n"); 373 for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , res_array[n] ); 374 #endif 375 376 get_cycle( &cycle ); 377 378 if ( success ) 379 { 380 printf("\n[SORT] success at cycle %d\n", (unsigned int)cycle ); 381 } 382 else 383 { 384 printf("\n[SORT] failure at cycle %d\n", (unsigned int)cycle ); 385 } 386 387 #if INTERACTIVE_MODE 388 idbg(); 414 #if CHECK_RESULT 415 int success = 1; 416 int* res_array = ( (total_threads == 2) || 417 (total_threads == 8) || 418 (total_threads == 32) || 419 (total_threads == 128) || 420 (total_threads == 512) ) ? array1 : array0; 421 422 for( n=0 ; n<(ARRAY_LENGTH-2) ; n++ ) 423 { 424 if ( res_array[n] > res_array[n+1] ) 425 { 426 printf("\n[sort] array[%d] = %d > array[%d] = %d\n", 427 n , res_array[n] , n+1 , res_array[n+1] ); 428 success = 0; 429 break; 430 } 431 } 432 433 if ( success ) printf("\n[sort] success\n"); 434 else printf("\n[sort] failure\n"); 435 #endif 436 437 #if INSTRUMENTATION 438 char name[64]; 439 char path[128]; 440 441 // build a file name from n_items / n_clusters / n_cores 442 if( USE_DQT_BARRIER ) snprintf( name , 64 , "sort_dqt_%d_%d_%d", 443 ARRAY_LENGTH, x_size * y_size, ncores ); 444 else snprintf( name , 64 , "sort_smp_%d_%d_%d", 445 ARRAY_LENGTH, x_size * y_size, ncores ); 446 447 // build file pathname 448 snprintf( path , 128 , "home/%s" , name ); 449 450 // compute results 451 unsigned int sequencial = (unsigned int)(seq_end_cycle - start_cycle); 452 unsigned int parallel = (unsigned int)(para_end_cycle - seq_end_cycle); 453 454 // display results on process terminal 455 printf("\n----- %s -----\n" 456 " - sequencial : %d cycles\n" 457 " - parallel : %d cycles\n", 458 name, sequencial, parallel ); 459 460 // open file 461 FILE * stream = fopen( path , NULL ); 462 if( stream == NULL ) 463 { 464 printf("\n[sort error] cannot open instrumentation file <%s>\n", name ); 465 exit(0); 466 } 467 468 // register results to file 469 fprintf( stream , "\n----- %s -----\n" 470 " - sequencial : %d cycles\n" 471 " - parallel : %d cycles\n", 472 name, sequencial, parallel ); 473 474 // close instrumentation file 475 if( fclose( stream ) ) 476 { 477 printf("\n[sort error] cannot close instrumentation file <%s>\n", name ); 478 exit(0); 479 } 389 480 #endif 390 481 … … 392 483 393 484 } // end main() 394 395 485 396 486 /*
Note: See TracChangeset
for help on using the changeset viewer.