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