source: soft/giet_vm/applications/transpose/transpose.c @ 778

Last change on this file since 778 was 778, checked in by alain, 8 years ago

O_CREAT

File size: 21.1 KB
RevLine 
[769]1//////////////////////////////////////////////////////////////////////////////////////////
[708]2// File   : transpose.c   
3// Date   : september 2015
4// author : Alain Greiner
[769]5//////////////////////////////////////////////////////////////////////////////////////////
[764]6// This multi-threaded aplication read a raw image (one byte per pixel)
7// stored on disk, transpose it, display the result on the frame buffer,
8// and store the transposed image on disk.
9// The input image can be interactively selected if the INTERACTIVE flag is set.
[708]10// It can run on a multi-processors, multi-clusters architecture, with one thread
11// per processor, and uses the POSIX threads API.
[764]12// It uses the giet_fat_mmap() to directly access the input and output files
13// in the kernel files cache. It does not use the CMA to display the result image.
[708]14//
15// The main() function can be launched on any processor P[x,y,l].
16// It makes the initialisations, launch (N-1) threads to run the execute() function
17// on the (N-1) other processors than P[x,y,l], call himself the execute() function,
18// and finally call the instrument() function to display instrumentation results
19// when the parallel execution is completed.
20//
[764]21// The buf_in[x,y] and buf_out[put buffers containing the direct ans transposed images
22// are distributed in clusters:
23// In each cluster[x,y], the thread running on processor P[x,y,0] uses the giet_fat_mmap()
24// function to map the buf_in[x,y] and buf_out[x,y] buffers containing a set of lines.
25// Then, all threads in cluster[x,y] read pixels from the local buf_in[x,y] buffer, and
26// write the pixels to the remote buf_out[x,y] buffers. Finally, each thread display
27// a part of the transposed image to the frame buffer.
[708]28//
[769]29// - The image size must fit the frame buffer width and height, that must be power of 2.
[708]30// - The number of clusters  must be a power of 2 no larger than 256.
31// - The number of processors per cluster must be a power of 2 no larger than 4.
[764]32// - The number of clusters cannot be larger than (image_size * image_size) / 4096,
33//   because the size of buf_in[x,y] and buf_out[x,y] must be multiple of 4096.
34//
35// The transpose_rw.c file contains a variant that use the giet_fat_read()
36// and giet_fat_write() system calls, to access the files.
[769]37//////////////////////////////////////////////////////////////////////////////////////////
[708]38
39#include "stdio.h"
[712]40#include "stdlib.h"
[708]41#include "user_barrier.h"
42#include "malloc.h"
43
[764]44#define BLOCK_SIZE            512                          // block size on disk
45#define X_MAX                 16                           // max number of clusters in row
46#define Y_MAX                 16                           // max number of clusters in column
47#define PROCS_MAX             4                            // max number of procs per cluster
48#define CLUSTER_MAX           (X_MAX * Y_MAX)              // max number of clusters
[775]49#define IMAGE_SIZE            128                          // default image size
50#define INPUT_FILE_PATH       "/misc/images_128.raw"       // default input file pathname
51#define OUTPUT_FILE_PATH      "/home/trsp_128.raw"         // default output file pathname
[764]52#define INTERACTIVE           0                            // interactive capture of filenames
[775]53#define VERBOSE               0                            // print comments on TTY
[708]54
[764]55
[708]56// macro to use a shared TTY
57#define printf(...);    { lock_acquire( &tty_lock ); \
58                          giet_tty_printf(__VA_ARGS__);  \
59                          lock_release( &tty_lock ); }
60
61///////////////////////////////////////////////////////
62// global variables stored in seg_data in cluster(0,0)
63///////////////////////////////////////////////////////
64
65// instrumentation counters for each processor in each cluster
[764]66unsigned int MMAP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
67unsigned int MMAP_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
[708]68unsigned int TRSP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
69unsigned int TRSP_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
70unsigned int DISP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
71unsigned int DISP_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
72
73// arrays of pointers on distributed buffers
74// one input buffer & one output buffer per cluster
75unsigned char*  buf_in [CLUSTER_MAX];
76unsigned char*  buf_out[CLUSTER_MAX];
77
78// lock protecting shared TTY
79user_lock_t  tty_lock;
80
81// synchronisation barrier (all threads)
82giet_sqt_barrier_t barrier;
83
[712]84// input & output files pathname and size
85char          input_file_name[256];
86char          output_file_name[256];
87unsigned int  image_size;
88
[764]89// input & output file descriptors
90int  fd_in;
91int  fd_out;
92
[708]93////////////////////////////////////////////
94__attribute__ ((constructor)) void execute()
95////////////////////////////////////////////
96{
97    unsigned int l;                            // line index for loops
98    unsigned int p;                            // pixel index for loops
99
100    // get processor identifiers
101    unsigned int x_id;                         // x cluster coordinate
102    unsigned int y_id;                         // y cluster coordinate
103    unsigned int p_id;                         // local processor index
104
105    giet_proc_xyp( &x_id, &y_id, &p_id);             
106
107    // get & check plat-form parameters
108    unsigned int x_size;                       // number of clusters in a row
109    unsigned int y_size;                       // number of clusters in a column
110    unsigned int nprocs;                       // number of processors per cluster
111   
112    giet_procs_number( &x_size , &y_size , &nprocs );
113
114    unsigned int nclusters     = x_size * y_size;               // number of clusters
115    unsigned int nthreads      = x_size * y_size * nprocs;      // number of threads
[712]116    unsigned int npixels       = image_size * image_size;       // pixels per image
[708]117    unsigned int cluster_id    = (x_id * y_size) + y_id;        // "continuous" index   
[764]118    unsigned int thread_id     = (cluster_id * nprocs) + p_id;  // "continuous" index
[708]119
120    // parallel load of image:
[764]121    // thread running on processor[x,y,0]
122    // map input & output files in buf_in & buf_out buffers.
[708]123
[764]124    MMAP_START[x_id][y_id][p_id] = giet_proctime();
[708]125
126    if ( p_id == 0 ) 
127    {
[775]128        // map buf_in
[764]129        unsigned int length = npixels / nclusters;
130        unsigned int offset = length * cluster_id;
131       
132        buf_in[cluster_id] =  giet_fat_mmap( NULL,
133                                             length,
134                                             MAP_PROT_READ,
135                                             MAP_SHARED,
136                                             fd_in,
137                                             offset );
138        if ( buf_in[cluster_id] == NULL )
[708]139        {
[764]140            printf("\n[TRANSPOSE ERROR] Thread[%d,%d,%d] cannot map input file\n",
141                   x_id , y_id , p_id );
142            giet_pthread_exit( NULL );
[708]143        }
[764]144                 
[769]145        if ( VERBOSE )
[775]146        {
147            printf("\n@@@ Thread[%d,%d,%d] call mmap for input file\n"
148                   " length = %x / offset = %x / buf_in = %x\n",
149                   x_id , y_id , p_id , length , offset , buf_in[cluster_id] );
150        }
151
152        // map buf_out           
[764]153        buf_out[cluster_id] = giet_fat_mmap( NULL,
154                                             length,
155                                             MAP_PROT_WRITE,
156                                             MAP_SHARED,
157                                             fd_out,
158                                             offset );
[775]159
[764]160        if ( buf_out[cluster_id] == NULL )
[708]161        {
[764]162            printf("\n[TRANSPOSE ERROR] Thread[%d,%d,%d] cannot map output file\n",
163                   x_id , y_id , p_id );
164            giet_pthread_exit( NULL );
[708]165        }
[764]166                   
[769]167        if ( VERBOSE )
[775]168        {
169            printf("\n@@@ Thread[%d,%d,%d] call mmap for output file\n"
170                   " length = %x / offset = %x / buf_out = %x\n",
171                   x_id , y_id , p_id , length , offset , buf_out[cluster_id] );
172        }
[764]173       
[708]174    }
175
[764]176    MMAP_END[x_id][y_id][p_id] = giet_proctime();
[708]177
178    /////////////////////////////
179    sqt_barrier_wait( &barrier );
180    /////////////////////////////
181
182    // parallel transpose from buf_in to buf_out
[712]183    // each thread makes the transposition for nlt lines (nlt = image_size/nthreads)
[708]184    // from line [thread_id*nlt] to line [(thread_id + 1)*nlt - 1]
185    // (p,l) are the absolute pixel coordinates in the source image
186
187    TRSP_START[x_id][y_id][p_id] = giet_proctime();
188
[712]189    unsigned int nlt   = image_size / nthreads;    // number of lines per thread
190    unsigned int nlc   = image_size / nclusters;   // number of lines per cluster
[708]191
192    unsigned int src_cluster;
193    unsigned int src_index;
194    unsigned int dst_cluster;
195    unsigned int dst_index;
196
197    unsigned char byte;
198
199    unsigned int first = thread_id * nlt;  // first line index for a given thread
200    unsigned int last  = first + nlt;      // last line index for a given thread
201
202    for ( l = first ; l < last ; l++ )
203    {
204        // in each iteration we transfer one byte
[712]205        for ( p = 0 ; p < image_size ; p++ )
[708]206        {
207            // read one byte from local buf_in
208            src_cluster = l / nlc;
[712]209            src_index   = (l % nlc)*image_size + p;
[708]210            byte        = buf_in[src_cluster][src_index];
211
212            // write one byte to remote buf_out
213            dst_cluster = p / nlc; 
[712]214            dst_index   = (p % nlc)*image_size + l;
[708]215            buf_out[dst_cluster][dst_index] = byte;
216        }
217    }
218
219    if ( (p_id == 0) && (x_id==0) && (y_id==0) )
220    {
[764]221        printf("\n[TRANSPOSE] Thread[%d,%d,%d] completes transpose at cycle %d\n", 
[708]222        x_id, y_id, p_id, giet_proctime() );
223    }
224
225    TRSP_END[x_id][y_id][p_id] = giet_proctime();
226
227    /////////////////////////////
228    sqt_barrier_wait( &barrier );
229    /////////////////////////////
230
231    // parallel display from local buf_out to frame buffer
232    // all threads contribute to display using memcpy...
233
234    DISP_START[x_id][y_id][p_id] = giet_proctime();
235
236    unsigned int  npt   = npixels / nthreads;   // number of pixels per thread
237
238    giet_fbf_sync_write( npt * thread_id, 
239                         &buf_out[cluster_id][p_id*npt], 
240                         npt );
241
242    if ( (x_id==0) && (y_id==0) && (p_id==0) )
243    {
[764]244        printf("\n[TRANSPOSE] Thread[%d,%d,%d] completes display at cycle %d\n",
[708]245               x_id, y_id, p_id, giet_proctime() );
246    }
247
248    DISP_END[x_id][y_id][p_id] = giet_proctime();
249
250    /////////////////////////////
251    sqt_barrier_wait( &barrier );
252    /////////////////////////////
253
[764]254    // all threads, but thread[0,0,0], suicide
[708]255    if ( (x_id != 0) || (y_id != 0) || (p_id != 0) ) 
256    giet_pthread_exit( "completed" );
257
258} // end execute()
259
260
261
262//////////////////////////////////////
263void instrument( unsigned int x_size,
264                 unsigned int y_size,
265                 unsigned int nprocs )
266//////////////////////////////////////
267{
268    unsigned int x, y, l;
269
270    unsigned int min_load_start = 0xFFFFFFFF;
271    unsigned int max_load_start = 0;
272    unsigned int min_load_ended = 0xFFFFFFFF;
273    unsigned int max_load_ended = 0;
274    unsigned int min_trsp_start = 0xFFFFFFFF;
275    unsigned int max_trsp_start = 0;
276    unsigned int min_trsp_ended = 0xFFFFFFFF;
277    unsigned int max_trsp_ended = 0;
278    unsigned int min_disp_start = 0xFFFFFFFF;
279    unsigned int max_disp_start = 0;
280    unsigned int min_disp_ended = 0xFFFFFFFF;
281    unsigned int max_disp_ended = 0;
[769]282 
[775]283    char string[64];
284
285    snprintf( string , 64 , "/home/transpose_%d_%d_%d" , x_size , y_size , nprocs );
286
[769]287    // open instrumentation file
[778]288    unsigned int fd = giet_fat_open( string , O_CREAT );
[769]289    if ( fd < 0 ) 
290    { 
[775]291        printf("\n[TRANSPOSE ERROR] cannot open instrumentation file %s\n", string );
[769]292        giet_pthread_exit( NULL );
293    }
[708]294
295    for (x = 0; x < x_size; x++)
296    {
297        for (y = 0; y < y_size; y++)
298        {
299            for ( l = 0 ; l < nprocs ; l++ )
300            {
[764]301                if (MMAP_START[x][y][l] < min_load_start)  min_load_start = MMAP_START[x][y][l];
302                if (MMAP_START[x][y][l] > max_load_start)  max_load_start = MMAP_START[x][y][l];
303                if (MMAP_END[x][y][l]   < min_load_ended)  min_load_ended = MMAP_END[x][y][l]; 
304                if (MMAP_END[x][y][l]   > max_load_ended)  max_load_ended = MMAP_END[x][y][l];
[708]305                if (TRSP_START[x][y][l] < min_trsp_start)  min_trsp_start = TRSP_START[x][y][l];
306                if (TRSP_START[x][y][l] > max_trsp_start)  max_trsp_start = TRSP_START[x][y][l];
307                if (TRSP_END[x][y][l]   < min_trsp_ended)  min_trsp_ended = TRSP_END[x][y][l];
308                if (TRSP_END[x][y][l]   > max_trsp_ended)  max_trsp_ended = TRSP_END[x][y][l];
309                if (DISP_START[x][y][l] < min_disp_start)  min_disp_start = DISP_START[x][y][l];
310                if (DISP_START[x][y][l] > max_disp_start)  max_disp_start = DISP_START[x][y][l];
311                if (DISP_END[x][y][l]   < min_disp_ended)  min_disp_ended = DISP_END[x][y][l];
312                if (DISP_END[x][y][l]   > max_disp_ended)  max_disp_ended = DISP_END[x][y][l];
313            }
314        }
315    }
316
[775]317    giet_tty_printf( "\n ------ %s ------\n" , string );
318    giet_fat_fprintf( fd , "\n ------ %s ------\n" , string );
[708]319
[775]320    giet_tty_printf( " - MMAP_START : min = %d / max = %d / med = %d / delta = %d\n",
[708]321           min_load_start, max_load_start, (min_load_start+max_load_start)/2, 
[775]322           max_load_start-min_load_start ); 
323
[769]324    giet_fat_fprintf( fd , " - MMAP_START : min = %d / max = %d / med = %d / delta = %d\n",
325           min_load_start, max_load_start, (min_load_start+max_load_start)/2, 
[775]326           max_load_start-min_load_start ); 
[708]327
[775]328    giet_tty_printf( " - MMAP_END   : min = %d / max = %d / med = %d / delta = %d\n",
[708]329           min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, 
[775]330           max_load_ended-min_load_ended ); 
331
[769]332    giet_fat_fprintf( fd , " - MMAP_END   : min = %d / max = %d / med = %d / delta = %d\n",
333           min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, 
[775]334           max_load_ended-min_load_ended ); 
[708]335
[775]336    giet_tty_printf( " - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n",
[708]337           min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, 
[775]338           max_trsp_start-min_trsp_start ); 
339
[769]340    giet_fat_fprintf( fd , " - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n",
341           min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, 
[775]342           max_trsp_start-min_trsp_start ); 
[708]343
[775]344    giet_tty_printf( " - TRSP_END   : min = %d / max = %d / med = %d / delta = %d\n",
[708]345           min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, 
[775]346           max_trsp_ended-min_trsp_ended ); 
347
[769]348    giet_fat_fprintf( fd , " - TRSP_END   : min = %d / max = %d / med = %d / delta = %d\n",
349           min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, 
[775]350           max_trsp_ended-min_trsp_ended ); 
[708]351
[775]352    giet_tty_printf( " - DISP_START : min = %d / max = %d / med = %d / delta = %d\n",
[708]353           min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, 
[775]354           max_disp_start-min_disp_start ); 
355
[769]356    giet_fat_fprintf( fd , " - DISP_START : min = %d / max = %d / med = %d / delta = %d\n",
357           min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, 
[775]358           max_disp_start-min_disp_start ); 
[708]359
[775]360    giet_tty_printf( " - DISP_END   : min = %d / max = %d / med = %d / delta = %d\n",
[708]361           min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, 
[775]362           max_disp_ended-min_disp_ended ); 
363
[769]364    giet_fat_fprintf( fd , " - DISP_END   : min = %d / max = %d / med = %d / delta = %d\n",
365           min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, 
[775]366           max_disp_ended-min_disp_ended ); 
[708]367
[769]368    giet_fat_close( fd );
369
[708]370}  // end instrument()
371
372
373
374//////////////////////////////////////////
375__attribute__ ((constructor)) void main()
376//////////////////////////////////////////
377{
378    // indexes for loops
379    unsigned int x , y , n;
380
381    // get identifiers for proc executing main
382    unsigned int x_id;                          // x cluster coordinate
383    unsigned int y_id;                          // y cluster coordinate
384    unsigned int p_id;                          // local processor index
385    giet_proc_xyp( &x_id , &y_id , &p_id );
386
387    // get & check plat-form parameters
388    unsigned int x_size;                       // number of clusters in a row
389    unsigned int y_size;                       // number of clusters in a column
390    unsigned int nprocs;                       // number of processors per cluster
391    giet_procs_number( &x_size , &y_size , &nprocs );
392
393    giet_pthread_assert( ((nprocs == 1) || (nprocs == 2) || (nprocs == 4)),
394                         "[TRANSPOSE ERROR] number of procs per cluster must be 1, 2 or 4");
395
396    giet_pthread_assert( ((x_size == 1) || (x_size == 2) || (x_size == 4) || 
[764]397                          (x_size == 8) || (x_size == 16)),
[708]398                         "[TRANSPOSE ERROR] x_size must be 1,2,4,8,16");
399
400    giet_pthread_assert( ((y_size == 1) || (y_size == 2) || (y_size == 4) || 
[764]401                          (y_size == 8) || (y_size == 16)),
[708]402                         "[TRANSPOSE ERROR] y_size must be 1,2,4,8,16");
403
[712]404    // compute number of threads
[708]405    unsigned int nthreads = x_size * y_size * nprocs;
406
407    // shared TTY allocation
408    giet_tty_alloc( 1 );     
409    lock_init( &tty_lock);
410
[712]411    // get FBF ownership and FBF size
412    unsigned int   width;
413    unsigned int   height;
414    giet_fbf_alloc();
415    giet_fbf_size( &width , &height );
[708]416
[712]417    printf("\n[TRANSPOSE] start at cycle %d on %d cores / FBF = %d * %d pixels\n",
418           giet_proctime(), nthreads , width , height );
419
[764]420    if ( INTERACTIVE ) // input_file_name, output_file_name, and size  acquisition
421    {
422        printf("\n[TRANSPOSE] enter path for input file / default is : %s\n> ", INPUT_FILE_PATH ); 
423        giet_tty_gets( input_file_name , 256 );
424        printf("\n");
425        if ( strcmp( input_file_name , "" ) == 0 ) strcpy( input_file_name , INPUT_FILE_PATH );
[712]426
[764]427        printf("\n[TRANSPOSE] enter path for output file / default is : %s\n> ", OUTPUT_FILE_PATH ); 
428        giet_tty_gets( output_file_name , 256 );
429        printf("\n");
430        if ( strcmp( output_file_name , "" ) == 0 ) strcpy( output_file_name , OUTPUT_FILE_PATH );
[712]431
[764]432        printf("\n[TRANSPOSE] enter image size / default is : %d\n> ", IMAGE_SIZE ); 
433        giet_tty_getw( &image_size );
434        printf("\n");
435        if ( image_size == 0 ) image_size = IMAGE_SIZE;
436    }
437    else
438    {
439        strcpy( input_file_name , INPUT_FILE_PATH );
440        strcpy( output_file_name , OUTPUT_FILE_PATH );
441        image_size = IMAGE_SIZE;
442    }
[712]443
[764]444    // check image size / number of clusters
445    giet_pthread_assert( ((((image_size * image_size) / (x_size * y_size)) & 0xFFF) == 0) ,
446                         "[TRANSPOSE ERROR] pixels per cluster must be multiple of 4096");
447   
[712]448    printf("\n[TRANSPOSE] input = %s / output = %s / size = %d\n",
449           input_file_name, output_file_name, image_size );
450
[708]451    // distributed heap initialisation
452    for ( x = 0 ; x < x_size ; x++ ) 
453    {
454        for ( y = 0 ; y < y_size ; y++ ) 
455        {
456            heap_init( x , y );
457        }
458    }
459
[764]460    // open input and output files
461    fd_in = giet_fat_open( input_file_name , O_RDONLY );  // read_only
462    if ( fd_in < 0 ) 
463    { 
464        printf("\n[TRANSPOSE ERROR] main cannot open file %s\n", input_file_name );
465        giet_pthread_exit( NULL );
466    }
467    else 
468    {
469        printf("\n[TRANSPOSE] main open file %s / fd = %d\n", input_file_name , fd_in );
470    }
471
[778]472    fd_out = giet_fat_open( output_file_name , O_CREAT );   // create if required
[764]473    if ( fd_out < 0 ) 
474    { 
475        printf("\n[TRANSPOSE ERROR] main cannot open file %s\n", output_file_name );
476        giet_pthread_exit(" open() failure");
477    }
478    else
479    {
480        printf("\n[TRANSPOSE] main open file %s / fd = %d\n", output_file_name , fd_out );
481    }
482
[708]483    // allocate thread[] array
484    pthread_t* thread = malloc( nthreads * sizeof(pthread_t) );
485
486    // barrier initialisation
487    sqt_barrier_init( &barrier, x_size , y_size , nprocs );
488
489    // Initialisation completed
[764]490    printf("\n[TRANSPOSE] main completes initialisation\n");
[708]491   
492    // launch other threads to run execute() function
493    for ( n = 1 ; n < nthreads ; n++ )
494    {
495        if ( giet_pthread_create( &thread[n],
496                                  NULL,                  // no attribute
497                                  &execute,
498                                  NULL ) )               // no argument
499        {
500            printf("\n[TRANSPOSE ERROR] creating thread %x\n", thread[n] );
501            giet_pthread_exit( NULL );
502        }
503    }
504
505    // run the execute() function
506    execute();
507
508    // wait other threads completion
509    for ( n = 1 ; n < nthreads ; n++ )
510    {
511        if ( giet_pthread_join( thread[n], NULL ) )
512        {
513            printf("\n[TRANSPOSE ERROR] joining thread %x\n", thread[n] );
514            giet_pthread_exit( NULL );
515        }
516        else
517        {
518            printf("\n[TRANSPOSE] thread %x joined at cycle %d\n",
519                   thread[n] , giet_proctime() );
520        }
521    }
522
523    // call the instrument() function
524    instrument( x_size , y_size , nprocs );
525
[764]526    // close input and output files
527    giet_fat_close( fd_in );
528    giet_fat_close( fd_out );
529
530    // suicide
[708]531    giet_pthread_exit( "completed" );
532   
533} // end main()
534
Note: See TracBrowser for help on using the repository browser.