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

Last change on this file since 712 was 712, checked in by alain, 9 years ago

Introduce the giet_fbf_size() and giet_fbf_alloc() system calls.

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