Changeset 676


Ignore:
Timestamp:
Nov 20, 2020, 12:11:35 AM (18 months ago)
Author:
alain
Message:

Introduce chat application to test the named pipes.

Location:
trunk/user
Files:
4 added
9 edited

Legend:

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

    r659 r676  
    88// per core, and uses the POSIX threads API.
    99//
    10 // The main() function can be launched on any processor P[x,y,l].
    11 // It makes the initialisations, launch (N-1) threads to run the execute() function
    12 // on the (N-1) other processors than P[x,y,l], call himself the execute() function,
    13 // and finally call the instrument() function to display instrumentation results
    14 // when the parallel execution is completed.
     10// The input image is read from a file and the output image is saved to another file.
     11//
     12// - number of clusters containing processors must be power of 2 no larger than 256.
     13// - number of processors per cluster must be power of 2 no larger than 4.
     14// - number of working threads is the number of cores availables in the hardware
     15//   architecture : nthreads = nclusters * ncores.
    1516//
    1617// The convolution kernel is defined in the execute() function.
    1718// It can be factored in two independant line and column convolution products.
    18 // The five buffers containing the image are distributed in clusters.
    19 // For the philips image, it is a [201]*[35] pixels rectangle, and the.
    20 //
    21 // The (1024 * 1024) pixels image is read from a file (2 bytes per pixel).
    2219//
    23 // - number of clusters containing processors must be power of 2 no larger than 256.
    24 // - number of processors per cluster must be power of 2 no larger than 4.
     20// The main() function can be launched on any processor.
     21// - It checks software requirements versus the hardware resources.
     22// - It open & maps the input file to a global <image_in> buffer.
     23// - it open & maps the output file to another global <image_out> buffer.
     24// - it open the instrumentation file.
     25// - it creates & activates two FBF windows to display input & output images.
     26// - it launches other threads to run in parallel the execute() function.
     27// - it saves the instrumentation results on disk.
     28// - it closes the input, output, & instrumentation files.
     29// - it deletes the FBF input & output windows.
    2530//
    26 // The number N of working threads is always defined by the number of cores availables
    27 // in the architecture, but this application supports three placement modes.
     31// The execute() function is executed in parallel by all threads. These threads are
     32// working on 5 arrays of distributed buffers, indexed by the cluster index [cid].
     33// - A[cid]: contain the distributed initial image (NL/NCLUSTERS lines per cluster).
     34// - B[cid]: is the result of horizontal filter, then transpose B <= Trsp(HF(A)
     35// - C[cid]: is the result of vertical image, then transpose : c <= Trsp(VF(B)
     36// - D[cid]: is the the difference between A and FH(A) : D <= A - FH(A)
     37// - Z[cid]: contain the distributed final image Z <= C + D
     38//
     39// It can be split in four phases separated by synchronisation barriers:
     40// 1. Initialisation:
     41//    Allocates the 5 A[cid],B[cid],C[cid],D[cid],Z[cid] buffers, initialise A[cid]
     42//    from the <image_in> buffer, and display the initial image on FBF if rquired.
     43// 2. Horizontal Filter:
     44//    Set B[cid] and D[cid] from A[cid]. Read data accesses are local, write data
     45//    accesses are remote, to implement the transpose.
     46// 3. Vertical Filter: 
     47//    Set C[cid] from B[cid]. Read data accesses are local, write data accesses
     48//    are remote, to implement the transpose.
     49// 4. Save results:
     50//    Set the Z[cid] from C[cid] and D[cid]. All read and write access are local.
     51//    Move the final image (Z[cid] buffer) to the <image_out> buffer.   
     52//
     53// This application supports three placement modes, implemented in the main() function.
    2854// In all modes, the working threads are identified by the [tid] continuous index
    2955// in range [0, NTHREADS-1], and defines how the lines are shared amongst the threads.
    3056// This continuous index can always be decomposed in two continuous sub-indexes:
    31 // tid == cid * ncores + lid,  where cid is in [0,NCLUSTERS-1] and lid in [0,NCORES-1].
     57// tid == cid * NCORES + lid,  where cid is in [0,NCLUSTERS-1] and lid in [0,NCORES-1].
    3258//
    3359// - NO_PLACEMENT: the main thread is itsef a working thread. The (N_1) other working
     
    3864//   but has tid = 0 (i.e. cid = 0 & tid = 0).
    3965//
    40 // - EXPLICIT_PLACEMENT: the main thread is again a working thread, but the placement of
     66// - EXPLICIT_PLACEMENT: the main thread is again a working thread, but the placement
    4167//   of the threads on the cores is explicitely controled by the main thread to have
    4268//   exactly one working thread per core, and the [cxy][lpid] core coordinates for a given
     
    4672// - PARALLEL_PLACEMENT: the main thread is not anymore a working thread, and uses the
    4773//   non standard pthread_parallel_create() function to avoid the costly sequencial
    48 //   loops for pthread_create() and pthread_join(). It garanty one working thread
     74//   loops for pthread_create() and pthread_join(). It garanties one working thread
    4975//   per core, and the same relation between the thread[tid] and the core[cxy][lpid].
    5076//
     
    6591
    6692#define VERBOSE_MAIN               1
    67 #define VERBOSE_EXEC               0
     93#define VERBOSE_EXEC               1
    6894#define SUPER_VERBOSE              0
    6995
     
    74100#define THREADS_MAX                (X_MAX * Y_MAX * CORES_MAX)
    75101
    76 #define IMAGE_IN_PATH              "misc/philips_1024_2.raw"
    77 #define IMAGE_IN_PIXEL_SIZE        2                               // 2 bytes per pixel
    78 
    79 #define IMAGE_OUT_PATH             "misc/philips_after_1O24.raw"
    80 #define IMAGE_OUT_PIXEL_SIZE       1                               // 1 bytes per pixel
    81 
    82 #define FBF_TYPE                   420
    83 #define NL                         1024
    84 #define NP                         1024
    85 #define NB_PIXELS                  (NP * NL)
     102#define IMAGE_TYPE                 420                         // pixel encoding type
     103#define INPUT_IMAGE_PATH           "misc/couple_512.raw"       // default image_in
     104#define OUTPUT_IMAGE_PATH          "misc/couple_conv_512.raw"  // default image_out
     105#define NL                         512                         // default nlines
     106#define NP                         512                         // default npixels
    86107
    87108#define NO_PLACEMENT               0
     
    89110#define PARALLEL_PLACEMENT         1
    90111
     112#define INTERACTIVE_MODE           0
    91113#define USE_DQT_BARRIER            1
    92114#define INITIAL_DISPLAY_ENABLE     1
     
    116138unsigned int V_BEG[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
    117139unsigned int V_END[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
    118 unsigned int D_BEG[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
    119 unsigned int D_END[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
     140unsigned int F_BEG[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
     141unsigned int F_END[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
    120142
    121143// pointer on buffer containing the input image, maped by the main to the input file
     
    128150unsigned int THREAD_EXIT_SUCCESS = 0;
    129151unsigned int THREAD_EXIT_FAILURE = 1;
     152
     153// pointer and identifier for FBF windows
     154void   *  in_win_buf;
     155int       in_wid;
     156void   *  out_win_buf;
     157int       out_wid;
    130158
    131159// synchronization barrier
     
    137165unsigned int  ncores;              // number of processors per cluster
    138166
     167// main thread continuous index
     168unsigned int     tid_main;
     169
    139170// arrays of pointers on distributed buffers in all clusters
    140 unsigned short * GA[CLUSTERS_MAX];
     171unsigned char * GA[CLUSTERS_MAX];
    141172int            * GB[CLUSTERS_MAX];
    142173int            * GC[CLUSTERS_MAX];
     
    153184pthread_parallel_work_args_t exec_args[THREADS_MAX];
    154185
    155 // main thread continuous index
    156 unsigned int     tid_main;
     186// image features
     187unsigned int   image_nl;
     188unsigned int   image_np;
     189char           input_image_path[128];
     190char           output_image_path[128];
    157191
    158192/////////////////////////////////////////////////////////////////////////////////////
     
    166200/////////////////
    167201void main( void )
     202/////////////////
    168203{
    169204    unsigned long long start_cycle;
     
    222257    unsigned int nthreads  = nclusters * ncores;
    223258
     259    // get input and output images pathnames and size
     260    if( INTERACTIVE_MODE )
     261    {
     262        // get image size
     263        printf("\n[convol] image nlines      : ");
     264        get_uint32( &image_nl );
     265
     266        printf("\n[convol] image npixels     : ");
     267        get_uint32( &image_np );
     268
     269        printf("\n[convol] input image path  : ");
     270        get_string( input_image_path , 128 );
     271
     272        printf("[convol] output image path : ");
     273        get_string( output_image_path , 128 );
     274    }
     275    else
     276    {
     277        image_nl = NL;
     278        image_np = NP;
     279        strcpy( input_image_path  , INPUT_IMAGE_PATH );
     280        strcpy( output_image_path , OUTPUT_IMAGE_PATH );
     281    }
     282
    224283    // main thread get FBF size and type
    225     unsigned int   fbf_width;
    226     unsigned int   fbf_height;
    227     unsigned int   fbf_type;
     284    int   fbf_width;
     285    int   fbf_height;
     286    int   fbf_type;
    228287    fbf_get_config( &fbf_width , &fbf_height , &fbf_type );
    229288
    230     if( (fbf_width != NP) || (fbf_height != NL) || (fbf_type != FBF_TYPE) )
    231     {
    232         printf("\n[convol error] image does not fit FBF size or type\n");
    233         exit( 0 );
    234     }
    235 
    236     if( nthreads > NL )
    237     {
    238         printf("\n[convol error] number of threads larger than number of lines\n");
     289    if( ((unsigned int)fbf_width  < image_np) ||
     290        ((unsigned int)fbf_height < image_nl) ||
     291        (fbf_type != IMAGE_TYPE) )
     292    {
     293        printf("\n[convol error] image not acceptable\n"
     294               "FBF width  = %d / npixels  = %d\n"
     295               "FBF height = %d / nlines   = %d\n"
     296               "FBF type   = %d / expected = %d\n",
     297               fbf_width, image_np, fbf_height, image_nl, fbf_type, IMAGE_TYPE );
     298        exit( 0 );
     299    }
     300
     301    if( nthreads > image_nl )
     302    {
     303        printf("\n[convol error] nthreads (%d] larger than nlines (%d)\n",
     304        nthreads , image_nl );
    239305        exit( 0 );
    240306    }
     
    248314        // build instrumentation file name
    249315        if( USE_DQT_BARRIER )
    250         snprintf( instru_name , 32 , "conv_dqt_no_place_%d_%d", x_size * y_size , ncores );
     316        snprintf( instru_name , 32 , "dqt_no_place_%d_%d", x_size * y_size , ncores );
    251317        else
    252         snprintf( instru_name , 32 , "conv_smp_no_place_%d_%d", x_size * y_size , ncores );
     318        snprintf( instru_name , 32 , "smp_no_place_%d_%d", x_size * y_size , ncores );
    253319    }
    254320
     
    260326        // build instrumentation file name
    261327        if( USE_DQT_BARRIER )
    262         snprintf( instru_name , 32 , "conv_dqt_explicit_%d_%d", x_size * y_size , ncores );
     328        snprintf( instru_name , 32 , "dqt_explicit_%d_%d", x_size * y_size , ncores );
    263329        else
    264         snprintf( instru_name , 32 , "conv_smp_explicit_%d_%d", x_size * y_size , ncores );
     330        snprintf( instru_name , 32 , "smp_explicit_%d_%d", x_size * y_size , ncores );
    265331    }
    266332
     
    272338        // build instrumentation file name
    273339        if( USE_DQT_BARRIER )
    274         snprintf( instru_name , 32 , "conv_dqt_parallel_%d_%d", x_size * y_size , ncores );
     340        snprintf( instru_name , 32 , "dqt_parallel_%d_%d", x_size * y_size , ncores );
    275341        else
    276         snprintf( instru_name , 32 , "conv_smp_parallel_%d_%d", x_size * y_size , ncores );
     342        snprintf( instru_name , 32 , "smp_parallel_%d_%d", x_size * y_size , ncores );
    277343    }
    278344
    279345    // open instrumentation file
    280     snprintf( instru_path , 64 , "/home/%s", instru_name );
     346    snprintf( instru_path , 64 , "/home/convol/%s", instru_name );
    281347    FILE * f_instru = fopen( instru_path , NULL );
    282348    if ( f_instru == NULL )
     
    289355printf("\n[convol] main on core[%x,%d] open instrumentation file %s\n",
    290356cxy_main, lid_main, instru_path );
     357#endif
     358
     359    // main create an FBF window for input image
     360    in_wid = fbf_create_window( 0,                   // l_zero
     361                                0,                   // p_zero
     362                                image_nl,            // lines
     363                                image_np,            // pixels
     364                                &in_win_buf );
     365    if( in_wid < 0 )
     366    {
     367        printf("\n[transpose error] cannot open FBF window for %s\n",
     368        input_image_path);
     369        exit( 0 );
     370    }
     371
     372    // activate window
     373    error = fbf_active_window( in_wid , 1 );
     374
     375    if( error )
     376    {
     377        printf("\n[transpose error] cannot activate window for %s\n",
     378        input_image_path );
     379        exit( 0 );
     380    }
     381
     382#if  VERBOSE_MAIN
     383printf("\n[convol] main on core[%x,%d] created FBF window (wid %d) for <%s>\n",
     384cxy_main, lid_main, in_wid, input_image_path );
     385#endif
     386
     387    // main create an FBF window for output image
     388    out_wid = fbf_create_window( 0,                   // l_zero
     389                                 image_np,            // p_zero
     390                                 image_nl,            // lines
     391                                 image_np,            // pixels
     392                                 &out_win_buf );
     393    if( out_wid < 0 )
     394    {
     395        printf("\n[transpose error] cannot create FBF window for %s\n",
     396        output_image_path);
     397        exit( 0 );
     398    }
     399
     400    // activate window
     401    error = fbf_active_window( out_wid , 1 );
     402
     403    if( error )
     404    {
     405        printf("\n[transpose error] cannot activate window for %s\n",
     406        output_image_path );
     407        exit( 0 );
     408    }
     409
     410#if  VERBOSE_MAIN
     411printf("\n[convol] main on core[%x,%d] created FBF window (wid %d) for <%s>\n",
     412cxy_main, lid_main, out_wid, output_image_path );
    291413#endif
    292414
     
    312434
    313435#if VERBOSE_MAIN
    314 printf("\n[convol] main on core[%x,%d] completes barrier init\n",
     436printf("\n[convol] main on core[%x,%d] completed barrier init\n",
    315437cxy_main, lid_main );
    316438#endif
    317439
    318440    // main open input file
    319     int fd_in = open( IMAGE_IN_PATH , O_RDONLY , 0 );
     441    int fd_in = open( input_image_path , O_RDONLY , 0 );
    320442
    321443    if ( fd_in < 0 )
    322444    {
    323         printf("\n[convol error] cannot open input file <%s>\n", IMAGE_IN_PATH );
    324         exit( 0 );
    325     }
    326 
    327 #if VERBOSE_MAIN
    328 printf("\n[convol] main on core[%x,%d] open file <%s>\n",
    329 cxy_main, lid_main, IMAGE_IN_PATH );
    330 #endif
    331    
    332     // main thread map image_in buffer to input file
     445        printf("\n[convol error] cannot open input file <%s>\n", input_image_path );
     446        exit( 0 );
     447    }
     448
     449    // main thread map input file to image_in buffer
    333450    image_in = (unsigned char *)mmap( NULL,
    334                                       NB_PIXELS * IMAGE_IN_PIXEL_SIZE,
     451                                      image_np * image_nl,
    335452                                      PROT_READ,
    336453                                      MAP_FILE | MAP_SHARED,
     
    339456    if ( image_in == NULL )
    340457    {
    341         printf("\n[convol error] main cannot map buffer to file %s\n", IMAGE_IN_PATH );
     458        printf("\n[convol error] main cannot map buffer to file %s\n", input_image_path );
    342459        exit( 0 );
    343460    }
    344461
    345462#if  VERBOSE_MAIN
    346 printf("\n[convol] main on core[%x,%x] map buffer to file <%s>\n",
    347 cxy_main, lid_main, IMAGE_IN_PATH );
     463printf("\n[convol] main on core[%x,%x] map <image_in> buffer to file <%s>\n",
     464cxy_main, lid_main, input_image_path );
    348465#endif
    349466
    350467    // main thread open output file
    351     int fd_out = open( IMAGE_OUT_PATH , O_CREAT , 0 );
     468    int fd_out = open( output_image_path , O_CREAT , 0 );
    352469
    353470    if ( fd_out < 0 )
    354471    {
    355         printf("\n[convol error] main cannot open file %s\n", IMAGE_OUT_PATH );
    356         exit( 0 );
    357     }
    358 
    359 #if  VERBOSE_MAIN
    360 printf("\n[convol] main on core[%x,%d] open file <%s>\n",
    361 cxy_main, lid_main, IMAGE_OUT_PATH );
    362 #endif
     472        printf("\n[convol error] main cannot open file %s\n", output_image_path );
     473        exit( 0 );
     474    }
    363475
    364476    // main thread map image_out buffer to output file
    365477    image_out = (unsigned char *)mmap( NULL,
    366                                        NB_PIXELS + IMAGE_OUT_PIXEL_SIZE,
     478                                       image_np * image_nl,
    367479                                       PROT_WRITE,
    368480                                       MAP_FILE | MAP_SHARED,
     
    371483    if ( image_out == NULL )
    372484    {
    373         printf("\n[convol error] main cannot map buffer to file %s\n", IMAGE_OUT_PATH );
     485        printf("\n[convol error] main cannot map buffer to file %s\n", output_image_path );
    374486        exit( 0 );
    375487    }
    376488
    377489#if  VERBOSE_MAIN
    378 printf("\n[convol] main on core[%x,%x] map buffer to file <%s>\n",
    379 cxy_main, lid_main, IMAGE_OUT_PATH );
     490printf("\n[convol] main on core[%x,%x] map <image_out> buffer to file <%s>\n",
     491cxy_main, lid_main, output_image_path );
    380492#endif
    381493
     
    389501{
    390502    // the tid value for the main thread is always 0
    391     // main thread creates new threads with tid in [1,nthreads-1] 
     503    // main thread creates other threads with tid in [1,nthreads-1] 
    392504    unsigned int tid;
    393505    for ( tid = 0 ; tid < nthreads ; tid++ )
     
    587699#endif
    588700
     701    // ask confirm for exit
     702    if( INTERACTIVE_MODE )
     703    {
     704        char byte;
     705        printf("\n[convol] press any key to to delete FBF windows and exit\n");
     706        getc( &byte );
     707    }
     708 
     709    // main thread delete FBF windows
     710    fbf_delete_window( in_wid );
     711    fbf_delete_window( out_wid );
     712
     713#if VERBOSE_MAIN
     714printf("\n[convol] main deleted FBF windows\n" );
     715#endif
     716
    589717    // main thread suicide
    590718    exit( 0 );
     
    597725
    598726
     727
     728
     729
     730
    599731//////////////////////////////////
    600732void * execute( void * arguments )
    601 
     733//////////////////////////////////
    602734{
    603735    unsigned long long date;
     
    628760    // thread [cid][lid] indexes, and the core coordinates [cxy][lpid]
    629761
    630     // get thread abstract identifiers 
     762    // get thread abstract identifiers[cid,lid]  from tid
    631763    unsigned int tid = args->tid;
    632764    unsigned int cid = tid / ncores;   
     
    642774#endif
    643775
    644     // build total number of threads and clusters from global variables
     776    // compute nthreads and nclusters from global variables
    645777    unsigned int nclusters = x_size * y_size;
    646778    unsigned int nthreads  = nclusters * ncores;
     
    652784    unsigned int z;                 // vertical filter index
    653785
    654     unsigned int lines_per_thread   = NL / nthreads;
    655     unsigned int lines_per_cluster  = NL / nclusters;
    656     unsigned int pixels_per_thread  = NP / nthreads;
    657     unsigned int pixels_per_cluster = NP / nclusters;
    658 
    659     // compute number of pixels stored in one abstract cluster cid
    660     unsigned int local_pixels = NL * NP / nclusters;       
    661 
    662     unsigned int first, last;
     786    unsigned int lines_per_thread   = image_nl / nthreads;
     787    unsigned int lines_per_cluster  = image_nl / nclusters;
     788    unsigned int pixels_per_thread  = image_np / nthreads;
     789    unsigned int pixels_per_cluster = image_np / nclusters;
     790
     791    // compute number of pixels stored in one cluster
     792    unsigned int local_pixels = image_nl * image_np / nclusters;       
    663793
    664794    get_cycle( &date );
    665795    START[cid][lid] = (unsigned int)date;
    666796
    667     // Each thread[cid][0] allocates 5 local buffers,
     797    // Each thread[cid][0] allocates 5 buffers local cluster cid
    668798    // and registers these 5 pointers in the global arrays
    669799    if ( lid == 0 )
    670800    {
    671         GA[cid] = malloc( local_pixels * sizeof( unsigned short ) );
     801        GA[cid] = malloc( local_pixels * sizeof( unsigned char ) );
    672802        GB[cid] = malloc( local_pixels * sizeof( int ) );
    673803        GC[cid] = malloc( local_pixels * sizeof( int ) );
     
    675805        GZ[cid] = malloc( local_pixels * sizeof( unsigned char ) );
    676806
    677         if( (GA[cid] == NULL) || (GB[cid] == NULL) || (GC[cid] == NULL) ||
    678             (GD[cid] == NULL) || (GZ[cid] == NULL) )
     807        if( (GA[cid] == NULL) ||
     808            (GB[cid] == NULL) ||
     809            (GC[cid] == NULL) ||
     810            (GD[cid] == NULL) ||
     811            (GZ[cid] == NULL) )
    679812        {
    680813            printf("\n[convol error] thread[%d] cannot allocate buf_in\n", tid );
     
    684817#if VERBOSE_EXEC
    685818get_cycle( &date );
    686 printf( "\n[convol] exec[%d] on core[%x,%d] allocated shared buffers / cycle %d\n"
     819printf("\n[convol] exec[%d] on core[%x,%d] allocated shared buffers / cycle %d\n"
    687820" GA %x / GB %x / GC %x / GD %x / GZ %x\n",
    688821tid, cxy , lpid, (unsigned int)date, GA[cid], GB[cid], GC[cid], GD[cid], GZ[cid] );
     
    694827    pthread_barrier_wait( &barrier );
    695828
    696     // Each thread[cid,lid] allocate and initialise in its private stack
     829    // Each thread[tid] allocates and initialises in its private stack
    697830    // a copy of the arrays of pointers on the distributed buffers.
    698     unsigned short * A[CLUSTERS_MAX];
     831    unsigned char * A[CLUSTERS_MAX];
    699832    int            * B[CLUSTERS_MAX];
    700833    int            * C[CLUSTERS_MAX];
     
    711844    }
    712845
    713     // Each thread[cid,0] access the file containing the input image, to load
    714     // the local A[cid] buffer. Other threads are waiting on the barrier.
    715     if ( lid==0 )
    716     {
    717         unsigned int size   = local_pixels * sizeof( unsigned short );
    718         unsigned int offset = size * cid;
    719 
    720         memcpy( A[cid],
    721                 image_in + offset,
    722                 size );
     846    unsigned int npixels  = image_np * lines_per_thread;     // pixels moved by any thread
     847    unsigned int g_offset = npixels * tid;             // offset in global buffer for tid
     848    unsigned int l_offset = npixels * lid;             // offset in local buffer for tid
     849
     850    // min and max line indexes handled by thread[tid] for a global buffer
     851    unsigned int global_lmin = tid * lines_per_thread;   
     852    unsigned int global_lmax = global_lmin + lines_per_thread; 
     853
     854    // min and max line indexes handled by thread[tid] for a local buffer
     855    unsigned int local_lmin  = lid * lines_per_thread;   
     856    unsigned int local_lmax  = local_lmin + lines_per_thread; 
     857
     858    // pmin and pmax pixel indexes handled by thread[tid] in a column
     859    unsigned int column_pmin = tid * pixels_per_thread; 
     860    unsigned int column_pmax = column_pmin + pixels_per_thread;
     861
     862    // Each thread[tid] copy npixels from image_in buffer to local A[cid] buffer
     863    memcpy( A[cid]   + l_offset,
     864            image_in + g_offset,
     865            npixels );
    723866 
    724867#if VERBOSE_EXEC
     
    728871#endif
    729872
    730     }
    731 
    732     // Optionnal parallel display of the initial image stored in A[c] buffers.
    733     // Eah thread[cid,lid] displays (NL/nthreads) lines.
    734 
     873    // Optionnal parallel display for the initial image
    735874    if ( INITIAL_DISPLAY_ENABLE )
    736875    {
    737         unsigned int line;
    738         unsigned int offset = lines_per_thread * lid;
    739 
    740         for ( l = 0 ; l < lines_per_thread ; l++ )
    741         {
    742             line = offset + l;
    743 
    744             // copy TA[cid] to TZ[cid]
    745             for ( p = 0 ; p < NP ; p++ )
    746             {
    747                 TZ(cid, line, p) = (unsigned char)(TA(cid, line, p) >> 8);
    748             }
    749 
    750             // display one line to frame buffer
    751             if (fbf_write( &TZ(cid, line, 0),                     // first pixel in TZ
    752                            NP,                                    // number of bytes
    753                            NP*(l + (tid * lines_per_thread))))    // offset in FBF
    754             {
    755                 printf("\n[convol error] in %s : thread[%d] cannot access FBF\n",
    756                 __FUNCTION__ , tid );
    757                 pthread_exit( &THREAD_EXIT_FAILURE );
    758             }
     876        // each thread[tid] copy npixels from A[cid] to in_win_buf buffer
     877        memcpy( in_win_buf + g_offset,
     878                A[cid]     + l_offset,
     879                npixels );
     880
     881        // refresh the FBF window
     882        if( fbf_refresh_window( in_wid , global_lmin , global_lmax ) )
     883        {
     884            printf("\n[convol error] in %s : thread[%d] cannot access FBF\n",
     885            __FUNCTION__ , tid );
     886            pthread_exit( &THREAD_EXIT_FAILURE );
    759887        }
    760888
     
    771899    ////////////////////////////////////////////////////////////
    772900    // parallel horizontal filter :
    773     // B <= convol(FH(A))
     901    // B <= Transpose(FH(A))
    774902    // D <= A - FH(A)
    775     // Each thread computes (NL/nthreads) lines.
     903    // Each thread computes (image_nl/nthreads) lines.
    776904    // The image must be extended :
    777905    // if (z<0)    TA(cid,l,z) == TA(cid,l,0)
    778     // if (z>NP-1) TA(cid,l,z) == TA(cid,l,NP-1)
     906    // if (z>image_np-1) TA(cid,l,z) == TA(cid,l,image_np-1)
    779907    ////////////////////////////////////////////////////////////
    780908
     
    782910    H_BEG[cid][lid] = (unsigned int)date;
    783911
    784     // l = absolute line index / p = absolute pixel index 
    785     // first & last define which lines are handled by a given thread
    786 
    787     first = tid * lines_per_thread;
    788     last  = first + lines_per_thread;
    789 
    790     for (l = first; l < last; l++)
     912    // l = global line index / p = absolute pixel index 
     913
     914    for (l = global_lmin; l < global_lmax; l++)
    791915    {
    792916        // src_c and src_l are the cluster index and the line index for A & D
     
    814938            TD(src_c, src_l, p) = (int) TA(src_c, src_l, p) - sum_p / hnorm;
    815939        }
    816         // second domain : from (hrange+1) to (NP-hrange-1)
    817         for (p = hrange + 1; p < NP - hrange; p++)
     940        // second domain : from (hrange+1) to (image_np-hrange-1)
     941        for (p = hrange + 1; p < image_np - hrange; p++)
    818942        {
    819943            // dst_c and dst_p are the cluster index and the pixel index for B
     
    825949            TD(src_c, src_l, p) = (int) TA(src_c, src_l, p) - sum_p / hnorm;
    826950        }
    827         // third domain : from (NP-hrange) to (NP-1)
    828         for (p = NP - hrange; p < NP; p++)
     951        // third domain : from (image_np-hrange) to (image_np-1)
     952        for (p = image_np - hrange; p < image_np; p++)
    829953        {
    830954            // dst_c and dst_p are the cluster index and the pixel index for B
    831955            int dst_c = p / pixels_per_cluster;
    832956            int dst_p = p % pixels_per_cluster;
    833             sum_p = sum_p + (int) TA(src_c, src_l, NP - 1)
     957            sum_p = sum_p + (int) TA(src_c, src_l, image_np - 1)
    834958                          - (int) TA(src_c, src_l, p - hrange - 1);
    835959            TB(dst_c, dst_p, l) = sum_p / hnorm;
     
    858982    ///////////////////////////////////////////////////////////////
    859983    // parallel vertical filter :
    860     // C <= transpose(FV(B))
    861     // Each thread computes (NP/nthreads) columns
     984    // C <= Transpose(FV(B))
     985    // Each thread computes (image_np/nthreads) columns
    862986    // The image must be extended :
    863987    // if (l<0)    TB(cid,p,l) == TB(cid,p,0)
    864     // if (l>NL-1)   TB(cid,p,l) == TB(cid,p,NL-1)
     988    // if (l>image_nl-1)   TB(cid,p,l) == TB(cid,p,image_nl-1)
    865989    ///////////////////////////////////////////////////////////////
    866990
     
    868992    V_BEG[cid][lid] = (unsigned int)date;
    869993
    870     // l = absolute line index / p = absolute pixel index
    871     // first & last define which pixels are handled by a given thread
    872 
    873     first = tid * pixels_per_thread;
    874     last  = first + pixels_per_thread;
    875 
    876     for (p = first; p < last; p++)
     994    // l = global line index / p = pixel index in column
     995
     996    for (p = column_pmin; p < column_pmax ; p++)
    877997    {
    878998        // src_c and src_p are the cluster index and the pixel index for B
     
    8831003
    8841004        // We use the specific values of the vertical ep-filter
    885         // To minimize the number of tests, the NL lines are split in three domains
     1005        // To minimize the number of tests, the image_nl lines are split in three domains
    8861006
    8871007        // first domain : explicit computation for the first 18 values
     
    8991019        }
    9001020        // second domain
    901         for (l = 18; l < NL - 17; l++)
     1021        for (l = 18; l < image_nl - 17; l++)
    9021022        {
    9031023            // dst_c and dst_l are the cluster index and the line index for C
     
    9191039        }
    9201040        // third domain
    921         for (l = NL - 17; l < NL; l++)
     1041        for (l = image_nl - 17; l < image_nl; l++)
    9221042        {
    9231043            // dst_c and dst_l are the cluster index and the line index for C
     
    9251045            int dst_l = l % lines_per_cluster;
    9261046
    927             sum_l = sum_l + TB(src_c, src_p, min(l + 4, NL - 1))
    928                   + TB(src_c, src_p, min(l + 8, NL - 1))
    929                   + TB(src_c, src_p, min(l + 11, NL - 1))
    930                   + TB(src_c, src_p, min(l + 15, NL - 1))
    931                   + TB(src_c, src_p, min(l + 17, NL - 1))
     1047            sum_l = sum_l + TB(src_c, src_p, min(l + 4, image_nl - 1))
     1048                  + TB(src_c, src_p, min(l + 8, image_nl - 1))
     1049                  + TB(src_c, src_p, min(l + 11, image_nl - 1))
     1050                  + TB(src_c, src_p, min(l + 15, image_nl - 1))
     1051                  + TB(src_c, src_p, min(l + 17, image_nl - 1))
    9321052                  - TB(src_c, src_p, l - 5)
    9331053                  - TB(src_c, src_p, l - 9)
     
    9581078    pthread_barrier_wait( &barrier );
    9591079
    960     // Optional parallel display of the final image Z <= D + C
    961     // Eah thread[x,y,p] displays (NL/nthreads) lines.
    962 
     1080    ///////////////////////////////////////////////////////////////
     1081    // build final image in local Z buffer from C & D local buffers
     1082    // store it in output image file, and display it on FBF.
     1083    // Z <= C + D
     1084    ///////////////////////////////////////////////////////////////
     1085
     1086    get_cycle( &date );
     1087    F_BEG[cid][lid] = (unsigned int)date;
     1088
     1089    // Each thread[tid] set local buffer Z[cid] from local buffers C[cid] & D[cid]
     1090
     1091    for( l = local_lmin ; l < local_lmax ; l++ )
     1092    {
     1093        for( p = 0 ; p < image_np ; p++ )
     1094        {
     1095            TZ(cid,l,p) = TC(cid,l,p) + TD(cid,l,p);
     1096        }
     1097    }
     1098
     1099    // Each thread[tid] copy npixels from Z[cid] buffer to image_out buffer
     1100    memcpy( image_out + g_offset,
     1101            Z[cid]    + l_offset,
     1102            npixels );
     1103
     1104    // Optional parallel display of the final image
    9631105    if ( FINAL_DISPLAY_ENABLE )
    9641106    {
    965         get_cycle( &date );
    966         D_BEG[cid][lid] = (unsigned int)date;
    967 
    968         unsigned int line;
    969         unsigned int offset = lines_per_thread * lid;
    970 
    971         for ( l = 0 ; l < lines_per_thread ; l++ )
    972         {
    973             line = offset + l;
    974 
    975             for ( p = 0 ; p < NP ; p++ )
    976             {
    977                 TZ(cid, line, p) =
    978                    (unsigned char)( (TD(cid, line, p) +
    979                                      TC(cid, line, p) ) >> 8 );
    980             }
    981 
    982             if (fbf_write( &TZ(cid, line, 0),                   // first pixel in TZ
    983                            NP,                                  // number of bytes
    984                            NP*(l + (tid * lines_per_thread))))  // offset in FBF
    985             {
    986                 printf("\n[convol error] thread[%d] cannot access FBF\n", tid );
    987                 pthread_exit( &THREAD_EXIT_FAILURE );
    988             }
    989         }
    990 
    991         get_cycle( &date );
    992         D_END[cid][lid] = (unsigned int)date;
    993 
    994 #if VERBOSE_EXEC
     1107        // each thread[tid] copy npixels from Z[cid] to out_win_buf buffer
     1108        memcpy( out_win_buf + g_offset,
     1109                Z[cid]      + l_offset,
     1110                npixels );
     1111
     1112        // refresh the FBF window
     1113        if( fbf_refresh_window( out_wid , global_lmin , global_lmax ) )
     1114        {
     1115            printf("\n[convol error] in %s : thread[%d] cannot access FBF\n",
     1116            __FUNCTION__ , tid );
     1117            pthread_exit( &THREAD_EXIT_FAILURE );
     1118        }
     1119
     1120#if VERBOSE_EXEC
    9951121get_cycle( &date );
    9961122printf( "\n[convol] exec[%d] on core[%x,%d] completed final display / cycle %d\n",
    997 tid , cxy , lid , (unsigned int)date );
     1123tid , cxy , lpid , (unsigned int)date );
    9981124#endif
    9991125
     
    10101136    }
    10111137
     1138    get_cycle( &date );
     1139    F_END[cid][lid] = (unsigned int)date;
     1140
    10121141    // thread termination depends on the placement policy
    10131142    if( PARALLEL_PLACEMENT )   
     
    10311160
    10321161} // end execute()
     1162
     1163
     1164
    10331165
    10341166
     
    10571189    unsigned int max_v_end = 0;
    10581190
    1059     unsigned int min_d_beg = 0xFFFFFFFF;
    1060     unsigned int max_d_beg = 0;
    1061 
    1062     unsigned int min_d_end = 0xFFFFFFFF;
    1063     unsigned int max_d_end = 0;
     1191    unsigned int min_f_beg = 0xFFFFFFFF;
     1192    unsigned int max_f_beg = 0;
     1193
     1194    unsigned int min_f_end = 0xFFFFFFFF;
     1195    unsigned int max_f_end = 0;
    10641196
    10651197    for (cc = 0; cc < nclusters; cc++)
     
    10821214            if (V_END[cc][pp] > max_v_end) max_v_end = V_END[cc][pp];
    10831215
    1084             if (D_BEG[cc][pp] < min_d_beg) min_d_beg = D_BEG[cc][pp];
    1085             if (D_BEG[cc][pp] > max_d_beg) max_d_beg = D_BEG[cc][pp];
    1086 
    1087             if (D_END[cc][pp] < min_d_end) min_d_end = D_END[cc][pp];
    1088             if (D_END[cc][pp] > max_d_end) max_d_end = D_END[cc][pp];
     1216            if (F_BEG[cc][pp] < min_f_beg) min_f_beg = F_BEG[cc][pp];
     1217            if (F_BEG[cc][pp] > max_f_beg) max_f_beg = F_BEG[cc][pp];
     1218
     1219            if (F_END[cc][pp] < min_f_end) min_f_end = F_END[cc][pp];
     1220            if (F_END[cc][pp] > max_f_end) max_f_end = F_END[cc][pp];
    10891221        }
    10901222    }
     
    11091241
    11101242    printf(" - D_BEG : min = %d / max = %d / med = %d / delta = %d\n",
    1111            min_d_beg, max_d_beg, (min_d_beg+max_d_beg)/2, max_d_beg-min_d_beg);
     1243           min_f_beg, max_f_beg, (min_f_beg+max_f_beg)/2, max_f_beg-min_f_beg);
    11121244
    11131245    printf(" - D_END : min = %d / max = %d / med = %d / delta = %d\n",
    1114            min_d_end, max_d_end, (min_d_end+max_d_end)/2, max_d_end-min_d_end);
     1246           min_f_end, max_f_end, (min_f_end+max_f_end)/2, max_f_end-min_f_end);
    11151247
    11161248    printf( "\n General Scenario   (Kcycles)\n" );
     
    11191251    printf( " - BARRIER HORI/VERT = %d\n", (min_v_beg - max_h_end)/1000 );
    11201252    printf( " - V_FILTER          = %d\n", (max_v_end - min_v_beg)/1000 );
    1121     printf( " - BARRIER VERT/DISP = %d\n", (min_d_beg - max_v_end)/1000 );
    1122     printf( " - DISPLAY           = %d\n", (max_d_end - min_d_beg)/1000 );
     1253    printf( " - BARRIER VERT/DISP = %d\n", (min_f_beg - max_v_end)/1000 );
     1254    printf( " - DISPLAY           = %d\n", (max_f_end - min_f_beg)/1000 );
    11231255    printf( " \nSEQUENCIAL = %d / PARALLEL = %d\n",
    11241256            SEQUENCIAL_TIME/1000, PARALLEL_TIME/1000 );
     
    11431275
    11441276    fprintf( f , " - D_BEG : min = %d / max = %d / med = %d / delta = %d\n",
    1145            min_d_beg, max_d_beg, (min_d_beg+max_d_beg)/2, max_d_beg-min_d_beg);
     1277           min_f_beg, max_f_beg, (min_f_beg+max_f_beg)/2, max_f_beg-min_f_beg);
    11461278
    11471279    fprintf( f , " - D_END : min = %d / max = %d / med = %d / delta = %d\n",
    1148            min_d_end, max_d_end, (min_d_end+max_d_end)/2, max_d_end-min_d_end);
     1280           min_f_end, max_f_end, (min_f_end+max_f_end)/2, max_f_end-min_f_end);
    11491281
    11501282    fprintf( f ,  "\n General Scenario (Kcycles)\n" );
     
    11531285    fprintf( f ,  " - BARRIER HORI/VERT = %d\n", (min_v_beg - max_h_end)/1000 );
    11541286    fprintf( f ,  " - V_FILTER          = %d\n", (max_v_end - min_v_beg)/1000 );
    1155     fprintf( f ,  " - BARRIER VERT/DISP = %d\n", (min_d_beg - max_v_end)/1000 );
    1156     fprintf( f ,  " - DISPLAY           = %d\n", (max_d_end - min_d_beg)/1000 );
     1287    fprintf( f ,  " - BARRIER VERT/DISP = %d\n", (min_f_beg - max_v_end)/1000 );
     1288    fprintf( f ,  " - SAVE              = %d\n", (max_f_end - min_f_beg)/1000 );
    11571289    fprintf( f ,  " \nSEQUENCIAL = %d / PARALLEL = %d\n",
    11581290    SEQUENCIAL_TIME/1000, PARALLEL_TIME/1000 );
  • trunk/user/display/display.c

    r657 r676  
    44//  author : Alain Greiner
    55///////////////////////////////////////////////////////////////////////////////////////
    6 //  This file describes the single thread "display" application.
    7 //  It uses the external chained buffer DMA to display a stream
    8 //  of images on the frame buffer. 
     6//  This file describes the single thread "display" application that simply
     7//  open a file containing a raw image stored on disk, ans distplay it on the
     8//  Frame Buffer without using the ALMOS-MKH windows manager.
    99///////////////////////////////////////////////////////////////////////////////////////
    1010
     
    1515#include <almosmkh.h>
    1616
    17 #define PATH_MAX_LENGHT    128
     17#define PATH_NAME   "misc/images_128.ram"
    1818
    1919#define FBF_TYPE    420         
     
    8080    }
    8181
     82    // check pixel encoding type
    8283    if( fbf_type != FBF_TYPE )
    8384    {
     
    8788    }
    8889
    89     // get pathname for input file
    90     while( 1 )
    91     {
    92         printf("\n[display] path = ");
    93 
    94         error = get_string( pathname , PATH_MAX_LENGHT );
    95 
    96         if ( error )  printf("\n[display error] cannot get path for input file\n" );
    97         else  break;
    98     }
    99 
    10090    // open file
    101     int fd = open( pathname , O_RDONLY , 0 );
    102 
     91    int fd = open( PATH_NAME , O_RDONLY , 0 );
    10392    if( fd < 0 )
    10493    {
  • trunk/user/init/init.c

    r659 r676  
    2020#include <shared_syscalls.h>
    2121
    22 #define DEBUG_PROCESS_INIT    1
     22#define DEBUG_PROCESS_INIT    0
    2323
    2424////////////////
     
    3636#endif
    3737
    38     // get Number of TXT channels from hard configuration
     38    // get number of TXT channels from hard configuration
    3939    hard_config_t config;     
     40
    4041    get_config( &config );
    4142
    4243    unsigned int  txt_channels = config.txt_channels;
    43     unsigned int  x_size       = config.x_size;
    44     unsigned int  y_size       = config.y_size;
    45     unsigned int  ncores       = config.ncores;
    4644
    4745    // check number of TXT channels
     
    4947    {
    5048        snprintf( string , 64 ,
    51         "\n[init ERROR] number of TXT channels must be larger than 1\n");
     49        "\n[init ERROR] number of TXT channels must be larger than 1");
    5250        display_string( string );
    5351        exit( EXIT_FAILURE );
     
    6462            // INIT display error message 
    6563            snprintf( string , 64 ,
    66             "[init ERROR] cannot fork child[%d] => suicide" , i );
     64            "\n[init ERROR] cannot fork child[%d] => suicide" , i );
    6765            display_string( string );
    6866
     
    7270        else if( ret_fork == 0 )                    // we are in CHILD[i] process
    7371        {
     72
     73#if DEBUG_PROCESS_INIT
     74            snprintf( string , 64 ,
     75            "\n[init] CHILD[%d] process forked / call execve", i );
     76            display_string( string );
     77#endif
    7478            // CHILD[i] process exec process KSH[i]
    7579            ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL );
     
    7983                // CHILD[i] display error message
    8084                snprintf( string , 64 ,
    81                 "[init ERROR] CHILD[%d] cannot exec KSH / ret_exec = %d" , i , ret_exec );
     85                "\n[init ERROR] CHILD[%d] cannot exec KSH" , i );
    8286                display_string( string );
    8387
     
    9094            // INIT display CHILD[i] process PID
    9195            snprintf( string , 64 ,
    92             "[init] (pid 0x1) created ksh[%d] (pid %x)", i , ret_fork );
     96            "\n[init] (pid 0x1) create ksh[%d] (pid %x)", i , ret_fork );
    9397            display_string( string );
    9498
     
    104108    unsigned int  cxy;           // cluster identifier
    105109    unsigned int  lid;           // core local index
     110
     111    unsigned int  x_size       = config.x_size;
     112    unsigned int  y_size       = config.y_size;
     113    unsigned int  ncores       = config.ncores;
    106114
    107115    // INIT displays processes and threads in all clusters
  • trunk/user/kleenex/kleenex.c

    r660 r676  
    1919#include <almosmkh.h>
    2020
    21 #define IN_FILENAME     "misc/philips_1024_2.raw"
    22 #define OUT_FILENAME    "misc/philips_1024.raw"
     21#define IN_FILENAME     "misc/philips_1024.raw"
     22#define OUT_FILENAME    "misc/philips_1024.new"
    2323#define FBF_TYPE        420         
    2424#define NPIXELS         1024
     
    3131    int                fd_out;
    3232
    33     unsigned int       fbf_width;
    34     unsigned int       fbf_height;
    35     unsigned int       fbf_type;
     33    int                fbf_width;
     34    int                fbf_height;
     35    int                fbf_type;
    3636
    3737    int                line;
     
    4242    unsigned long long start_cycle;
    4343
    44     unsigned short     buf_in[NPIXELS * NLINES * 2];
    45 //    unsigned char      buf_in[NPIXELS * NLINES];
     44    unsigned char      buf_in[NPIXELS * NLINES];
    4645    unsigned char      buf_out[NPIXELS * NLINES];
    4746
    4847    struct stat        st;
    4948
    50     int                size_in  = NPIXELS * NLINES * 2;
    51 //    int                size_in  = NPIXELS * NLINES;
     49    int                size_in  = NPIXELS * NLINES;
    5250    int                size_out = NPIXELS * NLINES;
    5351
     
    111109        {
    112110            unsigned int index = (line*NPIXELS) + pixel;
    113             buf_out[index] = (unsigned char)(buf_in[index]>>8);
    114 //            buf_out[index] = buf_in[index];
     111            buf_out[index] = buf_in[index];
    115112        }
    116113    }
  • trunk/user/ksh/ksh.c

    r659 r676  
    5858
    5959#define DEBUG_MAIN          0
    60 #define DEBUG_INTER         0
    6160#define DEBUG_EXECUTE       0
    6261#define DEBUG_CMD_CAT       0
     
    105104pthread_t       trdid;                      // interactive thread identifier
    106105
     106char            cmd[CMD_MAX_SIZE];                  // buffer for one command
     107
     108char            elf_path[PATH_MAX_SIZE];    // pathname for cmd_load command
     109
    107110char            pathname[PATH_MAX_SIZE];    // pathname for a file
    108111
    109 char            pathnew[PATH_MAX_SIZE];     // used by the rename command
     112char            pathnew[PATH_MAX_SIZE];     // used by the cmd_rename command
    110113 
    111 char            string[128];                // used by snprintf() for debug
     114char            string[256];                // used by snprintf() for debug
    112115
    113116//////////////////////////////////////////////////////////////////////////////////////////
     
    124127
    125128#if DEBUG_CMD_CAT
    126 snprintf( string , 128 , "[ksh] %s enters" , __FUNCTION__);
    127 display_string( string );
     129printf("\n[ksh] %s enters" , __FUNCTION__);
    128130#endif
    129131
     
    139141
    140142#if DEBUG_CMD_CAT
    141 snprintf( string , 128 , "[ksh] %s : after strcpy" , __FUNCTION__ );
    142 display_string( string );
     143printf("\n[ksh] %s : after strcpy" , __FUNCTION__ );
    143144#endif
    144145
     
    154155
    155156#if DEBUG_CMD_CAT
    156 snprintf( string , 128 , "[ksh] %s : file %s open", __FUNCTION__, pathname );
    157 display_string( string );
     157printf("\n[ksh] %s : file %s open", __FUNCTION__, pathname );
    158158#endif
    159159
     
    181181
    182182#if DEBUG_CMD_CAT
    183 snprintf( string , 128 , "[ksh] %s : size = %d",
    184 __FUNCTION__, size );
    185 display_string( string );
     183printf("\n[ksh] %s : size = %d", __FUNCTION__, size );
    186184#endif
    187185
     
    208206
    209207#if DEBUG_CMD_CAT
    210 snprintf( string , 128 , "[ksh] %s : mapped file %d to buffer %x",
    211 __FUNCTION__, fd , buf );
    212 display_string( string );
     208printf("\n[ksh] %s : mapped file %d to buffer %x", __FUNCTION__, fd , buf );
    213209#endif
    214210
     
    223219
    224220#if DEBUG_CMD_CAT
    225 snprintf( string , 128 , "[ksh] %s : unmapped file %d from buffer %x",
    226 __FUNCTION__, fd , buf );
    227 display_string( string );
     221printf("\n[ksh] %s : unmapped file %d from buffer %x", __FUNCTION__, fd , buf );
    228222#endif
    229223
     
    273267
    274268#if DEBUG_CMD_CP
    275 snprintf( string , 128 , "[ksh] enter %s" , __FUNCTION__);
    276 display_string( string );
     269printf("\n[ksh] enter %s" , __FUNCTION__);
    277270#endif
    278271
     
    297290
    298291#if DEBUG_CMD_CP
    299 snprintf( string , 128 , "[ksh] %s : file %s open", __FUNCTION__, argv[1] );
    300 display_string( string );
     292printf("\n[ksh] %s : file %s open", __FUNCTION__, argv[1] );
    301293#endif
    302294
     
    310302
    311303#if DEBUG_CMD_CP
    312 snprintf( string , 128 , "[ksh] %s : got stats for %s", __FUNCTION__, argv[1] );
    313 display_string( string );
     304printf("\n[ksh] %s : got stats for %s", __FUNCTION__, argv[1] );
    314305#endif
    315306
     
    335326
    336327#if DEBUG_CMD_CP
    337 snprintf( string , 128 , "[ksh] %s : file %s open", __FUNCTION__, argv[2] );
    338 display_string( string );
     328printf("\n[ksh] %s : file %s open", __FUNCTION__, argv[2] );
    339329#endif
    340330
     
    346336
    347337#if DEBUG_CMD_CP
    348 snprintf( string , 128 , "[ksh] %s : got stats for %s", __FUNCTION__, argv[2] );
    349 display_string( string );
     338printf("\n[ksh] %s : got stats for %s", __FUNCTION__, argv[2] );
    350339#endif
    351340
     
    370359
    371360#if DEBUG_CMD_CP
    372 snprintf( string , 128 , "[ksh] %s : read %d bytes from %s", __FUNCTION__, len, argv[1] );
    373 display_string( string );
     361printf("\n[ksh] %s : read %d bytes from %s", __FUNCTION__, len, argv[1] );
    374362#endif
    375363
     
    382370
    383371#if DEBUG_CMD_CP
    384 snprintf( string , 128 , "[ksh] %s : write %d bytes to %s", __FUNCTION__, len, argv[2] );
    385 display_string( string );
     372printf("\n[ksh] %s : write %d bytes to %s", __FUNCTION__, len, argv[2] );
    386373#endif
    387374
     
    416403               "         display  fat      min      nslots\n"
    417404               "         display  fat      cxy      0\n"
    418                "         display  socket   pid      fdid\n" );
     405               "         display  socket   pid      fdid\n"
     406               "         display  fd       pid\n"
     407               "         display  fbf      pid\n" );
    419408    }
    420409    ////////////////////////////////////
     
    578567        }
    579568    }
    580     ///////////////////////////////////////////
     569    /////////////////////////////////////////
    581570    else if( strcmp( argv[1] , "fat" ) == 0 )
    582571    {
     
    596585        }
    597586    }
    598     ///////////////////////////////////////////
     587    ////////////////////////////////////////////
    599588    else if( strcmp( argv[1] , "socket" ) == 0 )
    600589    {
     
    614603        }
    615604    }
     605    ////////////////////////////////////////
     606    else if( strcmp( argv[1] , "fd" ) == 0 )
     607    {
     608        if( argc != 3 )
     609        {
     610                    printf("  usage: display fd pid\n");
     611            }
     612        else
     613        {
     614            unsigned int pid = atoi(argv[2]);
     615           
     616            if( display_fd_array( pid ) )
     617            {
     618                printf("  error: cannot found process %x\n", pid );
     619            }
     620        }
     621    }
     622    ////////////////////////////////////////
     623    else if( strcmp( argv[1] , "fbf" ) == 0 )
     624    {
     625        if( argc != 3 )
     626        {
     627                    printf("  usage: display fbf pid\n"
     628                   "         display fbf 0 (all processes)");
     629            }
     630        else
     631        {
     632            unsigned int pid = atoi(argv[2]);
     633           
     634            if( display_fbf_windows( pid ) )
     635            {
     636                printf("  error: cannot found process %x\n", pid );
     637            }
     638        }
     639    }
    616640    ////
    617641    else
     
    632656        if (argc != 2)
    633657    {
    634                 printf("  usage: %s pid\n", argv[0]);
     658                printf("  usage: fg pid\n");
    635659        }
    636660    else
     
    658682        unsigned int i;
    659683
    660         if (argc != 1)
    661     {
    662                 printf("  usage: %s\n", argv[0]);
     684        if( (argc != 1) || (argv[0] == NULL) )
     685    {
     686                printf("  usage: help\n");
    663687        }
    664688    else
     
    676700}   // end cmd_help()
    677701
     702/////////////////////////////////////////////////
     703static void cmd_history( int argc , char **argv )
     704{
     705        unsigned int i;
     706
     707        if( (argc != 1) || (argv[0] == NULL) )
     708    {
     709                printf("  usage: history\n");
     710        }
     711    else
     712    {
     713            printf("--- registered commands ---\n");
     714            for (i = 0; i < LOG_DEPTH; i++)
     715        {
     716                    printf(" - %d\t: %s\n", i, &log_entries[i].buf);
     717            }
     718    }
     719
     720    // release semaphore to get next command
     721    sem_post( &semaphore );
     722
     723} // end cmd_history()
     724
    678725//////////////////////////////////////////////
    679726static void cmd_kill( int argc , char **argv )
     
    683730        if (argc != 2)
    684731    {
    685                 printf("  usage: %s pid\n", argv[0]);
     732                printf("  usage: kill pid\n", argv[0]);
    686733        }
    687734    else
     
    710757        int                  ret_fork;           // return value from fork
    711758        int                  ret_exec;           // return value from exec
    712     unsigned int         ksh_pid;            // KSH process PID
     759    unsigned int         cmd_ok;             // command arguments acceptable
    713760    unsigned int         background;         // background execution if non zero
    714     unsigned int         placement;          // placement specified if non zero
    715     unsigned int         cxy;                // target cluster if placement specified
     761    unsigned int         place;              // user placement if non zero
     762    char               * arg[5];             // array of pointers on main thread arguments
     763    unsigned int         args_nr;            // number of aruments in this array
     764
     765    char                 undef[] = { "undefined" };
     766
     767    // arguments analysis of argv[] array that contains at most 8 strings:
     768    // - the two first arguments ("cmd_type" & "elf_path") are mandatory
     769    // - the six next ("-pcxy","arg0","arg1","arg2","arg3","&") are optional
     770
     771     // analyse the optional arguments
     772    if( argc == 2 )                            // no optional arguments
     773    {
     774        cmd_ok     = 1;
     775        background = 0;
     776        place      = 0;
     777        arg[0]     = undef;
     778        arg[1]     = undef;
     779        arg[2]     = undef;
     780        arg[3]     = undef;
     781        args_nr    = 0;
     782    }
     783    else if( ((argc >= 4) && (argc <= 8)) &&
     784             (argv[2][0] == '-') && (argv[2][1] == 'p') &&           
     785             (strcmp(argv[argc-1] , "&" ) == 0) )     // background, place, 0 to 4 args
     786    {
     787        cmd_ok     = 1;
     788        background = 1;
     789        place      = 0xFF000000 | atoi( argv[2] + 2 );
     790        arg[0]     = (argc > 4) ? argv[3] : undef;
     791        arg[1]     = (argc > 5) ? argv[4] : undef;
     792        arg[2]     = (argc > 6) ? argv[5] : undef;
     793        arg[3]     = (argc > 7) ? argv[6] : undef;
     794        args_nr    = argc - 4;
     795    }
     796    else if( ((argc >= 3) && (argc <= 7)) &&
     797             (argv[2][0] == '-') && (argv[2][1] == 'p') &&
     798             (strcmp(argv[argc-1] , "&" ) != 0) )     // place, no background, 0 to 4 args
     799    {
     800        cmd_ok     = 1;
     801        background = 0;
     802        place      = 0xFF000000 | atoi( argv[2] + 2 );
     803        arg[0]     = (argc > 3) ? argv[3] : undef;
     804        arg[1]     = (argc > 4) ? argv[4] : undef;
     805        arg[2]     = (argc > 5) ? argv[5] : undef;
     806        arg[3]     = (argc > 6) ? argv[6] : undef;
     807        args_nr    = argc - 3;
     808    }
     809    else if( ((argc >=3) && (argc <= 7))  &&       
     810             ((argv[2][0] != '-') || (argv[2][1] != 'p')) &&
     811             (strcmp(argv[argc-1] , "&" ) == 0) )     // no place, background, 0 to 4 args
     812    {
     813        cmd_ok     = 1;
     814        background = 1;
     815        place      = 0;
     816        arg[0]     = (argc > 3) ? argv[2] : undef;
     817        arg[1]     = (argc > 4) ? argv[3] : undef;
     818        arg[2]     = (argc > 5) ? argv[4] : undef;
     819        arg[3]     = (argc > 6) ? argv[5] : undef;
     820        args_nr    = argc - 3;
     821    }
     822    else if( (argc >= 3) && (argc <= 6 ) )     // no place, no background, 0 to 4 args
     823    {
     824        cmd_ok     = 1;
     825        background = 0;
     826        place      = 0;
     827        arg[0]     = (argc > 2) ? argv[2] : undef;
     828        arg[1]     = (argc > 3) ? argv[3] : undef;
     829        arg[2]     = (argc > 4) ? argv[4] : undef;
     830        arg[3]     = (argc > 5) ? argv[5] : undef;
     831        args_nr    = argc - 2;
     832    }
     833    else                                    // illegal optional arguments
     834    {
     835        cmd_ok = 0;
     836        background = 0;
     837        place      = 0;
     838        arg[0]     = undef;
     839        arg[1]     = undef;
     840        arg[2]     = undef;
     841        arg[3]     = undef;
     842        args_nr    = 0;
     843    }
     844
     845    // check syntax errors
     846    if( cmd_ok == 0 )
     847    {
     848        printf("  usage: load elf_path [-pcxy] [arg0] [arg1] [arg2] [arg3] [&]\n");
     849
     850        // release semaphore to get next command
     851        sem_post( &semaphore );
     852
     853        return;
     854    }
     855
     856    // get elf_path
     857    strcpy( elf_path , argv[1] );
    716858
    717859#if DEBUG_CMD_LOAD
    718 snprintf( string , 128 , "[ksh] enter %s" , __FUNCTION__);
    719 display_string( string );
    720 #endif
    721 
    722         if( (argc < 2) || (argc > 4) ) 
    723     {
    724                 printf("  usage: %s pathname [cxy] [&]\n", argv[0] );
    725         }
    726     else
    727     {
    728             strcpy( pathname , argv[1] );
    729 
    730         if( argc == 2 )
    731         {
    732             background = 0;
    733             placement  = 0;
    734             cxy        = 0;
    735         }
    736         else if( argc == 3 )
    737         {
    738             if( (argv[2][0] == '&') && (argv[2][1] == 0) )
    739             {
    740                 background = 1;
    741                 placement  = 0;
    742                 cxy        = 0;
    743             }
    744             else
    745             {
    746                 background = 0;
    747                 placement  = 1;
    748                 cxy        = atoi( argv[2] );
    749             }
    750         }
    751         else  // argc == 4
    752         {
    753             background = ( (argv[3][0] == '&') && (argv[3][1] == 0) );
    754             placement  = 1;
    755             cxy        = atoi( argv[2] );
    756         }
    757 
    758         // get KSH process PID
    759         ksh_pid = getpid();
     860printf("\n[ksh] %s : path <%s> / place %x / args_nr %d / bg %d\n"
     861"                 arg0 %s / arg1 %s / arg2 %s / arg3 %s\n",
     862__FUNCTION__, elf_path , place , args_nr , background , arg[0] , arg[1] , arg[2] , arg[3] );
     863#endif
     864
     865    // set NULL pointers in args[] array
     866    if     ( arg[0] == undef ) arg[0] = NULL;
     867    else if( arg[1] == undef ) arg[1] = NULL;
     868    else if( arg[2] == undef ) arg[2] = NULL;
     869    else if( arg[3] == undef ) arg[3] = NULL;
     870    else                       arg[4] = NULL;
     871
     872    // set target cluster if required
     873    if( place ) place_fork( place & 0xFFFF );
     874
     875    // KSH process fork CHILD process
     876        ret_fork = fork();
     877
     878    if ( ret_fork < 0 )     // it is a failure reported to KSH
     879    {
     880        printf("  error: ksh process unable to fork\n");
     881    }
     882    else if (ret_fork == 0) // it is the CHILD process
     883    {
    760884
    761885#if DEBUG_CMD_LOAD
    762 snprintf( string , 128 , "[ksh] %s : <%s> / bg %d / place %d / cxy %x",
    763 __FUNCTION__, argv[1], background, placement, cxy );
    764 display_string( string );
    765 #endif
    766 
    767         // set target cluster if required
    768         if( placement ) place_fork( cxy );
    769 
    770         // KSH process fork CHILD process
    771             ret_fork = fork();
    772 
    773         if ( ret_fork < 0 )     // it is a failure reported to KSH
    774         {
    775             printf("  error: ksh process unable to fork\n");
    776         }
    777         else if (ret_fork == 0) // it is the CHILD process
    778         {
     886printf("\n[ksh] %s : child (pid %x) after fork, before exec\n",
     887 __FUNCTION__ , getpid() );
     888#endif
     889
     890        // CHILD process exec NEW process
     891        ret_exec = execve( elf_path , arg , NULL );
    779892
    780893#if DEBUG_CMD_LOAD
    781 snprintf( string , 128 , "[ksh] %s : child (%x) after fork, before exec",
    782 __FUNCTION__ , getpid() );
    783 display_string( string );
    784 #endif
    785 
    786             // CHILD process exec NEW process
    787             ret_exec = execve( pathname , NULL , NULL );
     894printf("\n[ksh] %s : child (pid %x) after exec / ret_exec %x\n",
     895 __FUNCTION__, getpid(), ret_exec );
     896#endif
     897
     898        // this is only executed in case of exec failure
     899        if( ret_exec )
     900        {
     901            printf("  error: child process unable to exec <%s>\n", pathname );
     902            exit( 0 );
     903        }   
     904        }
     905    else                    // it is the KSH process : ret_fork is the new process PID
     906    {
    788907
    789908#if DEBUG_CMD_LOAD
    790 snprintf( string , 128 , "[ksh] %s : child (%x) after exec / ret_exec %x",
    791 __FUNCTION__ , getpid(), ret_exec );
    792 display_string( string );
    793 #endif
    794 
    795             // this is only executed in case of exec failure
    796             if( ret_exec )
    797             {
    798                 printf("  error: child process unable to exec <%s>\n", pathname );
    799                 exit( 0 );
    800             }   
    801             }
    802         else                    // it is the KSH process : ret_fork is the new process PID
    803         {
    804 
    805 #if DEBUG_CMD_LOAD
    806 snprintf( string , 128 , "[ksh] %s : ksh (%x) after fork / ret_fork %x",
    807 __FUNCTION__, getpid(), ret_fork );
    808 display_string( string );
    809 #endif
    810             // when the new process is launched in background, the KSH process
    811             // takes the TXT ownership, and release the semaphore to get the next command.
    812             // Otherwise, the child process keep the TXT ownership, and the semaphore will
    813             // be released by the KSH main thread when the child process exit
    814 
    815             if( background )    //  KSH must keep TXT ownership
    816             {
    817                 // get back the TXT ownership
    818                 fg( ksh_pid );
    819 
    820                 // release semaphore to get next command
    821                 sem_post( &semaphore );
    822             }
     909printf("\n[ksh] %s : ksh (pid %x) after fork / ret_fork %x\n",
     910 __FUNCTION__, getpid(), ret_fork );
     911#endif
     912        // when the new process is launched in background, the KSH process
     913        // takes the TXT ownership, and releases the semaphore to get the next command.
     914        // Otherwise, the child process keep the TXT ownership, and the semaphore will
     915        // be released by the KSH main thread when the child process exit
     916
     917        if( background )    //  KSH must keep TXT ownership
     918        {
     919            // KSH get back the TXT ownership
     920            fg( getpid() );
     921
     922            // release semaphore to get next command
     923            sem_post( &semaphore );
    823924        }
    824925    }
    825926}   // end cmd_load
    826 
    827 /////////////////////////////////////////////
    828 static void cmd_log( int argc , char **argv )
    829 {
    830         unsigned int i;
    831 
    832         if (argc != 1)
    833     {
    834                 printf("  usage: %s\n", argv[0], argc );
    835         }
    836     else
    837     {
    838             printf("--- registered commands ---\n");
    839             for (i = 0; i < LOG_DEPTH; i++)
    840         {
    841                     printf(" - %d\t: %s\n", i, &log_entries[i].buf);
    842             }
    843     }
    844 
    845     // release semaphore to get next command
    846     sem_post( &semaphore );
    847 
    848 } // end cmd_log()
    849 
    850927
    851928////////////////////////////////////////////
     
    856933
    857934#if DEBUG_CMD_LS
    858 snprintf( string , 128 , "[ksh] enter %s" , __FUNCTION__);
    859 display_string( string );
     935printf("\n[ksh] enter %s" , __FUNCTION__);
    860936#endif
    861937
     
    876952
    877953#if DEBUG_CMD_LS
    878 snprintf( string , 128 , "[ksh] %s : directory <%s> open / DIR %x",
    879 __FUNCTION__, pathname , dir );
    880 display_string( string );
     954printf("\n[ksh] %s : directory <%s> open / DIR %x", __FUNCTION__, pathname , dir );
    881955#endif
    882956
     
    899973
    900974#if DEBUG_CMD_LS
    901 snprintf( string , 128 , "[ksh] %s : directory <%s> closed",
    902 __FUNCTION__, pathname );
    903 display_string( string );
     975printf("\n[ksh] %s : directory <%s> closed", __FUNCTION__, pathname );
    904976#endif
    905977
     
    9641036
    9651037#if DEBUG_CMD_PS
    966 snprintf( string , 128 , "[ksh] enter %s" , __FUNCTION__);
    967 display_string( string );
    968 #endif
    969 
    970         if (argc != 1)
    971     {
    972                 printf("  usage: %s\n", argv[0]);
     1038printf("\n[ksh] enter %s" , __FUNCTION__);
     1039#endif
     1040
     1041        if( (argc != 1) || (argv[0] == NULL) )
     1042    {
     1043                printf("  usage: ps\n");
    9731044        }
    9741045    else
     
    9871058
    9881059#if DEBUG_CMD_PS
    989 snprintf( string , 128 , "\n[ksh] %s : call display_cluster_process()", __FUNCTION__ );
    990 display_string( string );
     1060printf("\n[ksh] %s : call display_cluster_process()", __FUNCTION__ );
    9911061#endif
    9921062
     
    10051075static void cmd_pwd( int argc , char **argv )
    10061076{
    1007         if (argc != 1)
    1008     {
    1009                 printf("  usage: %s\n", argv[0]);
     1077        if( (argc != 1) || (argv[0] == NULL) )
     1078    {
     1079                printf("  usage: pwd\n");
    10101080        }
    10111081    else
     
    10311101        if (argc != 2)
    10321102    {
    1033                 printf("  usage: %s pathname\n", argv[0]);
     1103                printf("  usage: rm pathname\n");
    10341104        }
    10351105    else
     
    10561126        if (argc != 2)
    10571127    {
    1058                 printf("  usage: %s pathname\n", argv[0]);
     1128                printf("  usage: stat pathname\n");
    10591129        }
    10601130    else
     
    10841154static void cmd_rmdir( int argc , char **argv )
    10851155{
    1086     // same as cmd_rm()
    1087         cmd_rm (argc , argv );
    1088 }
     1156        if (argc != 2)
     1157    {
     1158                printf("  usage: rmdir pathname\n");
     1159        }
     1160    else
     1161    {
     1162            strcpy( pathname , argv[1] );
     1163
     1164        if ( unlink( pathname ) )
     1165        {
     1166                    printf("  error: unable to remove <%s>\n", pathname );
     1167            }
     1168    }
     1169
     1170    // release semaphore to get next command
     1171    sem_post( &semaphore );
     1172
     1173}  // end cmd_rmdir()
    10891174
    10901175///////////////////////////////////////////////
     
    11531238        { "load",    "load an user application",                        cmd_load    },
    11541239        { "help",    "list available commands",                         cmd_help    },
     1240        { "history", "list registered commands",                        cmd_history },
    11551241        { "kill",    "kill a process (all threads)",                    cmd_kill    },
    1156         { "log",     "list registered commands",                        cmd_log     },
    11571242        { "ls",      "list directory entries",                          cmd_ls      },
    11581243        { "mkdir",   "create a new directory",                          cmd_mkdir   },
     
    11791264
    11801265#if DEBUG_EXECUTE
    1181 snprintf( string , 128 , "[ksh] enter %s for command <%s>" , __FUNCTION__ , buf );
    1182 display_string( string );
    1183 #endif
    1184 
    1185         // build argc/argv
     1266printf("\n[ksh] enter %s for command <%s>" , __FUNCTION__ , buf );
     1267#endif
     1268
     1269        // build argc/argv :
     1270    // arg[0] is the command type
     1271    // - other arg[i] are the command arguments
     1272
    11861273        for (i = 0; i < len; i++)
    11871274    {
     
    12091296
    12101297#if DEBUG_EXECUTE
    1211 snprintf( string , 128 , "\n[ksh] in %s : argc = %d / arg0 = %s / arg1 = %s",
    1212 __FUNCTION__ , argc , argv[0], argv[1] );
     1298printf("\n[ksh] in %s : argc = %d / type = %s", __FUNCTION__ , argc , argv[0] );
    12131299#endif
    12141300
     
    12451331        unsigned int   state;                   // escape sequence state
    12461332
    1247         char           cmd[CMD_MAX_SIZE];               // buffer for one command
    12481333
    12491334/*  direct command to help debug
    12501335
    1251     int pid = getpid();
    1252 
    1253     if( pid == 3 )
     1336    if( getpid() == 2 )
    12541337    {
    12551338        if( sem_wait( &semaphore ) )
     
    12601343        else
    12611344        {
    1262             strcpy( cmd , "load bin/user/tcp_server.elf" );
     1345            strcpy( cmd , "load bin/user/chat.elf 0" );
    12631346            printf("[ksh] %s\n", cmd );
    12641347            execute( cmd );
    12651348        }
    12661349    }
    1267     else if( getpid() == 4 )
     1350
     1351    else if( getpid() == 3 )
    12681352    {
    12691353        if( sem_wait( &semaphore ) )
     
    12741358        else
    12751359        {
    1276             strcpy( cmd , "load bin/user/tcp_client.elf" );
     1360            strcpy( cmd , "load bin/user/chat.elf 1" );
    12771361            printf("[ksh] %s\n", cmd );
    12781362            execute( cmd );
     
    12811365
    12821366*/
    1283 
    12841367
    12851368        enum fsm_states
     
    13181401            state       = NORMAL;
    13191402        end_command = 0;
    1320 
    1321 #if DEBUG_INTER
    1322 snprintf( string , 128 , "[ksh] %s : request a new command", __FUNCTION__ );
    1323 display_string( string );
    1324 #endif
    13251403
    13261404        // internal loop on characters in one command
     
    13491427                                            cmd[count] = 0;
    13501428                        count++;
    1351 #if DEBUG_INTER
    1352 snprintf( string , 128 , "[ksh] %s : get command <%s>", __FUNCTION__, cmd );
    1353 display_string( string );
    1354 #endif
     1429
    13551430                        // register command in log_entries[] array
    13561431                                            strncpy( log_entries[ptw].buf , cmd , count );
     
    13591434                                            ptr = ptw;
    13601435
    1361 #if DEBUG_INTER
    1362 snprintf( string , 128 , "[ksh] %s : execute <%s>", __FUNCTION__, cmd );
    1363 display_string( string );
    1364 #endif
    13651436                        // echo character
    13661437                        putchar( c );
     
    14791550                }  // end internal while loop on characters
    14801551
    1481 #if DEBUG_INTER
    1482 snprintf( string , 128 , "\n[ksh] %s : complete <%s> command", __FUNCTION__, cmd );
    1483 display_string( string );
    1484 #endif
    1485 
    14861552        // block interactive thread if KSH loose TXT ownership
    14871553        if ( sem_wait( &semaphore ) )
     
    15181584
    15191585#if DEBUG_MAIN
    1520 snprintf( string , 128 , "\n[ksh] main thread started on core[%x,%d]", cxy , lid );
    1521 display_string( string );
     1586printf("\n[ksh] main started on core[%x,%d]", cxy , lid );
    15221587#endif
    15231588   
     
    15301595
    15311596#if DEBUG_MAIN
    1532 snprintf( string , 128 , "\n[ksh] main initialized semaphore" );
    1533 display_string( string );
     1597printf("\n[ksh] main initialized semaphore" );
    15341598#endif
    15351599   
     
    15441608                    NULL );
    15451609#if DEBUG_MAIN
    1546 snprintf( string , 128 , "\n[ksh] main thread launched interactive thread %x", trdid );
    1547 display_string( string );
     1610printf("\n[ksh] main thread launched interactive thread %x", trdid );
    15481611#endif
    15491612
  • trunk/user/sort/sort.c

    r659 r676  
    5454#include <hal_macros.h>
    5555
    56 #define ARRAY_LENGTH        2048            // number of items
     56#define ARRAY_LENGTH        64              // number of items
    5757#define MAX_THREADS         1024            // 16 * 16 * 4
    5858
  • trunk/user/tcp_client/tcp_client.c

    r660 r676  
    3939        size = get_string( buffer , BUF_SIZE );
    4040
    41 /*
    42         strcpy( buffer , "blip\n" );
    43         printf("%s", buffer );
    44         size = 5;
    45 
    46         if( send( fdid , buffer , size , 0 ) != size )
    47         {
    48             printf("\n[server] chat error : cannotl sent message\n");
    49             return;
    50         }
    51 */
    52 
    5341        // exit chat function when local message is the "exit" string
    5442        if( strncmp( "exit" , buffer , 4 ) == 0 )
    5543        {
     44            printf("\n[tcp_client stop] local message is <exit> => return to main\n" );
    5645            return;
    5746        }
     
    6251        if( nbytes != size )
    6352        {
    64             printf("\n[server] send error => chat return to main\n");
     53            printf("\n[tcp_client error] cannot send => return to main\n");
    6554            return;
    6655        }
     
    7059
    7160        // receive server message
    72         nbytes =recv ( fdid, buffer, BUF_SIZE, 0 );
     61        nbytes = recv( fdid , buffer , BUF_SIZE , 0 );
    7362
    7463        if( nbytes < 0 )
    7564        {
    76             printf("\n\n[server] receive error => return to main\n" );
     65            printf("\n\n[tcp_client error] cannot receive => return to main\n" );
    7766            return;
    7867        }
    7968        else if( nbytes == 0 )
    8069        {
    81             printf("\n\n[server] receive EOF => return to main\n" );
     70            printf("\n\n[tcp_client stop] receive EOF => return to main\n" );
    8271            return;
    8372        }
     
    10594    // get  start cycle
    10695    get_cycle( &start_cycle );
    107     printf("\n[client] starts at cycle %d\n", (unsigned int)start_cycle );
     96    printf("\n[tcp_client] starts at cycle %d\n", (unsigned int)start_cycle );
    10897
    10998    pid = getpid();
     
    116105    if( fdid < 0 )
    117106    {
    118         printf("\n[client error] cannot create socket\n");
     107        printf("\n[tcp_client error] cannot create socket\n");
    119108        exit( 0 );
    120109    }
    121110    else
    122111    {
    123         printf("\n[client] created socket[%x,%d]\n", pid, fdid );
     112        printf("\n[tcp_client] created socket[%x,%d]\n", pid, fdid );
    124113    }
    125114
     
    134123    if( error )
    135124    {
    136         printf("\n[client error] bind failure on socketi[%x,%d]\n", pid, fdid );
     125        printf("\n[tcp_client error] bind failure on socketi[%x,%d]\n", pid, fdid );
    137126        exit( 0 );
    138127    }
    139128
    140     printf("\n[client] socket[%x,%d] bound : [%x,%x]\n",
     129    printf("\n[tcp_client] socket[%x,%d] bound : [%x,%x]\n",
    141130    pid, fdid, client_sin.sin_addr, (unsigned int)client_sin.sin_port );
    142131
     
    151140    if( error )
    152141    {
    153         printf("\n[client error] connect failure on socket[%x,%d]\n", pid, fdid );
     142        printf("\n[tcp_client error] connect failure on socket[%x,%d]\n", pid, fdid );
    154143        exit( 0 );
    155144    }
    156145
    157     printf("\n[client] socket[%x,%d] connected to server [%x,%x]\n",
     146    printf("\n[tcp_client] socket[%x,%d] connected to server [%x,%x]\n",
    158147    pid, fdid, server_sin.sin_addr, server_sin.sin_port );
    159148
     
    164153    close( fdid );
    165154
    166     printf("\n[client] closed socket[%x,%d]\n", pid, fdid );
     155    printf("\n[tcp_client] closed socket[%x,%d]\n", pid, fdid );
    167156
    168157    exit(0);
  • trunk/user/tcp_server/tcp_server.c

    r668 r676  
    4040        if( nbytes < 0 )
    4141        {
    42             printf("\n\n[server] receive error => return to main\n" );
     42            printf("\n\n[tcp_server error] cannot receive => return to main\n" );
    4343            return;
    4444        }
    4545        else if( nbytes == 0 )
    4646        {
    47             printf("\n\n[server] receive EOF => return to main\n" );
     47            printf("\n\n[tcp_server stop] received EOF => return to main\n" );
    4848            return;
    4949        }
     
    6161        if( strncmp( "exit" , buffer , 4 ) == 0 )
    6262        {
     63            printf("\n[tcp_server stop] local message is <exit> => return to main\n" );
    6364            return;
    6465        }
     
    6970        if( nbytes != size )
    7071        {
    71             printf("\n[server] send error => chat return to main\n" );
     72            printf("\n[tcp_server error] cannot send => return to main\n" );
    7273            return;
    7374        }
     
    9798    pid = getpid();
    9899
    99     printf("\n[server] starts at cycle %d\n",
     100    printf("\n[tcp_server] starts at cycle %d\n",
    100101    (unsigned int)start_cycle );
    101102
     
    107108    if( listen_fdid < 0 )
    108109    {
    109         printf("\n[server error] cannot create socket\n");
     110        printf("\n[tcp_server error] cannot create socket\n");
    110111        exit( 0 );
    111112    }
    112113    else
    113114    {
    114         printf("\n[server] created socket[%x,%d]\n", pid, listen_fdid );
     115        printf("\n[tcp_server] created socket[%x,%d]\n", pid, listen_fdid );
    115116    }
    116117
     
    125126    if( error )
    126127    {
    127         printf("\n[server error] bind failure for socket[%x,%d]\n",
     128        printf("\n[tcp_server error] bind failure for socket[%x,%d]\n",
    128129        pid, listen_fdid );
    129130        exit( 0 );
    130131    }
    131132
    132     printf("\n[server] listening socket[%x,%d] bound : [%x,%x]\n",
     133    printf("\n[tcp_server] listening socket[%x,%d] bound : [%x,%x]\n",
    133134    pid, listen_fdid, server_sin.sin_addr, server_sin.sin_port );
    134135
     
    138139    if( error )
    139140    {
    140         printf("\n[server error] listen failure for socket[%x,%d]\n",
     141        printf("\n[tcp_server error] listen failure for socket[%x,%d]\n",
    141142        pid, listen_fdid );
    142143        exit( 0 );
    143144    }
    144145
    145     printf("\n[server] listening socket[%x,%d] waiting connection request\n",
     146    printf("\n[tcp_server] listening socket[%x,%d] waiting connection request\n",
    146147    pid, listen_fdid );
    147148   
     
    153154    if( chat_fdid < 0 )
    154155    {
    155         printf("\n[server error] accept failure on socket[%x,%d]\n",
     156        printf("\n[tcp_server error] accept failure on socket[%x,%d]\n",
    156157        pid, listen_fdid );
    157158        exit( 0 );
    158159    }
    159160
    160     printf("\n[server] chat socket[%x,%d] accepted client [%x,%x]\n",
     161    printf("\n[tcp_server] chat socket[%x,%d] accepted client [%x,%x]\n",
    161162    pid, chat_fdid , client_sin.sin_addr , client_sin.sin_port );
    162163
     
    168169    close( listen_fdid );
    169170
    170     printf("\n[server] closed both <listen> & <chat> sockets\n" );
     171    printf("\n[tcp_server] closed both <listen> & <chat> sockets\n" );
    171172
    172173    exit(0);
  • trunk/user/transpose/transpose.c

    r659 r676  
    5656// local buf_in[cid] buffer, and write pixels to all remote // buf_out[cid] buffers.
    5757//
    58 // - The image  must fit the frame buffer size, that must be power of 2.
     58// - The image must have [nlines = npixels = IMAGE_SIZE], and cannot exceed the FBF size.
    5959// - The number of clusters  must be a power of 2 no larger than 256.
    6060// - The number of cores per cluster must be a power of 2 no larger than 4.
     
    8080
    8181#define IMAGE_TYPE            420                          // pixel encoding type
    82 
    83 //#define IMAGE_SIZE            128                          // image size
    84 //#define INPUT_FILE_PATH       "/misc/images_128.raw"       // input file pathname
    85 //#define OUTPUT_FILE_PATH      "/misc/transposed_128.raw"   // output file pathname
    86 
    87 //#define IMAGE_SIZE            256                          // image size
    88 //#define INPUT_FILE_PATH       "/misc/lena_256.raw"         // input file pathname
    89 #//define OUTPUT_FILE_PATH      "/misc/transposed_256.raw"   // output file pathname
    90 
    91 //#define IMAGE_SIZE            512                          // image size
    92 //#define INPUT_FILE_PATH       "/misc/couple_512.raw"       // input file pathname
    93 //#define OUTPUT_FILE_PATH      "/misc/transposed_512.raw"   // output file pathname
    94 
    95 #define IMAGE_SIZE            1024                         // image size
    96 #define INPUT_FILE_PATH       "/misc/philips_1024.raw"     // input file pathname
    97 #define OUTPUT_FILE_PATH      "/misc/transposed_1024.raw"  // output file pathname
    98 
    99 #define SAVE_RESULT_FILE      0                            // save result image on disk
     82#define IMAGE_SIZE            256                          // default image size
     83#define INPUT_IMAGE_PATH      "/misc/lena_256.raw"         // default input image pathname
     84#define OUTPUT_IMAGE_PATH     "/misc/lena_trsp_256.raw"    // default output image pathname
     85
     86#define SAVE_RESULT_FILE      1                            // save result image on disk
    10087#define USE_DQT_BARRIER       0                            // quad-tree barrier if non zero
    10188
     
    10895#define VERBOSE_EXEC          1                            // exec function print comments
    10996
     97#define INTERACTIVE_MODE      1
    11098
    11199///////////////////////////////////////////////////////
     
    140128unsigned char *  buf_out[CLUSTERS_MAX];
    141129
    142 // pointer and identifier for dynamically allocated FBF window
    143 void   *  win_buf;
    144 int       wid;
     130// pointer and identifier for FBF windows
     131void   *  in_win_buf;
     132int       in_wid;
     133void   *  out_win_buf;
     134int       out_wid;
    145135
    146136// synchronisation barrier (all working threads)
     
    167157// array of thread attributes / indexed by [tid]
    168158pthread_attr_t                exec_attr[THREADS_MAX];
     159
     160// image features
     161unsigned int   image_size;
     162char           input_image_path[128];
     163char           output_image_path[128];
    169164
    170165////////////////////////////////////////////////////////////////
     
    234229    unsigned int nthreads  = nclusters * ncores;
    235230
    236     if( nthreads > IMAGE_SIZE )
    237     {
    238         printf("\n[transpose error] number of threads larger than number of lines\n");
    239         exit( 0 );
     231    // get input and output images path and size
     232    if( INTERACTIVE_MODE )
     233    {
     234        printf("\n - image size : ");
     235        get_uint32( &image_size );
     236
     237        printf("\n - input image path : ");
     238        get_string( input_image_path , 128 );
     239 
     240        printf(" - output image path : ");
     241        get_string( output_image_path , 128 );
     242    }
     243    else
     244    {
     245        image_size = IMAGE_SIZE;
     246        strcpy( input_image_path , INPUT_IMAGE_PATH );
     247        strcpy( input_image_path , OUTPUT_IMAGE_PATH );
    240248    }
    241249
    242250    // get FBF size and type
    243     unsigned int   fbf_width;
    244     unsigned int   fbf_height;
    245     unsigned int   fbf_type;
     251    int   fbf_width;
     252    int   fbf_height;
     253    int   fbf_type;
    246254    fbf_get_config( &fbf_width , &fbf_height , &fbf_type );
    247255
    248     if( (fbf_width < IMAGE_SIZE) || (fbf_height < IMAGE_SIZE) || (fbf_type != IMAGE_TYPE) )
    249     {
    250         printf("\n[transpose error] image does not fit FBF size or type\n");
     256    // check image
     257    if( nthreads > image_size )
     258    {
     259        printf("\n[transpose error] nthreads (%d) larger than image size (%d)\n",
     260               nthreads , image_size );
     261        exit( 0 );
     262    }
     263
     264    if( ((unsigned int)fbf_width  < image_size) ||
     265        ((unsigned int)fbf_height < image_size) ||
     266        (fbf_type != IMAGE_TYPE) )
     267    {
     268        printf("\n[transpose error] image not acceptable\n"
     269               "FBF width  = %d / npixels  = %d\n"
     270               "FBF height = %d / nlines   = %d\n"
     271               "FBF type   = %d / expected = %d\n",
     272               fbf_width, image_size, fbf_height, image_size, fbf_type, IMAGE_TYPE );
    251273        exit( 0 );
    252274    }
    253275
    254276    // define total number of pixels
    255     int npixels = IMAGE_SIZE * IMAGE_SIZE;
     277    int npixels = image_size * image_size;
    256278
    257279    // define instrumentation file name
     
    259281    {
    260282        printf("\n[transpose] %d cluster(s) / %d core(s) / <%s> / PID %x / NO_PLACE\n",
    261         nclusters, ncores, INPUT_FILE_PATH , getpid() );
     283        nclusters, ncores, input_image_path, getpid() );
    262284
    263285        // build instrumentation file name
    264286        if( USE_DQT_BARRIER )
    265287        snprintf( filename , 32 , "trsp_dqt_no_place_%d_%d_%d",
    266         IMAGE_SIZE , x_size * y_size , ncores );
     288        image_size , x_size * y_size , ncores );
    267289        else
    268290        snprintf( filename , 32 , "trsp_smp_no_place_%d_%d_%d",
    269         IMAGE_SIZE , x_size * y_size , ncores );
     291        image_size , x_size * y_size , ncores );
    270292    }
    271293
     
    273295    {
    274296        printf("\n[transpose] %d cluster(s) / %d core(s) / <%s> / PID %x / EXPLICIT\n",
    275         nclusters, ncores, INPUT_FILE_PATH , getpid() );
     297        nclusters, ncores, input_image_path, getpid() );
    276298
    277299        // build instrumentation file name
    278300        if( USE_DQT_BARRIER )
    279301        snprintf( filename , 32 , "trsp_dqt_explicit_%d_%d_%d",
    280         IMAGE_SIZE , x_size * y_size , ncores );
     302        image_size , x_size * y_size , ncores );
    281303        else
    282304        snprintf( filename , 32 , "trsp_smp_explicit_%d_%d_%d",
    283         IMAGE_SIZE , x_size * y_size , ncores );
     305        image_size , x_size * y_size , ncores );
    284306    }
    285307
     
    287309    {
    288310        printf("\n[transpose] %d cluster(s) / %d core(s) / <%s> / PID %x / PARALLEL\n",
    289         nclusters, ncores, INPUT_FILE_PATH , getpid() );
     311        nclusters, ncores, input_image_path, getpid() );
    290312
    291313        // build instrumentation file name
    292314        if( USE_DQT_BARRIER )
    293315        snprintf( filename , 32 , "trsp_dqt_parallel_%d_%d_%d",
    294         IMAGE_SIZE , x_size * y_size , ncores );
     316        image_size , x_size * y_size , ncores );
    295317        else
    296318        snprintf( filename , 32 , "trsp_smp_parallel_%d_%d_%d",
    297         IMAGE_SIZE , x_size * y_size , ncores );
    298     }
    299 
    300     // open a window in FBF
    301     wid = fbf_create_window( 0,             // l_zero
    302                              0,             // p_zero
    303                              IMAGE_SIZE,    // lines
    304                              IMAGE_SIZE,    // pixels
    305                              &win_buf );
    306     if( wid < 0)
    307     {
    308         printf("\n[transpose error] cannot open FBF window\n");
     319        image_size , x_size * y_size , ncores );
     320    }
     321
     322    // create an FBF window for input image
     323    in_wid = fbf_create_window( 0,                // l_zero
     324                                0,                // p_zero
     325                                image_size,       // lines
     326                                image_size,       // pixels
     327                                &in_win_buf );    // pointer on buffer in user space
     328    if( in_wid < 0)
     329    {
     330        printf("\n[transpose error] cannot create window for %s\n", input_image_path );
     331        exit( 0 );
     332    }
     333
     334    // activate window
     335    error = fbf_active_window( in_wid , 1 );
     336
     337    if( error )
     338    {
     339        printf("\n[transpose error] cannot activate window for %s\n", input_image_path );
    309340        exit( 0 );
    310341    }
    311342
    312343#if  VERBOSE_MAIN
    313 printf("\n[transpose] main on core[%x,%d] created FBF window %d / buffer %x\n",
    314 cxy_main, lid_main, wid , win_buf );
     344printf("\n[transpose] main on core[%x,%d] created window for %s / wid %d / buf %x\n",
     345cxy_main, lid_main, input_image_path, in_wid , in_win_buf );
     346#endif
     347
     348    // create an FBF window for output image
     349    out_wid = fbf_create_window( image_size,       // l_zero
     350                                 image_size,       // p_zero
     351                                 image_size,       // lines
     352                                 image_size,       // pixels
     353                                 &out_win_buf );   // pointer on buffer in user space
     354    if( out_wid < 0)
     355    {
     356        printf("\n[transpose error] cannot create window for %s\n", output_image_path );
     357        exit( 0 );
     358    }
     359
     360    // activate window
     361    error = fbf_active_window( out_wid , 1 );
     362
     363    if( error )
     364    {
     365        printf("\n[transpose error] cannot activate window for %s\n", output_image_path );
     366        exit( 0 );
     367    }
     368
     369#if  VERBOSE_MAIN
     370printf("\n[transpose] main on core[%x,%d] created window for %s / wid %d / buf %x\n",
     371cxy_main, lid_main, output_image_path, out_wid , out_win_buf );
    315372#endif
    316373
     
    356413
    357414    // open input file
    358     int fd_in = open( INPUT_FILE_PATH , O_RDONLY , 0 );
     415    int fd_in = open( input_image_path , O_RDONLY , 0 );
    359416
    360417    if ( fd_in < 0 )
    361418    {
    362         printf("\n[transpose error] main cannot open file %s\n", INPUT_FILE_PATH );
     419        printf("\n[transpose error] main cannot open file %s\n", input_image_path );
    363420        exit( 0 );
    364421    }
    365422
    366423#if  VERBOSE_MAIN
    367 printf("\n[transpose] main open file <%s> / fd = %d\n", INPUT_FILE_PATH , fd_in );
     424printf("\n[transpose] main open file <%s> / fd = %d\n", input_image_path , fd_in );
    368425#endif
    369426
    370427    // open output file
    371     int fd_out = open( OUTPUT_FILE_PATH , O_CREAT , 0 );
     428    int fd_out = open( output_image_path , O_CREAT , 0 );
    372429
    373430    if ( fd_out < 0 )
    374431    {
    375         printf("\n[transpose error] main cannot open file %s\n", OUTPUT_FILE_PATH );
     432        printf("\n[transpose error] main cannot open file %s\n", output_image_path );
    376433        exit( 0 );
    377434    }
     
    385442
    386443#if  VERBOSE_MAIN
    387 printf("\n[transpose] main moved file <%s> to buf_in\n", INPUT_FILE_PATH );
     444printf("\n[transpose] main moved file <%s> to buf_in\n", input_image_path );
    388445#endif
    389446
     
    621678#endif
    622679
    623     // delete FBF window
    624     if( fbf_delete_window( wid ) )
     680    // delete FBF windows
     681    if( fbf_delete_window( in_wid ) )
     682    if( fbf_delete_window( out_wid ) )
    625683    {
    626684        printf("\n[transpose error] main cannot delete FBF window\n");
    627685        exit( 0 );
    628686    }
     687
     688#if VERBOSE_MAIN
     689printf("\n[transpose] main deleted FBF windows\n");
     690#endif
    629691
    630692    // main thread suicide
     
    634696
    635697} // end main()
     698
     699
     700
     701
    636702
    637703
     
    646712    int                  error;
    647713
    648     unsigned char      * wbuf = win_buf;
    649  
    650714    pthread_parallel_work_args_t * args = (pthread_parallel_work_args_t *)arguments;
    651715
     
    678742
    679743    // compute total number of pixels per image
    680     unsigned int npixels = IMAGE_SIZE * IMAGE_SIZE;     
     744    unsigned int npixels = image_size * image_size;     
    681745
    682746    // compute total number of threads and clusters
     
    689753
    690754    // compute first and last line per thread
    691     unsigned int lines_per_cid = pixels_per_cid / IMAGE_SIZE;
    692     unsigned int lines_per_lid = pixels_per_lid / IMAGE_SIZE;
     755    unsigned int lines_per_cid = pixels_per_cid / image_size;
     756    unsigned int lines_per_lid = pixels_per_lid / image_size;
    693757
    694758    unsigned int line_first = (cid * lines_per_cid) + (lid * lines_per_lid);
     
    751815
    752816    // all local threads copy part of buf_in[cid] to FBF window for display
    753     memcpy( wbuf + (cid * pixels_per_cid) + (lid * pixels_per_lid),
     817    memcpy( in_win_buf + (cid * pixels_per_cid) + (lid * pixels_per_lid),
    754818            buf_in[cid] + (lid * pixels_per_lid),
    755819            pixels_per_lid );
     
    760824#endif
    761825
    762     // retresh window
    763     error = fbf_refresh_window( wid , line_first , line_last );
     826    // all threads contribute to input window refresh
     827    error = fbf_refresh_window( in_wid , line_first , line_last );
    764828
    765829    if( error )
     
    785849    // (l,p) are the absolute pixel coordinates in the dest image
    786850
    787     unsigned int nlt   = IMAGE_SIZE / nthreads;    // number of lines per thread
    788     unsigned int nlc   = IMAGE_SIZE / nclusters;   // number of lines per cluster
     851    unsigned int nlt   = image_size / nthreads;    // number of lines per thread
     852    unsigned int nlc   = image_size / nclusters;   // number of lines per cluster
    789853
    790854    unsigned int src_cid;
     
    802866    {
    803867        // loop on pixels in one line (one pixel per iteration)
    804         for ( p = 0 ; p < IMAGE_SIZE ; p++ )
     868        for ( p = 0 ; p < image_size ; p++ )
    805869        {
    806870            // read one byte from local buf_in
    807871            src_cid   = l / nlc;
    808             src_index = (l % nlc) * IMAGE_SIZE + p;
     872            src_index = (l % nlc) * image_size + p;
    809873
    810874            byte = buf_in[src_cid][src_index];
     
    812876            // write one byte to remote buf_out
    813877            dst_cid   = p / nlc;
    814             dst_index = (p % nlc) * IMAGE_SIZE + l;
     878            dst_index = (p % nlc) * image_size + l;
    815879
    816880            buf_out[dst_cid][dst_index] = byte;
     
    834898
    835899    // each local threads copy part of buf_out[cid] to FBF window for display
    836     memcpy( wbuf + (cid * pixels_per_cid) + (lid * pixels_per_lid),
     900    memcpy( out_win_buf + (cid * pixels_per_cid) + (lid * pixels_per_lid),
    837901            buf_out[cid] + (lid * pixels_per_lid),
    838902            pixels_per_lid );
     
    843907#endif
    844908
    845     // refresh window
    846     error = fbf_refresh_window( wid , line_first , line_last );
     909    // each thread contributes to output window refresh
     910    error = fbf_refresh_window( out_wid , line_first , line_last );
    847911
    848912    if( error )
Note: See TracChangeset for help on using the changeset viewer.