source: trunk/user/fft/fft.c @ 652

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

Introduce the three placement modes in "transpose", "convol', "fft" applications.

File size: 54.1 KB
Line 
1/*************************************************************************/
2/*                                                                       */
3/*  Copyright (c) 1994 Stanford University                               */
4/*                                                                       */
5/*  All rights reserved.                                                 */
6/*                                                                       */
7/*  Permission is given to use, copy, and modify this software for any   */
8/*  non-commercial purpose as long as this copyright notice is not       */
9/*  removed.  All other uses, including redistribution in whole or in    */
10/*  part, are forbidden without prior written permission.                */
11/*                                                                       */
12/*  This software is provided with absolutely no warranty and no         */
13/*  support.                                                             */
14/*                                                                       */
15/*************************************************************************/
16
17////////////////////////////////////////////////////////////////////////////////////////
18// This port of the SPLASH FFT benchmark on the ALMOS-MKH OS has been
19// done by Alain Greiner (august 2018).
20//
21// This application performs the 1D fast Fourier transfom for an array
22// of N complex points, using the Cooley-Tuckey FFT method.
23// The N data points are seen as a 2D array (rootN rows * rootN columns).
24// Each thread handle (rootN / nthreads) rows.
25// The N input data points can be initialised in three different modes:
26// - CONSTANT : all data points have the same [1,0] value
27// - COSIN    : data point n has [cos(n/N) , sin(n/N)] values
28// - RANDOM   : data points have pseudo random values
29//
30// The main parameters for this generic application are the following:     
31//  - M : N = 2**M = number of data points / M must be an even number.
32//  - T : nthreads = ncores defined by the hardware / must be power of 2.
33// The number of threads cannot be larger than the number of rows.
34//
35// This application uses 3 shared data arrays, that are dynamically
36// allocated and distributed in clusters, with one sub-buffer per cluster:
37// - data[N] contains N input data points,
38// - trans[N] contains N intermediate data points,
39// - twid[N] contains N coefs : exp(2*pi*i*j/N) / i and j in [0,rootN-1]
40// Each sub-buffer contains (N/nclusters) entries, with 2 double per entry.
41// These distributed buffers are allocated and initialised in parallel
42// by the working threads running on core 0 in each cluster.
43//
44// Each working thread allocates also a private coefs[rootN-1] buffer,
45// that contains all coefs required for a rootN points FFT.
46//
47// The actual number of cores and cluster in a given hardware architecture
48// is obtained by the get_config() syscall (x_size, y_size, ncores).
49// The max number of clusters is bounded by (X_MAX * Y_MAX).
50// The max number of cores per cluster is bounded by CORES_MAX.
51//
52// The number N of working threads is always defined by the number of cores availables
53// in the architecture, but this application supports three placement modes.
54// In all modes, the working threads are identified by the [tid] continuous index
55// in range [0, NTHREADS-1], and defines how the lines are shared amongst the threads.
56// This continuous index can always be decomposed in two continuous sub-indexes:
57// tid == cid * ncores + lid,  where cid is in [0,NCLUSTERS-1] and lid in [0,NCORES-1].
58//
59// - NO_PLACEMENT: the main thread is itsef a working thread. The (N_1) other working
60//   threads are created by the main thread, but the placement is done by the OS, using
61//   the DQDT for load balancing, and two working threads can be placed on the same core.
62//   The [cid,lid] are only abstract identifiers, and cannot be associated to a physical
63//   cluster or a physical core. In this mode, the main thread run on any cluster,
64//   but has tid = 0 (i.e. cid = 0 & tid = 0).
65//
66// - EXPLICIT_PLACEMENT: the main thread is again a working thread, but the placement of
67//   of the threads on the cores is explicitely controled by the main thread to have
68//   exactly one working thread per core, and the [cxy][lpid] core coordinates for a given
69//   thread[tid] can be directly derived from the [tid] value: [cid] is an alias for the
70//   physical cluster identifier, and [lid] is the local core index.
71//
72// - PARALLEL_PLACEMENT: the main thread is not anymore a working thread, and uses the
73//   non standard pthread_parallel_create() function to avoid the costly sequencial
74//   loops for pthread_create() and pthread_join(). It garanty one working thread
75//   per core, and the same relation between the thread[tid] and the core[cxy][lpid].
76//
77// Several others configuration parameters can be defined below:
78//  - USE_DQT_BARRIER : use a hierarchical barrier for working threads synchro
79//  - PRINT_ARRAY     : Print out complex data points arrays.
80//  - CHECK           : Perform both FFT and inverse FFT to check output/input.
81//  - DEBUG_MAIN      : Display intermediate results in main()
82//  - DEBUG_FFT1D     : Display intermediate results in FFT1D()
83//  - DEBUG_ROW       : Display intermedite results in FFTrow()
84//
85// Regarding final instrumentation:
86// - the sequencial initialisation time (init_time) is computed
87//   by the main thread in the main() function.
88// - The parallel execution time (parallel_time[i]) is computed by each
89//   working thread(i) in the work() function.
90// - The synchronisation time related to the barriers (sync_time[i])
91//   is computed by each thread(i) in the work() function.
92// The results are displayed on the TXT terminal, and registered on disk.
93///////////////////////////////////////////////////////////////////////////////////////
94
95#include <math.h>
96#include <stdio.h>
97#include <stdlib.h>
98#include <fcntl.h>
99#include <unistd.h>
100#include <pthread.h>
101#include <almosmkh.h>
102#include <hal_macros.h>
103
104// constants
105
106#define PI                      3.14159265359
107#define PAGE_SIZE               4096
108#define X_MAX                   16              // max number of clusters in a row
109#define Y_MAX                   16              // max number of clusters in a column
110#define CORES_MAX               4               // max number of cores in a cluster
111#define CLUSTERS_MAX            X_MAX * Y_MAX
112#define THREADS_MAX             CLUSTERS_MAX * CORES_MAX
113#define RANDOM                  0
114#define COSIN                   1
115#define CONSTANT                2
116
117// parameters
118
119#define NO_PLACEMENT            1
120#define EXPLICIT_PLACEMENT      0
121#define PARALLEL_PLACEMENT      0
122
123#define DEFAULT_M               18              // 256 K complex points
124#define USE_DQT_BARRIER         1               // use DDT barrier if non zero
125#define MODE                    COSIN           // DATA array initialisation mode
126#define CHECK                   0               
127#define DEBUG_MAIN              1               // trace main() function (detailed if odd)
128#define DEBUG_WORK              0               // trace work() function (detailed if odd)
129#define DEBUG_FFT1D             0               // trace FFT1D() function (detailed if odd)
130#define DEBUG_ROW               0               // trace FFTRow() function (detailed if odd)
131#define PRINT_ARRAY             0
132#define DISPLAY_SCHED_AND_VMM   0               // display final VMM state in all clusters
133
134// macro to swap two variables
135#define SWAP(a,b) { double tmp; tmp = a; a = b; b = tmp; }
136
137/////////////////////////////////////////////////////////////////////////////////////
138//             FFT global variables
139/////////////////////////////////////////////////////////////////////////////////////
140
141unsigned int   x_size;                     // platform global parameter
142unsigned int   y_size;                     // platform global parameter
143unsigned int   ncores;                     // platform global parameter
144
145unsigned int   nthreads;                   // total number of threads (one thread per core)
146unsigned int   nclusters;                  // total number of clusters
147unsigned int   M = DEFAULT_M;              // log2(number of points)
148unsigned int   N;                          // number of points (N = 2^M)         
149unsigned int   rootN;                      // rootN = 2^M/2   
150unsigned int   rows_per_thread;            // number of data "rows" handled by a single thread
151unsigned int   points_per_cluster;         // number of data points per cluster
152
153// arrays of pointers on distributed buffers (one sub-buffer per cluster)
154double *       data[CLUSTERS_MAX];         // original time-domain data
155double *       trans[CLUSTERS_MAX];        // used as auxiliary space for fft
156double *       twid[CLUSTERS_MAX];         // twiddle factor : exp(-2iPI*k*n/N)
157double *       bloup[CLUSTERS_MAX];        // used as auxiliary space for DFT
158
159// instrumentation counters
160unsigned int   pgfault_nr[THREADS_MAX];    // total number of page faults (per thread)
161unsigned int   pgfault_cost[THREADS_MAX];  // total page faults cost (per thread)
162unsigned int   pgfault_max[THREADS_MAX];   // max page faults cost (per thread)
163unsigned int   parallel_time[THREADS_MAX]; // total computation time (per thread)
164unsigned int   sync_time[THREADS_MAX];     // cumulated waiting time in barriers (per thread)
165unsigned int   init_time;                  // initialisation time (in main)
166
167// synchronisation barrier (all threads)
168pthread_barrier_t      barrier;
169pthread_barrierattr_t  barrier_attr;
170
171//return values at thread exit
172unsigned int   THREAD_EXIT_SUCCESS = 0;
173unsigned int   THREAD_EXIT_FAILURE = 1;
174
175// main thread continuous index
176unsigned int     tid_main; 
177
178// array of kernel thread identifiers / indexed by [tid]
179pthread_t      work_trdid[CLUSTERS_MAX * CORES_MAX];   
180
181// array of thread attributes / indexed by [tid]
182pthread_attr_t work_attr[CLUSTERS_MAX * CORES_MAX];
183
184// array of work function arguments / indexed by [tid]
185pthread_parallel_work_args_t work_args[CLUSTERS_MAX * CORES_MAX];
186
187/////////////////////////////////////////////////////////////////////////////////////
188//           functions declaration
189/////////////////////////////////////////////////////////////////////////////////////
190
191void work( pthread_parallel_work_args_t * args );
192
193double CheckSum( void );
194
195void InitD( double    ** data , 
196            unsigned int mode,
197            unsigned int tid );
198
199void InitT( double    ** twid,
200            unsigned int tid );
201
202void InitU( double * coefs );
203
204unsigned int BitReverse( unsigned int k );
205
206void FFT1D( int          direction,
207            double    ** x,
208            double    ** tmp,
209            double     * upriv, 
210            double    ** twid,
211            unsigned int tid,
212            unsigned int MyFirst,
213            unsigned int MyLast );
214
215void TwiddleOneCol( int          direction,
216                    unsigned int j,
217                    double    ** u,
218                    double    ** x,
219                    unsigned int offset_x );
220
221void Scale( double    ** x,
222            unsigned int offset_x );
223
224void Transpose( double    ** src, 
225                double    ** dest,
226                unsigned int MyFirst,
227                unsigned int MyLast );
228
229void Copy( double    ** src,
230           double    ** dest,
231           unsigned int MyFirst,
232           unsigned int MyLast );
233
234void Reverse( double    ** x, 
235              unsigned int offset_x );
236
237void FFTRow( int          direction,
238                double     * u,
239                double    ** x,
240                unsigned int offset_x );
241
242void PrintArray( double ** x,
243                 unsigned int size );
244
245void SimpleDft( int          direction,
246                unsigned int size,
247                double    ** src,
248                unsigned int src_offset,
249                double    ** dst,
250                unsigned int dst_offset );
251
252///////////////////////////////////////////////////////////////////
253// This main() function execute the sequencial initialisation
254// launch the parallel execution, and makes the instrumentation.
255///////////////////////////////////////////////////////////////////
256void main ( void )
257{
258    int                 error;
259
260    unsigned int        tid;               // continuous thread index
261
262    char                name[64];          // instrumentation file name
263    char                path[128];         // instrumentation path name
264    char                string[256];
265    int                 ret;
266
267    unsigned long long  start_init_cycle; 
268    unsigned long long  end_init_cycle;
269
270#if DEBUG_MAIN
271    unsigned long long  debug_cycle;
272#endif
273
274#if CHECK
275    double              ck1;               // for input/output checking
276    double              ck3;               // for input/output checking
277#endif
278   
279    int                 pid = getpid();
280
281    // check placement mode
282    if( (NO_PLACEMENT + EXPLICIT_PLACEMENT + PARALLEL_PLACEMENT) != 1 )
283    {
284        printf("\n[fft error] illegal placement mode\n");
285        exit( 0 );
286    }
287
288    // get FFT application start cycle
289    get_cycle( &start_init_cycle );
290
291    // get platform parameters
292    if( get_config( &x_size , &y_size , &ncores ) )
293    {
294        printf("\n[fft error] cannot get hardware configuration\n");
295        exit( 0 );
296    }
297
298    // check ncores
299    if( (ncores != 1) && (ncores != 2) && (ncores != 4) )
300    {
301        printf("\n[fft error] number of cores per cluster must be 1/2/4\n");
302        exit( 0 );
303    }
304
305    // check x_size
306    if( (x_size != 1) && (x_size != 2) && (x_size != 4) && (x_size != 8) && (x_size != 16) )
307    {
308        printf("\n[fft error] x_size must be 1/2/4/8/16\n");
309        exit( 0 );
310    }
311
312    // check y_size
313    if( (y_size != 1) && (y_size != 2) && (y_size != 4) && (y_size != 8) && (y_size != 16) )
314    {
315        printf("\n[fft error] y_size must be 1/2/4/8/16\n");
316        exit( 0 );
317    }
318
319    // get identifiers for core executing main
320    unsigned int  cxy_main;
321    unsigned int  lid_main;
322    get_core_id( &cxy_main , &lid_main );
323
324    // compute nthreads and nclusters
325    nthreads  = x_size * y_size * ncores;
326    nclusters = x_size * y_size;
327
328    // compute covering DQT size an level
329    unsigned int z = (x_size > y_size) ? x_size : y_size;
330    unsigned int root_level = (z == 1) ? 0 : (z == 2) ? 1 : (z == 4) ? 2 : (z == 8) ? 3 : 4;
331
332    // compute various constants depending on N and T
333    N                  = 1 << M;
334    rootN              = 1 << (M / 2);
335    rows_per_thread    = rootN / nthreads;
336    points_per_cluster = N / nclusters;
337 
338    // check N versus T
339    if( rootN < nthreads )
340    {
341        printf("\n[fft error] sqrt(N) must be larger than T\n");
342        exit( 0 );
343    }
344
345    // define instrumentation file name
346    if( NO_PLACEMENT )
347    {
348        printf("\n[fft] starts / %d points / %d thread(s) / PID %x / NO_PLACE\n",
349        N, nthreads, pid );
350
351        // build instrumentation file name
352        if( USE_DQT_BARRIER )
353        snprintf( name , 64 , "fft_dqt_no_place_%d_%d_%d", M , x_size * y_size , ncores );
354        else
355        snprintf( name , 64 , "fft_smp_no_place_%d_%d_%d", M , x_size * y_size , ncores );
356    }
357
358    if( EXPLICIT_PLACEMENT )
359    {
360        printf("\n[fft] starts / %d points / %d thread(s) / PID %x / EXPLICIT\n",
361        N, nthreads, pid );
362
363        // build instrumentation file name
364        if( USE_DQT_BARRIER )
365        snprintf( name , 64 , "fft_dqt_explicit_%d_%d_%d", M , x_size * y_size , ncores );
366        else
367        snprintf( name , 64 , "fft_smp_explicit_%d_%d_%d", M , x_size * y_size , ncores );
368    }
369
370    if( PARALLEL_PLACEMENT )
371    {
372        printf("\n[fft] starts / %d points / %d thread(s) / PID %x / PARALLEL\n",
373        N, nthreads, pid );
374
375        // build instrumentation file name
376        if( USE_DQT_BARRIER )
377        snprintf( name , 64 , "fft_dqt_parallel_%d_%d_%d", M , x_size * y_size , ncores );
378        else
379        snprintf( name , 64 , "fft_smp_parallel_%d_%d_%d", M , x_size * y_size , ncores );
380    }
381
382    // build instrumentation file pathname
383    snprintf( path , 128 , "/home/%s", name );
384
385    // open instrumentation file
386    FILE * f = fopen( path , NULL );
387    if ( f == NULL ) 
388    { 
389        printf("\n[fft error] cannot open instrumentation file <%s>\n", path );
390        exit( 0 );
391    }
392
393#if DEBUG_MAIN
394get_cycle( &debug_cycle );
395printf("\n[fft] main open instrumentation file <%s> at cycle %d\n",
396path, (unsigned int)debug_cycle );
397#endif
398
399#if CHECK
400ck1 = CheckSum();
401#endif
402
403#if PRINT_ARRAY
404printf("\nData values / base = %x\n", &data[0][0] );
405PrintArray( data , N );
406
407printf("\nTwiddle values / base = %x\n", &twid[0][0] );
408PrintArray( twid , N );
409
410SimpleDft( 1 , N , data , 0 , bloup , 0 );
411
412printf("\nExpected results / base = %x\n", &bloup[0][0] );
413PrintArray( bloup , N );
414#endif
415
416    // initialise barrier synchronizing all <work> threads
417    if( USE_DQT_BARRIER )
418    {
419        barrier_attr.x_size   = x_size;
420        barrier_attr.y_size   = y_size;
421        barrier_attr.nthreads = ncores;
422        error = pthread_barrier_init( &barrier, &barrier_attr , nthreads );
423    }
424    else
425    {
426        error = pthread_barrier_init( &barrier, NULL , nthreads );
427    }
428
429    if( error )
430    {
431        printf("\n[fft error] cannot initialize barrier\n");
432        exit( 0 );
433    }
434
435#if DEBUG_MAIN
436get_cycle( &debug_cycle );
437printf("\n[fft] main completes sequencial initialisation at cycle %d\n",
438(unsigned int)debug_cycle );
439#endif
440
441    // register sequencial time
442    get_cycle( &end_init_cycle );
443    init_time = (unsigned int)(end_init_cycle - start_init_cycle);
444
445    //////////////////
446    if( NO_PLACEMENT )
447    {
448        // the tid value for the main thread is always 0
449        // main thread creates new threads with tid in [1,nthreads-1] 
450        unsigned int tid;
451        for ( tid = 0 ; tid < nthreads ; tid++ )
452        {
453            // register tid value in work_args[tid] array
454            work_args[tid].tid = tid;
455           
456            // create other threads
457            if( tid > 0 )
458            {
459                if ( pthread_create( &work_trdid[tid], 
460                                     NULL,                  // no attribute
461                                     &work,
462                                     &work_args[tid] ) ) 
463                {
464                    printf("\n[fft error] cannot create thread %d\n", tid );
465                    exit( 0 );
466                }
467
468#if DEBUG_MAIN
469printf("\n[fft] main created thread %d\n", tid );
470#endif
471
472            }
473            else
474            {
475                tid_main = 0;
476            }
477        }  // end for tid
478
479        // main thread calls itself the execute() function
480        work( &work_args[0] );
481
482        // main thread wait other threads completion
483        for ( tid = 1 ; tid < nthreads ; tid++ )
484        {
485            unsigned int * status;
486
487            // main wait thread[tid] status
488            if ( pthread_join( work_trdid[tid], (void*)(&status)) )
489            {
490                printf("\n[fft error] main cannot join thread %d\n", tid );
491                exit( 0 );
492            }
493       
494            // check status
495            if( *status != THREAD_EXIT_SUCCESS )
496            {
497                printf("\n[fft error] thread %x returned failure\n", tid );
498                exit( 0 );
499            }
500
501#if DEBUG_MAIN
502printf("\n[fft] main successfully joined thread %x\n", tid );
503#endif
504       
505        }  // end for tid
506
507    }  // end if no_placement
508
509    ////////////////////////
510    if( EXPLICIT_PLACEMENT )
511    {
512        // main thread places each thread[tid] on a specific core[cxy][lid]
513        // but the actual thread creation is sequencial
514        unsigned int x;
515        unsigned int y;
516        unsigned int l;
517        unsigned int cxy;                   // cluster identifier
518        unsigned int tid;                   // thread continuous index
519
520        for( x = 0 ; x < x_size ; x++ )
521        {
522            for( y = 0 ; y < y_size ; y++ )
523            {
524                cxy = HAL_CXY_FROM_XY( x , y );
525                for( l = 0 ; l < ncores ; l++ )
526                {
527                    // compute thread continuous index
528                    tid = (((* y_size) + y) * ncores) + l;
529
530                    // register tid value in work_args[tid] array
531                    work_args[tid].tid = tid;
532
533                    // no thread created on the core running the main
534                    if( (cxy != cxy_main) || (l != lid_main) )
535                    {
536                        // define thread attributes
537                        work_attr[tid].attributes = PT_ATTR_CLUSTER_DEFINED |
538                                                    PT_ATTR_CORE_DEFINED;
539                        work_attr[tid].cxy        = cxy;
540                        work_attr[tid].lid        = l;
541 
542                        // create thread[tid] on core[cxy][l]
543                        if ( pthread_create( &work_trdid[tid],   
544                                             &work_attr[tid],   
545                                             &work,
546                                             &work_args[tid] ) )       
547                        {
548                            printf("\n[fft error] cannot create thread %d\n", tid );
549                            exit( 0 );
550                        }
551#if DEBUG_MAIN
552printf("\n[fft] main created thread[%d] on core[%x,%d]\n", tid, cxy, l );
553#endif
554                    }
555                    else
556                    {
557                        tid_main = tid;
558                    }
559                }
560            }
561        }
562
563        // main thread calls itself the execute() function
564        work( &work_args[tid_main] );
565
566        // main thread wait other threads completion
567        for( tid = 0 ; tid < nthreads ; tid++ )
568        {
569            // no other thread on the core running the main
570            if( tid != tid_main )
571            {
572                unsigned int * status;
573
574                // wait thread[tid]
575                if( pthread_join( work_trdid[tid] , (void*)(&status) ) )
576                {
577                    printf("\n[fft error] main cannot join thread %d\n", tid );
578                    exit( 0 );
579                }
580       
581                // check status
582                if( *status != THREAD_EXIT_SUCCESS )
583                {
584                    printf("\n[fft error] thread %d returned failure\n", tid );
585                    exit( 0 );
586                }
587#if DEBUG_MAIN
588printf("\n[fft] main joined thread %d on core[%x,%d]\n", tid , cxy , l );
589#endif
590            }
591        }
592    }  // end if explicit_placement
593
594    ////////////////////////
595    if( PARALLEL_PLACEMENT )
596    {
597        // create and execute the working threads
598        if( pthread_parallel_create( root_level , &work ) )
599        {
600            printf("\n[fft error] cannot create threads\n");
601            exit( 0 );
602        }
603    }
604
605#if DEBUG_MAIN
606get_cycle( &debug_cycle );
607printf("\n[fft] main resume for instrumentation at cycle %d\n",
608(unsigned int)debug_cycle) ;
609#endif
610
611#if PRINT_ARRAY
612printf("\nData values after FFT:\n");
613PrintArray( data , N );
614#endif
615
616#if CHECK
617ck3 = CheckSum();
618printf("\n*** Results ***\n");
619printf("Checksum difference is %f (%f, %f)\n", ck1 - ck3, ck1, ck3);
620if (fabs(ck1 - ck3) < 0.001)  printf("Results OK\n");
621else                          printf("Results KO\n");
622#endif
623
624    // display header on terminal, and save to file
625    printf("\n----- %s -----\n", name );
626
627    ret = fprintf( f , "\n----- %s -----\n", name );
628    if( ret < 0 )
629    {
630        printf("\n[fft error] cannot write header to file <%s>\n", path );
631        exit(0);
632    }
633
634    // initializes global (all threads) instrumentation values
635    unsigned int time_para      = parallel_time[0];
636    unsigned int time_sync      = sync_time[0];
637    unsigned int pgfaults_nr    = 0;
638    unsigned int pgfaults_cost  = 0;
639    unsigned int pgfaults_max   = pgfault_max[0];
640
641    // loop on threads to compute global instrumentation results
642    for (tid = 0 ; tid < nthreads ; tid++) 
643    {
644        snprintf( string , 256 ,
645        "- tid %d : Seq %d / Para %d / Sync %d / Pgfaults %d ( cost %d / max %d )\n",
646        tid, init_time, parallel_time[tid], sync_time[tid], 
647        pgfault_nr[tid], (pgfault_cost[tid] / pgfault_nr[tid]) , pgfault_max[tid] );
648
649        // save  to instrumentation file
650        fprintf( f , "%s" , string );
651        if( ret < 0 )
652        {
653            printf("\n[fft error] cannot save thread %d results to file <%s>\n", tid, path );
654            printf("%s", string );
655            exit(0);
656        }
657
658        // compute global values
659        if (parallel_time[tid] > time_para)    time_para      = parallel_time[tid];
660        if (sync_time[tid]     > time_sync)    time_sync      = sync_time[tid];
661
662        pgfaults_nr   += pgfault_nr[tid];
663        pgfaults_cost += pgfault_cost[tid];
664
665        if (pgfault_max[tid]   > pgfaults_max) pgfaults_max   = pgfault_max[tid];
666    }
667
668    // display global values on terminal and save to file
669    snprintf( string , 256 ,
670    "\nSeq %d / Para %d / Sync %d / Pgfaults %d ( cost %d / max %d )\n",
671    init_time, time_para, time_sync, pgfaults_nr, (pgfaults_cost / pgfaults_nr), pgfaults_max );
672
673    printf("%s", string );
674
675    // save global values to file
676    ret = fprintf( f , "%s", string );
677
678    if( ret < 0 )
679    {
680        printf("\n[fft error] cannot save global results to file <%s>\n", path );
681        exit(0);
682    }
683
684    // close instrumentation file
685    ret = fclose( f );
686
687    if( ret < 0 )
688    {
689        printf("\n[fft error] cannot close file <%s>\n", path );
690        exit(0);
691    }
692
693#if DEBUG_MAIN
694get_cycle( &debug_cycle );
695printf("\n[fft] main exit <%s> at cycle %d\n",
696path, (unsigned int)debug_cycle );
697#endif
698
699    exit( 0 );
700
701} // end main()
702
703/////////////////////////////////////////////////////////////////
704// This function is executed in parallel by all <work> threads.
705/////////////////////////////////////////////////////////////////
706void work( pthread_parallel_work_args_t * args ) 
707{
708    unsigned int        tid;              // this thread continuous index
709    unsigned int        lid;              // core local index
710    unsigned int        cid;              // cluster continuous index
711    pthread_barrier_t * parent_barrier;   // pointer on parent barrier
712
713    unsigned int        MyFirst;          // index first row allocated to thread
714    unsigned int        MyLast;           // index last row allocated to thread
715    double            * upriv;            // private array of FFT coefs
716
717    unsigned long long  parallel_start;
718    unsigned long long  parallel_stop;
719    unsigned long long  barrier_start;
720    unsigned long long  barrier_stop;
721
722    get_cycle( &parallel_start );
723
724    // get thread arguments
725    tid            = args->tid; 
726    parent_barrier = args->barrier;
727
728    // compute lid and cid from tid
729    lid            = tid % ncores;             
730    cid            = tid / ncores;
731           
732#if DEBUG_WORK
733printf("\n[fft] %s : thread %d enter / cycle %d\n",
734__FUNCTION__, tid, (unsigned int)parallel_start );
735#endif
736
737    // thread on core 0 allocates memory from the local cluster
738    // for the distributed data[], trans[], twid[] buffers
739    if( lid == 0 )
740    {
741        unsigned int data_size = (N / nclusters) * 2 * sizeof(double);
742
743        data[cid] = (double *)malloc( data_size ); 
744        if( data[cid] == NULL )
745        {
746            printf("\n[fft_error] in work : cannot allocate data[%d] buffer\n", cid );
747            pthread_barrier_wait( parent_barrier );
748            pthread_exit( NULL );
749        }
750       
751        trans[cid] = (double *)malloc( data_size ); 
752        if( trans[cid] == NULL )
753        {
754            printf("\n[fft_error] in work : cannot allocate trans[%d] buffer\n", cid );
755            pthread_barrier_wait( parent_barrier );
756            pthread_exit( NULL );
757        }
758       
759        twid[cid] = (double *)malloc( data_size ); 
760        if( twid[cid] == NULL )
761        {
762            printf("\n[fft_error] in work : cannot allocate twid[%d] buffer\n", cid );
763            pthread_barrier_wait( parent_barrier );
764            pthread_exit( NULL );
765        }
766    }
767
768    // BARRIER to wait distributed buffers allocation
769    get_cycle( &barrier_start );
770    pthread_barrier_wait( &barrier );
771    get_cycle( &barrier_stop );
772    sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
773
774#if DEBUG_WORK
775printf("\n[fft] %s : thread %d exit barrier for buffer allocation / cycle %d\n",
776__FUNCTION__, tid, (unsigned int)barrier_stop );
777#endif
778
779    // all threads contribute to data[] local array initialisation
780    InitD( data , MODE , tid ); 
781
782    // all threads contribute to data[] local array initialisation
783    InitT( twid , tid );
784   
785    // BARRIER to wait distributed buffers initialisation
786    get_cycle( &barrier_start );
787    pthread_barrier_wait( &barrier );
788    get_cycle( &barrier_stop );
789    sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
790
791#if DEBUG_WORK
792printf("\n[fft] %s : thread %d exit barrier for buffer initialisation / cycle %d\n",
793__FUNCTION__, tid, (unsigned int)barrier_stop );
794#endif
795
796    // all threads allocate memory from the local cluster
797    // for the private upriv[] buffer
798    upriv = (double *)malloc( (rootN - 1) * 2 * sizeof(double) );
799    if( upriv == NULL )
800    {
801        printf("\n[fft_error] in work : cannot allocate trans[%d] buffer\n", cid );
802        pthread_barrier_wait( parent_barrier );
803        pthread_exit( NULL );
804    }
805
806    // all threads initialise the private upriv[] array
807    InitU( upriv );
808
809    // all threads compute first and last rows handled by the thread
810    MyFirst = rootN * tid / nthreads;
811    MyLast  = rootN * (tid + 1) / nthreads;
812
813    // all threads perform forward FFT
814    FFT1D( 1 , data , trans , upriv , twid , tid , MyFirst , MyLast );
815
816#if CHECK
817get_cycle( &barrier_start );
818pthread_barrier_wait( &barrier );
819get_cycle( &barrier_stop );
820sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
821FFT1D( -1 , data , trans , upriv , twid , tid , MyFirst , MyLast );
822#endif
823
824    get_cycle( &parallel_stop );
825
826    // register parallel time in instrumentation counters
827    parallel_time[tid] = (unsigned int)(parallel_stop - parallel_start);
828
829    // get work thread info for page faults
830    thread_info_t info;
831    get_thread_info( &info );
832   
833    // register page faults in instrumentation counters
834    pgfault_nr[tid]   = info.false_pgfault_nr + 
835                        info.local_pgfault_nr + 
836                        info.global_pgfault_nr;
837    pgfault_cost[tid] = info.false_pgfault_cost + 
838                        info.local_pgfault_cost + 
839                        info.global_pgfault_cost;
840    pgfault_max[tid]  = info.false_pgfault_max + 
841                        info.local_pgfault_max + 
842                        info.global_pgfault_max;
843#if DEBUG_WORK
844printf("\n[fft] %s : thread %d completes fft / p_start %d / p_stop %d\n", 
845__FUNCTION__, tid, (unsigned int)parallel_start, (unsigned int)parallel_stop );
846#endif
847
848    //  work thread signals completion to main
849    pthread_barrier_wait( parent_barrier );
850
851#if DEBUG_WORK
852printf("\n[fft] %s : thread %d exit\n", 
853__FUNCTION__, tid );
854#endif
855
856#if DISPLAY_SCHED_AND_VMM
857printf("\n[fft] %s : thread %d exit\n", __FUNCTION__, tid );
858if( lid == 0 ) display_vmm( cxy , getpid() , 0 );
859#endif
860
861    //  work thread exit
862    pthread_exit( NULL );
863
864}  // end work()
865
866////////////////////////////////////////////////////////////////////////////////////////
867// This function makes the DFT from the src[nclusters][points_per_cluster] distributed
868// buffer, to the dst[nclusters][points_per_cluster] distributed buffer.
869////////////////////////////////////////////////////////////////////////////////////////
870void SimpleDft( int             direction,      // 1 direct / -1 reverse
871                unsigned int    size,           // number of points
872                double       ** src,            // source distributed buffer
873                unsigned int    src_offset,     // offset in source array
874                double       ** dst,            // destination distributed buffer
875                unsigned int    dst_offset )    // offset in destination array
876{
877    unsigned int  n , k;
878    double        phi;            // 2*PI*n*k/N
879    double        u_r;            // cos( phi )
880    double        u_c;            // sin( phi )
881    double        d_r;            // Re(data[n])
882    double        d_c;            // Im(data[n])
883    double        accu_r;         // Re(accu)
884    double        accu_c;         // Im(accu)
885    unsigned int  c_id;           // distributed buffer cluster index
886    unsigned int  c_offset;       // offset in distributed buffer
887
888    for ( k = 0 ; k < size ; k++ )       // loop on the output data points
889    {
890        // initialise accu
891        accu_r = 0;
892        accu_c = 0;
893
894        for ( n = 0 ; n < size ; n++ )   // loop on the input data points
895        {
896            // compute coef
897            phi = (double)(2*PI*n*k) / size;
898            u_r =  cos( phi );
899            u_c = -sin( phi ) * direction;
900
901            // get input data point
902            c_id     = (src_offset + n) / (points_per_cluster);
903            c_offset = (src_offset + n) % (points_per_cluster);
904            d_r      = src[c_id][2*c_offset];
905            d_c      = src[c_id][2*c_offset+1];
906
907            // increment accu
908            accu_r += ((u_r*d_r) - (u_c*d_c));
909            accu_c += ((u_r*d_c) + (u_c*d_r));
910        }
911
912        // scale for inverse DFT
913        if ( direction == -1 )
914        {
915            accu_r /= size;
916            accu_c /= size;
917        }
918
919        // set output data point
920        c_id     = (dst_offset + k) / (points_per_cluster);
921        c_offset = (dst_offset + k) % (points_per_cluster);
922        dst[c_id][2*c_offset]   = accu_r;
923        dst[c_id][2*c_offset+1] = accu_c;
924    }
925
926}  // end SimpleDft()
927
928///////////////////////
929double CheckSum( void )
930{
931    unsigned int         i , j;
932    unsigned int         c_id;
933    unsigned int         c_offset;
934    double               cks;
935
936    cks = 0.0;
937    for (j = 0; j < rootN ; j++) 
938    {
939        for (i = 0; i < rootN ; i++) 
940        {
941            c_id      = (rootN * j + i) / (points_per_cluster);
942            c_offset  = (rootN * j + i) % (points_per_cluster);
943
944            cks += data[c_id][2*c_offset] + data[c_id][2*c_offset+1];
945        }
946    }
947    return(cks);
948}
949
950//////////////////////////////////////////////////////////////////////////////////////
951// Each working thread <tid> contributes to initialize (rootN / nthreads) rows,
952// in the shared - and distributed - <data> array.
953//////////////////////////////////////////////////////////////////////////////////////
954void InitD(double      ** data,
955           unsigned int   mode,
956           unsigned int   tid ) 
957{
958    unsigned int    i , j;
959    unsigned int    c_id;
960    unsigned int    c_offset;
961    unsigned int    index;
962
963    // compute row_min and row_max
964    unsigned int    row_min = tid * rows_per_thread;
965    unsigned int    row_max = row_min + rows_per_thread;
966
967    for ( j = row_min ; j < row_max ; j++ )      // loop on rows
968    { 
969        for ( i = 0 ; i < rootN ; i++ )          // loop on points in a row
970        { 
971            index     = j * rootN + i;
972            c_id      = index / (points_per_cluster);
973            c_offset  = index % (points_per_cluster);
974
975            // complex input signal is random
976            if ( mode == RANDOM )               
977            {
978                data[c_id][2*c_offset]   = ( (double)rand() ) / 65536;
979                data[c_id][2*c_offset+1] = ( (double)rand() ) / 65536;
980            }
981           
982
983            // complex input signal is cos(n/N) / sin(n/N)
984            if ( mode == COSIN )               
985            {
986                double phi = (double)( 2 * PI * index) / N;
987                data[c_id][2*c_offset]   = cos( phi );
988                data[c_id][2*c_offset+1] = sin( phi );
989            }
990
991            // complex input signal is constant
992            if ( mode == CONSTANT )               
993            {
994                data[c_id][2*c_offset]   = 1.0;
995                data[c_id][2*c_offset+1] = 0.0;
996            }
997        }
998    }
999}
1000
1001///////////////////////////////////////////////////////////////////////////////////////
1002// Each working thread <tid> contributes to initialize (rootN / nthreads) rows,
1003// in the shared - and distributed - <twiddle> array.
1004///////////////////////////////////////////////////////////////////////////////////////
1005void InitT( double      ** twid,
1006            unsigned int   tid )
1007{
1008    unsigned int    i, j;
1009    unsigned int    index;
1010    unsigned int    c_id;
1011    unsigned int    c_offset;
1012    double  phi;
1013
1014    // compute row_min and row_max
1015    unsigned int    row_min = tid * rows_per_thread;
1016    unsigned int    row_max = row_min + rows_per_thread;
1017
1018    for ( j = row_min ; j < row_max ; j++ )      // loop on rows
1019    { 
1020        for ( i = 0 ; i < rootN ; i++ )          // loop on points in a row
1021        { 
1022            index     = j * rootN + i;
1023            c_id      = index / (points_per_cluster);
1024            c_offset  = index % (points_per_cluster);
1025
1026            phi = (double)(2.0 * PI * i * j) / N;
1027            twid[c_id][2*c_offset]   = cos( phi );
1028            twid[c_id][2*c_offset+1] = -sin( phi );
1029        }
1030    }
1031}
1032
1033///////////////////////////////////////////////////////////////////////////////////////
1034// Each working thread initialize the private <upriv> array / (rootN - 1) entries.
1035///////////////////////////////////////////////////////////////////////////////////////
1036void InitU( double * upriv ) 
1037{
1038    unsigned int    q; 
1039    unsigned int    j; 
1040    unsigned int    base; 
1041    unsigned int    n1;
1042    double          phi;
1043
1044    for (q = 0 ; ((unsigned int)(1 << q) < N) ; q++) 
1045    { 
1046        n1 = 1 << q;    // n1 == 2**q
1047        base = n1 - 1;
1048        for (j = 0; (j < n1) ; j++) 
1049        {
1050            if (base + j > rootN - 1) return;
1051
1052            phi = (double)(2.0 * PI * j) / (2 * n1);
1053            upriv[2*(base+j)]   = cos( phi );
1054            upriv[2*(base+j)+1] = -sin( phi );
1055        }
1056    }
1057}
1058
1059////////////////////////////////////////////////////////////////////////////////////////
1060// This function returns an index value that is the bit reverse of the input value.
1061////////////////////////////////////////////////////////////////////////////////////////
1062unsigned int BitReverse( unsigned int k ) 
1063{
1064    unsigned int i; 
1065    unsigned int j; 
1066    unsigned int tmp;
1067
1068    j = 0;
1069    tmp = k;
1070    for (i = 0; i < M/2 ; i++) 
1071    {
1072        j = 2 * j + (tmp & 0x1);
1073        tmp = tmp >> 1;
1074    }
1075    return j;
1076}
1077
1078////////////////////////////////////////////////////////////////////////////////////////
1079// This function perform the in place (direct or inverse) FFT on the N data points
1080// contained in the distributed buffers x[nclusters][points_per_cluster].
1081// It handles the (N) points 1D array as a (rootN*rootN) points 2D array.
1082// 1) it fft (rootN/nthreads ) rows from x to tmp.
1083// 2) it make (rootN/nthreads) FFT on the tmp rows and apply the twiddle factor.
1084// 3) it fft (rootN/nthreads) columns from tmp to x.
1085// 4) it make (rootN/nthreads) FFT on the x rows.
1086// It calls the FFTRow() 2*(rootN/nthreads) times to perform the in place FFT
1087// on the rootN points contained in a row.
1088////////////////////////////////////////////////////////////////////////////////////////
1089void FFT1D( int              direction,       // direct 1 / inverse -1
1090            double       **  x,               // input & output distributed data points array
1091            double       **  tmp,             // auxiliary distributed data points array
1092            double        *  upriv,           // local array containing coefs for rootN FFT
1093            double       **  twid,            // distributed arrays containing N twiddle factors
1094            unsigned int     tid,             // thread continuous index
1095            unsigned int     MyFirst, 
1096            unsigned int     MyLast )
1097{
1098    unsigned int j;
1099    unsigned long long barrier_start;
1100    unsigned long long barrier_stop;
1101
1102#if DEBUG_FFT1D
1103unsigned long long cycle;
1104get_cycle( &cycle );
1105printf("\n[fft] %s : thread %d enter / first %d / last %d / cycle %d\n",
1106__FUNCTION__, tid, MyFirst, MyLast, (unsigned int)cycle );
1107#endif
1108
1109    // fft (rootN/nthreads) rows from x to tmp
1110    Transpose( x , tmp , MyFirst , MyLast );
1111
1112#if( DEBUG_FFT1D & 1 )
1113get_cycle( &cycle );
1114printf("\n[fft] %s : thread %d after first fft / cycle %d\n",
1115__FUNCTION__, tid, (unsigned int)cycle );
1116if( PRINT_ARRAY ) PrintArray( tmp , N );
1117#endif
1118
1119    // BARRIER
1120    get_cycle( &barrier_start );
1121    pthread_barrier_wait( &barrier );
1122    get_cycle( &barrier_stop );
1123    sync_time[tid] = (unsigned int)(barrier_stop - barrier_start);
1124
1125#if( DEBUG_FFT1D & 1 )
1126get_cycle( &cycle );
1127printf("\n[fft] %s : thread %d exit barrier after first fft / cycle %d\n",
1128__FUNCTION__, tid, (unsigned int)cycle );
1129#endif
1130
1131    // do FFTs on rows of tmp (i.e. columns of x) and apply twiddle factor
1132    for (j = MyFirst; j < MyLast; j++) 
1133    {
1134        FFTRow( direction , upriv , tmp , j * rootN );
1135
1136        TwiddleOneCol( direction , j , twid , tmp , j * rootN );
1137    } 
1138
1139#if( DEBUG_FFT1D & 1 )
1140printf("\n[fft] %s : thread %d after first twiddle\n", __FUNCTION__, tid);
1141if( PRINT_ARRAY ) PrintArray( tmp , N );
1142#endif
1143
1144    // BARRIER
1145    get_cycle( &barrier_start );
1146    pthread_barrier_wait( &barrier );
1147    get_cycle( &barrier_stop );
1148
1149#if( DEBUG_FFT1D & 1 )
1150printf("\n[fft] %s : thread %d exit barrier after first twiddle\n", __FUNCTION__, tid);
1151#endif
1152
1153    sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
1154
1155    // fft tmp to x
1156    Transpose( tmp , x , MyFirst , MyLast );
1157
1158#if( DEBUG_FFT1D & 1 )
1159printf("\n[fft] %s : thread %d after second fft\n", __FUNCTION__, tid);
1160if( PRINT_ARRAY ) PrintArray( x , N );
1161#endif
1162
1163    // BARRIER
1164    get_cycle( &barrier_start );
1165    pthread_barrier_wait( &barrier );
1166    get_cycle( &barrier_stop );
1167
1168#if( DEBUG_FFT1D & 1 )
1169printf("\n[fft] %s : thread %d exit barrier after second fft\n", __FUNCTION__, tid);
1170#endif
1171
1172    sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
1173
1174    // do FFTs on rows of x and apply the scaling factor
1175    for (j = MyFirst; j < MyLast; j++) 
1176    {
1177        FFTRow( direction , upriv , x , j * rootN );
1178        if (direction == -1) Scale( x , j * rootN );
1179    }
1180
1181#if( DEBUG_FFT1D & 1 )
1182printf("\n[fft] %s : thread %d after FFT on rows\n", __FUNCTION__, tid);
1183if( PRINT_ARRAY ) PrintArray( x , N );
1184#endif
1185
1186    // BARRIER
1187    get_cycle( &barrier_start );
1188    pthread_barrier_wait( &barrier );
1189    get_cycle( &barrier_stop );
1190
1191#if( DEBUG_FFT1D & 1 )
1192printf("\n[fft] %s : thread %d exit barrier after FFT on rows\n", __FUNCTION__, tid);
1193#endif
1194    sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
1195
1196    // fft x to tmp
1197    Transpose( x , tmp , MyFirst , MyLast );
1198
1199#if( DEBUG_FFT1D & 1 )
1200printf("\n[fft] %s : thread %x after third fft\n", __FUNCTION__, tid);
1201if( PRINT_ARRAY ) PrintArray( x , N );
1202#endif
1203
1204    // BARRIER
1205    get_cycle( &barrier_start );
1206    pthread_barrier_wait( &barrier );
1207    get_cycle( &barrier_stop );
1208
1209#if( DEBUG_FFT1D & 1 )
1210printf("\n[fft] %s : thread %d exit barrier after third fft\n", __FUNCTION__, tid);
1211#endif
1212
1213    sync_time[tid] += (unsigned int)(barrier_stop - barrier_start);
1214    sync_time[tid] += (long)(barrier_stop - barrier_start);
1215
1216    // copy tmp to x
1217    Copy( tmp , x , MyFirst , MyLast );
1218
1219#if DEBUG_FFT1D
1220printf("\n[fft] %s : thread %d completed\n", __FUNCTION__, tid);
1221if( PRINT_ARRAY ) PrintArray( x , N );
1222#endif
1223
1224}  // end FFT1D()
1225
1226/////////////////////////////////////////////////////////////////////////////////////
1227// This function multiply all points contained in a row (rootN points) of the
1228// x[] array by the corresponding twiddle factor, contained in the u[] array.
1229/////////////////////////////////////////////////////////////////////////////////////
1230void TwiddleOneCol( int             direction, 
1231                    unsigned int    j,              // y coordinate in 2D view of coef array
1232                    double       ** u,              // coef array base address
1233                    double       ** x,              // data array base address
1234                    unsigned int    offset_x )      // first point in N points data array
1235{
1236    unsigned int i;
1237    double       omega_r; 
1238    double       omega_c; 
1239    double       x_r; 
1240    double       x_c;
1241    unsigned int c_id;
1242    unsigned int c_offset;
1243
1244    for (i = 0; i < rootN ; i++)  // loop on the rootN points
1245    {
1246        // get coef
1247        c_id      = (j * rootN + i) / (points_per_cluster);
1248        c_offset  = (j * rootN + i) % (points_per_cluster);
1249        omega_r = u[c_id][2*c_offset];
1250        omega_c = direction * u[c_id][2*c_offset+1];
1251
1252        // access data
1253        c_id      = (offset_x + i) / (points_per_cluster);
1254        c_offset  = (offset_x + i) % (points_per_cluster);   
1255        x_r = x[c_id][2*c_offset]; 
1256        x_c = x[c_id][2*c_offset+1];
1257
1258        x[c_id][2*c_offset]   = omega_r*x_r - omega_c * x_c;
1259        x[c_id][2*c_offset+1] = omega_r*x_c + omega_c * x_r;
1260    }
1261}  // end TwiddleOneCol()
1262
1263////////////////////////////
1264void Scale( double      ** x,           // data array base address
1265            unsigned int   offset_x )   // first point of the row to be scaled
1266{
1267    unsigned int i;
1268    unsigned int c_id;
1269    unsigned int c_offset;
1270
1271    for (i = 0; i < rootN ; i++) 
1272    {
1273        c_id      = (offset_x + i) / (points_per_cluster);
1274        c_offset  = (offset_x + i) % (points_per_cluster);
1275        x[c_id][2*c_offset]     /= N;
1276        x[c_id][2*c_offset + 1] /= N;
1277    }
1278}
1279
1280///////////////////////////////////
1281void Transpose( double      ** src,      // source buffer (array of pointers)
1282                double      ** dest,     // destination buffer (array of pointers)
1283                unsigned int   MyFirst,  // first row allocated to the thread
1284                unsigned int   MyLast )  // last row allocated to the thread
1285{
1286    unsigned int row;               // row index
1287    unsigned int point;             // data point index in a row
1288
1289    unsigned int index_src;         // absolute index in the source N points array
1290    unsigned int c_id_src;          // cluster for the source buffer
1291    unsigned int c_offset_src;      // offset in the source buffer
1292
1293    unsigned int index_dst;         // absolute index in the dest N points array
1294    unsigned int c_id_dst;          // cluster for the dest buffer
1295    unsigned int c_offset_dst;      // offset in the dest buffer
1296
1297   
1298    // scan all data points allocated to the thread
1299    // (between MyFirst row and MyLast row) from the source buffer
1300    // and write these points to the destination buffer
1301    for ( row = MyFirst ; row < MyLast ; row++ )       // loop on the rows
1302    {
1303        for ( point = 0 ; point < rootN ; point++ )    // loop on points in row
1304        {
1305            index_src    = row * rootN + point;
1306            c_id_src     = index_src / (points_per_cluster);
1307            c_offset_src = index_src % (points_per_cluster);
1308
1309            index_dst    = point * rootN + row;
1310            c_id_dst     = index_dst / (points_per_cluster);
1311            c_offset_dst = index_dst % (points_per_cluster);
1312
1313            dest[c_id_dst][2*c_offset_dst]   = src[c_id_src][2*c_offset_src];
1314            dest[c_id_dst][2*c_offset_dst+1] = src[c_id_src][2*c_offset_src+1];
1315        }
1316    }
1317}  // end Transpose()
1318
1319//////////////////////////////
1320void Copy( double      ** src,      // source buffer (array of pointers)
1321           double      ** dest,     // destination buffer (array of pointers)
1322           unsigned int   MyFirst,  // first row allocated to the thread
1323           unsigned int   MyLast )  // last row allocated to the thread
1324{
1325    unsigned int row;                  // row index
1326    unsigned int point;                // data point index in a row
1327
1328    unsigned int index;                // absolute index in the N points array
1329    unsigned int c_id;                 // cluster index
1330    unsigned int c_offset;             // offset in local buffer
1331
1332    // scan all data points allocated to the thread
1333    for ( row = MyFirst ; row < MyLast ; row++ )       // loop on the rows
1334    {
1335        for ( point = 0 ; point < rootN ; point++ )    // loop on points in row
1336        {
1337            index    = row * rootN + point;
1338            c_id     = index / (points_per_cluster);
1339            c_offset = index % (points_per_cluster);
1340
1341            dest[c_id][2*c_offset]   = src[c_id][2*c_offset];
1342            dest[c_id][2*c_offset+1] = src[c_id][2*c_offset+1];
1343        }
1344    }
1345}  // end Copy()
1346
1347///////////////////////////////
1348void Reverse( double      ** x, 
1349              unsigned int   offset_x )
1350{
1351    unsigned int j, k;
1352    unsigned int c_id_j;
1353    unsigned int c_offset_j;
1354    unsigned int c_id_k;
1355    unsigned int c_offset_k;
1356
1357    for (k = 0 ; k < rootN ; k++) 
1358    {
1359        j = BitReverse( k );
1360        if (j > k) 
1361        {
1362            c_id_j      = (offset_x + j) / (points_per_cluster);
1363            c_offset_j  = (offset_x + j) % (points_per_cluster);
1364            c_id_k      = (offset_x + k) / (points_per_cluster);
1365            c_offset_k  = (offset_x + k) % (points_per_cluster);
1366
1367            SWAP(x[c_id_j][2*c_offset_j]  , x[c_id_k][2*c_offset_k]);
1368            SWAP(x[c_id_j][2*c_offset_j+1], x[c_id_k][2*c_offset_k+1]);
1369        }
1370    }
1371}
1372
1373/////////////////////////////////////////////////////////////////////////////
1374// This function makes the in-place FFT on all points contained in a row
1375// (i.e. rootN points) of the x[nclusters][points_per_cluster] array.
1376/////////////////////////////////////////////////////////////////////////////
1377void FFTRow( int            direction,  // 1 direct / -1 inverse
1378                double       * u,          // private coefs array
1379                double      ** x,          // array of pointers on distributed buffers
1380                unsigned int   offset_x )  // absolute offset in the x array
1381{
1382    unsigned int     j;
1383    unsigned int     k;
1384    unsigned int     q;
1385    unsigned int     L;
1386    unsigned int     r;
1387    unsigned int     Lstar;
1388    double * u1; 
1389
1390    unsigned int     offset_x1;     // index first butterfly input
1391    unsigned int     offset_x2;     // index second butterfly output
1392
1393    double           omega_r;       // real part butterfy coef
1394    double           omega_c;       // complex part butterfly coef
1395
1396    double           tau_r;
1397    double           tau_c;
1398
1399    double           d1_r;          // real part first butterfly input
1400    double           d1_c;          // imag part first butterfly input
1401    double           d2_r;          // real part second butterfly input
1402    double           d2_c;          // imag part second butterfly input
1403
1404    unsigned int     c_id_1;        // cluster index for first butterfly input
1405    unsigned int     c_offset_1;    // offset for first butterfly input
1406    unsigned int     c_id_2;        // cluster index for second butterfly input
1407    unsigned int     c_offset_2;    // offset for second butterfly input
1408
1409#if DEBUG_ROW
1410unsigned int p;
1411printf("\n[fft] ROW data in / %d points / offset = %d\n", rootN , offset_x );
1412
1413for ( p = 0 ; p < rootN ; p++ )
1414{
1415    unsigned int index    = offset_x + p;
1416    unsigned int c_id     = index / (points_per_cluster);
1417    unsigned int c_offset = index % (points_per_cluster);
1418    printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] );
1419}
1420printf("\n");
1421#endif
1422
1423    // This makes the rootN input points reordering
1424    Reverse( x , offset_x ); 
1425
1426#if DEBUG_ROW
1427printf("\n[fft] ROW data after reverse / %d points / offset = %d\n", rootN , offset_x );
1428
1429for ( p = 0 ; p < rootN ; p++ )
1430{
1431    unsigned int index    = offset_x + p;
1432    unsigned int c_id     = index / (points_per_cluster);
1433    unsigned int c_offset = index % (points_per_cluster);
1434    printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] );
1435}
1436printf("\n");
1437#endif
1438
1439    // This implements the multi-stages, in place Butterfly network
1440    for (q = 1; q <= M/2 ; q++)     // loop on stages
1441    {
1442        L = 1 << q;       // number of points per subset for current stage
1443        r = rootN / L;    // number of subsets
1444        Lstar = L / 2;
1445        u1 = &u[2 * (Lstar - 1)];
1446        for (k = 0; k < r; k++)     // loop on the subsets
1447        {
1448            offset_x1  = offset_x + (k * L);            // index first point
1449            offset_x2  = offset_x + (k * L + Lstar);    // index second point
1450
1451#if (DEBUG_ROW & 1)
1452printf("\n ### q = %d / k = %d / x1 = %d / x2 = %d\n", q , k , offset_x1 , offset_x2 );
1453#endif
1454            // makes all in-place butterfly(s) for subset
1455            for (j = 0; j < Lstar; j++) 
1456            {
1457                // get coef
1458                omega_r = u1[2*j];
1459                omega_c = direction * u1[2*j+1];
1460
1461                // get d[x1] address and value
1462                c_id_1      = (offset_x1 + j) / (points_per_cluster);
1463                c_offset_1  = (offset_x1 + j) % (points_per_cluster);
1464                d1_r        = x[c_id_1][2*c_offset_1];
1465                d1_c        = x[c_id_1][2*c_offset_1+1];
1466
1467                // get d[x2] address and value
1468                c_id_2      = (offset_x2 + j) / (points_per_cluster);
1469                c_offset_2  = (offset_x2 + j) % (points_per_cluster);
1470                d2_r        = x[c_id_2][2*c_offset_2];
1471                d2_c        = x[c_id_2][2*c_offset_2+1];
1472
1473#if (DEBUG_ROW & 1)
1474printf("\n ### d1_in = (%f , %f) / d2_in = (%f , %f) / coef = (%f , %f)\n", 
1475                d1_r , d1_c , d2_r , d2_c , omega_r , omega_c);
1476#endif
1477                // tau = omega * d[x2]
1478                tau_r = omega_r * d2_r - omega_c * d2_c;
1479                tau_c = omega_r * d2_c + omega_c * d2_r;
1480
1481                // set new value for d[x1] = d[x1] + omega * d[x2]
1482                x[c_id_1][2*c_offset_1]   = d1_r + tau_r;
1483                x[c_id_1][2*c_offset_1+1] = d1_c + tau_c;
1484
1485                // set new value for d[x2] = d[x1] - omega * d[x2]
1486                x[c_id_2][2*c_offset_2]   = d1_r - tau_r;
1487                x[c_id_2][2*c_offset_2+1] = d1_c - tau_c;
1488
1489#if (DEBUG_ROW & 1)
1490printf("\n ### d1_out = (%f , %f) / d2_out = (%f , %f)\n", 
1491                d1_r + tau_r , d1_c + tau_c , d2_r - tau_r , d2_c - tau_c );
1492#endif
1493            }
1494        }
1495    }
1496
1497#if DEBUG_ROW
1498printf("\n[fft] ROW data out / %d points / offset = %d\n", rootN , offset_x );
1499for ( p = 0 ; p < rootN ; p++ )
1500{
1501    unsigned int index    = offset_x + p;
1502    unsigned int c_id     = index / (points_per_cluster);
1503    unsigned int c_offset = index % (points_per_cluster);
1504    printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] );
1505}
1506printf("\n");
1507#endif
1508
1509}  // end FFTRow()
1510
1511///////////////////////////////////////
1512void PrintArray( double       ** array,
1513                 unsigned int    size ) 
1514{
1515    unsigned int  i;
1516    unsigned int  c_id;
1517    unsigned int  c_offset;
1518
1519    // float display
1520    for (i = 0; i < size ; i++) 
1521    {
1522        c_id      = i / (points_per_cluster);
1523        c_offset  = i % (points_per_cluster);
1524
1525        printf(" %f  %f |", array[c_id][2*c_offset], array[c_id][2*c_offset+1]);
1526
1527        if ( (i+1) % 4 == 0)  printf("\n");
1528    }
1529    printf("\n");
1530}
1531
1532
1533// Local Variables:
1534// tab-width: 4
1535// c-basic-offset: 4
1536// c-file-offsets:((innamespace . 0)(inline-open . 0))
1537// indent-tabs-mode: nil
1538// End:
1539
1540// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
1541
Note: See TracBrowser for help on using the repository browser.