source: trunk/user/transpose/transpose.c @ 665

Last change on this file since 665 was 659, checked in by alain, 4 years ago

euh...

File size: 37.8 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////////
2// File   : transpose.c   
3// Date   : september 2019
4// author : Alain Greiner
5//////////////////////////////////////////////////////////////////////////////////////////
6// This multi-threaded aplication read a raw image (one byte per pixel)
7// stored on disk, transposes it, displays the result on the frame buffer,
8// and stores the transposed image on disk.
9//
10// The image size and the pixel encoding type are defined by the IMAGE_SIZE and
11// IMAGE_TYPE global parameters.
12//
13// It can run on a multi-cores, multi-clusters architecture, where (X_SIZE * Y_SIZE)
14// is the number of clusters and NCORES the number of cores per cluster.
15// A core is identified by two indexes [cxy,lid] : cxy is the cluster identifier,
16// (that is NOT required to be a continuous index), and lid is the local core index,
17// (that must be in the [0,NCORES-1] range).
18//
19// The main() function can run on any core in any cluster. This main thread
20// makes the initialisations, load the input file to the "image_in" buffer,
21// launches the working threads, calls the instrument() function when all working
22// threads complete, and saves the result "image_out" buffer to the output file.
23//
24// The number of working threads is always defined by the number of cores availables
25// in the architecture, but this application supports three placement modes.
26// In all modes, the working threads are identified by the [tid] continuous index
27// in range [0, NTHREADS-1], and defines how the lines are shared amongst the threads.
28// This continuous index can always be decomposed in two continuous sub-indexes:
29// tid == cid * ncores + lid,  where cid is in [0,NCLUSTERS-1] and lid in [0,NCORES-1].
30//
31// - NO_PLACEMENT: the main thread is itsef a working thread. The (N_1) other working
32//   threads are created by the main thread, but the placement is done by the OS, using
33//   the DQDT for load balancing, and two working threads can be placed on the same core.
34//   The [cid,lid] are only abstract identifiers, and cannot be associated to a physical
35//   cluster or a physical core. In this mode, the main thread run on any cluster,
36//   but has tid = 0 (i.e. cid = 0 & tid = 0).
37//
38// - EXPLICIT_PLACEMENT: the main thread is again a working thread, but the placement of
39//   of the threads on the cores is explicitely controled by the main thread to have
40//   exactly one working thread per core, and the [cxy][lpid] core coordinates for a given
41//   thread[tid] can be directly derived from the [tid] value: [cid] is an alias for the
42//   physical cluster identifier, and [lid] is the local core index.
43//
44// - PARALLEL_PLACEMENT: the main thread is not anymore a working thread, and uses the
45//   non standard pthread_parallel_create() function to avoid the costly sequencial
46//   loops for pthread_create() and pthread_join(). It garanty one working thread
47//   per core, and the same relation between the thread[tid] and the core[cxy][lpid].
48//   
49// Each working thread[cid][lid] run the "execute" function, that uses the "buf_in" and
50// "buf_out" local buffers, containing the direct and transposed images:
51// Each thread[cid][0] allocates two buf_in[cid] and buf_out[cid] buffers, load from
52// "image_in" to buf_in[cid] all lines that must be handled by the threads sharing the
53// same cid, and finally save from buf_out[cid] to "image_out" all lines that have been
54// transposed to buf_out[cid].
55// Each thread[cid][lid] in the group defined by the cid index read pixels from the
56// local buf_in[cid] buffer, and write pixels to all remote // buf_out[cid] buffers.
57//
58// - The image  must fit the frame buffer size, that must be power of 2.
59// - The number of clusters  must be a power of 2 no larger than 256.
60// - The number of cores per cluster must be a power of 2 no larger than 4.
61// - The number of threads cannot be larger than IMAGE_SIZE.
62//
63//////////////////////////////////////////////////////////////////////////////////////////
64
65#include <sys/mman.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <unistd.h>
69#include <pthread.h>
70#include <string.h>
71#include <almosmkh.h>
72#include <fcntl.h>
73#include <hal_macros.h>
74
75#define X_MAX                 16                           // max number of clusters in row
76#define Y_MAX                 16                           // max number of clusters in column
77#define CORES_MAX             4                            // max number of cores per cluster
78#define CLUSTERS_MAX          (X_MAX * Y_MAX)              // max number of clusters
79#define THREADS_MAX           (X_MAX * Y_MAX * CORES_MAX)  // max number of threads
80
81#define IMAGE_TYPE            420                          // pixel encoding type
82
83//#define IMAGE_SIZE            128                          // image size
84//#define INPUT_FILE_PATH       "/misc/images_128.raw"       // input file pathname
85//#define OUTPUT_FILE_PATH      "/misc/transposed_128.raw"   // output file pathname
86
87//#define IMAGE_SIZE            256                          // image size
88//#define INPUT_FILE_PATH       "/misc/lena_256.raw"         // input file pathname
89#//define OUTPUT_FILE_PATH      "/misc/transposed_256.raw"   // output file pathname
90
91//#define IMAGE_SIZE            512                          // image size
92//#define INPUT_FILE_PATH       "/misc/couple_512.raw"       // input file pathname
93//#define OUTPUT_FILE_PATH      "/misc/transposed_512.raw"   // output file pathname
94
95#define IMAGE_SIZE            1024                         // image size
96#define INPUT_FILE_PATH       "/misc/philips_1024.raw"     // input file pathname
97#define OUTPUT_FILE_PATH      "/misc/transposed_1024.raw"  // output file pathname
98
99#define SAVE_RESULT_FILE      0                            // save result image on disk
100#define USE_DQT_BARRIER       0                            // quad-tree barrier if non zero
101
102#define NO_PLACEMENT          0                            // uncontrolefdthread placement
103#define EXPLICIT_PLACEMENT    1                            // explicit threads placement
104#define PARALLEL_PLACEMENT    0                            // parallel threads placement
105
106#define VERBOSE_MAIN          1                            // main function print comments
107#define VERBOSE_MAIN_DETAILED 0                            // main function print comments
108#define VERBOSE_EXEC          1                            // exec function print comments
109
110
111///////////////////////////////////////////////////////
112//                global variables
113///////////////////////////////////////////////////////
114
115// global instrumentation counters for the main thread
116unsigned int SEQUENCIAL_TIME = 0;
117unsigned int PARALLEL_TIME   = 0;
118
119// instrumentation counters for each thread in each cluster
120// indexed by [cid][lid] : cluster continuous index / thread local index
121unsigned int ALOC_START[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
122unsigned int ALOC_END  [CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
123unsigned int LOAD_START[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
124unsigned int LOAD_END  [CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
125unsigned int TRSP_START[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
126unsigned int TRSP_END  [CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
127unsigned int SAVE_START[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
128unsigned int SAVE_END  [CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
129unsigned int FREE_START[CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
130unsigned int FREE_END  [CLUSTERS_MAX][CORES_MAX] = {{ 0 }};
131
132// buffer containing the input image, loaded by the main from input file
133unsigned char  image_in[IMAGE_SIZE * IMAGE_SIZE];
134
135// buffer containing the output image, saved by the main to output file
136unsigned char  image_out[IMAGE_SIZE * IMAGE_SIZE];
137
138// arrays of pointers on distributed buffers indexed by [cid]
139unsigned char *  buf_in [CLUSTERS_MAX];
140unsigned char *  buf_out[CLUSTERS_MAX];
141
142// pointer and identifier for dynamically allocated FBF window
143void   *  win_buf;
144int       wid;
145
146// synchronisation barrier (all working threads)
147pthread_barrier_t   barrier;
148
149// platform parameters
150unsigned int  x_size;              // number of clusters in a row
151unsigned int  y_size;              // number of clusters in a column
152unsigned int  ncores;              // number of cores per cluster
153
154// main thread continuous index
155unsigned int     tid_main; 
156
157//return values at thread exit
158unsigned int THREAD_EXIT_SUCCESS = 0;
159unsigned int THREAD_EXIT_FAILURE = 1;
160
161// array of kernel thread identifiers / indexed by [tid]
162pthread_t                     exec_trdid[THREADS_MAX];   
163
164// array of execute function arguments / indexed by [tid]
165pthread_parallel_work_args_t  exec_args[THREADS_MAX];
166
167// array of thread attributes / indexed by [tid]
168pthread_attr_t                exec_attr[THREADS_MAX];
169
170////////////////////////////////////////////////////////////////
171//             functions declaration
172////////////////////////////////////////////////////////////////
173
174void * execute( void * arguments );
175
176void instrument( FILE * f , char * filename );
177
178////////////////
179int main( void )
180{
181    unsigned long long start_cycle;
182    unsigned long long end_sequencial_cycle;
183    unsigned long long end_parallel_cycle;
184
185    char               filename[32];      // instrumentation file name
186    char               pathname[64];      // instrumentation file pathname
187
188    int error;
189
190    /////////////////////////////////////////////////////////////////////////////////
191    get_cycle( &start_cycle );
192    /////////////////////////////////////////////////////////////////////////////////
193
194    if( (NO_PLACEMENT + EXPLICIT_PLACEMENT + PARALLEL_PLACEMENT) != 1 )
195    {
196        printf("\n[transpose error] illegal placement\n");
197        exit( 0 );
198    }
199
200    // get & check plat-form parameters
201    hard_config_t  config;
202    get_config( &config );
203    x_size = config.x_size;
204    y_size = config.y_size;
205    ncores = config.ncores;
206
207    if((ncores != 1) && (ncores != 2) && (ncores != 4))
208    {
209        printf("\n[transpose error] number of cores per cluster must be 1/2/4\n");
210        exit( 0 );
211    }
212
213    if( (x_size != 1) && (x_size != 2) && (x_size != 4) && 
214        (x_size != 8) && (x_size != 16) )
215    {
216        printf("\n[transpose error] x_size must be 1/2/4/8/16\n");
217        exit( 0 );
218    }
219       
220    if( (y_size != 1) && (y_size != 2) && (y_size != 4) && 
221        (y_size != 8) && (y_size != 16) )
222    {
223        printf("\n[transpose error] y_size must be 1/2/4/8/16\n");
224        exit( 0 );
225    }
226       
227    // get identifiers for core executing main
228    unsigned int  cxy_main;
229    unsigned int  lid_main;
230    get_core_id( &cxy_main , &lid_main );
231
232    // compute number of threads
233    unsigned int nclusters = x_size * y_size;
234    unsigned int nthreads  = nclusters * ncores;
235
236    if( nthreads > IMAGE_SIZE )
237    {
238        printf("\n[transpose error] number of threads larger than number of lines\n");
239        exit( 0 );
240    }
241
242    // get FBF size and type
243    unsigned int   fbf_width;
244    unsigned int   fbf_height;
245    unsigned int   fbf_type;
246    fbf_get_config( &fbf_width , &fbf_height , &fbf_type );
247
248    if( (fbf_width < IMAGE_SIZE) || (fbf_height < IMAGE_SIZE) || (fbf_type != IMAGE_TYPE) )
249    {
250        printf("\n[transpose error] image does not fit FBF size or type\n");
251        exit( 0 );
252    }
253
254    // define total number of pixels
255    int npixels = IMAGE_SIZE * IMAGE_SIZE;
256
257    // define instrumentation file name
258    if( NO_PLACEMENT )
259    {
260        printf("\n[transpose] %d cluster(s) / %d core(s) / <%s> / PID %x / NO_PLACE\n",
261        nclusters, ncores, INPUT_FILE_PATH , getpid() );
262
263        // build instrumentation file name
264        if( USE_DQT_BARRIER )
265        snprintf( filename , 32 , "trsp_dqt_no_place_%d_%d_%d",
266        IMAGE_SIZE , x_size * y_size , ncores );
267        else
268        snprintf( filename , 32 , "trsp_smp_no_place_%d_%d_%d",
269        IMAGE_SIZE , x_size * y_size , ncores );
270    }
271
272    if( EXPLICIT_PLACEMENT )
273    {
274        printf("\n[transpose] %d cluster(s) / %d core(s) / <%s> / PID %x / EXPLICIT\n",
275        nclusters, ncores, INPUT_FILE_PATH , getpid() );
276
277        // build instrumentation file name
278        if( USE_DQT_BARRIER )
279        snprintf( filename , 32 , "trsp_dqt_explicit_%d_%d_%d",
280        IMAGE_SIZE , x_size * y_size , ncores );
281        else
282        snprintf( filename , 32 , "trsp_smp_explicit_%d_%d_%d",
283        IMAGE_SIZE , x_size * y_size , ncores );
284    }
285
286    if( PARALLEL_PLACEMENT )
287    {
288        printf("\n[transpose] %d cluster(s) / %d core(s) / <%s> / PID %x / PARALLEL\n",
289        nclusters, ncores, INPUT_FILE_PATH , getpid() );
290
291        // build instrumentation file name
292        if( USE_DQT_BARRIER )
293        snprintf( filename , 32 , "trsp_dqt_parallel_%d_%d_%d",
294        IMAGE_SIZE , x_size * y_size , ncores );
295        else
296        snprintf( filename , 32 , "trsp_smp_parallel_%d_%d_%d",
297        IMAGE_SIZE , x_size * y_size , ncores );
298    }
299
300    // open a window in FBF
301    wid = fbf_create_window( 0,             // l_zero
302                             0,             // p_zero
303                             IMAGE_SIZE,    // lines
304                             IMAGE_SIZE,    // pixels
305                             &win_buf );
306    if( wid < 0) 
307    {
308        printf("\n[transpose error] cannot open FBF window\n");
309        exit( 0 );
310    }
311
312#if  VERBOSE_MAIN
313printf("\n[transpose] main on core[%x,%d] created FBF window %d / buffer %x\n",
314cxy_main, lid_main, wid , win_buf );
315#endif
316
317    // open instrumentation file
318    snprintf( pathname , 64 , "/home/%s", filename );
319    FILE * f = fopen( pathname , NULL );
320
321    if ( f == NULL ) 
322    { 
323        printf("\n[transpose error] cannot open instru file %s\n", pathname );
324        exit( 0 );
325    }
326
327#if  VERBOSE_MAIN
328printf("\n[transpose] main on core[%x,%d] open instrumentation file %s\n",
329cxy_main, lid_main, pathname );
330#endif
331
332    // main thread initializes barrier
333    if( USE_DQT_BARRIER )
334    {
335        pthread_barrierattr_t attr;
336        attr.x_size   = x_size;
337        attr.y_size   = y_size;
338        attr.nthreads = ncores;
339        error = pthread_barrier_init( &barrier, &attr , nthreads );
340    }
341    else
342    {
343        error = pthread_barrier_init( &barrier, NULL , nthreads );
344    }
345
346    if( error )
347    { 
348        printf("\n[transpose error] main cannot initialize barrier\n" );
349        exit( 0 );
350    }
351
352#if  VERBOSE_MAIN
353printf("\n[transpose] main on core[%x,%d] completed barrier initialisation\n",
354cxy_main, lid_main );
355#endif
356
357    // open input file
358    int fd_in = open( INPUT_FILE_PATH , O_RDONLY , 0 ); 
359
360    if ( fd_in < 0 ) 
361    { 
362        printf("\n[transpose error] main cannot open file %s\n", INPUT_FILE_PATH );
363        exit( 0 );
364    }
365
366#if  VERBOSE_MAIN
367printf("\n[transpose] main open file <%s> / fd = %d\n", INPUT_FILE_PATH , fd_in );
368#endif
369
370    // open output file
371    int fd_out = open( OUTPUT_FILE_PATH , O_CREAT , 0 ); 
372
373    if ( fd_out < 0 ) 
374    { 
375        printf("\n[transpose error] main cannot open file %s\n", OUTPUT_FILE_PATH );
376        exit( 0 );
377    }
378
379    // move input image to input buffer
380    if( read( fd_in , image_in , npixels ) != npixels )
381    {
382        printf("\n[transpose error] main cannot read input image\n");
383        exit( 0 );
384    }
385
386#if  VERBOSE_MAIN
387printf("\n[transpose] main moved file <%s> to buf_in\n", INPUT_FILE_PATH );
388#endif
389
390    /////////////////////////////////////////////////////////////////////////////////////
391    get_cycle( &end_sequencial_cycle );
392    SEQUENCIAL_TIME = (unsigned int)(end_sequencial_cycle - start_cycle);
393    /////////////////////////////////////////////////////////////////////////////////////
394
395    //////////////////
396    if( NO_PLACEMENT )
397    {
398        // the tid value for the main thread is always 0
399        // main thread creates new threads with tid in [1,nthreads-1] 
400        unsigned int tid;
401        for ( tid = 0 ; tid < nthreads ; tid++ )
402        {
403            // register tid value in exec_args[tid] array
404            exec_args[tid].tid = tid;
405           
406            // create other threads
407            if( tid > 0 )
408            {
409                if ( pthread_create( &exec_trdid[tid], 
410                                     NULL,                  // no attribute
411                                     &execute,
412                                     &exec_args[tid] ) ) 
413                {
414                    printf("\n[transpose error] cannot create thread %d\n", tid );
415                    exit( 0 );
416                }
417
418#if VERBOSE_MAIN_DETAILED
419printf("\n[transpose] main created thread %d\n", tid );
420#endif
421
422            }
423            else
424            {
425                tid_main = 0;
426            }
427        }  // end for tid
428
429        // main thread calls itself the execute() function
430        execute( &exec_args[0] );
431
432        // main thread wait other threads completion
433        for ( tid = 1 ; tid < nthreads ; tid++ )
434        {
435            unsigned int * status;
436
437            // main wait thread[tid] status
438            if ( pthread_join( exec_trdid[tid], (void*)(&status)) )
439            {
440                printf("\n[transpose error] main cannot join thread %d\n", tid );
441                exit( 0 );
442            }
443       
444            // check status
445            if( *status != THREAD_EXIT_SUCCESS )
446            {
447                printf("\n[transpose error] thread %x returned failure\n", tid );
448                exit( 0 );
449            }
450
451#if VERBOSE_MAIN_DETAILED
452printf("\n[transpose] main joined thread %x\n", tid );
453#endif
454       
455        }  // end for tid
456
457    }  // end if no_placement
458
459    ////////////////////////
460    if( EXPLICIT_PLACEMENT )
461    {
462        // main thread places each other threads on a specific core[cxy][lid]
463        // but the actual thread creation is sequencial
464        unsigned int x;
465        unsigned int y;
466        unsigned int l;
467        unsigned int cxy;                   // cluster identifier
468        unsigned int tid;                   // thread continuous index
469
470        for( x = 0 ; x < x_size ; x++ )
471        {
472            for( y = 0 ; y < y_size ; y++ )
473            {
474                cxy = HAL_CXY_FROM_XY( x , y );
475                for( l = 0 ; l < ncores ; l++ )
476                {
477                    // compute thread continuous index
478                    tid = (((* y_size) + y) * ncores) + l;
479
480                    // register tid value in exec_args[tid] array
481                    exec_args[tid].tid = tid;
482
483                    // no thread created on the core running the main
484                    if( (cxy != cxy_main) || (l != lid_main) )
485                    {
486                        // define thread attributes
487                        exec_attr[tid].attributes = PT_ATTR_CLUSTER_DEFINED |
488                                                    PT_ATTR_CORE_DEFINED;
489                        exec_attr[tid].cxy        = cxy;
490                        exec_attr[tid].lid        = l;
491 
492                        // create thread[tid] on core[cxy][l]
493                        if ( pthread_create( &exec_trdid[tid],   
494                                             &exec_attr[tid],   
495                                             &execute,
496                                             &exec_args[tid] ) )       
497                        {
498                            printf("\n[transpose error] cannot create thread %d\n", tid );
499                            exit( 0 );
500                        }
501
502#if VERBOSE_MAIN_DETAILED
503printf("\n[transpose] main created thread[%d] on core[%x,%d]\n", tid, cxy, l );
504#endif
505                    }
506                    else
507                    {
508                        tid_main = tid;
509                    }
510                }
511            }
512        }
513
514        // main thread calls itself the execute() function
515        execute( &exec_args[tid_main] );
516
517        // main thread wait other threads completion
518        for( tid = 0 ; tid < nthreads ; tid++ )
519        {
520            // no other thread on the core running the main
521            if( tid != tid_main )
522            {
523                unsigned int * status;
524
525                // wait thread[tid]
526                if( pthread_join( exec_trdid[tid] , (void*)(&status) ) )
527                {
528                    printf("\n[transpose error] main cannot join thread %d\n", tid );
529                    exit( 0 );
530                }
531       
532                // check status
533                if( *status != THREAD_EXIT_SUCCESS )
534                {
535                    printf("\n[transpose error] thread %d returned failure\n", tid );
536                    exit( 0 );
537                }
538
539#if VERBOSE_MAIN_DETAILED
540printf("\n[transpose] main joined thread %d\n", tid );
541#endif
542            }
543        }
544    }  // end if explicit_placement
545
546    ////////////////////////
547    if( PARALLEL_PLACEMENT )
548    {
549        // compute covering DQT size an level
550        unsigned int z          = (x_size > y_size) ? x_size : y_size;
551        unsigned int root_level = ((z == 1) ? 0 : 
552                                  ((z == 2) ? 1 : 
553                                  ((z == 4) ? 2 : 
554                                  ((z == 8) ? 3 : 4))));
555
556        // create & execute the working threads
557        if( pthread_parallel_create( root_level , &execute ) )
558        {
559            printf("\n[transpose error] in %s\n", __FUNCTION__ );
560            exit( 0 );
561        }
562    }  // end if parallel_placement
563
564
565    /////////////////////////////////////////////////////////////////////////////
566    get_cycle( &end_parallel_cycle );
567    PARALLEL_TIME = (unsigned int)(end_parallel_cycle - end_sequencial_cycle);
568    /////////////////////////////////////////////////////////////////////////////
569
570    // register instrumentation results
571    instrument( f , filename );
572
573#if VERBOSE_MAIN
574printf("\n[transpose] main completed instrumentation\n");
575#endif
576
577/*
578    printf("\n> ");
579    getchar();
580
581    // move window
582    if( fbf_move_window( wid , 100 , 100 ) )
583    {
584        printf("\n[transpose error] main cannot move FBF window\n");
585        exit( 0 );
586    }
587
588    printf("\n> ");
589    getchar();
590*/   
591    // save image_out to output file
592    if( write( fd_out , image_out , npixels ) != npixels )
593    {
594        printf("\n[transpose error] main cannot write output image\n");
595        exit( 0 );
596    }
597
598#if VERBOSE_MAIN
599printf("\n[transpose] main saved buf_out to output file\n");
600#endif
601
602    // close input file
603    close( fd_in );
604
605#if VERBOSE_MAIN
606printf("\n[transpose] main closed input file\n");
607#endif
608
609    // close output file
610    close( fd_out );
611
612#if VERBOSE_MAIN
613printf("\n[transpose] main closed output file\n");
614#endif
615
616    // close instrumentation file
617    fclose( f );
618
619#if VERBOSE_MAIN
620printf("\n[transpose] main closed instrumentation file\n");
621#endif
622
623    // delete FBF window
624    if( fbf_delete_window( wid ) )
625    {
626        printf("\n[transpose error] main cannot delete FBF window\n");
627        exit( 0 );
628    }
629
630    // main thread suicide
631    exit( 0 );
632   
633    return 0;
634
635} // end main()
636
637
638
639
640//////////////////////////////////
641void * execute( void * arguments ) 
642{
643    unsigned long long   date;
644    unsigned int         l;         // line index for loop
645    unsigned int         p;         // pixel index for loop
646    int                  error;
647
648    unsigned char      * wbuf = win_buf;
649 
650    pthread_parallel_work_args_t * args = (pthread_parallel_work_args_t *)arguments;
651
652    // WARNING
653    //A thread is identified by the tid index, defined in the "args" structure.
654    // This index being in range [0,nclusters*ncores-1] we can always write
655    //       tid == cid * ncores + lid
656    // with cid in [0,nclusters-1] and lid in [0,ncores-1].
657    // if NO_PLACEMENT, there is no relation between these
658    // thread [cid][lid] indexes, and the core coordinates [cxy][lpid]
659
660    // get thread abstract identifiers
661    unsigned int tid = args->tid;
662    unsigned int cid = tid / ncores;    // abstract cluster index
663    unsigned int lid = tid % ncores;    // local thread index
664
665#if VERBOSE_EXEC
666unsigned int cxy;
667unsigned int lpid;
668get_core_id( &cxy , &lpid );   // get core physical identifiers
669#endif
670
671#if VERBOSE_EXEC
672printf("\n[transpose] exec[%d] on core[%x,%d] enters parallel exec\n",
673tid , cxy , lpid );
674#endif
675
676    get_cycle( &date );
677    ALOC_START[cid][lid] = (unsigned int)date;
678
679    // compute total number of pixels per image
680    unsigned int npixels = IMAGE_SIZE * IMAGE_SIZE;     
681
682    // compute total number of threads and clusters
683    unsigned int nclusters = x_size * y_size;
684    unsigned int nthreads  = nclusters * ncores;
685
686    // compute number of pixels per cid & per thread
687    unsigned int pixels_per_cid = npixels / nclusters;
688    unsigned int pixels_per_lid = pixels_per_cid / ncores;
689
690    // compute first and last line per thread
691    unsigned int lines_per_cid = pixels_per_cid / IMAGE_SIZE;
692    unsigned int lines_per_lid = pixels_per_lid / IMAGE_SIZE;
693
694    unsigned int line_first = (cid * lines_per_cid) + (lid * lines_per_lid);
695    unsigned int line_last  = line_first + lines_per_lid;
696
697    // Each thread[cid,0] allocates two local buffers, and register the base
698    // adresses in the global variable buf_in_ptr[cid] & buf_out_ptr[cid].
699   
700    if( lid == 0 )
701    {
702        // allocate buf_in
703        buf_in[cid] = (unsigned char *)malloc( pixels_per_cid );
704
705        if( buf_in[cid] == NULL )
706        {
707            printf("\n[transpose error] thread[%d] cannot allocate buf_in\n", tid );
708            pthread_exit( &THREAD_EXIT_FAILURE );
709        }
710
711#if VERBOSE_EXEC
712printf("\n[transpose] exec[%d] on core[%x,%d] allocated buf_in = %x\n",
713tid , cxy , lpid , buf_in );
714#endif
715
716        // allocate buf_out
717        buf_out[cid] = (unsigned char *)malloc( pixels_per_cid );
718
719        if( buf_out[cid] == NULL )
720        {
721            printf("\n[transpose error] thread[%d] cannot allocate buf_in\n", tid );
722            pthread_exit( &THREAD_EXIT_FAILURE );
723        }
724
725#if VERBOSE_EXEC
726printf("\n[transpose] exec[%d] on core[%x,%d] allocated buf_out = %x\n",
727tid , cxy , lpid , buf_out );
728#endif
729
730    }
731
732    get_cycle( &date );
733    ALOC_END[cid][lid] = (unsigned int)date;
734
735    /////////////////////////////////
736    pthread_barrier_wait( &barrier );
737    /////////////////////////////////
738
739    get_cycle( &date );
740    LOAD_START[cid][lid] = (unsigned int)date;
741
742    // all threads copy relevant part of the image_in to buf_in[cid]
743    memcpy( buf_in[cid] + (lid * pixels_per_lid), 
744            image_in + (cid * pixels_per_cid) + (lid * pixels_per_lid),
745            pixels_per_lid );
746
747#if VERBOSE_EXEC
748printf("\n[transpose] exec[%d] on core[%x,%d] loaded buf_in[%d]\n",
749tid , cxy , lpid , cid );
750#endif
751
752    // all local threads copy part of buf_in[cid] to FBF window for display
753    memcpy( wbuf + (cid * pixels_per_cid) + (lid * pixels_per_lid),
754            buf_in[cid] + (lid * pixels_per_lid),
755            pixels_per_lid );
756
757#if  VERBOSE_EXEC
758printf("\n[transpose] exec[%d] on core[%x,%d] loaded buf_in to FBF (first %d / last %d)\n",
759tid , cxy , lpid , line_first , line_last );
760#endif
761
762    // retresh window
763    error = fbf_refresh_window( wid , line_first , line_last );
764
765    if( error )
766    {
767        printf("\n[transpose error] exec[%d] cannot refresh FBF window\n", tid );
768        exit( 0 );
769    }
770
771    get_cycle( &date );
772    LOAD_END[cid][lid] = (unsigned int)date;
773
774    /////////////////////////////////
775    pthread_barrier_wait( &barrier );
776    /////////////////////////////////
777
778    get_cycle( &date );
779    TRSP_START[cid][lid] = (unsigned int)date;
780
781    // All threads contribute to parallel transpose from buf_in to buf_out:
782    // each thread makes the transposition for nlt lines (nlt = npixels/nthreads)
783    // from line [tid*nlt] to line [(tid + 1)*nlt - 1]
784    // (p,l) are the absolute pixel coordinates in the source image
785    // (l,p) are the absolute pixel coordinates in the dest image
786
787    unsigned int nlt   = IMAGE_SIZE / nthreads;    // number of lines per thread
788    unsigned int nlc   = IMAGE_SIZE / nclusters;   // number of lines per cluster
789
790    unsigned int src_cid;
791    unsigned int src_index;
792    unsigned int dst_cid;
793    unsigned int dst_index;
794
795    unsigned char byte;
796
797    unsigned int first = tid * nlt;        // first line index for a given thread
798    unsigned int last  = first + nlt;      // last line index for a given thread
799
800    // loop on lines handled by this thread
801    for ( l = first ; l < last ; l++ )
802    {
803        // loop on pixels in one line (one pixel per iteration)
804        for ( p = 0 ; p < IMAGE_SIZE ; p++ )
805        {
806            // read one byte from local buf_in
807            src_cid   = l / nlc;
808            src_index = (l % nlc) * IMAGE_SIZE + p;
809
810            byte = buf_in[src_cid][src_index];
811
812            // write one byte to remote buf_out
813            dst_cid   = p / nlc; 
814            dst_index = (p % nlc) * IMAGE_SIZE + l;
815
816            buf_out[dst_cid][dst_index] = byte;
817        }
818    }
819
820#if VERBOSE_EXEC
821printf("\n[transpose] exec[%d] on core[%x,%d] completes transpose\n",
822tid , cxy , lpid );
823#endif
824
825    get_cycle( &date );
826    TRSP_END[cid][lid] = (unsigned int)date;
827
828    /////////////////////////////////
829    pthread_barrier_wait( &barrier );
830    /////////////////////////////////
831
832    get_cycle( &date );
833    SAVE_START[cid][lid] = (unsigned int)date;
834
835    // each local threads copy part of buf_out[cid] to FBF window for display
836    memcpy( wbuf + (cid * pixels_per_cid) + (lid * pixels_per_lid),
837            buf_out[cid] + (lid * pixels_per_lid),
838            pixels_per_lid );
839
840#if  VERBOSE_EXEC
841printf("\n[transpose] exec[%d] on core[%x,%d] loaded buf_out to FBF (first %d / last %d)\n",
842tid , cxy , lpid , line_first , line_last );
843#endif
844
845    // refresh window
846    error = fbf_refresh_window( wid , line_first , line_last );
847
848    if( error )
849    {
850        printf("\n[transpose error] exec[%d] cannot refresh FBF window\n", tid );
851        exit( 0 );
852    }
853
854    // each local thread copy relevant part of buf_out to image_out
855    memcpy( image_out + (cid * pixels_per_cid) + (lid * pixels_per_lid),
856            buf_out[cid] + (lid * pixels_per_lid),
857            pixels_per_lid );
858
859#if VERBOSE_EXEC
860printf("\n[transpose] exec[%d] on core[%x,%d] saved buf_out[%d]\n",
861tid , cxy , lpid , cid );
862#endif
863
864    get_cycle( &date );
865    SAVE_END[cid][lid] = (unsigned int)date;
866
867    /////////////////////////////////
868    pthread_barrier_wait( &barrier );
869    /////////////////////////////////
870
871    get_cycle( &date );
872    FREE_START[cid][lid] = (unsigned int)date;
873
874    // Each thread[cid,0] release local buffers buf_in & buf_out
875
876    if( lid == 0 )
877    {
878        // release local buffers
879        free( buf_in[cid] );
880        free( buf_out[cid] );
881
882#if VERBOSE_EXEC
883printf("\n[transpose] exec[%d] on core[%x,%d] released buf_in & buf_out\n",
884tid , cxy , lpid );
885#endif
886
887    }
888
889    get_cycle( &date );
890    FREE_END[cid][lid] = (unsigned int)date;
891
892    /////////////////////////////////
893    pthread_barrier_wait( &barrier );
894    /////////////////////////////////
895   
896    // thread termination depends on the placement policy
897    if( PARALLEL_PLACEMENT )   
898    {
899        // <work> threads are runing in detached mode, and
900        // each thread must signal completion by calling barrier
901        // passed in arguments before exit
902
903        pthread_barrier_wait( args->barrier );
904
905        pthread_exit( &THREAD_EXIT_SUCCESS );
906    }
907    else
908    {
909        // <work> threads are running in attached mode
910        // each thread, but de main, simply exit
911        if ( tid != tid_main ) 
912        {
913
914#if VERBOSE_EXEC
915printf("\n[transpose] exec[%d] on core[%x,%d] exit\n",
916tid , cxy , lpid );
917#endif
918            pthread_exit( &THREAD_EXIT_SUCCESS );
919        }
920    }
921
922    return NULL;
923
924} // end execute()
925
926
927
928//////////////////////////
929void instrument( FILE * f,
930                 char * filename )
931{
932    unsigned int cid;
933    unsigned int l;
934
935    unsigned int min_aloc_start = 0xFFFFFFFF;
936    unsigned int max_aloc_start = 0;
937    unsigned int min_aloc_ended = 0xFFFFFFFF;
938    unsigned int max_aloc_ended = 0;
939    unsigned int min_load_start = 0xFFFFFFFF;
940    unsigned int max_load_start = 0;
941    unsigned int min_load_ended = 0xFFFFFFFF;
942    unsigned int max_load_ended = 0;
943    unsigned int min_trsp_start = 0xFFFFFFFF;
944    unsigned int max_trsp_start = 0;
945    unsigned int min_trsp_ended = 0xFFFFFFFF;
946    unsigned int max_trsp_ended = 0;
947    unsigned int min_save_start = 0xFFFFFFFF;
948    unsigned int max_save_start = 0;
949    unsigned int min_save_ended = 0xFFFFFFFF;
950    unsigned int max_save_ended = 0;
951    unsigned int min_free_start = 0xFFFFFFFF;
952    unsigned int max_free_start = 0;
953    unsigned int min_free_ended = 0xFFFFFFFF;
954    unsigned int max_free_ended = 0;
955 
956    for (cid = 0; cid < (x_size * y_size) ; cid++)
957    {
958        for ( l = 0 ; l < ncores ; l++ )
959        {
960            if (ALOC_START[cid][l] < min_aloc_start)  min_aloc_start = ALOC_START[cid][l];
961            if (ALOC_START[cid][l] > max_aloc_start)  max_aloc_start = ALOC_START[cid][l];
962            if (ALOC_END[cid][l]   < min_aloc_ended)  min_aloc_ended = ALOC_END[cid][l]; 
963            if (ALOC_END[cid][l]   > max_aloc_ended)  max_aloc_ended = ALOC_END[cid][l];
964            if (LOAD_START[cid][l] < min_load_start)  min_load_start = LOAD_START[cid][l];
965            if (LOAD_START[cid][l] > max_load_start)  max_load_start = LOAD_START[cid][l];
966            if (LOAD_END[cid][l]   < min_load_ended)  min_load_ended = LOAD_END[cid][l]; 
967            if (LOAD_END[cid][l]   > max_load_ended)  max_load_ended = LOAD_END[cid][l];
968            if (TRSP_START[cid][l] < min_trsp_start)  min_trsp_start = TRSP_START[cid][l];
969            if (TRSP_START[cid][l] > max_trsp_start)  max_trsp_start = TRSP_START[cid][l];
970            if (TRSP_END[cid][l]   < min_trsp_ended)  min_trsp_ended = TRSP_END[cid][l];
971            if (TRSP_END[cid][l]   > max_trsp_ended)  max_trsp_ended = TRSP_END[cid][l];
972            if (SAVE_START[cid][l] < min_save_start)  min_save_start = SAVE_START[cid][l];
973            if (SAVE_START[cid][l] > max_save_start)  max_save_start = SAVE_START[cid][l];
974            if (SAVE_END[cid][l]   < min_save_ended)  min_save_ended = SAVE_END[cid][l];
975            if (SAVE_END[cid][l]   > max_save_ended)  max_save_ended = SAVE_END[cid][l];
976            if (FREE_START[cid][l] < min_free_start)  min_free_start = FREE_START[cid][l];
977            if (FREE_START[cid][l] > max_free_start)  max_free_start = FREE_START[cid][l];
978            if (FREE_END[cid][l]   < min_free_ended)  min_free_ended = FREE_END[cid][l];
979            if (FREE_END[cid][l]   > max_free_ended)  max_free_ended = FREE_END[cid][l];
980        }
981    }
982
983    printf( "\n ------ %s ------\n" , filename );
984    fprintf( f , "\n ------ %s ------\n" , filename );
985
986    printf( " - ALOC_START : min = %d / max = %d / delta = %d\n",
987           min_aloc_start, max_aloc_start, max_aloc_start-min_aloc_start ); 
988    fprintf( f , " - ALOC_START : min = %d / max = %d / delta = %d\n",
989           min_aloc_start, max_aloc_start, max_aloc_start-min_aloc_start ); 
990
991    printf( " - ALOC_END   : min = %d / max = %d / delta = %d\n",
992           min_aloc_start, max_aloc_start, max_aloc_start-min_aloc_start ); 
993    fprintf( f , " - ALOC_END   : min = %d / max = %d / delta = %d\n",
994           min_aloc_start, max_aloc_start, max_aloc_start-min_aloc_start ); 
995
996    printf( " - LOAD_START : min = %d / max = %d / delta = %d\n",
997           min_load_start, max_load_start, max_load_start-min_load_start ); 
998    fprintf( f , " - LOAD_START : min = %d / max = %d / delta = %d\n",
999           min_load_start, max_load_start, max_load_start-min_load_start ); 
1000
1001    printf( " - LOAD_END   : min = %d / max = %d / delta = %d\n",
1002           min_load_ended, max_load_ended, max_load_ended-min_load_ended ); 
1003    fprintf( f , " - LOAD_END   : min = %d / max = %d / delta = %d\n",
1004           min_load_ended, max_load_ended, max_load_ended-min_load_ended ); 
1005
1006    printf( " - TRSP_START : min = %d / max = %d / delta = %d\n",
1007           min_trsp_start, max_trsp_start, max_trsp_start-min_trsp_start ); 
1008    fprintf( f , " - TRSP_START : min = %d / max = %d / delta = %d\n",
1009           min_trsp_start, max_trsp_start, max_trsp_start-min_trsp_start ); 
1010
1011    printf( " - TRSP_END   : min = %d / max = %d / delta = %d\n",
1012           min_trsp_ended, max_trsp_ended, max_trsp_ended-min_trsp_ended ); 
1013    fprintf( f , " - TRSP_END   : min = %d / max = %d / delta = %d\n",
1014           min_trsp_ended, max_trsp_ended, max_trsp_ended-min_trsp_ended ); 
1015
1016    printf( " - SAVE_START : min = %d / max = %d / delta = %d\n",
1017           min_save_start, max_save_start, max_save_start-min_save_start ); 
1018    fprintf( f , " - SAVE_START : min = %d / max = %d / delta = %d\n",
1019           min_save_start, max_save_start, max_save_start-min_save_start ); 
1020
1021    printf( " - SAVE_END   : min = %d / max = %d / delta = %d\n",
1022           min_save_ended, max_save_ended, max_save_ended-min_save_ended ); 
1023    fprintf( f , " - SAVE_END   : min = %d / max = %d / delta = %d\n",
1024           min_save_ended, max_save_ended, max_save_ended-min_save_ended ); 
1025
1026    printf( " - FREE_START : min = %d / max = %d / delta = %d\n",
1027           min_free_start, max_free_start, max_free_start-min_free_start ); 
1028    fprintf( f , " - FREE_START : min = %d / max = %d / delta = %d\n",
1029           min_free_start, max_free_start, max_free_start-min_free_start ); 
1030
1031    printf( " - FREE_END   : min = %d / max = %d / delta = %d\n",
1032           min_free_start, max_free_start, max_free_start-min_free_start ); 
1033    fprintf( f , " - FREE_END   : min = %d / max = %d / delta = %d\n",
1034           min_free_start, max_free_start, max_free_start-min_free_start ); 
1035
1036
1037    printf( "\n   Sequencial %d"
1038            "\n   Parallel   %d"
1039            "\n   Alloc      %d"
1040            "\n   Load       %d"
1041            "\n   Transpose  %d"
1042            "\n   Save       %d"
1043            "\n   Free       %d\n" ,
1044            SEQUENCIAL_TIME / 1000 ,
1045            PARALLEL_TIME / 1000 ,
1046            (max_aloc_ended - min_aloc_start) / 1000 ,
1047            (max_load_ended - min_load_start) / 1000 ,
1048            (max_trsp_ended - min_trsp_start) / 1000 ,
1049            (max_save_ended - min_save_start) / 1000 ,
1050            (max_free_ended - min_free_start) / 1000 );
1051
1052    fprintf( f , "\n   Sequencial %d"
1053            "\n   Parallel   %d"
1054            "\n   Alloc      %d"
1055            "\n   Load       %d"
1056            "\n   Transpose  %d"
1057            "\n   Save       %d"
1058            "\n   Free       %d\n" ,
1059            SEQUENCIAL_TIME / 1000 ,
1060            PARALLEL_TIME / 1000 ,
1061            (max_aloc_ended - min_aloc_start) / 1000 ,
1062            (max_load_ended - min_load_start) / 1000 ,
1063            (max_trsp_ended - min_trsp_start) / 1000 ,
1064            (max_save_ended - min_save_start) / 1000 ,
1065            (max_free_ended - min_free_start) / 1000 );
1066}  // end instrument()
1067
1068
1069
1070
Note: See TracBrowser for help on using the repository browser.