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

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

Modify the transpose application to use replace the
giet_fat_read() / giet_fat_write() system calls
by the giet_far_mmap() system call, in order to
avoid one copy of data in user space.

The read/write version is still available as transpose_rw.c

File size: 19.2 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 size.
30// - The block size in block device must be 512 bytes.
31// - The number of clusters  must be a power of 2 no larger than 256.
32// - The number of processors per cluster must be a power of 2 no larger than 4.
33// - The number of clusters cannot be larger than (image_size * image_size) / 4096,
34//   because the size of buf_in[x,y] and buf_out[x,y] must be multiple of 4096.
35//
36// The transpose_rw.c file contains a variant that use the giet_fat_read()
37// and giet_fat_write() system calls, to access the files.
38///////////////////////////////////////////////////////////////////////////////////////
39
40#include "stdio.h"
41#include "stdlib.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            256                          // default image size
51#define INPUT_FILE_PATH       "/misc/lena_256.raw"         // default input file pathname
52#define OUTPUT_FILE_PATH      "/home/lena_transposed.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 and buf_out
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 TRANSPOSE_DEBUG
147        printf("\n@@@ Thread[%d,%d,%d] call mmap : length = %x / offset = %x / buf_in = %x\n",
148               x_id , y_id , p_id , length , offset , buf_in[cluster_id] );
149           
150        buf_out[cluster_id] = giet_fat_mmap( NULL,
151                                             length,
152                                             MAP_PROT_WRITE,
153                                             MAP_SHARED,
154                                             fd_out,
155                                             offset );
156        if ( buf_out[cluster_id] == NULL )
157        {
158            printf("\n[TRANSPOSE ERROR] Thread[%d,%d,%d] cannot map output file\n",
159                   x_id , y_id , p_id );
160            giet_pthread_exit( NULL );
161        }
162                   
163        if TRANSPOSE_DEBUG
164        printf("\n@@@ Thread[%d,%d,%d] call mmap : length = %x / offset = %x / buf_out = %x\n",
165               x_id , y_id , p_id , length , offset , buf_out[cluster_id] );
166       
167    }
168
169    MMAP_END[x_id][y_id][p_id] = giet_proctime();
170
171    /////////////////////////////
172    sqt_barrier_wait( &barrier );
173    /////////////////////////////
174
175    // parallel transpose from buf_in to buf_out
176    // each thread makes the transposition for nlt lines (nlt = image_size/nthreads)
177    // from line [thread_id*nlt] to line [(thread_id + 1)*nlt - 1]
178    // (p,l) are the absolute pixel coordinates in the source image
179
180    TRSP_START[x_id][y_id][p_id] = giet_proctime();
181
182    unsigned int nlt   = image_size / nthreads;    // number of lines per thread
183    unsigned int nlc   = image_size / nclusters;   // number of lines per cluster
184
185    unsigned int src_cluster;
186    unsigned int src_index;
187    unsigned int dst_cluster;
188    unsigned int dst_index;
189
190    unsigned char byte;
191
192    unsigned int first = thread_id * nlt;  // first line index for a given thread
193    unsigned int last  = first + nlt;      // last line index for a given thread
194
195    for ( l = first ; l < last ; l++ )
196    {
197        // in each iteration we transfer one byte
198        for ( p = 0 ; p < image_size ; p++ )
199        {
200            // read one byte from local buf_in
201            src_cluster = l / nlc;
202            src_index   = (l % nlc)*image_size + p;
203            byte        = buf_in[src_cluster][src_index];
204
205            // write one byte to remote buf_out
206            dst_cluster = p / nlc; 
207            dst_index   = (p % nlc)*image_size + l;
208            buf_out[dst_cluster][dst_index] = byte;
209        }
210    }
211
212    if ( (p_id == 0) && (x_id==0) && (y_id==0) )
213    {
214        printf("\n[TRANSPOSE] Thread[%d,%d,%d] completes transpose at cycle %d\n", 
215        x_id, y_id, p_id, giet_proctime() );
216    }
217
218    TRSP_END[x_id][y_id][p_id] = giet_proctime();
219
220    /////////////////////////////
221    sqt_barrier_wait( &barrier );
222    /////////////////////////////
223
224    // parallel display from local buf_out to frame buffer
225    // all threads contribute to display using memcpy...
226
227    DISP_START[x_id][y_id][p_id] = giet_proctime();
228
229    unsigned int  npt   = npixels / nthreads;   // number of pixels per thread
230
231    giet_fbf_sync_write( npt * thread_id, 
232                         &buf_out[cluster_id][p_id*npt], 
233                         npt );
234
235    if ( (x_id==0) && (y_id==0) && (p_id==0) )
236    {
237        printf("\n[TRANSPOSE] Thread[%d,%d,%d] completes display at cycle %d\n",
238               x_id, y_id, p_id, giet_proctime() );
239    }
240
241    DISP_END[x_id][y_id][p_id] = giet_proctime();
242
243    /////////////////////////////
244    sqt_barrier_wait( &barrier );
245    /////////////////////////////
246
247    // all threads, but thread[0,0,0], suicide
248    if ( (x_id != 0) || (y_id != 0) || (p_id != 0) ) 
249    giet_pthread_exit( "completed" );
250
251} // end execute()
252
253
254
255//////////////////////////////////////
256void instrument( unsigned int x_size,
257                 unsigned int y_size,
258                 unsigned int nprocs )
259//////////////////////////////////////
260{
261    unsigned int x, y, l;
262
263    unsigned int min_load_start = 0xFFFFFFFF;
264    unsigned int max_load_start = 0;
265    unsigned int min_load_ended = 0xFFFFFFFF;
266    unsigned int max_load_ended = 0;
267    unsigned int min_trsp_start = 0xFFFFFFFF;
268    unsigned int max_trsp_start = 0;
269    unsigned int min_trsp_ended = 0xFFFFFFFF;
270    unsigned int max_trsp_ended = 0;
271    unsigned int min_disp_start = 0xFFFFFFFF;
272    unsigned int max_disp_start = 0;
273    unsigned int min_disp_ended = 0xFFFFFFFF;
274    unsigned int max_disp_ended = 0;
275
276    for (x = 0; x < x_size; x++)
277    {
278        for (y = 0; y < y_size; y++)
279        {
280            for ( l = 0 ; l < nprocs ; l++ )
281            {
282                if (MMAP_START[x][y][l] < min_load_start)  min_load_start = MMAP_START[x][y][l];
283                if (MMAP_START[x][y][l] > max_load_start)  max_load_start = MMAP_START[x][y][l];
284                if (MMAP_END[x][y][l]   < min_load_ended)  min_load_ended = MMAP_END[x][y][l]; 
285                if (MMAP_END[x][y][l]   > max_load_ended)  max_load_ended = MMAP_END[x][y][l];
286                if (TRSP_START[x][y][l] < min_trsp_start)  min_trsp_start = TRSP_START[x][y][l];
287                if (TRSP_START[x][y][l] > max_trsp_start)  max_trsp_start = TRSP_START[x][y][l];
288                if (TRSP_END[x][y][l]   < min_trsp_ended)  min_trsp_ended = TRSP_END[x][y][l];
289                if (TRSP_END[x][y][l]   > max_trsp_ended)  max_trsp_ended = TRSP_END[x][y][l];
290                if (DISP_START[x][y][l] < min_disp_start)  min_disp_start = DISP_START[x][y][l];
291                if (DISP_START[x][y][l] > max_disp_start)  max_disp_start = DISP_START[x][y][l];
292                if (DISP_END[x][y][l]   < min_disp_ended)  min_disp_ended = DISP_END[x][y][l];
293                if (DISP_END[x][y][l]   > max_disp_ended)  max_disp_ended = DISP_END[x][y][l];
294            }
295        }
296    }
297
298    printf("\n   ---------------- Instrumentation Results ---------------------\n");
299
300    printf(" - MMAP_START : min = %d / max = %d / med = %d / delta = %d\n",
301           min_load_start, max_load_start, (min_load_start+max_load_start)/2, 
302           max_load_start-min_load_start); 
303
304    printf(" - MMAP_END   : min = %d / max = %d / med = %d / delta = %d\n",
305           min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, 
306           max_load_ended-min_load_ended); 
307
308    printf(" - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n",
309           min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, 
310           max_trsp_start-min_trsp_start); 
311
312    printf(" - TRSP_END   : min = %d / max = %d / med = %d / delta = %d\n",
313           min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, 
314           max_trsp_ended-min_trsp_ended); 
315
316    printf(" - DISP_START : min = %d / max = %d / med = %d / delta = %d\n",
317           min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, 
318           max_disp_start-min_disp_start); 
319
320    printf(" - DISP_END   : min = %d / max = %d / med = %d / delta = %d\n",
321           min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, 
322           max_disp_ended-min_disp_ended); 
323
324}  // end instrument()
325
326
327
328//////////////////////////////////////////
329__attribute__ ((constructor)) void main()
330//////////////////////////////////////////
331{
332    // indexes for loops
333    unsigned int x , y , n;
334
335    // get identifiers for proc executing main
336    unsigned int x_id;                          // x cluster coordinate
337    unsigned int y_id;                          // y cluster coordinate
338    unsigned int p_id;                          // local processor index
339    giet_proc_xyp( &x_id , &y_id , &p_id );
340
341    // get & check plat-form parameters
342    unsigned int x_size;                       // number of clusters in a row
343    unsigned int y_size;                       // number of clusters in a column
344    unsigned int nprocs;                       // number of processors per cluster
345    giet_procs_number( &x_size , &y_size , &nprocs );
346
347    giet_pthread_assert( ((nprocs == 1) || (nprocs == 2) || (nprocs == 4)),
348                         "[TRANSPOSE ERROR] number of procs per cluster must be 1, 2 or 4");
349
350    giet_pthread_assert( ((x_size == 1) || (x_size == 2) || (x_size == 4) || 
351                          (x_size == 8) || (x_size == 16)),
352                         "[TRANSPOSE ERROR] x_size must be 1,2,4,8,16");
353
354    giet_pthread_assert( ((y_size == 1) || (y_size == 2) || (y_size == 4) || 
355                          (y_size == 8) || (y_size == 16)),
356                         "[TRANSPOSE ERROR] y_size must be 1,2,4,8,16");
357
358    // compute number of threads
359    unsigned int nthreads = x_size * y_size * nprocs;
360
361    // shared TTY allocation
362    giet_tty_alloc( 1 );     
363    lock_init( &tty_lock);
364
365    // get FBF ownership and FBF size
366    unsigned int   width;
367    unsigned int   height;
368    giet_fbf_alloc();
369    giet_fbf_size( &width , &height );
370
371    printf("\n[TRANSPOSE] start at cycle %d on %d cores / FBF = %d * %d pixels\n",
372           giet_proctime(), nthreads , width , height );
373
374    if ( INTERACTIVE ) // input_file_name, output_file_name, and size  acquisition
375    {
376        printf("\n[TRANSPOSE] enter path for input file / default is : %s\n> ", INPUT_FILE_PATH ); 
377        giet_tty_gets( input_file_name , 256 );
378        printf("\n");
379        if ( strcmp( input_file_name , "" ) == 0 ) strcpy( input_file_name , INPUT_FILE_PATH );
380
381        printf("\n[TRANSPOSE] enter path for output file / default is : %s\n> ", OUTPUT_FILE_PATH ); 
382        giet_tty_gets( output_file_name , 256 );
383        printf("\n");
384        if ( strcmp( output_file_name , "" ) == 0 ) strcpy( output_file_name , OUTPUT_FILE_PATH );
385
386        printf("\n[TRANSPOSE] enter image size / default is : %d\n> ", IMAGE_SIZE ); 
387        giet_tty_getw( &image_size );
388        printf("\n");
389        if ( image_size == 0 ) image_size = IMAGE_SIZE;
390    }
391    else
392    {
393        strcpy( input_file_name , INPUT_FILE_PATH );
394        strcpy( output_file_name , OUTPUT_FILE_PATH );
395        image_size = IMAGE_SIZE;
396    }
397
398    // check image size / number of clusters
399    giet_pthread_assert( ((((image_size * image_size) / (x_size * y_size)) & 0xFFF) == 0) ,
400                         "[TRANSPOSE ERROR] pixels per cluster must be multiple of 4096");
401   
402    printf("\n[TRANSPOSE] input = %s / output = %s / size = %d\n",
403           input_file_name, output_file_name, image_size );
404
405    // distributed heap initialisation
406    for ( x = 0 ; x < x_size ; x++ ) 
407    {
408        for ( y = 0 ; y < y_size ; y++ ) 
409        {
410            heap_init( x , y );
411        }
412    }
413
414    // open input and output files
415    fd_in = giet_fat_open( input_file_name , O_RDONLY );  // read_only
416    if ( fd_in < 0 ) 
417    { 
418        printf("\n[TRANSPOSE ERROR] main cannot open file %s\n", input_file_name );
419        giet_pthread_exit( NULL );
420    }
421    else 
422    {
423        printf("\n[TRANSPOSE] main open file %s / fd = %d\n", input_file_name , fd_in );
424    }
425
426    fd_out = giet_fat_open( output_file_name , O_CREATE );   // create if required
427    if ( fd_out < 0 ) 
428    { 
429        printf("\n[TRANSPOSE ERROR] main cannot open file %s\n", output_file_name );
430        giet_pthread_exit(" open() failure");
431    }
432    else
433    {
434        printf("\n[TRANSPOSE] main open file %s / fd = %d\n", output_file_name , fd_out );
435    }
436
437    // allocate thread[] array
438    pthread_t* thread = malloc( nthreads * sizeof(pthread_t) );
439
440    // barrier initialisation
441    sqt_barrier_init( &barrier, x_size , y_size , nprocs );
442
443    // Initialisation completed
444    printf("\n[TRANSPOSE] main completes initialisation\n");
445   
446    // launch other threads to run execute() function
447    for ( n = 1 ; n < nthreads ; n++ )
448    {
449        if ( giet_pthread_create( &thread[n],
450                                  NULL,                  // no attribute
451                                  &execute,
452                                  NULL ) )               // no argument
453        {
454            printf("\n[TRANSPOSE ERROR] creating thread %x\n", thread[n] );
455            giet_pthread_exit( NULL );
456        }
457    }
458
459    // run the execute() function
460    execute();
461
462    // wait other threads completion
463    for ( n = 1 ; n < nthreads ; n++ )
464    {
465        if ( giet_pthread_join( thread[n], NULL ) )
466        {
467            printf("\n[TRANSPOSE ERROR] joining thread %x\n", thread[n] );
468            giet_pthread_exit( NULL );
469        }
470        else
471        {
472            printf("\n[TRANSPOSE] thread %x joined at cycle %d\n",
473                   thread[n] , giet_proctime() );
474        }
475    }
476
477    // call the instrument() function
478    instrument( x_size , y_size , nprocs );
479
480    // close input and output files
481    giet_fat_close( fd_in );
482    giet_fat_close( fd_out );
483
484    // suicide
485    giet_pthread_exit( "completed" );
486   
487} // end main()
488
Note: See TracBrowser for help on using the repository browser.