[469] | 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 | |
---|
[652] | 17 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 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). |
---|
[637] | 24 | // Each thread handle (rootN / nthreads) rows. |
---|
| 25 | // The N input data points can be initialised in three different modes: |
---|
[469] | 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 | // |
---|
[628] | 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. |
---|
[637] | 33 | // The number of threads cannot be larger than the number of rows. |
---|
[628] | 34 | // |
---|
[637] | 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. |
---|
[469] | 43 | // |
---|
[637] | 44 | // Each working thread allocates also a private coefs[rootN-1] buffer, |
---|
| 45 | // that contains all coefs required for a rootN points FFT. |
---|
[469] | 46 | // |
---|
[637] | 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 | // |
---|
[652] | 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]. |
---|
[469] | 58 | // |
---|
[652] | 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 | // |
---|
[469] | 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 |
---|
[637] | 89 | // working thread(i) in the work() function. |
---|
[469] | 90 | // - The synchronisation time related to the barriers (sync_time[i]) |
---|
[637] | 91 | // is computed by each thread(i) in the work() function. |
---|
[469] | 92 | // The results are displayed on the TXT terminal, and registered on disk. |
---|
[652] | 93 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 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 | |
---|
[652] | 119 | #define NO_PLACEMENT 1 |
---|
| 120 | #define EXPLICIT_PLACEMENT 0 |
---|
| 121 | #define PARALLEL_PLACEMENT 0 |
---|
| 122 | |
---|
[644] | 123 | #define DEFAULT_M 18 // 256 K complex points |
---|
[637] | 124 | #define USE_DQT_BARRIER 1 // use DDT barrier if non zero |
---|
[628] | 125 | #define MODE COSIN // DATA array initialisation mode |
---|
| 126 | #define CHECK 0 |
---|
[644] | 127 | #define DEBUG_MAIN 1 // trace main() function (detailed if odd) |
---|
[638] | 128 | #define DEBUG_WORK 0 // trace work() function (detailed if odd) |
---|
[628] | 129 | #define DEBUG_FFT1D 0 // trace FFT1D() function (detailed if odd) |
---|
[574] | 130 | #define DEBUG_ROW 0 // trace FFTRow() function (detailed if odd) |
---|
| 131 | #define PRINT_ARRAY 0 |
---|
[640] | 132 | #define DISPLAY_SCHED_AND_VMM 0 // display final VMM state in all clusters |
---|
[469] | 133 | |
---|
| 134 | // macro to swap two variables |
---|
| 135 | #define SWAP(a,b) { double tmp; tmp = a; a = b; b = tmp; } |
---|
| 136 | |
---|
[588] | 137 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[644] | 138 | // FFT global variables |
---|
[588] | 139 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 140 | |
---|
[652] | 141 | unsigned int x_size; // platform global parameter |
---|
| 142 | unsigned int y_size; // platform global parameter |
---|
| 143 | unsigned int ncores; // platform global parameter |
---|
[588] | 144 | |
---|
[473] | 145 | unsigned int nthreads; // total number of threads (one thread per core) |
---|
| 146 | unsigned int nclusters; // total number of clusters |
---|
| 147 | unsigned int M = DEFAULT_M; // log2(number of points) |
---|
| 148 | unsigned int N; // number of points (N = 2^M) |
---|
| 149 | unsigned int rootN; // rootN = 2^M/2 |
---|
| 150 | unsigned int rows_per_thread; // number of data "rows" handled by a single thread |
---|
| 151 | unsigned int points_per_cluster; // number of data points per cluster |
---|
[469] | 152 | |
---|
| 153 | // arrays of pointers on distributed buffers (one sub-buffer per cluster) |
---|
| 154 | double * data[CLUSTERS_MAX]; // original time-domain data |
---|
[652] | 155 | double * trans[CLUSTERS_MAX]; // used as auxiliary space for fft |
---|
[637] | 156 | double * twid[CLUSTERS_MAX]; // twiddle factor : exp(-2iPI*k*n/N) |
---|
[469] | 157 | double * bloup[CLUSTERS_MAX]; // used as auxiliary space for DFT |
---|
| 158 | |
---|
| 159 | // instrumentation counters |
---|
[641] | 160 | unsigned int pgfault_nr[THREADS_MAX]; // total number of page faults (per thread) |
---|
| 161 | unsigned int pgfault_cost[THREADS_MAX]; // total page faults cost (per thread) |
---|
| 162 | unsigned int pgfault_max[THREADS_MAX]; // max page faults cost (per thread) |
---|
[588] | 163 | unsigned int parallel_time[THREADS_MAX]; // total computation time (per thread) |
---|
| 164 | unsigned int sync_time[THREADS_MAX]; // cumulated waiting time in barriers (per thread) |
---|
| 165 | unsigned int init_time; // initialisation time (in main) |
---|
[469] | 166 | |
---|
| 167 | // synchronisation barrier (all threads) |
---|
| 168 | pthread_barrier_t barrier; |
---|
[628] | 169 | pthread_barrierattr_t barrier_attr; |
---|
[469] | 170 | |
---|
[652] | 171 | //return values at thread exit |
---|
| 172 | unsigned int THREAD_EXIT_SUCCESS = 0; |
---|
| 173 | unsigned int THREAD_EXIT_FAILURE = 1; |
---|
[469] | 174 | |
---|
[652] | 175 | // main thread continuous index |
---|
| 176 | unsigned int tid_main; |
---|
[637] | 177 | |
---|
[652] | 178 | // array of kernel thread identifiers / indexed by [tid] |
---|
| 179 | pthread_t work_trdid[CLUSTERS_MAX * CORES_MAX]; |
---|
[637] | 180 | |
---|
[652] | 181 | // array of thread attributes / indexed by [tid] |
---|
| 182 | pthread_attr_t work_attr[CLUSTERS_MAX * CORES_MAX]; |
---|
[637] | 183 | |
---|
[652] | 184 | // array of work function arguments / indexed by [tid] |
---|
| 185 | pthread_parallel_work_args_t work_args[CLUSTERS_MAX * CORES_MAX]; |
---|
| 186 | |
---|
[637] | 187 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 188 | // functions declaration |
---|
[637] | 189 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 190 | |
---|
[652] | 191 | void work( pthread_parallel_work_args_t * args ); |
---|
[469] | 192 | |
---|
[503] | 193 | double CheckSum( void ); |
---|
[469] | 194 | |
---|
[637] | 195 | void InitD( double ** data , |
---|
| 196 | unsigned int mode, |
---|
| 197 | unsigned int tid ); |
---|
[469] | 198 | |
---|
[637] | 199 | void InitT( double ** twid, |
---|
| 200 | unsigned int tid ); |
---|
[469] | 201 | |
---|
[637] | 202 | void InitU( double * coefs ); |
---|
[469] | 203 | |
---|
[473] | 204 | unsigned int BitReverse( unsigned int k ); |
---|
[469] | 205 | |
---|
[473] | 206 | void FFT1D( int direction, |
---|
| 207 | double ** x, |
---|
| 208 | double ** tmp, |
---|
| 209 | double * upriv, |
---|
| 210 | double ** twid, |
---|
[637] | 211 | unsigned int tid, |
---|
[473] | 212 | unsigned int MyFirst, |
---|
| 213 | unsigned int MyLast ); |
---|
[469] | 214 | |
---|
[473] | 215 | void TwiddleOneCol( int direction, |
---|
| 216 | unsigned int j, |
---|
| 217 | double ** u, |
---|
| 218 | double ** x, |
---|
| 219 | unsigned int offset_x ); |
---|
[469] | 220 | |
---|
[473] | 221 | void Scale( double ** x, |
---|
| 222 | unsigned int offset_x ); |
---|
[469] | 223 | |
---|
[473] | 224 | void Transpose( double ** src, |
---|
| 225 | double ** dest, |
---|
| 226 | unsigned int MyFirst, |
---|
| 227 | unsigned int MyLast ); |
---|
[469] | 228 | |
---|
[473] | 229 | void Copy( double ** src, |
---|
| 230 | double ** dest, |
---|
| 231 | unsigned int MyFirst, |
---|
| 232 | unsigned int MyLast ); |
---|
[469] | 233 | |
---|
[473] | 234 | void Reverse( double ** x, |
---|
| 235 | unsigned int offset_x ); |
---|
[469] | 236 | |
---|
[574] | 237 | void FFTRow( int direction, |
---|
[473] | 238 | double * u, |
---|
| 239 | double ** x, |
---|
| 240 | unsigned int offset_x ); |
---|
[469] | 241 | |
---|
[473] | 242 | void PrintArray( double ** x, |
---|
| 243 | unsigned int size ); |
---|
[469] | 244 | |
---|
[473] | 245 | void SimpleDft( int direction, |
---|
| 246 | unsigned int size, |
---|
| 247 | double ** src, |
---|
| 248 | unsigned int src_offset, |
---|
| 249 | double ** dst, |
---|
| 250 | unsigned int dst_offset ); |
---|
[469] | 251 | |
---|
| 252 | /////////////////////////////////////////////////////////////////// |
---|
| 253 | // This main() function execute the sequencial initialisation |
---|
| 254 | // launch the parallel execution, and makes the instrumentation. |
---|
| 255 | /////////////////////////////////////////////////////////////////// |
---|
[503] | 256 | void main ( void ) |
---|
[469] | 257 | { |
---|
[628] | 258 | int error; |
---|
| 259 | |
---|
[637] | 260 | unsigned int tid; // continuous thread index |
---|
[469] | 261 | |
---|
[637] | 262 | char name[64]; // instrumentation file name |
---|
| 263 | char path[128]; // instrumentation path name |
---|
| 264 | char string[256]; |
---|
| 265 | int ret; |
---|
| 266 | |
---|
[469] | 267 | unsigned long long start_init_cycle; |
---|
[588] | 268 | unsigned long long end_init_cycle; |
---|
[469] | 269 | |
---|
[637] | 270 | #if DEBUG_MAIN |
---|
| 271 | unsigned long long debug_cycle; |
---|
| 272 | #endif |
---|
| 273 | |
---|
[469] | 274 | #if CHECK |
---|
[637] | 275 | double ck1; // for input/output checking |
---|
| 276 | double ck3; // for input/output checking |
---|
[469] | 277 | #endif |
---|
| 278 | |
---|
[640] | 279 | int pid = getpid(); |
---|
| 280 | |
---|
[652] | 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 | |
---|
[469] | 288 | // get FFT application start cycle |
---|
[588] | 289 | get_cycle( &start_init_cycle ); |
---|
[469] | 290 | |
---|
[637] | 291 | // get platform parameters |
---|
[469] | 292 | if( get_config( &x_size , &y_size , &ncores ) ) |
---|
| 293 | { |
---|
[628] | 294 | printf("\n[fft error] cannot get hardware configuration\n"); |
---|
[469] | 295 | exit( 0 ); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | // check ncores |
---|
| 299 | if( (ncores != 1) && (ncores != 2) && (ncores != 4) ) |
---|
| 300 | { |
---|
[628] | 301 | printf("\n[fft error] number of cores per cluster must be 1/2/4\n"); |
---|
[469] | 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 | { |
---|
[628] | 308 | printf("\n[fft error] x_size must be 1/2/4/8/16\n"); |
---|
[469] | 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 | { |
---|
[628] | 315 | printf("\n[fft error] y_size must be 1/2/4/8/16\n"); |
---|
[469] | 316 | exit( 0 ); |
---|
| 317 | } |
---|
| 318 | |
---|
[652] | 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 | |
---|
[637] | 324 | // compute nthreads and nclusters |
---|
[469] | 325 | nthreads = x_size * y_size * ncores; |
---|
| 326 | nclusters = x_size * y_size; |
---|
| 327 | |
---|
[637] | 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 | |
---|
[469] | 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 | { |
---|
[628] | 341 | printf("\n[fft error] sqrt(N) must be larger than T\n"); |
---|
[469] | 342 | exit( 0 ); |
---|
| 343 | } |
---|
| 344 | |
---|
[652] | 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 ); |
---|
[469] | 350 | |
---|
[652] | 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 | } |
---|
[469] | 357 | |
---|
[652] | 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 |
---|
[637] | 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 ); |
---|
[469] | 391 | } |
---|
| 392 | |
---|
[635] | 393 | #if DEBUG_MAIN |
---|
[637] | 394 | get_cycle( &debug_cycle ); |
---|
[652] | 395 | printf("\n[fft] main open instrumentation file <%s> at cycle %d\n", |
---|
[637] | 396 | path, (unsigned int)debug_cycle ); |
---|
[635] | 397 | #endif |
---|
[574] | 398 | |
---|
[469] | 399 | #if CHECK |
---|
[473] | 400 | ck1 = CheckSum(); |
---|
[469] | 401 | #endif |
---|
| 402 | |
---|
[574] | 403 | #if PRINT_ARRAY |
---|
[469] | 404 | printf("\nData values / base = %x\n", &data[0][0] ); |
---|
| 405 | PrintArray( data , N ); |
---|
| 406 | |
---|
| 407 | printf("\nTwiddle values / base = %x\n", &twid[0][0] ); |
---|
| 408 | PrintArray( twid , N ); |
---|
| 409 | |
---|
| 410 | SimpleDft( 1 , N , data , 0 , bloup , 0 ); |
---|
| 411 | |
---|
| 412 | printf("\nExpected results / base = %x\n", &bloup[0][0] ); |
---|
| 413 | PrintArray( bloup , N ); |
---|
| 414 | #endif |
---|
| 415 | |
---|
[637] | 416 | // initialise barrier synchronizing all <work> threads |
---|
[628] | 417 | if( USE_DQT_BARRIER ) |
---|
[473] | 418 | { |
---|
[628] | 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"); |
---|
[473] | 432 | exit( 0 ); |
---|
| 433 | } |
---|
[469] | 434 | |
---|
[635] | 435 | #if DEBUG_MAIN |
---|
[637] | 436 | get_cycle( &debug_cycle ); |
---|
[652] | 437 | printf("\n[fft] main completes sequencial initialisation at cycle %d\n", |
---|
[637] | 438 | (unsigned int)debug_cycle ); |
---|
[635] | 439 | #endif |
---|
[473] | 440 | |
---|
[652] | 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 ) |
---|
[469] | 447 | { |
---|
[652] | 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++ ) |
---|
[469] | 452 | { |
---|
[652] | 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 | } |
---|
[588] | 467 | |
---|
[652] | 468 | #if DEBUG_MAIN |
---|
| 469 | printf("\n[fft] main created thread %d\n", tid ); |
---|
| 470 | #endif |
---|
| 471 | |
---|
| 472 | } |
---|
| 473 | else |
---|
[469] | 474 | { |
---|
[652] | 475 | tid_main = 0; |
---|
| 476 | } |
---|
| 477 | } // end for tid |
---|
[469] | 478 | |
---|
[652] | 479 | // main thread calls itself the execute() function |
---|
| 480 | work( &work_args[0] ); |
---|
[469] | 481 | |
---|
[652] | 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 ); |
---|
[469] | 492 | } |
---|
[652] | 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 |
---|
| 502 | printf("\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 = (((x * 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 |
---|
| 552 | printf("\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 | } |
---|
[469] | 561 | } |
---|
| 562 | |
---|
[652] | 563 | // main thread calls itself the execute() function |
---|
| 564 | work( &work_args[tid_main] ); |
---|
[637] | 565 | |
---|
[652] | 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 |
---|
| 588 | printf("\n[fft] main joined thread %d on core[%x,%d]\n", tid , cxy , l ); |
---|
[635] | 589 | #endif |
---|
[652] | 590 | } |
---|
| 591 | } |
---|
| 592 | } // end if explicit_placement |
---|
[628] | 593 | |
---|
[652] | 594 | //////////////////////// |
---|
| 595 | if( PARALLEL_PLACEMENT ) |
---|
[469] | 596 | { |
---|
[652] | 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 | } |
---|
[637] | 603 | } |
---|
[469] | 604 | |
---|
[637] | 605 | #if DEBUG_MAIN |
---|
| 606 | get_cycle( &debug_cycle ); |
---|
| 607 | printf("\n[fft] main resume for instrumentation at cycle %d\n", |
---|
| 608 | (unsigned int)debug_cycle) ; |
---|
[588] | 609 | #endif |
---|
[469] | 610 | |
---|
[574] | 611 | #if PRINT_ARRAY |
---|
[469] | 612 | printf("\nData values after FFT:\n"); |
---|
| 613 | PrintArray( data , N ); |
---|
| 614 | #endif |
---|
| 615 | |
---|
| 616 | #if CHECK |
---|
[473] | 617 | ck3 = CheckSum(); |
---|
[469] | 618 | printf("\n*** Results ***\n"); |
---|
| 619 | printf("Checksum difference is %f (%f, %f)\n", ck1 - ck3, ck1, ck3); |
---|
| 620 | if (fabs(ck1 - ck3) < 0.001) printf("Results OK\n"); |
---|
| 621 | else printf("Results KO\n"); |
---|
| 622 | #endif |
---|
| 623 | |
---|
[628] | 624 | // display header on terminal, and save to file |
---|
| 625 | printf("\n----- %s -----\n", name ); |
---|
[469] | 626 | |
---|
[628] | 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 | } |
---|
[469] | 633 | |
---|
[641] | 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 |
---|
[588] | 642 | for (tid = 0 ; tid < nthreads ; tid++) |
---|
| 643 | { |
---|
[641] | 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] ); |
---|
[588] | 648 | |
---|
[637] | 649 | // save to instrumentation file |
---|
[628] | 650 | fprintf( f , "%s" , string ); |
---|
| 651 | if( ret < 0 ) |
---|
| 652 | { |
---|
[641] | 653 | printf("\n[fft error] cannot save thread %d results to file <%s>\n", tid, path ); |
---|
[637] | 654 | printf("%s", string ); |
---|
[628] | 655 | exit(0); |
---|
| 656 | } |
---|
[641] | 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]; |
---|
[649] | 661 | |
---|
| 662 | pgfaults_nr += pgfault_nr[tid]; |
---|
| 663 | pgfaults_cost += pgfault_cost[tid]; |
---|
| 664 | |
---|
[641] | 665 | if (pgfault_max[tid] > pgfaults_max) pgfaults_max = pgfault_max[tid]; |
---|
[588] | 666 | } |
---|
| 667 | |
---|
[641] | 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 ); |
---|
[588] | 672 | |
---|
[641] | 673 | printf("%s", string ); |
---|
| 674 | |
---|
| 675 | // save global values to file |
---|
| 676 | ret = fprintf( f , "%s", string ); |
---|
| 677 | |
---|
| 678 | if( ret < 0 ) |
---|
[469] | 679 | { |
---|
[641] | 680 | printf("\n[fft error] cannot save global results to file <%s>\n", path ); |
---|
| 681 | exit(0); |
---|
[469] | 682 | } |
---|
| 683 | |
---|
[641] | 684 | // close instrumentation file |
---|
| 685 | ret = fclose( f ); |
---|
| 686 | |
---|
[628] | 687 | if( ret < 0 ) |
---|
| 688 | { |
---|
[641] | 689 | printf("\n[fft error] cannot close file <%s>\n", path ); |
---|
[628] | 690 | exit(0); |
---|
| 691 | } |
---|
[469] | 692 | |
---|
[637] | 693 | #if DEBUG_MAIN |
---|
| 694 | get_cycle( &debug_cycle ); |
---|
[641] | 695 | printf("\n[fft] main exit <%s> at cycle %d\n", |
---|
[637] | 696 | path, (unsigned int)debug_cycle ); |
---|
| 697 | #endif |
---|
[469] | 698 | |
---|
[628] | 699 | exit( 0 ); |
---|
| 700 | |
---|
[469] | 701 | } // end main() |
---|
| 702 | |
---|
[637] | 703 | ///////////////////////////////////////////////////////////////// |
---|
| 704 | // This function is executed in parallel by all <work> threads. |
---|
| 705 | ///////////////////////////////////////////////////////////////// |
---|
[652] | 706 | void work( pthread_parallel_work_args_t * args ) |
---|
[469] | 707 | { |
---|
[637] | 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 |
---|
[469] | 712 | |
---|
[637] | 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 | |
---|
[469] | 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 | |
---|
[652] | 722 | get_cycle( ¶llel_start ); |
---|
| 723 | |
---|
[637] | 724 | // get thread arguments |
---|
| 725 | tid = args->tid; |
---|
[652] | 726 | parent_barrier = args->barrier; |
---|
[469] | 727 | |
---|
[652] | 728 | // compute lid and cid from tid |
---|
| 729 | lid = tid % ncores; |
---|
| 730 | cid = tid / ncores; |
---|
| 731 | |
---|
[637] | 732 | #if DEBUG_WORK |
---|
[629] | 733 | printf("\n[fft] %s : thread %d enter / cycle %d\n", |
---|
[637] | 734 | __FUNCTION__, tid, (unsigned int)parallel_start ); |
---|
[588] | 735 | #endif |
---|
| 736 | |
---|
[638] | 737 | // thread on core 0 allocates memory from the local cluster |
---|
[637] | 738 | // for the distributed data[], trans[], twid[] buffers |
---|
| 739 | if( lid == 0 ) |
---|
| 740 | { |
---|
[638] | 741 | unsigned int data_size = (N / nclusters) * 2 * sizeof(double); |
---|
[637] | 742 | |
---|
[638] | 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 | } |
---|
[637] | 766 | } |
---|
| 767 | |
---|
[638] | 768 | // BARRIER to wait distributed buffers allocation |
---|
[629] | 769 | get_cycle( &barrier_start ); |
---|
| 770 | pthread_barrier_wait( &barrier ); |
---|
| 771 | get_cycle( &barrier_stop ); |
---|
[637] | 772 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[629] | 773 | |
---|
[637] | 774 | #if DEBUG_WORK |
---|
[638] | 775 | printf("\n[fft] %s : thread %d exit barrier for buffer allocation / cycle %d\n", |
---|
[637] | 776 | __FUNCTION__, tid, (unsigned int)barrier_stop ); |
---|
[630] | 777 | #endif |
---|
[629] | 778 | |
---|
[638] | 779 | // all threads contribute to data[] local array initialisation |
---|
[637] | 780 | InitD( data , MODE , tid ); |
---|
[469] | 781 | |
---|
[638] | 782 | // all threads contribute to data[] local array initialisation |
---|
[637] | 783 | InitT( twid , tid ); |
---|
| 784 | |
---|
[638] | 785 | // BARRIER to wait distributed buffers initialisation |
---|
[637] | 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 |
---|
[638] | 792 | printf("\n[fft] %s : thread %d exit barrier for buffer initialisation / cycle %d\n", |
---|
[637] | 793 | __FUNCTION__, tid, (unsigned int)barrier_stop ); |
---|
| 794 | #endif |
---|
| 795 | |
---|
[638] | 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 |
---|
[637] | 810 | MyFirst = rootN * tid / nthreads; |
---|
| 811 | MyLast = rootN * (tid + 1) / nthreads; |
---|
[469] | 812 | |
---|
[638] | 813 | // all threads perform forward FFT |
---|
[637] | 814 | FFT1D( 1 , data , trans , upriv , twid , tid , MyFirst , MyLast ); |
---|
[469] | 815 | |
---|
| 816 | #if CHECK |
---|
| 817 | get_cycle( &barrier_start ); |
---|
| 818 | pthread_barrier_wait( &barrier ); |
---|
| 819 | get_cycle( &barrier_stop ); |
---|
[637] | 820 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
| 821 | FFT1D( -1 , data , trans , upriv , twid , tid , MyFirst , MyLast ); |
---|
[469] | 822 | #endif |
---|
| 823 | |
---|
| 824 | get_cycle( ¶llel_stop ); |
---|
| 825 | |
---|
[641] | 826 | // register parallel time in instrumentation counters |
---|
[637] | 827 | parallel_time[tid] = (unsigned int)(parallel_stop - parallel_start); |
---|
[630] | 828 | |
---|
[641] | 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; |
---|
[637] | 843 | #if DEBUG_WORK |
---|
| 844 | printf("\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 ); |
---|
[588] | 846 | #endif |
---|
[469] | 847 | |
---|
[637] | 848 | // work thread signals completion to main |
---|
| 849 | pthread_barrier_wait( parent_barrier ); |
---|
[588] | 850 | |
---|
[637] | 851 | #if DEBUG_WORK |
---|
| 852 | printf("\n[fft] %s : thread %d exit\n", |
---|
| 853 | __FUNCTION__, tid ); |
---|
| 854 | #endif |
---|
[469] | 855 | |
---|
[640] | 856 | #if DISPLAY_SCHED_AND_VMM |
---|
| 857 | printf("\n[fft] %s : thread %d exit\n", __FUNCTION__, tid ); |
---|
| 858 | if( lid == 0 ) display_vmm( cxy , getpid() , 0 ); |
---|
| 859 | #endif |
---|
| 860 | |
---|
[637] | 861 | // work thread exit |
---|
| 862 | pthread_exit( NULL ); |
---|
| 863 | |
---|
| 864 | } // end work() |
---|
| 865 | |
---|
[469] | 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 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 870 | void 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 |
---|
[469] | 876 | { |
---|
[473] | 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 |
---|
[469] | 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); |
---|
[473] | 904 | d_r = src[c_id][2*c_offset]; |
---|
| 905 | d_c = src[c_id][2*c_offset+1]; |
---|
[469] | 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 | |
---|
[582] | 928 | /////////////////////// |
---|
[503] | 929 | double CheckSum( void ) |
---|
[469] | 930 | { |
---|
[473] | 931 | unsigned int i , j; |
---|
| 932 | unsigned int c_id; |
---|
| 933 | unsigned int c_offset; |
---|
[629] | 934 | double cks; |
---|
[469] | 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 | |
---|
[637] | 950 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 951 | // Each working thread <tid> contributes to initialize (rootN / nthreads) rows, |
---|
| 952 | // in the shared - and distributed - <data> array. |
---|
| 953 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 954 | void InitD(double ** data, |
---|
| 955 | unsigned int mode, |
---|
| 956 | unsigned int tid ) |
---|
[469] | 957 | { |
---|
[473] | 958 | unsigned int i , j; |
---|
| 959 | unsigned int c_id; |
---|
| 960 | unsigned int c_offset; |
---|
| 961 | unsigned int index; |
---|
[469] | 962 | |
---|
[637] | 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 |
---|
[469] | 968 | { |
---|
[637] | 969 | for ( i = 0 ; i < rootN ; i++ ) // loop on points in a row |
---|
[469] | 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 | { |
---|
[637] | 978 | data[c_id][2*c_offset] = ( (double)rand() ) / 65536; |
---|
| 979 | data[c_id][2*c_offset+1] = ( (double)rand() ) / 65536; |
---|
[469] | 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; |
---|
[637] | 987 | data[c_id][2*c_offset] = cos( phi ); |
---|
| 988 | data[c_id][2*c_offset+1] = sin( phi ); |
---|
[469] | 989 | } |
---|
| 990 | |
---|
| 991 | // complex input signal is constant |
---|
| 992 | if ( mode == CONSTANT ) |
---|
| 993 | { |
---|
[637] | 994 | data[c_id][2*c_offset] = 1.0; |
---|
| 995 | data[c_id][2*c_offset+1] = 0.0; |
---|
[469] | 996 | } |
---|
| 997 | } |
---|
| 998 | } |
---|
| 999 | } |
---|
| 1000 | |
---|
[637] | 1001 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1002 | // Each working thread <tid> contributes to initialize (rootN / nthreads) rows, |
---|
| 1003 | // in the shared - and distributed - <twiddle> array. |
---|
| 1004 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1005 | void InitT( double ** twid, |
---|
| 1006 | unsigned int tid ) |
---|
[469] | 1007 | { |
---|
[637] | 1008 | unsigned int i, j; |
---|
| 1009 | unsigned int index; |
---|
[473] | 1010 | unsigned int c_id; |
---|
| 1011 | unsigned int c_offset; |
---|
[469] | 1012 | double phi; |
---|
| 1013 | |
---|
[637] | 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 |
---|
[469] | 1019 | { |
---|
[637] | 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); |
---|
[469] | 1025 | |
---|
[637] | 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 ); |
---|
[469] | 1029 | } |
---|
| 1030 | } |
---|
| 1031 | } |
---|
| 1032 | |
---|
[637] | 1033 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1034 | // Each working thread initialize the private <upriv> array / (rootN - 1) entries. |
---|
| 1035 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1036 | void InitU( double * upriv ) |
---|
[469] | 1037 | { |
---|
[637] | 1038 | unsigned int q; |
---|
| 1039 | unsigned int j; |
---|
| 1040 | unsigned int base; |
---|
| 1041 | unsigned int n1; |
---|
[638] | 1042 | double phi; |
---|
[469] | 1043 | |
---|
[637] | 1044 | for (q = 0 ; ((unsigned int)(1 << q) < N) ; q++) |
---|
[469] | 1045 | { |
---|
[637] | 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; |
---|
[469] | 1051 | |
---|
[637] | 1052 | phi = (double)(2.0 * PI * j) / (2 * n1); |
---|
| 1053 | upriv[2*(base+j)] = cos( phi ); |
---|
| 1054 | upriv[2*(base+j)+1] = -sin( phi ); |
---|
[469] | 1055 | } |
---|
| 1056 | } |
---|
| 1057 | } |
---|
| 1058 | |
---|
| 1059 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1060 | // This function returns an index value that is the bit reverse of the input value. |
---|
| 1061 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 1062 | unsigned int BitReverse( unsigned int k ) |
---|
[469] | 1063 | { |
---|
[473] | 1064 | unsigned int i; |
---|
| 1065 | unsigned int j; |
---|
| 1066 | unsigned int tmp; |
---|
[469] | 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. |
---|
[652] | 1082 | // 1) it fft (rootN/nthreads ) rows from x to tmp. |
---|
[469] | 1083 | // 2) it make (rootN/nthreads) FFT on the tmp rows and apply the twiddle factor. |
---|
[652] | 1084 | // 3) it fft (rootN/nthreads) columns from tmp to x. |
---|
[469] | 1085 | // 4) it make (rootN/nthreads) FFT on the x rows. |
---|
[574] | 1086 | // It calls the FFTRow() 2*(rootN/nthreads) times to perform the in place FFT |
---|
[469] | 1087 | // on the rootN points contained in a row. |
---|
| 1088 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 1089 | void 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 |
---|
[637] | 1094 | unsigned int tid, // thread continuous index |
---|
[473] | 1095 | unsigned int MyFirst, |
---|
| 1096 | unsigned int MyLast ) |
---|
[469] | 1097 | { |
---|
[473] | 1098 | unsigned int j; |
---|
[469] | 1099 | unsigned long long barrier_start; |
---|
| 1100 | unsigned long long barrier_stop; |
---|
| 1101 | |
---|
[574] | 1102 | #if DEBUG_FFT1D |
---|
[588] | 1103 | unsigned long long cycle; |
---|
| 1104 | get_cycle( &cycle ); |
---|
[629] | 1105 | printf("\n[fft] %s : thread %d enter / first %d / last %d / cycle %d\n", |
---|
[637] | 1106 | __FUNCTION__, tid, MyFirst, MyLast, (unsigned int)cycle ); |
---|
[574] | 1107 | #endif |
---|
| 1108 | |
---|
[652] | 1109 | // fft (rootN/nthreads) rows from x to tmp |
---|
[469] | 1110 | Transpose( x , tmp , MyFirst , MyLast ); |
---|
| 1111 | |
---|
[574] | 1112 | #if( DEBUG_FFT1D & 1 ) |
---|
| 1113 | get_cycle( &cycle ); |
---|
[652] | 1114 | printf("\n[fft] %s : thread %d after first fft / cycle %d\n", |
---|
[637] | 1115 | __FUNCTION__, tid, (unsigned int)cycle ); |
---|
[574] | 1116 | if( PRINT_ARRAY ) PrintArray( tmp , N ); |
---|
[469] | 1117 | #endif |
---|
| 1118 | |
---|
| 1119 | // BARRIER |
---|
| 1120 | get_cycle( &barrier_start ); |
---|
| 1121 | pthread_barrier_wait( &barrier ); |
---|
| 1122 | get_cycle( &barrier_stop ); |
---|
[637] | 1123 | sync_time[tid] = (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1124 | |
---|
[574] | 1125 | #if( DEBUG_FFT1D & 1 ) |
---|
| 1126 | get_cycle( &cycle ); |
---|
[652] | 1127 | printf("\n[fft] %s : thread %d exit barrier after first fft / cycle %d\n", |
---|
[637] | 1128 | __FUNCTION__, tid, (unsigned int)cycle ); |
---|
[574] | 1129 | #endif |
---|
| 1130 | |
---|
[469] | 1131 | // do FFTs on rows of tmp (i.e. columns of x) and apply twiddle factor |
---|
| 1132 | for (j = MyFirst; j < MyLast; j++) |
---|
| 1133 | { |
---|
[574] | 1134 | FFTRow( direction , upriv , tmp , j * rootN ); |
---|
| 1135 | |
---|
[469] | 1136 | TwiddleOneCol( direction , j , twid , tmp , j * rootN ); |
---|
| 1137 | } |
---|
| 1138 | |
---|
[574] | 1139 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1140 | printf("\n[fft] %s : thread %d after first twiddle\n", __FUNCTION__, tid); |
---|
[574] | 1141 | if( PRINT_ARRAY ) PrintArray( tmp , N ); |
---|
[469] | 1142 | #endif |
---|
| 1143 | |
---|
| 1144 | // BARRIER |
---|
| 1145 | get_cycle( &barrier_start ); |
---|
| 1146 | pthread_barrier_wait( &barrier ); |
---|
| 1147 | get_cycle( &barrier_stop ); |
---|
| 1148 | |
---|
[574] | 1149 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1150 | printf("\n[fft] %s : thread %d exit barrier after first twiddle\n", __FUNCTION__, tid); |
---|
[574] | 1151 | #endif |
---|
| 1152 | |
---|
[637] | 1153 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1154 | |
---|
[652] | 1155 | // fft tmp to x |
---|
[469] | 1156 | Transpose( tmp , x , MyFirst , MyLast ); |
---|
| 1157 | |
---|
[574] | 1158 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1159 | printf("\n[fft] %s : thread %d after second fft\n", __FUNCTION__, tid); |
---|
[574] | 1160 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1161 | #endif |
---|
| 1162 | |
---|
| 1163 | // BARRIER |
---|
| 1164 | get_cycle( &barrier_start ); |
---|
| 1165 | pthread_barrier_wait( &barrier ); |
---|
| 1166 | get_cycle( &barrier_stop ); |
---|
| 1167 | |
---|
[574] | 1168 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1169 | printf("\n[fft] %s : thread %d exit barrier after second fft\n", __FUNCTION__, tid); |
---|
[574] | 1170 | #endif |
---|
| 1171 | |
---|
[637] | 1172 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1173 | |
---|
| 1174 | // do FFTs on rows of x and apply the scaling factor |
---|
| 1175 | for (j = MyFirst; j < MyLast; j++) |
---|
| 1176 | { |
---|
[574] | 1177 | FFTRow( direction , upriv , x , j * rootN ); |
---|
[469] | 1178 | if (direction == -1) Scale( x , j * rootN ); |
---|
| 1179 | } |
---|
| 1180 | |
---|
[574] | 1181 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1182 | printf("\n[fft] %s : thread %d after FFT on rows\n", __FUNCTION__, tid); |
---|
[574] | 1183 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1184 | #endif |
---|
| 1185 | |
---|
| 1186 | // BARRIER |
---|
| 1187 | get_cycle( &barrier_start ); |
---|
| 1188 | pthread_barrier_wait( &barrier ); |
---|
| 1189 | get_cycle( &barrier_stop ); |
---|
| 1190 | |
---|
[574] | 1191 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1192 | printf("\n[fft] %s : thread %d exit barrier after FFT on rows\n", __FUNCTION__, tid); |
---|
[574] | 1193 | #endif |
---|
[637] | 1194 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1195 | |
---|
[652] | 1196 | // fft x to tmp |
---|
[469] | 1197 | Transpose( x , tmp , MyFirst , MyLast ); |
---|
| 1198 | |
---|
[574] | 1199 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1200 | printf("\n[fft] %s : thread %x after third fft\n", __FUNCTION__, tid); |
---|
[574] | 1201 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1202 | #endif |
---|
| 1203 | |
---|
| 1204 | // BARRIER |
---|
| 1205 | get_cycle( &barrier_start ); |
---|
| 1206 | pthread_barrier_wait( &barrier ); |
---|
| 1207 | get_cycle( &barrier_stop ); |
---|
| 1208 | |
---|
[574] | 1209 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1210 | printf("\n[fft] %s : thread %d exit barrier after third fft\n", __FUNCTION__, tid); |
---|
[574] | 1211 | #endif |
---|
| 1212 | |
---|
[637] | 1213 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
| 1214 | sync_time[tid] += (long)(barrier_stop - barrier_start); |
---|
[469] | 1215 | |
---|
| 1216 | // copy tmp to x |
---|
| 1217 | Copy( tmp , x , MyFirst , MyLast ); |
---|
| 1218 | |
---|
| 1219 | #if DEBUG_FFT1D |
---|
[637] | 1220 | printf("\n[fft] %s : thread %d completed\n", __FUNCTION__, tid); |
---|
[574] | 1221 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 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 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 1230 | void 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 |
---|
[469] | 1235 | { |
---|
[473] | 1236 | unsigned int i; |
---|
[469] | 1237 | double omega_r; |
---|
| 1238 | double omega_c; |
---|
| 1239 | double x_r; |
---|
| 1240 | double x_c; |
---|
[473] | 1241 | unsigned int c_id; |
---|
| 1242 | unsigned int c_offset; |
---|
[469] | 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 | |
---|
[473] | 1263 | //////////////////////////// |
---|
| 1264 | void Scale( double ** x, // data array base address |
---|
| 1265 | unsigned int offset_x ) // first point of the row to be scaled |
---|
[469] | 1266 | { |
---|
[473] | 1267 | unsigned int i; |
---|
| 1268 | unsigned int c_id; |
---|
| 1269 | unsigned int c_offset; |
---|
[469] | 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); |
---|
[473] | 1275 | x[c_id][2*c_offset] /= N; |
---|
| 1276 | x[c_id][2*c_offset + 1] /= N; |
---|
[469] | 1277 | } |
---|
| 1278 | } |
---|
| 1279 | |
---|
[473] | 1280 | /////////////////////////////////// |
---|
| 1281 | void 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 |
---|
[469] | 1285 | { |
---|
[473] | 1286 | unsigned int row; // row index |
---|
| 1287 | unsigned int point; // data point index in a row |
---|
[469] | 1288 | |
---|
[473] | 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 |
---|
[469] | 1292 | |
---|
[473] | 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 |
---|
[469] | 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 | |
---|
[473] | 1319 | ////////////////////////////// |
---|
| 1320 | void 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 |
---|
[469] | 1324 | { |
---|
[473] | 1325 | unsigned int row; // row index |
---|
| 1326 | unsigned int point; // data point index in a row |
---|
[469] | 1327 | |
---|
[473] | 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 |
---|
[469] | 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 | |
---|
[473] | 1347 | /////////////////////////////// |
---|
| 1348 | void Reverse( double ** x, |
---|
| 1349 | unsigned int offset_x ) |
---|
[469] | 1350 | { |
---|
[473] | 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; |
---|
[469] | 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 | ///////////////////////////////////////////////////////////////////////////// |
---|
[574] | 1377 | void FFTRow( int direction, // 1 direct / -1 inverse |
---|
[473] | 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 |
---|
[469] | 1381 | { |
---|
[473] | 1382 | unsigned int j; |
---|
| 1383 | unsigned int k; |
---|
| 1384 | unsigned int q; |
---|
| 1385 | unsigned int L; |
---|
| 1386 | unsigned int r; |
---|
| 1387 | unsigned int Lstar; |
---|
[469] | 1388 | double * u1; |
---|
| 1389 | |
---|
[473] | 1390 | unsigned int offset_x1; // index first butterfly input |
---|
| 1391 | unsigned int offset_x2; // index second butterfly output |
---|
[469] | 1392 | |
---|
[473] | 1393 | double omega_r; // real part butterfy coef |
---|
| 1394 | double omega_c; // complex part butterfly coef |
---|
[469] | 1395 | |
---|
[473] | 1396 | double tau_r; |
---|
| 1397 | double tau_c; |
---|
[469] | 1398 | |
---|
[473] | 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 |
---|
[469] | 1403 | |
---|
[473] | 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 |
---|
[469] | 1408 | |
---|
[574] | 1409 | #if DEBUG_ROW |
---|
[469] | 1410 | unsigned int p; |
---|
[628] | 1411 | printf("\n[fft] ROW data in / %d points / offset = %d\n", rootN , offset_x ); |
---|
[574] | 1412 | |
---|
[469] | 1413 | for ( p = 0 ; p < rootN ; p++ ) |
---|
| 1414 | { |
---|
[473] | 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); |
---|
[469] | 1418 | printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] ); |
---|
| 1419 | } |
---|
| 1420 | printf("\n"); |
---|
| 1421 | #endif |
---|
| 1422 | |
---|
| 1423 | // This makes the rootN input points reordering |
---|
| 1424 | Reverse( x , offset_x ); |
---|
| 1425 | |
---|
[574] | 1426 | #if DEBUG_ROW |
---|
[628] | 1427 | printf("\n[fft] ROW data after reverse / %d points / offset = %d\n", rootN , offset_x ); |
---|
[574] | 1428 | |
---|
[469] | 1429 | for ( p = 0 ; p < rootN ; p++ ) |
---|
| 1430 | { |
---|
[473] | 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); |
---|
[469] | 1434 | printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] ); |
---|
| 1435 | } |
---|
| 1436 | printf("\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 | |
---|
[574] | 1451 | #if (DEBUG_ROW & 1) |
---|
| 1452 | printf("\n ### q = %d / k = %d / x1 = %d / x2 = %d\n", q , k , offset_x1 , offset_x2 ); |
---|
[469] | 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 | |
---|
[574] | 1473 | #if (DEBUG_ROW & 1) |
---|
[469] | 1474 | printf("\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 | |
---|
[574] | 1489 | #if (DEBUG_ROW & 1) |
---|
[469] | 1490 | printf("\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 | |
---|
[574] | 1497 | #if DEBUG_ROW |
---|
[628] | 1498 | printf("\n[fft] ROW data out / %d points / offset = %d\n", rootN , offset_x ); |
---|
[469] | 1499 | for ( p = 0 ; p < rootN ; p++ ) |
---|
| 1500 | { |
---|
[473] | 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); |
---|
[469] | 1504 | printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] ); |
---|
| 1505 | } |
---|
| 1506 | printf("\n"); |
---|
| 1507 | #endif |
---|
| 1508 | |
---|
[574] | 1509 | } // end FFTRow() |
---|
[469] | 1510 | |
---|
[473] | 1511 | /////////////////////////////////////// |
---|
| 1512 | void PrintArray( double ** array, |
---|
| 1513 | unsigned int size ) |
---|
[469] | 1514 | { |
---|
[473] | 1515 | unsigned int i; |
---|
| 1516 | unsigned int c_id; |
---|
| 1517 | unsigned int c_offset; |
---|
[469] | 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 | |
---|