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

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

1) Introduce the string library.
2) Introduce a heap in the shell application.

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