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

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

Modify all applications to support two new rules:
1) introduce a local Makefile for each application.
2) change "application.elf" name to "application/appli.elf" name in the application.py" file.
Introduce the shell application.

File size: 22.8 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        // diplay disk content
140        giet_fat_list( "/" );
141        giet_fat_list( "/misc" );
142        giet_fat_list( "/home" );
143        giet_fat_list( "/build" );
144        giet_fat_list( "/build/kernel" );
145        giet_fat_list( "/build/transpose" );
146
147        global_init_ok = 1;
148    }
149    else 
150    {
151        while ( global_init_ok == 0 );
152    }
153   
154    ///////////////////////////////////////////////////////////////////////
155    // In each cluster, only task running on processor[x,y,0] allocates
156    // the local buffers containing the images in the distributed heap
157    // (one buf_in and one buf_out per cluster).
158    // Other processors in cluster wait completion.
159    ///////////////////////////////////////////////////////////////////////
160
161    if ( lpid == 0 ) 
162    {
163        buf_in[cluster_id]  = remote_malloc( npixels/nclusters, x, y );
164        buf_out[cluster_id] = remote_malloc( npixels/nclusters, x, y );
165
166        if ( (x==0) && (y==0) )
167        giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes buffer allocation"
168                        " for cluster[%d,%d] at cycle %d\n"
169                        " - buf_in  = %x\n"
170                        " - buf_out = %x\n",
171                        x, y, lpid, x, y, giet_proctime(), 
172                        (unsigned int)buf_in[cluster_id], (unsigned int)buf_out[cluster_id] );
173
174        ///////////////////////////////////////////////////////////////////////
175        // In each cluster, only task running on procesor[x,y,0] open the
176        // three private file descriptors for the three files
177        ///////////////////////////////////////////////////////////////////////
178
179        // open initial file
180        fd_initial = giet_fat_open( INITIAL_FILE_PATH , O_RDONLY );  // read_only
181        if ( fd_initial < 0 ) 
182        { 
183            giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot open file %s\n",
184                            x , y , lpid , INITIAL_FILE_PATH );
185            giet_exit(" open() failure");
186        }
187        else if ( (x==0) && (y==0) && (lpid==0) )
188        {
189            giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file %s / fd = %d\n",
190                            INITIAL_FILE_PATH , fd_initial );
191        }
192
193        // open transposed file
194        fd_transposed = giet_fat_open( TRANSPOSED_FILE_PATH , O_CREATE );   // create if required
195        if ( fd_transposed < 0 ) 
196        { 
197            giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot open file %s\n",
198                            x , y , lpid , TRANSPOSED_FILE_PATH );
199            giet_exit(" open() failure");
200        }
201        else if ( (x==0) && (y==0) && (lpid==0) )
202        {
203            giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file %s / fd = %d\n",
204                            TRANSPOSED_FILE_PATH , fd_transposed );
205        }
206
207        // open restored file
208        fd_restored = giet_fat_open( RESTORED_FILE_PATH , O_CREATE );   // create if required
209        if ( fd_restored < 0 ) 
210        { 
211            giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot open file %s\n",
212                            x , y , lpid , RESTORED_FILE_PATH );
213            giet_exit(" open() failure");
214        }
215        else if ( (x==0) && (y==0) && (lpid==0) )
216        {
217            giet_shr_printf("\n[TRANSPOSE] Proc [0,0,0] open file %s / fd = %d\n",
218                            RESTORED_FILE_PATH , fd_restored );
219        }
220
221        local_init_ok[x][y] = 1;
222    }
223    else
224    {
225        while( local_init_ok[x][y] == 0 );
226    }
227
228    ///////////////////////////////////////////////////////////////////////
229    // Main loop / two iterations:
230    // - first makes  initial    => transposed
231    // - second makes transposed => restored
232    // All processors execute this main loop.
233    ///////////////////////////////////////////////////////////////////////
234
235    unsigned int fd_in  = fd_initial;
236    unsigned int fd_out = fd_transposed;
237
238    while (iteration < 2)
239    {
240        ///////////////////////////////////////////////////////////////////////
241        // pseudo parallel load from disk to buf_in buffers: npixels/nclusters
242        // only task running on processor(x,y,0) does it
243        ///////////////////////////////////////////////////////////////////////
244
245        LOAD_START[x][y][lpid] = giet_proctime();
246
247        if (lpid == 0)
248        {
249            unsigned int offset = ((npixels*cluster_id)/nclusters);
250            if ( giet_fat_lseek( fd_in,
251                                 offset,
252                                 SEEK_SET ) != offset )
253            {
254                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot seek fd = %d\n",
255                                x , y , lpid , fd_in );
256                giet_exit(" seek() failure");
257            }
258
259            unsigned int pixels = npixels / nclusters;
260            if ( giet_fat_read( fd_in,
261                                buf_in[cluster_id],
262                                pixels ) != pixels )
263            {
264                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot read fd = %d\n",
265                                x , y , lpid , fd_in );
266                giet_exit(" read() failure");
267            }
268
269            if ( (x==0) && (y==0) )
270            giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes load"
271                            "  for iteration %d at cycle %d\n",
272                            x, y, lpid, iteration, giet_proctime() );
273        }
274
275        LOAD_END[x][y][lpid] = giet_proctime();
276
277        /////////////////////////////
278        sqt_barrier_wait( &barrier );
279
280        ///////////////////////////////////////////////////////////////////////
281        // parallel transpose from buf_in to buf_out
282        // each task makes the transposition for nlt lines (nlt = NN/ntasks)
283        // from line [task_id*nlt] to line [(task_id + 1)*nlt - 1]
284        // (p,l) are the absolute pixel coordinates in the source image
285        ///////////////////////////////////////////////////////////////////////
286
287        TRSP_START[x][y][lpid] = giet_proctime();
288
289        unsigned int nlt   = NN / ntasks;      // number of lines per task
290        unsigned int nlc   = NN / nclusters;   // number of lines per cluster
291
292        unsigned int src_cluster;
293        unsigned int src_index;
294        unsigned int dst_cluster;
295        unsigned int dst_index;
296
297        unsigned char byte;
298
299        unsigned int first = task_id * nlt;    // first line index for a given task
300        unsigned int last  = first + nlt;      // last line index for a given task
301
302        for ( l = first ; l < last ; l++ )
303        {
304            check_line_before[l] = 0;
305         
306            // in each iteration we transfer one byte
307            for ( p = 0 ; p < NN ; p++ )
308            {
309                // read one byte from local buf_in
310                src_cluster = l / nlc;
311                src_index   = (l % nlc)*NN + p;
312                byte        = buf_in[src_cluster][src_index];
313
314                // compute checksum
315                check_line_before[l] = check_line_before[l] + byte;
316
317                // write one byte to remote buf_out
318                dst_cluster = p / nlc; 
319                dst_index   = (p % nlc)*NN + l;
320                buf_out[dst_cluster][dst_index] = byte;
321            }
322        }
323
324        if ( lpid == 0 )
325        {
326            if ( (x==0) && (y==0) )
327            giet_shr_printf("\n[TRANSPOSE] proc [%d,%d,0] completes transpose"
328                            " for iteration %d at cycle %d\n", 
329                            x, y, iteration, giet_proctime() );
330
331        }
332        TRSP_END[x][y][lpid] = giet_proctime();
333
334        /////////////////////////////
335        sqt_barrier_wait( &barrier );
336
337        ///////////////////////////////////////////////////////////////////////
338        // parallel display from local buf_out to frame buffer
339        // all tasks contribute to display using memcpy...
340        ///////////////////////////////////////////////////////////////////////
341
342        DISP_START[x][y][lpid] = giet_proctime();
343
344        unsigned int  npt   = npixels / ntasks;   // number of pixels per task
345
346        giet_fbf_sync_write( npt * task_id, 
347                             &buf_out[cluster_id][lpid*npt], 
348                             npt );
349
350        if ( (x==0) && (y==0) && (lpid==0) )
351        giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes display"
352                        " for iteration %d at cycle %d\n",
353                        x, y, lpid, iteration, giet_proctime() );
354
355        DISP_END[x][y][lpid] = giet_proctime();
356
357        /////////////////////////////
358        sqt_barrier_wait( &barrier );
359
360        ///////////////////////////////////////////////////////////////////////
361        // pseudo parallel store : buf_out buffers to disk : npixels/nclusters
362        // only task running on processor(x,y,0) does it
363        ///////////////////////////////////////////////////////////////////////
364
365        STOR_START[x][y][lpid] = giet_proctime();
366
367        if ( lpid == 0 )
368        {
369            unsigned int offset = ((npixels*cluster_id)/nclusters);
370            if ( giet_fat_lseek( fd_out,
371                                 offset,
372                                 SEEK_SET ) != offset )
373            {
374                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot seek fr = %d\n",
375                                x , y , lpid , fd_out );
376                giet_exit(" seek() failure");
377            }
378
379            unsigned int pixels = npixels / nclusters;
380            if ( giet_fat_write( fd_out,
381                                 buf_out[cluster_id],
382                                 pixels ) != pixels )
383            {
384                giet_shr_printf("\n[TRANSPOSE ERROR] Proc [%d,%d,%d] cannot write fd = %d\n",
385                                x , y , lpid , fd_out );
386                giet_exit(" write() failure");
387            }
388
389            if ( (x==0) && (y==0) )
390            giet_shr_printf("\n[TRANSPOSE] Proc [%d,%d,%d] completes store"
391                            "  for iteration %d at cycle %d\n",
392                            x, y, lpid, iteration, giet_proctime() );
393        }
394
395        STOR_END[x][y][lpid] = giet_proctime();
396
397        /////////////////////////////
398        sqt_barrier_wait( &barrier );
399
400        // instrumentation done by processor [0,0,0]
401        if ( (x==0) && (y==0) && (lpid==0) && INSTRUMENTATION_OK )
402        {
403            int cx , cy , pp ;
404            unsigned int min_load_start = 0xFFFFFFFF;
405            unsigned int max_load_start = 0;
406            unsigned int min_load_ended = 0xFFFFFFFF;
407            unsigned int max_load_ended = 0;
408            unsigned int min_trsp_start = 0xFFFFFFFF;
409            unsigned int max_trsp_start = 0;
410            unsigned int min_trsp_ended = 0xFFFFFFFF;
411            unsigned int max_trsp_ended = 0;
412            unsigned int min_disp_start = 0xFFFFFFFF;
413            unsigned int max_disp_start = 0;
414            unsigned int min_disp_ended = 0xFFFFFFFF;
415            unsigned int max_disp_ended = 0;
416            unsigned int min_stor_start = 0xFFFFFFFF;
417            unsigned int max_stor_start = 0;
418            unsigned int min_stor_ended = 0xFFFFFFFF;
419            unsigned int max_stor_ended = 0;
420
421            for (cx = 0; cx < x_size; cx++)
422            {
423            for (cy = 0; cy < y_size; cy++)
424            {
425            for (pp = 0; pp < NB_PROCS_MAX; pp++)
426            {
427                if (LOAD_START[cx][cy][pp] < min_load_start)  min_load_start = LOAD_START[cx][cy][pp];
428                if (LOAD_START[cx][cy][pp] > max_load_start)  max_load_start = LOAD_START[cx][cy][pp];
429                if (LOAD_END[cx][cy][pp]   < min_load_ended)  min_load_ended = LOAD_END[cx][cy][pp]; 
430                if (LOAD_END[cx][cy][pp]   > max_load_ended)  max_load_ended = LOAD_END[cx][cy][pp];
431                if (TRSP_START[cx][cy][pp] < min_trsp_start)  min_trsp_start = TRSP_START[cx][cy][pp];
432                if (TRSP_START[cx][cy][pp] > max_trsp_start)  max_trsp_start = TRSP_START[cx][cy][pp];
433                if (TRSP_END[cx][cy][pp]   < min_trsp_ended)  min_trsp_ended = TRSP_END[cx][cy][pp];
434                if (TRSP_END[cx][cy][pp]   > max_trsp_ended)  max_trsp_ended = TRSP_END[cx][cy][pp];
435                if (DISP_START[cx][cy][pp] < min_disp_start)  min_disp_start = DISP_START[cx][cy][pp];
436                if (DISP_START[cx][cy][pp] > max_disp_start)  max_disp_start = DISP_START[cx][cy][pp];
437                if (DISP_END[cx][cy][pp]   < min_disp_ended)  min_disp_ended = DISP_END[cx][cy][pp];
438                if (DISP_END[cx][cy][pp]   > max_disp_ended)  max_disp_ended = DISP_END[cx][cy][pp];
439                if (STOR_START[cx][cy][pp] < min_stor_start)  min_stor_start = STOR_START[cx][cy][pp];
440                if (STOR_START[cx][cy][pp] > max_stor_start)  max_stor_start = STOR_START[cx][cy][pp];
441                if (STOR_END[cx][cy][pp]   < min_stor_ended)  min_stor_ended = STOR_END[cx][cy][pp];
442                if (STOR_END[cx][cy][pp]   > max_stor_ended)  max_stor_ended = STOR_END[cx][cy][pp];
443            }
444            }
445            }
446
447            giet_shr_printf("\n   ---------------- Instrumentation Results ---------------------\n");
448
449            giet_shr_printf(" - LOAD_START : min = %d / max = %d / med = %d / delta = %d\n",
450                            min_load_start, max_load_start, (min_load_start+max_load_start)/2, 
451                            max_load_start-min_load_start); 
452
453            giet_shr_printf(" - LOAD_END   : min = %d / max = %d / med = %d / delta = %d\n",
454                            min_load_ended, max_load_ended, (min_load_ended+max_load_ended)/2, 
455                            max_load_ended-min_load_ended); 
456
457            giet_shr_printf(" - TRSP_START : min = %d / max = %d / med = %d / delta = %d\n",
458                            min_trsp_start, max_trsp_start, (min_trsp_start+max_trsp_start)/2, 
459                            max_trsp_start-min_trsp_start); 
460
461            giet_shr_printf(" - TRSP_END   : min = %d / max = %d / med = %d / delta = %d\n",
462                            min_trsp_ended, max_trsp_ended, (min_trsp_ended+max_trsp_ended)/2, 
463                            max_trsp_ended-min_trsp_ended); 
464
465            giet_shr_printf(" - DISP_START : min = %d / max = %d / med = %d / delta = %d\n",
466                            min_disp_start, max_disp_start, (min_disp_start+max_disp_start)/2, 
467                            max_disp_start-min_disp_start); 
468
469            giet_shr_printf(" - DISP_END   : min = %d / max = %d / med = %d / delta = %d\n",
470                            min_disp_ended, max_disp_ended, (min_disp_ended+max_disp_ended)/2, 
471                            max_disp_ended-min_disp_ended); 
472
473            giet_shr_printf(" - STOR_START : min = %d / max = %d / med = %d / delta = %d\n",
474                            min_stor_start, max_stor_start, (min_stor_start+max_stor_start)/2, 
475                            max_stor_start-min_stor_start); 
476
477            giet_shr_printf(" - STOR_END   : min = %d / max = %d / med = %d / delta = %d\n",
478                            min_stor_ended, max_stor_ended, (min_stor_ended+max_stor_ended)/2, 
479                            max_stor_ended-min_stor_ended); 
480        }
481
482        /////////////////////////////
483        sqt_barrier_wait( &barrier );
484
485        // update iteration variables
486        fd_in  = fd_transposed;
487        fd_out = fd_restored;
488        iteration++;
489
490    } // end while     
491
492    ///////////////////////////////////////////////////////////////////////
493    // In each cluster, only task running on Processor[x,y,0] releases
494    // the distributed buffers and close the file descriptors.
495    ///////////////////////////////////////////////////////////////////////
496
497    if ( lpid==0 )
498    {
499        free( buf_in[cluster_id] );
500        free( buf_out[cluster_id] );
501
502        giet_fat_close( fd_initial );
503        giet_fat_close( fd_transposed );
504        giet_fat_close( fd_restored );
505    }
506
507    // display disk content
508    if ( (x==0) && (y == 0) && (lpid == 0) )
509    { 
510        giet_fat_list( "/" );
511        giet_fat_list( "/misc" );
512        giet_fat_list( "/home" );
513        giet_fat_list( "/build" );
514        giet_fat_list( "/build/kernel" );
515        giet_fat_list( "/build/transpose" );
516
517        giet_fat_remove( "/home/lena_transposed" , 0 );
518        giet_fat_remove( "/home/lena_restored" , 0 );
519
520        giet_fat_remove( "/home" , 1 );
521
522        giet_fat_list( "/" );
523        giet_fat_list( "/misc" );
524        giet_fat_list( "/home" );
525        giet_fat_list( "/build" );
526        giet_fat_list( "/build/kernel" );
527        giet_fat_list( "/build/transpose" );
528    }
529   
530    giet_exit("Completed");
531
532} // end main()
533
534// Local Variables:
535// tab-width: 3
536// c-basic-offset:
537// c-file-offsets:((innamespace . 0)(inline-open . 0))
538// indent-tabs-mode: nil
539// End:
540
541// vim: filetype=cpp:expandtab:shiftwidth=3:tabstop=3:softtabstop=3
542
543
544
Note: See TracBrowser for help on using the repository browser.