source: soft/giet_vm/applications/transpose/main.c @ 661

Last change on this file since 661 was 661, checked in by guerin, 9 years ago

remove deprecated giet_fat_list()

File size: 22.1 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////////
2// File   : main.c   (for transpose application)
3// Date   : february 2014
4// author : Alain Greiner
5///////////////////////////////////////////////////////////////////////////////////////
6// This multi-threaded application makes a transpose for a NN*NN pixels image.
7// It can run on a multi-processors, multi-clusters architecture, with one thread
8// per processor.
9//
10// The image is read from a file (one byte per pixel), transposed and
11// saved in a second file. Then the transposed image is read from the second file,
12// transposed again and saved in a third file.
13//
14// The input and output buffers containing the image are distributed in all clusters.
15//
16// - The image size NN must fit the frame buffer size.
17// - The block size in block device must be 512 bytes.
18// - The number of clusters  must be a power of 2 no larger than 64.
19// - The number of processors per cluster must be a power of 2 no larger than 4.
20//
21// For each image the application makes a self test (checksum for each line).
22// The actual display on the frame buffer depends on frame buffer availability.
23///////////////////////////////////////////////////////////////////////////////////////
24
25#include "stdio.h"
26#include "user_barrier.h"
27#include "malloc.h"
28
29#define BLOCK_SIZE            512                         // block size on disk
30#define X_MAX                 8                           // max number of clusters in row
31#define Y_MAX                 8                           // max number of clusters in column
32#define PROCS_MAX             4                           // max number of procs per cluster
33#define CLUSTER_MAX           (X_MAX * Y_MAX)             // max number of clusters
34#define NN                    256                         // image size : nlines = npixels
35#define INITIAL_FILE_PATH     "misc/lena.raw"             // pathname on virtual disk
36#define TRANSPOSED_FILE_PATH  "/home/lena_transposed.raw" // pathname on virtual disk
37#define RESTORED_FILE_PATH    "/home/lena_restored.raw"   // pathname on virtual disk
38#define INSTRUMENTATION_OK    1                           // display statistics on TTY
39
40///////////////////////////////////////////////////////
41// global variables stored in seg_data in cluster(0,0)
42///////////////////////////////////////////////////////
43
44// instrumentation counters for each processor in each cluster
45unsigned int LOAD_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
46unsigned int LOAD_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
47unsigned int TRSP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
48unsigned int TRSP_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
49unsigned int DISP_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
50unsigned int DISP_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
51unsigned int STOR_START[X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
52unsigned int STOR_END  [X_MAX][Y_MAX][PROCS_MAX] = {{{ 0 }}};
53
54// arrays of pointers on distributed buffers
55// one input buffer & one output buffer per cluster
56unsigned char*  buf_in [CLUSTER_MAX];
57unsigned char*  buf_out[CLUSTER_MAX];
58
59// checksum variables
60unsigned check_line_before[NN];
61unsigned check_line_after[NN];
62
63// global synchronisation barrier
64giet_sqt_barrier_t barrier;
65
66volatile unsigned int global_init_ok = 0;
67volatile unsigned int local_init_ok[X_MAX][Y_MAX] = {{ 0 }};
68
69//////////////////////////////////////////
70__attribute__ ((constructor)) void main()
71//////////////////////////////////////////
72{
73    unsigned int l;                  // line index for loops
74    unsigned int p;                  // pixel index for loops
75
76    // processor identifiers
77    unsigned int x;                  // x cluster coordinate
78    unsigned int y;                  // y cluster coordinate
79    unsigned int lpid;               // local processor index
80
81    // plat-form parameters
82    unsigned int x_size;             // number of clusters in a row
83    unsigned int y_size;             // number of clusters in a column
84    unsigned int nprocs;             // number of processors per cluster
85   
86    giet_proc_xyp( &x, &y, &lpid);             
87
88    giet_procs_number( &x_size , &y_size , &nprocs );
89
90    unsigned int nclusters     = x_size * y_size;               // number of clusters
91    unsigned int ntasks        = x_size * y_size * nprocs;      // number of tasks
92    unsigned int npixels       = NN * NN;                       // pixels per image
93    unsigned int iteration     = 0;                             // iiteration iter
94    int          fd_initial    = 0;                             // initial file descriptor
95    int          fd_transposed = 0;                             // transposed file descriptor
96    int          fd_restored   = 0;                             // restored file descriptor
97    unsigned int cluster_id    = (x * y_size) + y;              // "continuous" index   
98    unsigned int task_id       = (cluster_id * nprocs) + lpid;  // "continuous" task index
99
100
101    ///////////////////////////////////////////////////////////////////////
102    // Processor [0,0,0] makes global initialisation
103    // It includes parameters checking, heap and barrier initialization.
104    // Others processors wait initialisation completion
105    ///////////////////////////////////////////////////////////////////////
106
107    if ( (x==0) && (y==0) && (lpid==0) )
108    {
109        if ((nprocs != 1) && (nprocs != 2) && (nprocs != 4))
110        { 
111            giet_exit("[TRANSPOSE ERROR] number of procs per cluster must be 1, 2 or 4");
112        }
113        if ((nclusters != 1) && (nclusters != 2) && (nclusters != 4) && 
114            (nclusters != 8) && (nclusters != 16) && (nclusters != 32) && (nclusters != 64) )
115        {
116            giet_exit("[TRANSPOSE ERROR] number of clusters must be 1,2,4,8,16,32,64");
117        }
118        if ( ntasks > NN )
119        {
120            giet_exit("[TRANSPOSE ERROR] number of tasks larger than number of lines");
121        }
122
123        // distributed heap initialisation
124        unsigned int cx , cy;
125        for ( cx = 0 ; cx < x_size ; cx++ ) 
126        {
127            for ( cy = 0 ; cy < y_size ; cy++ ) 
128            {
129                heap_init( cx , cy );
130            }
131        }
132
133        // barrier initialisation
134        sqt_barrier_init( &barrier, x_size , y_size , nprocs );
135
136        giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] completes heap & barrier init at cycle %d\n",
137                        giet_proctime() );
138
139        global_init_ok = 1;
140    }
141    else 
142    {
143        while ( global_init_ok == 0 );
144    }
145   
146    ///////////////////////////////////////////////////////////////////////
147    // In each cluster, only task running on processor[x,y,0] allocates
148    // the local buffers containing the images in the distributed heap
149    // (one buf_in and one buf_out per cluster).
150    // Other processors in cluster wait completion.
151    ///////////////////////////////////////////////////////////////////////
152
153    if ( lpid == 0 ) 
154    {
155        buf_in[cluster_id]  = remote_malloc( npixels/nclusters, x, y );
156        buf_out[cluster_id] = remote_malloc( npixels/nclusters, x, y );
157
158        if ( (x==0) && (y==0) )
159        giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes buffer allocation"
160                        " for cluster[%d,%d] at cycle %d\n"
161                        " - buf_in  = %x\n"
162                        " - buf_out = %x\n",
163                        x, y, lpid, x, y, giet_proctime(), 
164                        (unsigned int)buf_in[cluster_id], (unsigned int)buf_out[cluster_id] );
165
166        ///////////////////////////////////////////////////////////////////////
167        // In each cluster, only task running on procesor[x,y,0] open the
168        // three private file descriptors for the three files
169        ///////////////////////////////////////////////////////////////////////
170
171        // open initial file
172        fd_initial = giet_fat_open( INITIAL_FILE_PATH , O_RDONLY );  // read_only
173        if ( fd_initial < 0 ) 
174        { 
175            giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot open file %s\n",
176                            x , y , lpid , INITIAL_FILE_PATH );
177            giet_exit(" open() failure");
178        }
179        else if ( (x==0) && (y==0) && (lpid==0) )
180        {
181            giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file %s / fd = %d\n",
182                            INITIAL_FILE_PATH , fd_initial );
183        }
184
185        // open transposed file
186        fd_transposed = giet_fat_open( TRANSPOSED_FILE_PATH , O_CREATE );   // create if required
187        if ( fd_transposed < 0 ) 
188        { 
189            giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot open file %s\n",
190                            x , y , lpid , TRANSPOSED_FILE_PATH );
191            giet_exit(" open() failure");
192        }
193        else if ( (x==0) && (y==0) && (lpid==0) )
194        {
195            giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file %s / fd = %d\n",
196                            TRANSPOSED_FILE_PATH , fd_transposed );
197        }
198
199        // open restored file
200        fd_restored = giet_fat_open( RESTORED_FILE_PATH , O_CREATE );   // create if required
201        if ( fd_restored < 0 ) 
202        { 
203            giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot open file %s\n",
204                            x , y , lpid , RESTORED_FILE_PATH );
205            giet_exit(" open() failure");
206        }
207        else if ( (x==0) && (y==0) && (lpid==0) )
208        {
209            giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file %s / fd = %d\n",
210                            RESTORED_FILE_PATH , fd_restored );
211        }
212
213        local_init_ok[x][y] = 1;
214    }
215    else
216    {
217        while( local_init_ok[x][y] == 0 );
218    }
219
220    ///////////////////////////////////////////////////////////////////////
221    // Main loop / two iterations:
222    // - first makes  initial    => transposed
223    // - second makes transposed => restored
224    // All processors execute this main loop.
225    ///////////////////////////////////////////////////////////////////////
226
227    unsigned int fd_in  = fd_initial;
228    unsigned int fd_out = fd_transposed;
229
230    while (iteration < 2)
231    {
232        ///////////////////////////////////////////////////////////////////////
233        // pseudo parallel load from disk to buf_in buffers: npixels/nclusters
234        // only task running on processor(x,y,0) does it
235        ///////////////////////////////////////////////////////////////////////
236
237        LOAD_START[x][y][lpid] = giet_proctime();
238
239        if (lpid == 0)
240        {
241            unsigned int offset = ((npixels*cluster_id)/nclusters);
242            if ( giet_fat_lseek( fd_in,
243                                 offset,
244                                 SEEK_SET ) != offset )
245            {
246                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot seek fd = %d\n",
247                                x , y , lpid , fd_in );
248                giet_exit(" seek() failure");
249            }
250
251            unsigned int pixels = npixels / nclusters;
252            if ( giet_fat_read( fd_in,
253                                buf_in[cluster_id],
254                                pixels ) != pixels )
255            {
256                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot read fd = %d\n",
257                                x , y , lpid , fd_in );
258                giet_exit(" read() failure");
259            }
260
261            if ( (x==0) && (y==0) )
262            giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes load"
263                            "  for iteration %d at cycle %d\n",
264                            x, y, lpid, iteration, giet_proctime() );
265        }
266
267        LOAD_END[x][y][lpid] = giet_proctime();
268
269        /////////////////////////////
270        sqt_barrier_wait( &barrier );
271
272        ///////////////////////////////////////////////////////////////////////
273        // parallel transpose from buf_in to buf_out
274        // each task makes the transposition for nlt lines (nlt = NN/ntasks)
275        // from line [task_id*nlt] to line [(task_id + 1)*nlt - 1]
276        // (p,l) are the absolute pixel coordinates in the source image
277        ///////////////////////////////////////////////////////////////////////
278
279        TRSP_START[x][y][lpid] = giet_proctime();
280
281        unsigned int nlt   = NN / ntasks;      // number of lines per task
282        unsigned int nlc   = NN / nclusters;   // number of lines per cluster
283
284        unsigned int src_cluster;
285        unsigned int src_index;
286        unsigned int dst_cluster;
287        unsigned int dst_index;
288
289        unsigned char byte;
290
291        unsigned int first = task_id * nlt;    // first line index for a given task
292        unsigned int last  = first + nlt;      // last line index for a given task
293
294        for ( l = first ; l < last ; l++ )
295        {
296            check_line_before[l] = 0;
297         
298            // in each iteration we transfer one byte
299            for ( p = 0 ; p < NN ; p++ )
300            {
301                // read one byte from local buf_in
302                src_cluster = l / nlc;
303                src_index   = (l % nlc)*NN + p;
304                byte        = buf_in[src_cluster][src_index];
305
306                // compute checksum
307                check_line_before[l] = check_line_before[l] + byte;
308
309                // write one byte to remote buf_out
310                dst_cluster = p / nlc; 
311                dst_index   = (p % nlc)*NN + l;
312                buf_out[dst_cluster][dst_index] = byte;
313            }
314        }
315
316        if ( lpid == 0 )
317        {
318            if ( (x==0) && (y==0) )
319            giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] completes transpose"
320                            " for iteration %d at cycle %d\n", 
321                            x, y, iteration, giet_proctime() );
322
323        }
324        TRSP_END[x][y][lpid] = giet_proctime();
325
326        /////////////////////////////
327        sqt_barrier_wait( &barrier );
328
329        ///////////////////////////////////////////////////////////////////////
330        // parallel display from local buf_out to frame buffer
331        // all tasks contribute to display using memcpy...
332        ///////////////////////////////////////////////////////////////////////
333
334        DISP_START[x][y][lpid] = giet_proctime();
335
336        unsigned int  npt   = npixels / ntasks;   // number of pixels per task
337
338        giet_fbf_sync_write( npt * task_id, 
339                             &buf_out[cluster_id][lpid*npt], 
340                             npt );
341
342        if ( (x==0) && (y==0) && (lpid==0) )
343        giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes display"
344                        " for iteration %d at cycle %d\n",
345                        x, y, lpid, iteration, giet_proctime() );
346
347        DISP_END[x][y][lpid] = giet_proctime();
348
349        /////////////////////////////
350        sqt_barrier_wait( &barrier );
351
352        ///////////////////////////////////////////////////////////////////////
353        // pseudo parallel store : buf_out buffers to disk : npixels/nclusters
354        // only task running on processor(x,y,0) does it
355        ///////////////////////////////////////////////////////////////////////
356
357        STOR_START[x][y][lpid] = giet_proctime();
358
359        if ( lpid == 0 )
360        {
361            unsigned int offset = ((npixels*cluster_id)/nclusters);
362            if ( giet_fat_lseek( fd_out,
363                                 offset,
364                                 SEEK_SET ) != offset )
365            {
366                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot seek fr = %d\n",
367                                x , y , lpid , fd_out );
368                giet_exit(" seek() failure");
369            }
370
371            unsigned int pixels = npixels / nclusters;
372            if ( giet_fat_write( fd_out,
373                                 buf_out[cluster_id],
374                                 pixels ) != pixels )
375            {
376                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot write fd = %d\n",
377                                x , y , lpid , fd_out );
378                giet_exit(" write() failure");
379            }
380
381            if ( (x==0) && (y==0) )
382            giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes store"
383                            "  for iteration %d at cycle %d\n",
384                            x, y, lpid, iteration, giet_proctime() );
385        }
386
387        STOR_END[x][y][lpid] = giet_proctime();
388
389        /////////////////////////////
390        sqt_barrier_wait( &barrier );
391
392        // instrumentation done by processor [0,0,0]
393        if ( (x==0) && (y==0) && (lpid==0) && INSTRUMENTATION_OK )
394        {
395            int cx , cy , pp ;
396            unsigned int min_load_start = 0xFFFFFFFF;
397            unsigned int max_load_start = 0;
398            unsigned int min_load_ended = 0xFFFFFFFF;
399            unsigned int max_load_ended = 0;
400            unsigned int min_trsp_start = 0xFFFFFFFF;
401            unsigned int max_trsp_start = 0;
402            unsigned int min_trsp_ended = 0xFFFFFFFF;
403            unsigned int max_trsp_ended = 0;
404            unsigned int min_disp_start = 0xFFFFFFFF;
405            unsigned int max_disp_start = 0;
406            unsigned int min_disp_ended = 0xFFFFFFFF;
407            unsigned int max_disp_ended = 0;
408            unsigned int min_stor_start = 0xFFFFFFFF;
409            unsigned int max_stor_start = 0;
410            unsigned int min_stor_ended = 0xFFFFFFFF;
411            unsigned int max_stor_ended = 0;
412
413            for (cx = 0; cx < x_size; cx++)
414            {
415            for (cy = 0; cy < y_size; cy++)
416            {
417            for (pp = 0; pp < NB_PROCS_MAX; pp++)
418            {
419                if (LOAD_START[cx][cy][pp] < min_load_start)  min_load_start = LOAD_START[cx][cy][pp];
420                if (LOAD_START[cx][cy][pp] > max_load_start)  max_load_start = LOAD_START[cx][cy][pp];
421                if (LOAD_END[cx][cy][pp]   < min_load_ended)  min_load_ended = LOAD_END[cx][cy][pp]; 
422                if (LOAD_END[cx][cy][pp]   > max_load_ended)  max_load_ended = LOAD_END[cx][cy][pp];
423                if (TRSP_START[cx][cy][pp] < min_trsp_start)  min_trsp_start = TRSP_START[cx][cy][pp];
424                if (TRSP_START[cx][cy][pp] > max_trsp_start)  max_trsp_start = TRSP_START[cx][cy][pp];
425                if (TRSP_END[cx][cy][pp]   < min_trsp_ended)  min_trsp_ended = TRSP_END[cx][cy][pp];
426                if (TRSP_END[cx][cy][pp]   > max_trsp_ended)  max_trsp_ended = TRSP_END[cx][cy][pp];
427                if (DISP_START[cx][cy][pp] < min_disp_start)  min_disp_start = DISP_START[cx][cy][pp];
428                if (DISP_START[cx][cy][pp] > max_disp_start)  max_disp_start = DISP_START[cx][cy][pp];
429                if (DISP_END[cx][cy][pp]   < min_disp_ended)  min_disp_ended = DISP_END[cx][cy][pp];
430                if (DISP_END[cx][cy][pp]   > max_disp_ended)  max_disp_ended = DISP_END[cx][cy][pp];
431                if (STOR_START[cx][cy][pp] < min_stor_start)  min_stor_start = STOR_START[cx][cy][pp];
432                if (STOR_START[cx][cy][pp] > max_stor_start)  max_stor_start = STOR_START[cx][cy][pp];
433                if (STOR_END[cx][cy][pp]   < min_stor_ended)  min_stor_ended = STOR_END[cx][cy][pp];
434                if (STOR_END[cx][cy][pp]   > max_stor_ended)  max_stor_ended = STOR_END[cx][cy][pp];
435            }
436            }
437            }
438
439            giet_shr_printf("\n   ---------------- Instrumentation Results ---------------------\n");
440
441            giet_shr_printf(" - LOAD_START : min = %d / max = %d / med = %d / delta = %d\n",
442                            min_load_start, max_load_start, (min_load_start+max_load_start)/2, 
443                            max_load_start-min_load_start); 
444
445            giet_shr_printf(" - LOAD_END   : min = %d / max = %d / med = %d / delta = %d\n",
446                            min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, 
447                            max_load_ended-min_load_ended); 
448
449            giet_shr_printf(" - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n",
450                            min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, 
451                            max_trsp_start-min_trsp_start); 
452
453            giet_shr_printf(" - TRSP_END   : min = %d / max = %d / med = %d / delta = %d\n",
454                            min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, 
455                            max_trsp_ended-min_trsp_ended); 
456
457            giet_shr_printf(" - DISP_START : min = %d / max = %d / med = %d / delta = %d\n",
458                            min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, 
459                            max_disp_start-min_disp_start); 
460
461            giet_shr_printf(" - DISP_END   : min = %d / max = %d / med = %d / delta = %d\n",
462                            min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, 
463                            max_disp_ended-min_disp_ended); 
464
465            giet_shr_printf(" - STOR_START : min = %d / max = %d / med = %d / delta = %d\n",
466                            min_stor_start, max_stor_start, (min_stor_start+max_stor_start)/2, 
467                            max_stor_start-min_stor_start); 
468
469            giet_shr_printf(" - STOR_END   : min = %d / max = %d / med = %d / delta = %d\n",
470                            min_stor_ended, max_stor_ended, (min_stor_ended+max_stor_ended)/2, 
471                            max_stor_ended-min_stor_ended); 
472        }
473
474        /////////////////////////////
475        sqt_barrier_wait( &barrier );
476
477        // update iteration variables
478        fd_in  = fd_transposed;
479        fd_out = fd_restored;
480        iteration++;
481
482    } // end while     
483
484    ///////////////////////////////////////////////////////////////////////
485    // In each cluster, only task running on Processor[x,y,0] releases
486    // the distributed buffers and close the file descriptors.
487    ///////////////////////////////////////////////////////////////////////
488
489    if ( lpid==0 )
490    {
491        free( buf_in[cluster_id] );
492        free( buf_out[cluster_id] );
493
494        giet_fat_close( fd_initial );
495        giet_fat_close( fd_transposed );
496        giet_fat_close( fd_restored );
497    }
498
499    // clean up
500    if ( (x==0) && (y == 0) && (lpid == 0) )
501    { 
502        giet_fat_remove( "/home/lena_transposed" , 0 );
503        giet_fat_remove( "/home/lena_restored" , 0 );
504
505        giet_fat_remove( "/home" , 1 );
506    }
507   
508    giet_exit("Completed");
509
510} // end main()
511
512// Local Variables:
513// tab-width: 3
514// c-basic-offset:
515// c-file-offsets:((innamespace . 0)(inline-open . 0))
516// indent-tabs-mode: nil
517// End:
518
519// vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3
520
521
522
Note: See TracBrowser for help on using the repository browser.