Changeset 588 for trunk


Ignore:
Timestamp:
Nov 1, 2018, 12:44:35 PM (6 years ago)
Author:
alain
Message:

Introduce a signal based synchro between INIT and KSH processes
to sequencialize multiple KSH[i] processes creation.

Location:
trunk/user
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/user/fft/fft.c

    r582 r588  
    2929//
    3030// This application uses 4 shared data arrays, that are distributed
    31 // in all clusters (one sub-buffer per cluster):
     31// in all clusters (one buffer per cluster):
    3232// - data[N] contains N input data points, with 2 double per point.
    3333// - trans[N] contains N intermediate data points, 2 double per point.
     
    8686// parameters
    8787
    88 #define DEFAULT_M               6
     88#define DEFAULT_M               14              // 16 K data points
    8989#define MODE                    COSIN
    9090#define CHECK                   0
    91 #define DEBUG_MAIN              1               // trace main() function (detailed if odd)
    92 #define DEBUG_FFT1D             1               // trace FFT1D() function (detailed if odd)
     91#define DEBUG_MAIN              0               // trace main() function (detailed if odd)
     92#define DEBUG_SLAVE             0               // trace slave() function (detailed if odd)
     93#define DEBUG_FFT1D             0               // trace FFT1D() function (detailed if odd)
    9394#define DEBUG_ROW               0               // trace FFTRow() function (detailed if odd)
    9495#define PRINT_ARRAY             0
     
    9798#define SWAP(a,b) { double tmp; tmp = a; a = b; b = tmp; }
    9899
    99 /////////////////////////////////////////////////////////////////////////////////
     100/////////////////////////////////////////////////////////////////////////////////////
     101//             structure containing the arguments for the slave() function
     102/////////////////////////////////////////////////////////////////////////////////////
     103
     104typedef struct args_s
     105{
     106    unsigned int   tid;                    // thread continuous index
     107    unsigned int   main_tid;               // main thread continuous index
     108}
     109args_t;
     110
     111/////////////////////////////////////////////////////////////////////////////////////
    100112//             global variables
    101 /////////////////////////////////////////////////////////////////////////////////
     113/////////////////////////////////////////////////////////////////////////////////////
    102114
    103115unsigned int   x_size;                     // number of clusters per row in the mesh
     
    120132
    121133// instrumentation counters
    122 long           parallel_time[THREADS_MAX]; // total computation time (per thread)
    123 long           sync_time[THREADS_MAX];     // cumulative waiting time in barriers (per thread)
    124 long           init_time;                  // initialisation time (in main)
     134unsigned int   parallel_time[THREADS_MAX]; // total computation time (per thread)
     135unsigned int   sync_time[THREADS_MAX];     // cumulated waiting time in barriers (per thread)
     136unsigned int   init_time;                  // initialisation time (in main)
    125137
    126138// synchronisation barrier (all threads)
     
    131143pthread_t       trdid[THREADS_MAX];        // kernel threads identifiers
    132144pthread_attr_t  attr[THREADS_MAX];         // POSIX thread attributes
    133 unsigned int    args[THREADS_MAX];         // slave function arguments
     145args_t          args[THREADS_MAX];         // slave function arguments
    134146
    135147/////////////////////////////////////////////////////////////////////////////////
     
    137149/////////////////////////////////////////////////////////////////////////////////
    138150
    139 void slave( unsigned int * tid );
     151void slave( args_t * args );
    140152
    141153double CheckSum( void );
     
    215227
    216228    unsigned long long  start_init_cycle;
    217     unsigned long long  start_exec_cycle;
    218     unsigned long long  end_exec_cycle;
     229    unsigned long long  end_init_cycle;
    219230
    220231#if CHECK
     
    224235   
    225236    // get FFT application start cycle
    226     if( get_cycle( &start_init_cycle ) )
    227     {
    228         printf("[FFT ERROR] cannot get start cycle\n");
    229     }
     237    get_cycle( &start_init_cycle );
    230238
    231239    // get platform parameters to compute nthreads & nclusters
     
    279287    main_tid = (((main_x * y_size) + main_y) * ncores) + main_lid;
    280288
    281     printf("\n[FFT] main starts on core[%x,%d] / %d complex points / %d thread(s)\n",
    282     main_cxy, main_lid, N, nthreads );
     289    printf("\n[FFT] starts on core[%x,%d] / %d complex points / %d thread(s) / PID %x\n",
     290    main_cxy, main_lid, N, nthreads, getpid() );
    283291
    284292    // allocate memory for the distributed data[i], trans[i], umain[i], twid[i] buffers
     
    307315    InitT( twid );
    308316
    309     printf("\n[FFT] main complete arrays init\n");
     317    printf("\n[FFT] main completes arrays init\n");
    310318
    311319#if CHECK
     
    344352        for (y = 0 ; y < y_size ; y++)
    345353        {
     354            // compute cluster identifier
     355            cxy = HAL_CXY_FROM_XY( x , y );
     356
    346357            for ( lid = 0 ; lid < ncores ; lid++ )
    347358            {
     
    351362                // set thread attributes
    352363                attr[tid].attributes = PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED;
    353                 attr[tid].cxy        = HAL_CXY_FROM_XY( x , y );
     364                attr[tid].cxy        = cxy;
    354365                attr[tid].lid        = lid;
    355366
    356367                // set slave function argument
    357                 args[tid] = tid;
     368                args[tid].tid      = tid;
     369                args[tid].main_tid = main_tid;
    358370
    359371                // create thread
     
    368380                        exit( 0 );
    369381                    }
    370 #if DEBUG_MAIN
    371 printf("\n[FFT] main created thread %x on core %d in cluster(%d,%d) \n", tid, lid, x, y );
     382#if DEBUG_MAIN
     383unsigned long long debug_cycle;
     384get_cycle( &debug_cycle );
     385printf("\n[FFT] main created thread %x on core[%x,%d] / cycle %d\n",
     386tid, cxy, lid, (unsigned int)debug_cycle );
    372387#endif
    373388                }
     
    377392
    378393    // register sequencial initalisation completion cycle
    379     get_cycle( &start_exec_cycle );
    380     init_time = (long)(start_exec_cycle - start_init_cycle);
    381     printf("\n[FFT] main enter parallel execution\n");
     394    get_cycle( &end_init_cycle );
     395    init_time = (unsigned int)(end_init_cycle - start_init_cycle);
     396
     397    printf("\n[FFT] main enters parallel execution\n");
    382398   
    383     // main execute itself the slave() function
     399    // main itself executes the slave() function
    384400    slave( &args[main_tid] );
    385401
     
    396412                if( tid != main_tid )
    397413                {
    398 #if DEBUG_MAIN
    399 printf("\n[FFT] main join thread %x\n", trdid[tid] );
    400 #endif
    401414                    if( pthread_join( trdid[tid] , NULL ) )
    402415                    {
    403                         printf("\n[FFT ERROR] joining thread %x\n", trdid[tid] );
     416                        printf("\n[FFT ERROR] in main thread joining thread %x\n", tid );
    404417                        exit( 0 );
    405418                    }
     419                   
     420#if DEBUG_MAIN
     421printf("\n[FFT] main thread %d joined thread %d\n", main_tid, tid );
     422#endif
    406423
    407424                }
     
    409426        }
    410427    }
    411 
    412     // register parallel execution completion cycle
    413     get_cycle( &end_exec_cycle );
    414     printf("\n[FFT] complete parallel execution / cycle %d\n", (long)end_exec_cycle );
    415428
    416429#if PRINT_ARRAY
     
    433446
    434447    // open instrumentation file
    435     FILE * f = fopen( string , NULL );
    436     if ( f == NULL )
    437     {
    438         printf("\n[FFT ERROR] cannot open instrumentation file %s\n", string );
    439         exit( 0 );
    440     }
     448//  FILE * f = fopen( string , NULL );
     449//  if ( f == NULL )
     450//  {
     451//      printf("\n[FFT ERROR] cannot open instrumentation file %s\n", string );
     452//      exit( 0 );
     453//  }
    441454
    442455    snprintf( string , 256 , "\n[FFT] instrumentation : (%dx%dx%d) threads / %d points\n",
     
    445458    // display on terminal, and save to instrumentation file
    446459    printf( "%s" , string );
    447     fprintf( f , string );
    448 
     460//  fprintf( f , string );
     461
     462    for (tid = 0 ; tid < nthreads ; tid++)
     463    {
     464        snprintf( string , 256 , "\ntid %d : Init %d / Parallel %d / Sync %d\n",
     465        tid, init_time, parallel_time[tid], sync_time[tid] );
     466
     467        // display on terminal, and save to instrumentation file
     468        printf("%s" , string );
     469//      fprintf( f , string );
     470    }
     471
     472    // close instrumentation file and exit
     473//  fclose( f );
     474                             
     475
     476/*
    449477    long min_para = parallel_time[0];
    450478    long max_para = parallel_time[0];
     
    461489
    462490    snprintf( string , 256 , "\n      Init       Parallel   Barrier\n"
    463                              "MIN : %d  |  %d  |  %d   (cycles)\n"
    464                              "MAX : %d  |  %d  |  %d   (cycles)\n",
     491                             "MIN : %d\t | %d\t | %d\t   (cycles)\n"
     492                             "MAX : %d\t | %d\t | %d\t   (cycles)\n",
    465493                             (int)init_time, (int)min_para, (int)min_sync,
    466494                             (int)init_time, (int)max_para, (int)max_sync );
    467 
    468     // display on terminal, and save to instrumentation file
    469     printf("%s" , string );
    470     fprintf( f , string );
    471 
    472     // close instrumentation file and exit
    473     fclose( f );
    474 
    475     exit( 0 );
     495*/
     496
     497    pthread_exit( NULL );
    476498
    477499} // end main()
     
    480502// This function is executed in parallel by all threads.
    481503///////////////////////////////////////////////////////////////
    482 void slave( unsigned int * tid )
     504void slave( args_t * args )
    483505{
    484506    unsigned int   i;
    485     unsigned int   MyNum;           // continuous thread index
     507    unsigned int   MyNum;           // this thread index
     508    unsigned int   MainNum;         // main thread index
    486509    unsigned int   MyFirst;         // index first row allocated to thread
    487510    unsigned int   MyLast;          // index last row allocated to thread
     
    495518    unsigned long long  barrier_stop;
    496519
    497     MyNum = *tid;
    498 
    499     // BARRIER before parallel exec
    500     pthread_barrier_wait( &barrier );
     520    MyNum   = args->tid;
     521    MainNum = args->main_tid;
    501522
    502523    // initialise instrumentation
    503524    get_cycle( &parallel_start );
     525
     526#if DEBUG_SLAVE
     527printf("\n[FFT] %s : thread %x enter / cycle %d\n",
     528__FUNCTION__, MyNum, (unsigned int)parallel_start );
     529#endif
    504530
    505531    // allocate and initialise local array upriv[]
     
    526552    get_cycle( &barrier_stop );
    527553
    528     sync_time[MyNum] = (long)(barrier_stop - barrier_start);
     554    sync_time[MyNum] += (barrier_stop - barrier_start);
    529555
    530556#if CHECK
     
    540566    // register computation time
    541567    get_cycle( &parallel_stop );
    542     parallel_time[MyNum] = (long)(parallel_stop - parallel_start);
    543 
    544     // exit if MyNum != 0
    545     if( MyNum ) pthread_exit( 0 );
     568    parallel_time[MyNum] = (parallel_stop - parallel_start);
     569
     570#if DEBUG_SLAVE
     571printf("\n[FFT] %s : thread %x exit / parallel_time %d / sync_time %d / cycle %d\n",
     572__FUNCTION__, MyNum, parallel_time[MyNum], sync_time[MyNum], (unsigned int)parallel_stop );
     573#endif
     574
     575    // exit only if MyNum != MainNum
     576    if( MyNum != MainNum ) pthread_exit( NULL );
    546577
    547578}  // end slave()
     
    772803
    773804#if DEBUG_FFT1D
    774 printf("\n[FFT] %s : thread %x enter / first %d / last %d\n",
    775 __FUNCTION__, MyNum, MyFirst, MyLast );
     805unsigned long long cycle;
     806get_cycle( &cycle );
     807printf("\n[FFT] %s : thread %x enter / first %d / last %d / cycle %d\n",
     808__FUNCTION__, MyNum, MyFirst, MyLast, (unsigned int)cycle );
    776809#endif
    777810
     
    780813
    781814#if( DEBUG_FFT1D & 1 )
    782 unsigned long long cycle;
    783815get_cycle( &cycle );
    784816printf("\n[FFT] %s : thread %x after first transpose / cycle %d\n",
     
    891923if( PRINT_ARRAY ) PrintArray( x , N );
    892924#endif
    893 
    894925
    895926}  // end FFT1D()
  • trunk/user/init/init.c

    r581 r588  
    2121#include <sys/wait.h>
    2222
    23 #define DEBUG_PROCESS_INIT    1
     23#define DEBUG_PROCESS_INIT    0
    2424
    2525// TODO improve the get_config() syscall to return nb_txt_channels
     
    8383            snprintf( string , 64 , "[INIT] created KSH[%d] / pid = %x", i , ret_fork );
    8484            display_string( string );
     85
     86            // wait signal from KSH[i]
     87            pause();
    8588        }
    8689    }
     
    102105    for( x = 0 ; x < x_size ; x++ )
    103106    {
    104         for( y = 0 ; y < x_size ; y++ )
     107        for( y = 0 ; y < y_size ; y++ )
    105108        {
    106109            cxy = HAL_CXY_FROM_XY( x , y );
  • trunk/user/ksh/Makefile

    r457 r588  
    1616           -I$(LIBALMOSMKH_INCLUDE)  \
    1717           -I$(LIBSEMAPHORE_INCLUDE) \
    18            -I$(SHARED_INCLUDE)
     18           -I$(SHARED_INCLUDE)       \
     19           -I$(HAL_INCLUDE)
    1920
    2021compile: dirs build/ksh.elf
  • trunk/user/ksh/ksh.c

    r574 r588  
    4545#include <almosmkh.h>
    4646#include <semaphore.h>
     47#include <hal_macros.h>
    4748
    4849#define CMD_MAX_SIZE   (256)    // max number of characters in one command
     
    319320            cxy = atoi(argv[2]);
    320321
    321         if( display_cluster_processes( cxy ) )
     322        if( display_cluster_processes( cxy , 0 ) )
    322323        {
    323324            printf("  error: illegal argument cxy = %x\n", cxy );
     
    465466        char               * pathname;           // path to .elf file
    466467    unsigned int         background;         // background execution if non zero
    467 
    468         if( (argc < 2) || (argc > 3) ) 
    469     {
    470                 printf("  usage: %s pathname [&] / argc = %d\n", argv[0], argc );  // @@@
     468    unsigned int         placement;          // placement specified if non zero
     469    unsigned int         cxy;                // target cluster if placement specified
     470
     471        if( (argc < 2) || (argc > 4) ) 
     472    {
     473                printf("  usage: %s pathname [cxy] [&]\n", argv[0] );
    471474                return;
    472475        }
     
    474477        pathname = argv[1];
    475478
    476     if( argc == 3 ) background = (argv[2][0] == '&');
    477     else            background = 0;
     479    if( argc == 2 )
     480    {
     481        background = 0;
     482        placement  = 0;
     483        cxy        = 0;
     484    }
     485    else if( argc == 3 )
     486    {
     487        if( (argv[2][0] == '&') && (argv[2][1] == 0) )
     488        {
     489            background = 1;
     490            placement  = 0;
     491            cxy        = 0;
     492        }
     493        else
     494        {
     495            background = 0;
     496            placement  = 1;
     497            cxy        = atoi( argv[2] );
     498        }
     499    }
     500    else  // argc == 4
     501    {
     502        background = ( (argv[3][0] == '&') && (argv[3][1] == 0) );
     503        placement  = 1;
     504        cxy        = atoi( argv[2] );
     505    }
    478506
    479507    // get KSH process PID
     
    483511long long unsigned cycle;
    484512get_cycle( &cycle );
    485 printf("\n@@@ %s : KSH PID %x before fork / path %s / background %d / cycle %d\n",
    486 __FUNCTION__, ksh_pid, argv[1], background, (int)cycle );
     513printf("\n[KSH] %s : ksh_pid %x / path %s / bg %d / place %d (%x) / cycle %d\n",
     514__FUNCTION__, ksh_pid, argv[1], background, placement, cxy, (int)cycle );
    487515#endif
     516
     517    // set target cluster if required
     518    if( placement ) place_fork( cxy );
    488519
    489520    // KSH process fork CHILD process
     
    500531#if CMD_LOAD_DEBUG
    501532get_cycle( &cycle );
    502 printf("\n@@@ %s : CHILD_PID %x after fork, before exec / cycle %d\n",
     533printf("\n[KSH] %s : child_pid %x after fork, before exec / cycle %d\n",
    503534__FUNCTION__ , getpid(), (int)cycle );
    504535#endif
     
    509540#if CMD_LOAD_DEBUG
    510541get_cycle( &cycle );
    511 printf("\n@@@ %s : CHILD_PID %x after exec / ret_exec %d / cycle %d\n",
     542printf("\n[KSH] %s : child_pid %x after exec / ret_exec %d / cycle %d\n",
    512543__FUNCTION__ , getpid(), ret_exec, (int)cycle );
    513544#endif
     
    525556#if CMD_LOAD_DEBUG
    526557get_cycle( &cycle );
    527 printf("\n@@@ %s : KSH_PID %x after fork / ret_fork %x / cycle %d\n",
     558printf("\n[KSH] %s : ksh_pid %x after fork / ret_fork %x / cycle %d\n",
    528559__FUNCTION__, getpid(), ret_fork, (int)cycle );
    529560#endif
     
    636667
    637668}  // end cmd_mv
     669
     670
     671////////////////////////////////////////////
     672static void cmd_ps( int argc , char **argv )
     673{
     674    unsigned int x_size;
     675    unsigned int y_size;
     676    unsigned int ncores;
     677    unsigned int x;
     678    unsigned int y;
     679
     680        if (argc != 1)
     681    {
     682                printf("  usage: %s\n", argv[0]);
     683                return;
     684        }
     685
     686    // get platform config
     687    get_config( &x_size , &y_size , &ncores );
     688
     689    // scan all clusers
     690    for( x = 0 ; x < x_size ; x++ )
     691    {
     692        for( y = 0 ; y < y_size ; y++ )
     693        {
     694            display_cluster_processes( HAL_CXY_FROM_XY(x,y), 1 );  // only owned processes
     695        }
     696    }
     697
     698    // release semaphore to get next command
     699    sem_post( &semaphore );
     700
     701}  // end cmd_ps()
    638702
    639703/////////////////////////////////////////////
     
    758822        { "mv",      "move a file in file system",                      cmd_mv      },
    759823        { "pwd",     "print current working directory",                 cmd_pwd     },
     824        { "ps",      "display all processes",                           cmd_ps      },
    760825        { "rm",      "remove a file from file system",                  cmd_rm      },
    761826        { "rmdir",   "remove a directory from file system",             cmd_rmdir   },
     
    830895        unsigned int   state;                   // escape sequence state
    831896
    832 /* This can be used to simplify debug, as it avoids interactive mode
    833 
    834 for( i=1 ; 1 ; i++ )
    835 {
    836     if( sem_wait( &semaphore ) )
    837     {
    838         printf("\n[ksh error] cannot found semafore\n" );
    839         exit( 1 );
    840     }
    841     else
    842     {
    843         printf("\n[ksh] %d for fft\n", i );
    844     }
    845     strcpy( buf , "load /bin/user/fft.elf" );
    846     parse( buf );
     897
     898/* To lauch one application without interactive mode
     899   
     900if( sem_wait( &semaphore ) )
     901{
     902    printf("\n[ksh error] cannot found semafore\n" );
     903    exit( 1 );
    847904}
     905else
     906{
     907    printf("\n[ksh] for fft\n");
     908}
     909
     910strcpy( buf , "load /bin/user/fft.elf" );
     911parse( buf );
    848912
    849913*/
     
    10811145printf("\n[ksh] main launched interactive thread => wait children termination\n" );
    10821146#endif
     1147
     1148    // signal INIT process
     1149    kill( 1 , SIGCONT );
    10831150   
    10841151    // enter infinite loop monitoring children processes termination
  • trunk/user/sort/sort.c

    r574 r588  
    2828#include <hal_macros.h>
    2929
    30 #define ARRAY_LENGTH        0x100    // 256 values
    31 
    32 #define MAX_THREADS         1024     // 16 * 16 * 4
     30#define ARRAY_LENGTH        0x1000    // 4096 values
     31
     32#define MAX_THREADS         1024       // 16 * 16 * 4
    3333
    3434#define DISPLAY_ARRAY       0
     
    6060
    6161////////////////////////////////////
    62 static void bubbleSort(
    63     int * array,
    64     unsigned int length,
    65     unsigned int init_pos )
     62static void bubbleSort( int * array,
     63                        unsigned int length,
     64                        unsigned int init_pos )
    6665{
    6766    unsigned int i;
     
    8483
    8584
    86 /////////////////////////
    87 static void merge(
    88     const int * src,
    89     int * dst,
    90     int length,
    91     int init_pos_src_a,
    92     int init_pos_src_b,
    93     int init_pos_dst )
     85///////////////////////////////////
     86static 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 )
    9492{
    9593    int i;
     
    129127}  // end merge()
    130128
    131 /////////////////////////
     129//////////////////////////////////////
    132130static void sort( const args_t * ptr )
    133131{
     
    252250    }
    253251
    254     printf("\n[SORT] main starts on core[%x,%d] / %d thread(s) / %d values\n",
    255     main_cxy, main_lid, threads, ARRAY_LENGTH );
     252    printf("\n[SORT] main starts on core[%x,%d] / %d thread(s) / %d values / PID %x\n",
     253    main_cxy, main_lid, threads, ARRAY_LENGTH, getpid() );
    256254
    257255    // Barrier initialization
Note: See TracChangeset for help on using the changeset viewer.