Changeset 635 for trunk/user/sort
- Timestamp:
- Jun 26, 2019, 11:42:37 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/user/sort/sort.c
r629 r635 1 /* 2 * sort.c - Parallel sort 3 * 4 * Author Cesar Fuguet Tortolero (2013) 5 * Alain Greiner (2019) 6 * 7 * Copyright (c) UPMC Sorbonne Universites 8 * 9 * This is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; version 2.0 of the License. 12 * 13 * It is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with ALMOS-MKH; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 1 23 /////////////////////////////////////////////////////////////////////////////// 2 // File : sort.c 3 // Date : November 2013 4 // Author : Cesar Fuguet Tortolero <cesar.fuguet-tortolero@lip6.fr> 5 /////////////////////////////////////////////////////////////////////////////// 6 // This multi-threaded application implement a multi-stage sort application. 7 // The various stages are separated by synchronisation barriers. 24 // This multi-threaded application implement a multi-stage sort. 25 // It has been writen by Cesar Fuget Tortolero in 2013. 26 // It has been ported on ALMOS-MKH by Alain Greiner in 2019. 27 // 8 28 // There is one thread per physical cores. 9 29 // Computation is organised as a binary tree: … … 15 35 // Number_of_stages = number of barriers = log2(Number_of_threads) 16 36 // 37 // The various stages are separated by synchronisation barriers, and the 38 // main thread uses the join syscall to check that all threads completed 39 // before printing the computation time (sequencial & parallel). 40 // These results can be - optionnaly - registered in an instrumentation file. 41 // 17 42 // Constraints : 18 43 // - It supports up to 1024 cores: x_size, y_size, and ncores must be … … 29 54 #include <hal_macros.h> 30 55 31 #define ARRAY_LENGTH 4096// number of items56 #define ARRAY_LENGTH 128 // number of items 32 57 #define MAX_THREADS 1024 // 16 * 16 * 4 33 58 34 59 #define USE_DQT_BARRIER 1 // use DQT barrier if non zero 35 60 #define DISPLAY_ARRAY 0 // display items values before and after 36 #define DEBUG_MAIN 1 // trace main function 37 #define DEBUG_SORT 1 // trace sort function 38 #define INTERACTIVE_MODE 0 // activate idbg() during instrumentation 61 #define DEBUG_MAIN 0 // trace main function 62 #define DEBUG_SORT 0 // trace sort function 39 63 #define CHECK_RESULT 0 // for debug 40 64 #define INSTRUMENTATION 1 // register computation times on file 41 #define IDBG 0 // activate interactive debug in main42 65 43 66 ///////////////////////////////////////////////////////////// … … 62 85 pthread_barrier_t barrier; // synchronisation variables 63 86 64 pthread_attr_t attr[MAX_THREADS]; // thread attributes (one per thread) 65 args_t arg[MAX_THREADS]; // sort function arguments (one per thread) 87 pthread_t trdid[MAX_THREADS]; // kernel identifiers 88 pthread_attr_t attr[MAX_THREADS]; // thread attributes 89 args_t arg[MAX_THREADS]; // sort function arguments 66 90 67 91 //////////////////////////////////// … … 277 301 unsigned int lid; // core local index for a thread 278 302 unsigned int n; // index in array to sort 279 pthread_t trdid; // kernel allocated thread index (unused)280 303 pthread_barrierattr_t barrier_attr; // barrier attributes 281 304 … … 353 376 // launch other threads to execute sort() function 354 377 // on cores other than the core running the main thread 355 for ( x =0 ; x<x_size ; x++ )356 { 357 for ( y =0 ; y<y_size ; y++ )358 { 359 for ( lid =0 ; lid<ncores ; lid++ )378 for ( x = 0 ; x < x_size ; x++ ) 379 { 380 for ( y = 0 ; y < y_size ; y++ ) 381 { 382 for ( lid = 0 ; lid < ncores ; lid++ ) 360 383 { 384 // compute thread user index (continuous index) 361 385 thread_uid = (((x * y_size) + y) * ncores) + lid; 362 386 363 // set sortarguments for all threads387 // set arguments for all threads 364 388 arg[thread_uid].threads = total_threads; 365 389 arg[thread_uid].thread_uid = thread_uid; … … 367 391 368 392 // set thread attributes for all threads 369 attr[thread_uid].attributes = PT_ATTR_DETACH | 370 PT_ATTR_CLUSTER_DEFINED | 371 PT_ATTR_CORE_DEFINED; 393 attr[thread_uid].attributes = PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED; 372 394 attr[thread_uid].cxy = HAL_CXY_FROM_XY( x , y ); 373 395 attr[thread_uid].lid = lid; … … 375 397 if( thread_uid != main_uid ) 376 398 { 377 if ( pthread_create( &trdid , // not used because no join399 if ( pthread_create( &trdid[thread_uid], // buffer for kernel identifier 378 400 &attr[thread_uid], // thread attributes 379 401 &sort, // entry function … … 383 405 exit( 0 ); 384 406 } 385 else 386 { 387 #if DEBUG_MAIN 407 408 #if (DEBUG_MAIN & 1) 388 409 printf("\n[sort] main created thread %x \n", thread_uid ); 389 410 #endif 390 }391 411 } 392 412 } … … 402 422 #endif 403 423 404 #if INTERACTIVE_MODE405 idbg();406 #endif407 408 424 // the main thread run also the sort() function 409 425 sort( &arg[main_uid] ); 410 426 427 // wait other threads completion 428 for ( x = 0 ; x < x_size ; x++ ) 429 { 430 for ( y = 0 ; y < y_size ; y++ ) 431 { 432 for ( lid = 0 ; lid < ncores ; lid++ ) 433 { 434 // compute thread continuous index 435 thread_uid = (((x * y_size) + y) * ncores) + lid; 436 437 if( thread_uid != main_uid ) 438 { 439 if( pthread_join( trdid[thread_uid] , NULL ) ) 440 { 441 printf("\n[fft error] in main thread %d joining thread %d\n", 442 main_uid , thread_uid ); 443 exit( 0 ); 444 } 445 446 #if (DEBUG_MAIN & 1) 447 printf("\n[fft] main thread %d joined thread %d\n", main_uid, thread_uid ); 448 #endif 449 450 } 451 } 452 } 453 } 454 411 455 //////////////////////////// 412 456 get_cycle( ¶_end_cycle ); … … 417 461 // destroy barrier 418 462 pthread_barrier_destroy( &barrier ); 419 420 #if INTERACTIVE_MODE421 idbg();422 #endif423 463 424 464 #if CHECK_RESULT
Note: See TracChangeset
for help on using the changeset viewer.