[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] |
---|
[656] | 179 | pthread_t work_trdid[CLUSTERS_MAX * CORES_MAX]; |
---|
[637] | 180 | |
---|
[652] | 181 | // array of thread attributes / indexed by [tid] |
---|
[656] | 182 | pthread_attr_t work_attr[CLUSTERS_MAX * CORES_MAX]; |
---|
[637] | 183 | |
---|
[652] | 184 | // array of work function arguments / indexed by [tid] |
---|
[656] | 185 | pthread_parallel_work_args_t work_args[CLUSTERS_MAX * CORES_MAX]; |
---|
[652] | 186 | |
---|
[637] | 187 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 188 | // functions declaration |
---|
[637] | 189 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[469] | 190 | |
---|
[656] | 191 | void * work( void * 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 | /////////////////////////////////////////////////////////////////// |
---|
[656] | 256 | int 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 |
---|
[659] | 292 | hard_config_t config; |
---|
| 293 | if( get_config( &config ) ) |
---|
[469] | 294 | { |
---|
[628] | 295 | printf("\n[fft error] cannot get hardware configuration\n"); |
---|
[469] | 296 | exit( 0 ); |
---|
| 297 | } |
---|
| 298 | |
---|
[659] | 299 | x_size = config.x_size; |
---|
| 300 | y_size = config.y_size; |
---|
| 301 | ncores = config.ncores; |
---|
| 302 | |
---|
[469] | 303 | // check ncores |
---|
| 304 | if( (ncores != 1) && (ncores != 2) && (ncores != 4) ) |
---|
| 305 | { |
---|
[628] | 306 | printf("\n[fft error] number of cores per cluster must be 1/2/4\n"); |
---|
[469] | 307 | exit( 0 ); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | // check x_size |
---|
| 311 | if( (x_size != 1) && (x_size != 2) && (x_size != 4) && (x_size != 8) && (x_size != 16) ) |
---|
| 312 | { |
---|
[628] | 313 | printf("\n[fft error] x_size must be 1/2/4/8/16\n"); |
---|
[469] | 314 | exit( 0 ); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | // check y_size |
---|
| 318 | if( (y_size != 1) && (y_size != 2) && (y_size != 4) && (y_size != 8) && (y_size != 16) ) |
---|
| 319 | { |
---|
[628] | 320 | printf("\n[fft error] y_size must be 1/2/4/8/16\n"); |
---|
[469] | 321 | exit( 0 ); |
---|
| 322 | } |
---|
| 323 | |
---|
[652] | 324 | // get identifiers for core executing main |
---|
| 325 | unsigned int cxy_main; |
---|
| 326 | unsigned int lid_main; |
---|
| 327 | get_core_id( &cxy_main , &lid_main ); |
---|
| 328 | |
---|
[637] | 329 | // compute nthreads and nclusters |
---|
[469] | 330 | nthreads = x_size * y_size * ncores; |
---|
| 331 | nclusters = x_size * y_size; |
---|
| 332 | |
---|
[637] | 333 | // compute covering DQT size an level |
---|
| 334 | unsigned int z = (x_size > y_size) ? x_size : y_size; |
---|
| 335 | unsigned int root_level = (z == 1) ? 0 : (z == 2) ? 1 : (z == 4) ? 2 : (z == 8) ? 3 : 4; |
---|
| 336 | |
---|
[469] | 337 | // compute various constants depending on N and T |
---|
| 338 | N = 1 << M; |
---|
| 339 | rootN = 1 << (M / 2); |
---|
| 340 | rows_per_thread = rootN / nthreads; |
---|
| 341 | points_per_cluster = N / nclusters; |
---|
| 342 | |
---|
| 343 | // check N versus T |
---|
| 344 | if( rootN < nthreads ) |
---|
| 345 | { |
---|
[628] | 346 | printf("\n[fft error] sqrt(N) must be larger than T\n"); |
---|
[469] | 347 | exit( 0 ); |
---|
| 348 | } |
---|
| 349 | |
---|
[652] | 350 | // define instrumentation file name |
---|
| 351 | if( NO_PLACEMENT ) |
---|
| 352 | { |
---|
| 353 | printf("\n[fft] starts / %d points / %d thread(s) / PID %x / NO_PLACE\n", |
---|
| 354 | N, nthreads, pid ); |
---|
[469] | 355 | |
---|
[652] | 356 | // build instrumentation file name |
---|
| 357 | if( USE_DQT_BARRIER ) |
---|
| 358 | snprintf( name , 64 , "fft_dqt_no_place_%d_%d_%d", M , x_size * y_size , ncores ); |
---|
| 359 | else |
---|
| 360 | snprintf( name , 64 , "fft_smp_no_place_%d_%d_%d", M , x_size * y_size , ncores ); |
---|
| 361 | } |
---|
[469] | 362 | |
---|
[652] | 363 | if( EXPLICIT_PLACEMENT ) |
---|
| 364 | { |
---|
| 365 | printf("\n[fft] starts / %d points / %d thread(s) / PID %x / EXPLICIT\n", |
---|
| 366 | N, nthreads, pid ); |
---|
| 367 | |
---|
| 368 | // build instrumentation file name |
---|
| 369 | if( USE_DQT_BARRIER ) |
---|
| 370 | snprintf( name , 64 , "fft_dqt_explicit_%d_%d_%d", M , x_size * y_size , ncores ); |
---|
| 371 | else |
---|
| 372 | snprintf( name , 64 , "fft_smp_explicit_%d_%d_%d", M , x_size * y_size , ncores ); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | if( PARALLEL_PLACEMENT ) |
---|
| 376 | { |
---|
| 377 | printf("\n[fft] starts / %d points / %d thread(s) / PID %x / PARALLEL\n", |
---|
| 378 | N, nthreads, pid ); |
---|
| 379 | |
---|
| 380 | // build instrumentation file name |
---|
| 381 | if( USE_DQT_BARRIER ) |
---|
| 382 | snprintf( name , 64 , "fft_dqt_parallel_%d_%d_%d", M , x_size * y_size , ncores ); |
---|
| 383 | else |
---|
| 384 | snprintf( name , 64 , "fft_smp_parallel_%d_%d_%d", M , x_size * y_size , ncores ); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | // build instrumentation file pathname |
---|
[637] | 388 | snprintf( path , 128 , "/home/%s", name ); |
---|
| 389 | |
---|
| 390 | // open instrumentation file |
---|
| 391 | FILE * f = fopen( path , NULL ); |
---|
| 392 | if ( f == NULL ) |
---|
| 393 | { |
---|
| 394 | printf("\n[fft error] cannot open instrumentation file <%s>\n", path ); |
---|
| 395 | exit( 0 ); |
---|
[469] | 396 | } |
---|
| 397 | |
---|
[635] | 398 | #if DEBUG_MAIN |
---|
[637] | 399 | get_cycle( &debug_cycle ); |
---|
[652] | 400 | printf("\n[fft] main open instrumentation file <%s> at cycle %d\n", |
---|
[637] | 401 | path, (unsigned int)debug_cycle ); |
---|
[635] | 402 | #endif |
---|
[574] | 403 | |
---|
[469] | 404 | #if CHECK |
---|
[473] | 405 | ck1 = CheckSum(); |
---|
[469] | 406 | #endif |
---|
| 407 | |
---|
[574] | 408 | #if PRINT_ARRAY |
---|
[469] | 409 | printf("\nData values / base = %x\n", &data[0][0] ); |
---|
| 410 | PrintArray( data , N ); |
---|
| 411 | |
---|
| 412 | printf("\nTwiddle values / base = %x\n", &twid[0][0] ); |
---|
| 413 | PrintArray( twid , N ); |
---|
| 414 | |
---|
| 415 | SimpleDft( 1 , N , data , 0 , bloup , 0 ); |
---|
| 416 | |
---|
| 417 | printf("\nExpected results / base = %x\n", &bloup[0][0] ); |
---|
| 418 | PrintArray( bloup , N ); |
---|
| 419 | #endif |
---|
| 420 | |
---|
[637] | 421 | // initialise barrier synchronizing all <work> threads |
---|
[628] | 422 | if( USE_DQT_BARRIER ) |
---|
[473] | 423 | { |
---|
[628] | 424 | barrier_attr.x_size = x_size; |
---|
| 425 | barrier_attr.y_size = y_size; |
---|
| 426 | barrier_attr.nthreads = ncores; |
---|
| 427 | error = pthread_barrier_init( &barrier, &barrier_attr , nthreads ); |
---|
| 428 | } |
---|
| 429 | else |
---|
| 430 | { |
---|
| 431 | error = pthread_barrier_init( &barrier, NULL , nthreads ); |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | if( error ) |
---|
| 435 | { |
---|
| 436 | printf("\n[fft error] cannot initialize barrier\n"); |
---|
[473] | 437 | exit( 0 ); |
---|
| 438 | } |
---|
[469] | 439 | |
---|
[635] | 440 | #if DEBUG_MAIN |
---|
[637] | 441 | get_cycle( &debug_cycle ); |
---|
[652] | 442 | printf("\n[fft] main completes sequencial initialisation at cycle %d\n", |
---|
[637] | 443 | (unsigned int)debug_cycle ); |
---|
[635] | 444 | #endif |
---|
[473] | 445 | |
---|
[652] | 446 | // register sequencial time |
---|
| 447 | get_cycle( &end_init_cycle ); |
---|
| 448 | init_time = (unsigned int)(end_init_cycle - start_init_cycle); |
---|
| 449 | |
---|
| 450 | ////////////////// |
---|
| 451 | if( NO_PLACEMENT ) |
---|
[469] | 452 | { |
---|
[652] | 453 | // the tid value for the main thread is always 0 |
---|
| 454 | // main thread creates new threads with tid in [1,nthreads-1] |
---|
| 455 | unsigned int tid; |
---|
| 456 | for ( tid = 0 ; tid < nthreads ; tid++ ) |
---|
[469] | 457 | { |
---|
[652] | 458 | // register tid value in work_args[tid] array |
---|
| 459 | work_args[tid].tid = tid; |
---|
| 460 | |
---|
| 461 | // create other threads |
---|
| 462 | if( tid > 0 ) |
---|
| 463 | { |
---|
| 464 | if ( pthread_create( &work_trdid[tid], |
---|
| 465 | NULL, // no attribute |
---|
| 466 | &work, |
---|
| 467 | &work_args[tid] ) ) |
---|
| 468 | { |
---|
| 469 | printf("\n[fft error] cannot create thread %d\n", tid ); |
---|
| 470 | exit( 0 ); |
---|
| 471 | } |
---|
[588] | 472 | |
---|
[652] | 473 | #if DEBUG_MAIN |
---|
| 474 | printf("\n[fft] main created thread %d\n", tid ); |
---|
| 475 | #endif |
---|
| 476 | |
---|
| 477 | } |
---|
| 478 | else |
---|
[469] | 479 | { |
---|
[652] | 480 | tid_main = 0; |
---|
| 481 | } |
---|
| 482 | } // end for tid |
---|
[469] | 483 | |
---|
[652] | 484 | // main thread calls itself the execute() function |
---|
| 485 | work( &work_args[0] ); |
---|
[469] | 486 | |
---|
[652] | 487 | // main thread wait other threads completion |
---|
| 488 | for ( tid = 1 ; tid < nthreads ; tid++ ) |
---|
| 489 | { |
---|
| 490 | unsigned int * status; |
---|
| 491 | |
---|
| 492 | // main wait thread[tid] status |
---|
| 493 | if ( pthread_join( work_trdid[tid], (void*)(&status)) ) |
---|
| 494 | { |
---|
| 495 | printf("\n[fft error] main cannot join thread %d\n", tid ); |
---|
| 496 | exit( 0 ); |
---|
[469] | 497 | } |
---|
[652] | 498 | |
---|
| 499 | // check status |
---|
| 500 | if( *status != THREAD_EXIT_SUCCESS ) |
---|
| 501 | { |
---|
| 502 | printf("\n[fft error] thread %x returned failure\n", tid ); |
---|
| 503 | exit( 0 ); |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | #if DEBUG_MAIN |
---|
| 507 | printf("\n[fft] main successfully joined thread %x\n", tid ); |
---|
| 508 | #endif |
---|
| 509 | |
---|
| 510 | } // end for tid |
---|
| 511 | |
---|
| 512 | } // end if no_placement |
---|
| 513 | |
---|
| 514 | //////////////////////// |
---|
| 515 | if( EXPLICIT_PLACEMENT ) |
---|
| 516 | { |
---|
| 517 | // main thread places each thread[tid] on a specific core[cxy][lid] |
---|
| 518 | // but the actual thread creation is sequencial |
---|
| 519 | unsigned int x; |
---|
| 520 | unsigned int y; |
---|
| 521 | unsigned int l; |
---|
| 522 | unsigned int cxy; // cluster identifier |
---|
| 523 | unsigned int tid; // thread continuous index |
---|
| 524 | |
---|
| 525 | for( x = 0 ; x < x_size ; x++ ) |
---|
| 526 | { |
---|
| 527 | for( y = 0 ; y < y_size ; y++ ) |
---|
| 528 | { |
---|
| 529 | cxy = HAL_CXY_FROM_XY( x , y ); |
---|
| 530 | for( l = 0 ; l < ncores ; l++ ) |
---|
| 531 | { |
---|
| 532 | // compute thread continuous index |
---|
| 533 | tid = (((x * y_size) + y) * ncores) + l; |
---|
| 534 | |
---|
| 535 | // register tid value in work_args[tid] array |
---|
| 536 | work_args[tid].tid = tid; |
---|
| 537 | |
---|
| 538 | // no thread created on the core running the main |
---|
| 539 | if( (cxy != cxy_main) || (l != lid_main) ) |
---|
| 540 | { |
---|
| 541 | // define thread attributes |
---|
| 542 | work_attr[tid].attributes = PT_ATTR_CLUSTER_DEFINED | |
---|
| 543 | PT_ATTR_CORE_DEFINED; |
---|
| 544 | work_attr[tid].cxy = cxy; |
---|
| 545 | work_attr[tid].lid = l; |
---|
| 546 | |
---|
| 547 | // create thread[tid] on core[cxy][l] |
---|
| 548 | if ( pthread_create( &work_trdid[tid], |
---|
| 549 | &work_attr[tid], |
---|
| 550 | &work, |
---|
| 551 | &work_args[tid] ) ) |
---|
| 552 | { |
---|
| 553 | printf("\n[fft error] cannot create thread %d\n", tid ); |
---|
| 554 | exit( 0 ); |
---|
| 555 | } |
---|
| 556 | #if DEBUG_MAIN |
---|
| 557 | printf("\n[fft] main created thread[%d] on core[%x,%d]\n", tid, cxy, l ); |
---|
| 558 | #endif |
---|
| 559 | } |
---|
| 560 | else |
---|
| 561 | { |
---|
| 562 | tid_main = tid; |
---|
| 563 | } |
---|
| 564 | } |
---|
| 565 | } |
---|
[469] | 566 | } |
---|
| 567 | |
---|
[652] | 568 | // main thread calls itself the execute() function |
---|
| 569 | work( &work_args[tid_main] ); |
---|
[637] | 570 | |
---|
[652] | 571 | // main thread wait other threads completion |
---|
| 572 | for( tid = 0 ; tid < nthreads ; tid++ ) |
---|
| 573 | { |
---|
| 574 | // no other thread on the core running the main |
---|
| 575 | if( tid != tid_main ) |
---|
| 576 | { |
---|
| 577 | unsigned int * status; |
---|
| 578 | |
---|
| 579 | // wait thread[tid] |
---|
| 580 | if( pthread_join( work_trdid[tid] , (void*)(&status) ) ) |
---|
| 581 | { |
---|
| 582 | printf("\n[fft error] main cannot join thread %d\n", tid ); |
---|
| 583 | exit( 0 ); |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | // check status |
---|
| 587 | if( *status != THREAD_EXIT_SUCCESS ) |
---|
| 588 | { |
---|
| 589 | printf("\n[fft error] thread %d returned failure\n", tid ); |
---|
| 590 | exit( 0 ); |
---|
| 591 | } |
---|
| 592 | #if DEBUG_MAIN |
---|
| 593 | printf("\n[fft] main joined thread %d on core[%x,%d]\n", tid , cxy , l ); |
---|
[635] | 594 | #endif |
---|
[652] | 595 | } |
---|
| 596 | } |
---|
| 597 | } // end if explicit_placement |
---|
[628] | 598 | |
---|
[652] | 599 | //////////////////////// |
---|
| 600 | if( PARALLEL_PLACEMENT ) |
---|
[469] | 601 | { |
---|
[652] | 602 | // create and execute the working threads |
---|
| 603 | if( pthread_parallel_create( root_level , &work ) ) |
---|
| 604 | { |
---|
| 605 | printf("\n[fft error] cannot create threads\n"); |
---|
| 606 | exit( 0 ); |
---|
| 607 | } |
---|
[637] | 608 | } |
---|
[469] | 609 | |
---|
[637] | 610 | #if DEBUG_MAIN |
---|
| 611 | get_cycle( &debug_cycle ); |
---|
| 612 | printf("\n[fft] main resume for instrumentation at cycle %d\n", |
---|
| 613 | (unsigned int)debug_cycle) ; |
---|
[588] | 614 | #endif |
---|
[469] | 615 | |
---|
[574] | 616 | #if PRINT_ARRAY |
---|
[469] | 617 | printf("\nData values after FFT:\n"); |
---|
| 618 | PrintArray( data , N ); |
---|
| 619 | #endif |
---|
| 620 | |
---|
| 621 | #if CHECK |
---|
[473] | 622 | ck3 = CheckSum(); |
---|
[469] | 623 | printf("\n*** Results ***\n"); |
---|
| 624 | printf("Checksum difference is %f (%f, %f)\n", ck1 - ck3, ck1, ck3); |
---|
| 625 | if (fabs(ck1 - ck3) < 0.001) printf("Results OK\n"); |
---|
| 626 | else printf("Results KO\n"); |
---|
| 627 | #endif |
---|
| 628 | |
---|
[628] | 629 | // display header on terminal, and save to file |
---|
| 630 | printf("\n----- %s -----\n", name ); |
---|
[469] | 631 | |
---|
[628] | 632 | ret = fprintf( f , "\n----- %s -----\n", name ); |
---|
| 633 | if( ret < 0 ) |
---|
| 634 | { |
---|
| 635 | printf("\n[fft error] cannot write header to file <%s>\n", path ); |
---|
| 636 | exit(0); |
---|
| 637 | } |
---|
[469] | 638 | |
---|
[641] | 639 | // initializes global (all threads) instrumentation values |
---|
| 640 | unsigned int time_para = parallel_time[0]; |
---|
| 641 | unsigned int time_sync = sync_time[0]; |
---|
| 642 | unsigned int pgfaults_nr = 0; |
---|
| 643 | unsigned int pgfaults_cost = 0; |
---|
| 644 | unsigned int pgfaults_max = pgfault_max[0]; |
---|
| 645 | |
---|
| 646 | // loop on threads to compute global instrumentation results |
---|
[588] | 647 | for (tid = 0 ; tid < nthreads ; tid++) |
---|
| 648 | { |
---|
[641] | 649 | snprintf( string , 256 , |
---|
| 650 | "- tid %d : Seq %d / Para %d / Sync %d / Pgfaults %d ( cost %d / max %d )\n", |
---|
| 651 | tid, init_time, parallel_time[tid], sync_time[tid], |
---|
| 652 | pgfault_nr[tid], (pgfault_cost[tid] / pgfault_nr[tid]) , pgfault_max[tid] ); |
---|
[588] | 653 | |
---|
[637] | 654 | // save to instrumentation file |
---|
[628] | 655 | fprintf( f , "%s" , string ); |
---|
| 656 | if( ret < 0 ) |
---|
| 657 | { |
---|
[641] | 658 | printf("\n[fft error] cannot save thread %d results to file <%s>\n", tid, path ); |
---|
[637] | 659 | printf("%s", string ); |
---|
[628] | 660 | exit(0); |
---|
| 661 | } |
---|
[641] | 662 | |
---|
| 663 | // compute global values |
---|
| 664 | if (parallel_time[tid] > time_para) time_para = parallel_time[tid]; |
---|
| 665 | if (sync_time[tid] > time_sync) time_sync = sync_time[tid]; |
---|
[649] | 666 | |
---|
| 667 | pgfaults_nr += pgfault_nr[tid]; |
---|
| 668 | pgfaults_cost += pgfault_cost[tid]; |
---|
| 669 | |
---|
[641] | 670 | if (pgfault_max[tid] > pgfaults_max) pgfaults_max = pgfault_max[tid]; |
---|
[588] | 671 | } |
---|
| 672 | |
---|
[641] | 673 | // display global values on terminal and save to file |
---|
| 674 | snprintf( string , 256 , |
---|
| 675 | "\nSeq %d / Para %d / Sync %d / Pgfaults %d ( cost %d / max %d )\n", |
---|
| 676 | init_time, time_para, time_sync, pgfaults_nr, (pgfaults_cost / pgfaults_nr), pgfaults_max ); |
---|
[588] | 677 | |
---|
[641] | 678 | printf("%s", string ); |
---|
| 679 | |
---|
| 680 | // save global values to file |
---|
| 681 | ret = fprintf( f , "%s", string ); |
---|
| 682 | |
---|
| 683 | if( ret < 0 ) |
---|
[469] | 684 | { |
---|
[641] | 685 | printf("\n[fft error] cannot save global results to file <%s>\n", path ); |
---|
| 686 | exit(0); |
---|
[469] | 687 | } |
---|
| 688 | |
---|
[641] | 689 | // close instrumentation file |
---|
| 690 | ret = fclose( f ); |
---|
| 691 | |
---|
[628] | 692 | if( ret < 0 ) |
---|
| 693 | { |
---|
[641] | 694 | printf("\n[fft error] cannot close file <%s>\n", path ); |
---|
[628] | 695 | exit(0); |
---|
| 696 | } |
---|
[469] | 697 | |
---|
[637] | 698 | #if DEBUG_MAIN |
---|
| 699 | get_cycle( &debug_cycle ); |
---|
[641] | 700 | printf("\n[fft] main exit <%s> at cycle %d\n", |
---|
[637] | 701 | path, (unsigned int)debug_cycle ); |
---|
| 702 | #endif |
---|
[469] | 703 | |
---|
[628] | 704 | exit( 0 ); |
---|
| 705 | |
---|
[656] | 706 | return 0; |
---|
| 707 | |
---|
[469] | 708 | } // end main() |
---|
| 709 | |
---|
[637] | 710 | ///////////////////////////////////////////////////////////////// |
---|
| 711 | // This function is executed in parallel by all <work> threads. |
---|
| 712 | ///////////////////////////////////////////////////////////////// |
---|
[656] | 713 | void * work( void * arguments ) |
---|
[469] | 714 | { |
---|
[637] | 715 | unsigned int tid; // this thread continuous index |
---|
| 716 | unsigned int lid; // core local index |
---|
| 717 | unsigned int cid; // cluster continuous index |
---|
[469] | 718 | |
---|
[637] | 719 | unsigned int MyFirst; // index first row allocated to thread |
---|
| 720 | unsigned int MyLast; // index last row allocated to thread |
---|
| 721 | double * upriv; // private array of FFT coefs |
---|
| 722 | |
---|
[469] | 723 | unsigned long long parallel_start; |
---|
| 724 | unsigned long long parallel_stop; |
---|
| 725 | unsigned long long barrier_start; |
---|
| 726 | unsigned long long barrier_stop; |
---|
| 727 | |
---|
[652] | 728 | get_cycle( ¶llel_start ); |
---|
| 729 | |
---|
[637] | 730 | // get thread arguments |
---|
[656] | 731 | pthread_parallel_work_args_t * args = (pthread_parallel_work_args_t *)arguments; |
---|
| 732 | |
---|
| 733 | tid = args->tid; |
---|
| 734 | pthread_barrier_t * parent_barrier = args->barrier; |
---|
[469] | 735 | |
---|
[652] | 736 | // compute lid and cid from tid |
---|
| 737 | lid = tid % ncores; |
---|
| 738 | cid = tid / ncores; |
---|
| 739 | |
---|
[637] | 740 | #if DEBUG_WORK |
---|
[629] | 741 | printf("\n[fft] %s : thread %d enter / cycle %d\n", |
---|
[637] | 742 | __FUNCTION__, tid, (unsigned int)parallel_start ); |
---|
[588] | 743 | #endif |
---|
| 744 | |
---|
[638] | 745 | // thread on core 0 allocates memory from the local cluster |
---|
[637] | 746 | // for the distributed data[], trans[], twid[] buffers |
---|
| 747 | if( lid == 0 ) |
---|
| 748 | { |
---|
[638] | 749 | unsigned int data_size = (N / nclusters) * 2 * sizeof(double); |
---|
[637] | 750 | |
---|
[638] | 751 | data[cid] = (double *)malloc( data_size ); |
---|
| 752 | if( data[cid] == NULL ) |
---|
| 753 | { |
---|
| 754 | printf("\n[fft_error] in work : cannot allocate data[%d] buffer\n", cid ); |
---|
| 755 | pthread_barrier_wait( parent_barrier ); |
---|
| 756 | pthread_exit( NULL ); |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | trans[cid] = (double *)malloc( data_size ); |
---|
| 760 | if( trans[cid] == NULL ) |
---|
| 761 | { |
---|
| 762 | printf("\n[fft_error] in work : cannot allocate trans[%d] buffer\n", cid ); |
---|
| 763 | pthread_barrier_wait( parent_barrier ); |
---|
| 764 | pthread_exit( NULL ); |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | twid[cid] = (double *)malloc( data_size ); |
---|
| 768 | if( twid[cid] == NULL ) |
---|
| 769 | { |
---|
| 770 | printf("\n[fft_error] in work : cannot allocate twid[%d] buffer\n", cid ); |
---|
| 771 | pthread_barrier_wait( parent_barrier ); |
---|
| 772 | pthread_exit( NULL ); |
---|
| 773 | } |
---|
[637] | 774 | } |
---|
| 775 | |
---|
[638] | 776 | // BARRIER to wait distributed buffers allocation |
---|
[629] | 777 | get_cycle( &barrier_start ); |
---|
| 778 | pthread_barrier_wait( &barrier ); |
---|
| 779 | get_cycle( &barrier_stop ); |
---|
[637] | 780 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[629] | 781 | |
---|
[637] | 782 | #if DEBUG_WORK |
---|
[638] | 783 | printf("\n[fft] %s : thread %d exit barrier for buffer allocation / cycle %d\n", |
---|
[637] | 784 | __FUNCTION__, tid, (unsigned int)barrier_stop ); |
---|
[630] | 785 | #endif |
---|
[629] | 786 | |
---|
[638] | 787 | // all threads contribute to data[] local array initialisation |
---|
[637] | 788 | InitD( data , MODE , tid ); |
---|
[469] | 789 | |
---|
[638] | 790 | // all threads contribute to data[] local array initialisation |
---|
[637] | 791 | InitT( twid , tid ); |
---|
| 792 | |
---|
[638] | 793 | // BARRIER to wait distributed buffers initialisation |
---|
[637] | 794 | get_cycle( &barrier_start ); |
---|
| 795 | pthread_barrier_wait( &barrier ); |
---|
| 796 | get_cycle( &barrier_stop ); |
---|
| 797 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
| 798 | |
---|
| 799 | #if DEBUG_WORK |
---|
[638] | 800 | printf("\n[fft] %s : thread %d exit barrier for buffer initialisation / cycle %d\n", |
---|
[637] | 801 | __FUNCTION__, tid, (unsigned int)barrier_stop ); |
---|
| 802 | #endif |
---|
| 803 | |
---|
[638] | 804 | // all threads allocate memory from the local cluster |
---|
| 805 | // for the private upriv[] buffer |
---|
| 806 | upriv = (double *)malloc( (rootN - 1) * 2 * sizeof(double) ); |
---|
| 807 | if( upriv == NULL ) |
---|
| 808 | { |
---|
| 809 | printf("\n[fft_error] in work : cannot allocate trans[%d] buffer\n", cid ); |
---|
| 810 | pthread_barrier_wait( parent_barrier ); |
---|
| 811 | pthread_exit( NULL ); |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | // all threads initialise the private upriv[] array |
---|
| 815 | InitU( upriv ); |
---|
| 816 | |
---|
| 817 | // all threads compute first and last rows handled by the thread |
---|
[637] | 818 | MyFirst = rootN * tid / nthreads; |
---|
| 819 | MyLast = rootN * (tid + 1) / nthreads; |
---|
[469] | 820 | |
---|
[638] | 821 | // all threads perform forward FFT |
---|
[637] | 822 | FFT1D( 1 , data , trans , upriv , twid , tid , MyFirst , MyLast ); |
---|
[469] | 823 | |
---|
| 824 | #if CHECK |
---|
| 825 | get_cycle( &barrier_start ); |
---|
| 826 | pthread_barrier_wait( &barrier ); |
---|
| 827 | get_cycle( &barrier_stop ); |
---|
[637] | 828 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
| 829 | FFT1D( -1 , data , trans , upriv , twid , tid , MyFirst , MyLast ); |
---|
[469] | 830 | #endif |
---|
| 831 | |
---|
| 832 | get_cycle( ¶llel_stop ); |
---|
| 833 | |
---|
[641] | 834 | // register parallel time in instrumentation counters |
---|
[637] | 835 | parallel_time[tid] = (unsigned int)(parallel_stop - parallel_start); |
---|
[630] | 836 | |
---|
[641] | 837 | // get work thread info for page faults |
---|
| 838 | thread_info_t info; |
---|
| 839 | get_thread_info( &info ); |
---|
| 840 | |
---|
| 841 | // register page faults in instrumentation counters |
---|
| 842 | pgfault_nr[tid] = info.false_pgfault_nr + |
---|
| 843 | info.local_pgfault_nr + |
---|
| 844 | info.global_pgfault_nr; |
---|
| 845 | pgfault_cost[tid] = info.false_pgfault_cost + |
---|
| 846 | info.local_pgfault_cost + |
---|
| 847 | info.global_pgfault_cost; |
---|
| 848 | pgfault_max[tid] = info.false_pgfault_max + |
---|
| 849 | info.local_pgfault_max + |
---|
| 850 | info.global_pgfault_max; |
---|
[637] | 851 | #if DEBUG_WORK |
---|
| 852 | printf("\n[fft] %s : thread %d completes fft / p_start %d / p_stop %d\n", |
---|
| 853 | __FUNCTION__, tid, (unsigned int)parallel_start, (unsigned int)parallel_stop ); |
---|
[588] | 854 | #endif |
---|
[469] | 855 | |
---|
[637] | 856 | // work thread signals completion to main |
---|
| 857 | pthread_barrier_wait( parent_barrier ); |
---|
[588] | 858 | |
---|
[637] | 859 | #if DEBUG_WORK |
---|
| 860 | printf("\n[fft] %s : thread %d exit\n", |
---|
| 861 | __FUNCTION__, tid ); |
---|
| 862 | #endif |
---|
[469] | 863 | |
---|
[640] | 864 | #if DISPLAY_SCHED_AND_VMM |
---|
| 865 | printf("\n[fft] %s : thread %d exit\n", __FUNCTION__, tid ); |
---|
| 866 | if( lid == 0 ) display_vmm( cxy , getpid() , 0 ); |
---|
| 867 | #endif |
---|
| 868 | |
---|
[637] | 869 | // work thread exit |
---|
| 870 | pthread_exit( NULL ); |
---|
| 871 | |
---|
[656] | 872 | return NULL; |
---|
| 873 | |
---|
[637] | 874 | } // end work() |
---|
| 875 | |
---|
[469] | 876 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
| 877 | // This function makes the DFT from the src[nclusters][points_per_cluster] distributed |
---|
| 878 | // buffer, to the dst[nclusters][points_per_cluster] distributed buffer. |
---|
| 879 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 880 | void SimpleDft( int direction, // 1 direct / -1 reverse |
---|
| 881 | unsigned int size, // number of points |
---|
| 882 | double ** src, // source distributed buffer |
---|
| 883 | unsigned int src_offset, // offset in source array |
---|
| 884 | double ** dst, // destination distributed buffer |
---|
| 885 | unsigned int dst_offset ) // offset in destination array |
---|
[469] | 886 | { |
---|
[473] | 887 | unsigned int n , k; |
---|
| 888 | double phi; // 2*PI*n*k/N |
---|
| 889 | double u_r; // cos( phi ) |
---|
| 890 | double u_c; // sin( phi ) |
---|
| 891 | double d_r; // Re(data[n]) |
---|
| 892 | double d_c; // Im(data[n]) |
---|
| 893 | double accu_r; // Re(accu) |
---|
| 894 | double accu_c; // Im(accu) |
---|
| 895 | unsigned int c_id; // distributed buffer cluster index |
---|
| 896 | unsigned int c_offset; // offset in distributed buffer |
---|
[469] | 897 | |
---|
| 898 | for ( k = 0 ; k < size ; k++ ) // loop on the output data points |
---|
| 899 | { |
---|
| 900 | // initialise accu |
---|
| 901 | accu_r = 0; |
---|
| 902 | accu_c = 0; |
---|
| 903 | |
---|
| 904 | for ( n = 0 ; n < size ; n++ ) // loop on the input data points |
---|
| 905 | { |
---|
| 906 | // compute coef |
---|
| 907 | phi = (double)(2*PI*n*k) / size; |
---|
| 908 | u_r = cos( phi ); |
---|
| 909 | u_c = -sin( phi ) * direction; |
---|
| 910 | |
---|
| 911 | // get input data point |
---|
| 912 | c_id = (src_offset + n) / (points_per_cluster); |
---|
| 913 | c_offset = (src_offset + n) % (points_per_cluster); |
---|
[473] | 914 | d_r = src[c_id][2*c_offset]; |
---|
| 915 | d_c = src[c_id][2*c_offset+1]; |
---|
[469] | 916 | |
---|
| 917 | // increment accu |
---|
| 918 | accu_r += ((u_r*d_r) - (u_c*d_c)); |
---|
| 919 | accu_c += ((u_r*d_c) + (u_c*d_r)); |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | // scale for inverse DFT |
---|
| 923 | if ( direction == -1 ) |
---|
| 924 | { |
---|
| 925 | accu_r /= size; |
---|
| 926 | accu_c /= size; |
---|
| 927 | } |
---|
| 928 | |
---|
| 929 | // set output data point |
---|
| 930 | c_id = (dst_offset + k) / (points_per_cluster); |
---|
| 931 | c_offset = (dst_offset + k) % (points_per_cluster); |
---|
| 932 | dst[c_id][2*c_offset] = accu_r; |
---|
| 933 | dst[c_id][2*c_offset+1] = accu_c; |
---|
| 934 | } |
---|
| 935 | |
---|
| 936 | } // end SimpleDft() |
---|
| 937 | |
---|
[582] | 938 | /////////////////////// |
---|
[503] | 939 | double CheckSum( void ) |
---|
[469] | 940 | { |
---|
[473] | 941 | unsigned int i , j; |
---|
| 942 | unsigned int c_id; |
---|
| 943 | unsigned int c_offset; |
---|
[629] | 944 | double cks; |
---|
[469] | 945 | |
---|
| 946 | cks = 0.0; |
---|
| 947 | for (j = 0; j < rootN ; j++) |
---|
| 948 | { |
---|
| 949 | for (i = 0; i < rootN ; i++) |
---|
| 950 | { |
---|
| 951 | c_id = (rootN * j + i) / (points_per_cluster); |
---|
| 952 | c_offset = (rootN * j + i) % (points_per_cluster); |
---|
| 953 | |
---|
| 954 | cks += data[c_id][2*c_offset] + data[c_id][2*c_offset+1]; |
---|
| 955 | } |
---|
| 956 | } |
---|
| 957 | return(cks); |
---|
| 958 | } |
---|
| 959 | |
---|
[637] | 960 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 961 | // Each working thread <tid> contributes to initialize (rootN / nthreads) rows, |
---|
| 962 | // in the shared - and distributed - <data> array. |
---|
| 963 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 964 | void InitD(double ** data, |
---|
| 965 | unsigned int mode, |
---|
| 966 | unsigned int tid ) |
---|
[469] | 967 | { |
---|
[473] | 968 | unsigned int i , j; |
---|
| 969 | unsigned int c_id; |
---|
| 970 | unsigned int c_offset; |
---|
| 971 | unsigned int index; |
---|
[469] | 972 | |
---|
[637] | 973 | // compute row_min and row_max |
---|
| 974 | unsigned int row_min = tid * rows_per_thread; |
---|
| 975 | unsigned int row_max = row_min + rows_per_thread; |
---|
| 976 | |
---|
| 977 | for ( j = row_min ; j < row_max ; j++ ) // loop on rows |
---|
[469] | 978 | { |
---|
[637] | 979 | for ( i = 0 ; i < rootN ; i++ ) // loop on points in a row |
---|
[469] | 980 | { |
---|
| 981 | index = j * rootN + i; |
---|
| 982 | c_id = index / (points_per_cluster); |
---|
| 983 | c_offset = index % (points_per_cluster); |
---|
| 984 | |
---|
| 985 | // complex input signal is random |
---|
| 986 | if ( mode == RANDOM ) |
---|
| 987 | { |
---|
[637] | 988 | data[c_id][2*c_offset] = ( (double)rand() ) / 65536; |
---|
| 989 | data[c_id][2*c_offset+1] = ( (double)rand() ) / 65536; |
---|
[469] | 990 | } |
---|
| 991 | |
---|
| 992 | |
---|
| 993 | // complex input signal is cos(n/N) / sin(n/N) |
---|
| 994 | if ( mode == COSIN ) |
---|
| 995 | { |
---|
| 996 | double phi = (double)( 2 * PI * index) / N; |
---|
[637] | 997 | data[c_id][2*c_offset] = cos( phi ); |
---|
| 998 | data[c_id][2*c_offset+1] = sin( phi ); |
---|
[469] | 999 | } |
---|
| 1000 | |
---|
| 1001 | // complex input signal is constant |
---|
| 1002 | if ( mode == CONSTANT ) |
---|
| 1003 | { |
---|
[637] | 1004 | data[c_id][2*c_offset] = 1.0; |
---|
| 1005 | data[c_id][2*c_offset+1] = 0.0; |
---|
[469] | 1006 | } |
---|
| 1007 | } |
---|
| 1008 | } |
---|
| 1009 | } |
---|
| 1010 | |
---|
[637] | 1011 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1012 | // Each working thread <tid> contributes to initialize (rootN / nthreads) rows, |
---|
| 1013 | // in the shared - and distributed - <twiddle> array. |
---|
| 1014 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1015 | void InitT( double ** twid, |
---|
| 1016 | unsigned int tid ) |
---|
[469] | 1017 | { |
---|
[637] | 1018 | unsigned int i, j; |
---|
| 1019 | unsigned int index; |
---|
[473] | 1020 | unsigned int c_id; |
---|
| 1021 | unsigned int c_offset; |
---|
[469] | 1022 | double phi; |
---|
| 1023 | |
---|
[637] | 1024 | // compute row_min and row_max |
---|
| 1025 | unsigned int row_min = tid * rows_per_thread; |
---|
| 1026 | unsigned int row_max = row_min + rows_per_thread; |
---|
| 1027 | |
---|
| 1028 | for ( j = row_min ; j < row_max ; j++ ) // loop on rows |
---|
[469] | 1029 | { |
---|
[637] | 1030 | for ( i = 0 ; i < rootN ; i++ ) // loop on points in a row |
---|
| 1031 | { |
---|
| 1032 | index = j * rootN + i; |
---|
| 1033 | c_id = index / (points_per_cluster); |
---|
| 1034 | c_offset = index % (points_per_cluster); |
---|
[469] | 1035 | |
---|
[637] | 1036 | phi = (double)(2.0 * PI * i * j) / N; |
---|
| 1037 | twid[c_id][2*c_offset] = cos( phi ); |
---|
| 1038 | twid[c_id][2*c_offset+1] = -sin( phi ); |
---|
[469] | 1039 | } |
---|
| 1040 | } |
---|
| 1041 | } |
---|
| 1042 | |
---|
[637] | 1043 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1044 | // Each working thread initialize the private <upriv> array / (rootN - 1) entries. |
---|
| 1045 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1046 | void InitU( double * upriv ) |
---|
[469] | 1047 | { |
---|
[637] | 1048 | unsigned int q; |
---|
| 1049 | unsigned int j; |
---|
| 1050 | unsigned int base; |
---|
| 1051 | unsigned int n1; |
---|
[638] | 1052 | double phi; |
---|
[469] | 1053 | |
---|
[637] | 1054 | for (q = 0 ; ((unsigned int)(1 << q) < N) ; q++) |
---|
[469] | 1055 | { |
---|
[637] | 1056 | n1 = 1 << q; // n1 == 2**q |
---|
| 1057 | base = n1 - 1; |
---|
| 1058 | for (j = 0; (j < n1) ; j++) |
---|
| 1059 | { |
---|
| 1060 | if (base + j > rootN - 1) return; |
---|
[469] | 1061 | |
---|
[637] | 1062 | phi = (double)(2.0 * PI * j) / (2 * n1); |
---|
| 1063 | upriv[2*(base+j)] = cos( phi ); |
---|
| 1064 | upriv[2*(base+j)+1] = -sin( phi ); |
---|
[469] | 1065 | } |
---|
| 1066 | } |
---|
| 1067 | } |
---|
| 1068 | |
---|
| 1069 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1070 | // This function returns an index value that is the bit reverse of the input value. |
---|
| 1071 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 1072 | unsigned int BitReverse( unsigned int k ) |
---|
[469] | 1073 | { |
---|
[473] | 1074 | unsigned int i; |
---|
| 1075 | unsigned int j; |
---|
| 1076 | unsigned int tmp; |
---|
[469] | 1077 | |
---|
| 1078 | j = 0; |
---|
| 1079 | tmp = k; |
---|
| 1080 | for (i = 0; i < M/2 ; i++) |
---|
| 1081 | { |
---|
| 1082 | j = 2 * j + (tmp & 0x1); |
---|
| 1083 | tmp = tmp >> 1; |
---|
| 1084 | } |
---|
| 1085 | return j; |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1089 | // This function perform the in place (direct or inverse) FFT on the N data points |
---|
| 1090 | // contained in the distributed buffers x[nclusters][points_per_cluster]. |
---|
| 1091 | // It handles the (N) points 1D array as a (rootN*rootN) points 2D array. |
---|
[652] | 1092 | // 1) it fft (rootN/nthreads ) rows from x to tmp. |
---|
[469] | 1093 | // 2) it make (rootN/nthreads) FFT on the tmp rows and apply the twiddle factor. |
---|
[652] | 1094 | // 3) it fft (rootN/nthreads) columns from tmp to x. |
---|
[469] | 1095 | // 4) it make (rootN/nthreads) FFT on the x rows. |
---|
[574] | 1096 | // It calls the FFTRow() 2*(rootN/nthreads) times to perform the in place FFT |
---|
[469] | 1097 | // on the rootN points contained in a row. |
---|
| 1098 | //////////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 1099 | void FFT1D( int direction, // direct 1 / inverse -1 |
---|
| 1100 | double ** x, // input & output distributed data points array |
---|
| 1101 | double ** tmp, // auxiliary distributed data points array |
---|
| 1102 | double * upriv, // local array containing coefs for rootN FFT |
---|
| 1103 | double ** twid, // distributed arrays containing N twiddle factors |
---|
[637] | 1104 | unsigned int tid, // thread continuous index |
---|
[473] | 1105 | unsigned int MyFirst, |
---|
| 1106 | unsigned int MyLast ) |
---|
[469] | 1107 | { |
---|
[473] | 1108 | unsigned int j; |
---|
[469] | 1109 | unsigned long long barrier_start; |
---|
| 1110 | unsigned long long barrier_stop; |
---|
| 1111 | |
---|
[574] | 1112 | #if DEBUG_FFT1D |
---|
[588] | 1113 | unsigned long long cycle; |
---|
| 1114 | get_cycle( &cycle ); |
---|
[629] | 1115 | printf("\n[fft] %s : thread %d enter / first %d / last %d / cycle %d\n", |
---|
[637] | 1116 | __FUNCTION__, tid, MyFirst, MyLast, (unsigned int)cycle ); |
---|
[574] | 1117 | #endif |
---|
| 1118 | |
---|
[652] | 1119 | // fft (rootN/nthreads) rows from x to tmp |
---|
[469] | 1120 | Transpose( x , tmp , MyFirst , MyLast ); |
---|
| 1121 | |
---|
[574] | 1122 | #if( DEBUG_FFT1D & 1 ) |
---|
| 1123 | get_cycle( &cycle ); |
---|
[652] | 1124 | printf("\n[fft] %s : thread %d after first fft / cycle %d\n", |
---|
[637] | 1125 | __FUNCTION__, tid, (unsigned int)cycle ); |
---|
[574] | 1126 | if( PRINT_ARRAY ) PrintArray( tmp , N ); |
---|
[469] | 1127 | #endif |
---|
| 1128 | |
---|
| 1129 | // BARRIER |
---|
| 1130 | get_cycle( &barrier_start ); |
---|
| 1131 | pthread_barrier_wait( &barrier ); |
---|
| 1132 | get_cycle( &barrier_stop ); |
---|
[637] | 1133 | sync_time[tid] = (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1134 | |
---|
[574] | 1135 | #if( DEBUG_FFT1D & 1 ) |
---|
| 1136 | get_cycle( &cycle ); |
---|
[652] | 1137 | printf("\n[fft] %s : thread %d exit barrier after first fft / cycle %d\n", |
---|
[637] | 1138 | __FUNCTION__, tid, (unsigned int)cycle ); |
---|
[574] | 1139 | #endif |
---|
| 1140 | |
---|
[469] | 1141 | // do FFTs on rows of tmp (i.e. columns of x) and apply twiddle factor |
---|
| 1142 | for (j = MyFirst; j < MyLast; j++) |
---|
| 1143 | { |
---|
[574] | 1144 | FFTRow( direction , upriv , tmp , j * rootN ); |
---|
| 1145 | |
---|
[469] | 1146 | TwiddleOneCol( direction , j , twid , tmp , j * rootN ); |
---|
| 1147 | } |
---|
| 1148 | |
---|
[574] | 1149 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1150 | printf("\n[fft] %s : thread %d after first twiddle\n", __FUNCTION__, tid); |
---|
[574] | 1151 | if( PRINT_ARRAY ) PrintArray( tmp , N ); |
---|
[469] | 1152 | #endif |
---|
| 1153 | |
---|
| 1154 | // BARRIER |
---|
| 1155 | get_cycle( &barrier_start ); |
---|
| 1156 | pthread_barrier_wait( &barrier ); |
---|
| 1157 | get_cycle( &barrier_stop ); |
---|
| 1158 | |
---|
[574] | 1159 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1160 | printf("\n[fft] %s : thread %d exit barrier after first twiddle\n", __FUNCTION__, tid); |
---|
[574] | 1161 | #endif |
---|
| 1162 | |
---|
[637] | 1163 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1164 | |
---|
[652] | 1165 | // fft tmp to x |
---|
[469] | 1166 | Transpose( tmp , x , MyFirst , MyLast ); |
---|
| 1167 | |
---|
[574] | 1168 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1169 | printf("\n[fft] %s : thread %d after second fft\n", __FUNCTION__, tid); |
---|
[574] | 1170 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1171 | #endif |
---|
| 1172 | |
---|
| 1173 | // BARRIER |
---|
| 1174 | get_cycle( &barrier_start ); |
---|
| 1175 | pthread_barrier_wait( &barrier ); |
---|
| 1176 | get_cycle( &barrier_stop ); |
---|
| 1177 | |
---|
[574] | 1178 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1179 | printf("\n[fft] %s : thread %d exit barrier after second fft\n", __FUNCTION__, tid); |
---|
[574] | 1180 | #endif |
---|
| 1181 | |
---|
[637] | 1182 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1183 | |
---|
| 1184 | // do FFTs on rows of x and apply the scaling factor |
---|
| 1185 | for (j = MyFirst; j < MyLast; j++) |
---|
| 1186 | { |
---|
[574] | 1187 | FFTRow( direction , upriv , x , j * rootN ); |
---|
[469] | 1188 | if (direction == -1) Scale( x , j * rootN ); |
---|
| 1189 | } |
---|
| 1190 | |
---|
[574] | 1191 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1192 | printf("\n[fft] %s : thread %d after FFT on rows\n", __FUNCTION__, tid); |
---|
[574] | 1193 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1194 | #endif |
---|
| 1195 | |
---|
| 1196 | // BARRIER |
---|
| 1197 | get_cycle( &barrier_start ); |
---|
| 1198 | pthread_barrier_wait( &barrier ); |
---|
| 1199 | get_cycle( &barrier_stop ); |
---|
| 1200 | |
---|
[574] | 1201 | #if( DEBUG_FFT1D & 1 ) |
---|
[637] | 1202 | printf("\n[fft] %s : thread %d exit barrier after FFT on rows\n", __FUNCTION__, tid); |
---|
[574] | 1203 | #endif |
---|
[637] | 1204 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
[469] | 1205 | |
---|
[652] | 1206 | // fft x to tmp |
---|
[469] | 1207 | Transpose( x , tmp , MyFirst , MyLast ); |
---|
| 1208 | |
---|
[574] | 1209 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1210 | printf("\n[fft] %s : thread %x after third fft\n", __FUNCTION__, tid); |
---|
[574] | 1211 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1212 | #endif |
---|
| 1213 | |
---|
| 1214 | // BARRIER |
---|
| 1215 | get_cycle( &barrier_start ); |
---|
| 1216 | pthread_barrier_wait( &barrier ); |
---|
| 1217 | get_cycle( &barrier_stop ); |
---|
| 1218 | |
---|
[574] | 1219 | #if( DEBUG_FFT1D & 1 ) |
---|
[652] | 1220 | printf("\n[fft] %s : thread %d exit barrier after third fft\n", __FUNCTION__, tid); |
---|
[574] | 1221 | #endif |
---|
| 1222 | |
---|
[637] | 1223 | sync_time[tid] += (unsigned int)(barrier_stop - barrier_start); |
---|
| 1224 | sync_time[tid] += (long)(barrier_stop - barrier_start); |
---|
[469] | 1225 | |
---|
| 1226 | // copy tmp to x |
---|
| 1227 | Copy( tmp , x , MyFirst , MyLast ); |
---|
| 1228 | |
---|
| 1229 | #if DEBUG_FFT1D |
---|
[637] | 1230 | printf("\n[fft] %s : thread %d completed\n", __FUNCTION__, tid); |
---|
[574] | 1231 | if( PRINT_ARRAY ) PrintArray( x , N ); |
---|
[469] | 1232 | #endif |
---|
| 1233 | |
---|
| 1234 | } // end FFT1D() |
---|
| 1235 | |
---|
| 1236 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 1237 | // This function multiply all points contained in a row (rootN points) of the |
---|
| 1238 | // x[] array by the corresponding twiddle factor, contained in the u[] array. |
---|
| 1239 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
[473] | 1240 | void TwiddleOneCol( int direction, |
---|
| 1241 | unsigned int j, // y coordinate in 2D view of coef array |
---|
| 1242 | double ** u, // coef array base address |
---|
| 1243 | double ** x, // data array base address |
---|
| 1244 | unsigned int offset_x ) // first point in N points data array |
---|
[469] | 1245 | { |
---|
[473] | 1246 | unsigned int i; |
---|
[469] | 1247 | double omega_r; |
---|
| 1248 | double omega_c; |
---|
| 1249 | double x_r; |
---|
| 1250 | double x_c; |
---|
[473] | 1251 | unsigned int c_id; |
---|
| 1252 | unsigned int c_offset; |
---|
[469] | 1253 | |
---|
| 1254 | for (i = 0; i < rootN ; i++) // loop on the rootN points |
---|
| 1255 | { |
---|
| 1256 | // get coef |
---|
| 1257 | c_id = (j * rootN + i) / (points_per_cluster); |
---|
| 1258 | c_offset = (j * rootN + i) % (points_per_cluster); |
---|
| 1259 | omega_r = u[c_id][2*c_offset]; |
---|
| 1260 | omega_c = direction * u[c_id][2*c_offset+1]; |
---|
| 1261 | |
---|
| 1262 | // access data |
---|
| 1263 | c_id = (offset_x + i) / (points_per_cluster); |
---|
| 1264 | c_offset = (offset_x + i) % (points_per_cluster); |
---|
| 1265 | x_r = x[c_id][2*c_offset]; |
---|
| 1266 | x_c = x[c_id][2*c_offset+1]; |
---|
| 1267 | |
---|
| 1268 | x[c_id][2*c_offset] = omega_r*x_r - omega_c * x_c; |
---|
| 1269 | x[c_id][2*c_offset+1] = omega_r*x_c + omega_c * x_r; |
---|
| 1270 | } |
---|
| 1271 | } // end TwiddleOneCol() |
---|
| 1272 | |
---|
[473] | 1273 | //////////////////////////// |
---|
| 1274 | void Scale( double ** x, // data array base address |
---|
| 1275 | unsigned int offset_x ) // first point of the row to be scaled |
---|
[469] | 1276 | { |
---|
[473] | 1277 | unsigned int i; |
---|
| 1278 | unsigned int c_id; |
---|
| 1279 | unsigned int c_offset; |
---|
[469] | 1280 | |
---|
| 1281 | for (i = 0; i < rootN ; i++) |
---|
| 1282 | { |
---|
| 1283 | c_id = (offset_x + i) / (points_per_cluster); |
---|
| 1284 | c_offset = (offset_x + i) % (points_per_cluster); |
---|
[473] | 1285 | x[c_id][2*c_offset] /= N; |
---|
| 1286 | x[c_id][2*c_offset + 1] /= N; |
---|
[469] | 1287 | } |
---|
| 1288 | } |
---|
| 1289 | |
---|
[473] | 1290 | /////////////////////////////////// |
---|
| 1291 | void Transpose( double ** src, // source buffer (array of pointers) |
---|
| 1292 | double ** dest, // destination buffer (array of pointers) |
---|
| 1293 | unsigned int MyFirst, // first row allocated to the thread |
---|
| 1294 | unsigned int MyLast ) // last row allocated to the thread |
---|
[469] | 1295 | { |
---|
[473] | 1296 | unsigned int row; // row index |
---|
| 1297 | unsigned int point; // data point index in a row |
---|
[469] | 1298 | |
---|
[473] | 1299 | unsigned int index_src; // absolute index in the source N points array |
---|
| 1300 | unsigned int c_id_src; // cluster for the source buffer |
---|
| 1301 | unsigned int c_offset_src; // offset in the source buffer |
---|
[469] | 1302 | |
---|
[473] | 1303 | unsigned int index_dst; // absolute index in the dest N points array |
---|
| 1304 | unsigned int c_id_dst; // cluster for the dest buffer |
---|
| 1305 | unsigned int c_offset_dst; // offset in the dest buffer |
---|
[469] | 1306 | |
---|
| 1307 | |
---|
| 1308 | // scan all data points allocated to the thread |
---|
| 1309 | // (between MyFirst row and MyLast row) from the source buffer |
---|
| 1310 | // and write these points to the destination buffer |
---|
| 1311 | for ( row = MyFirst ; row < MyLast ; row++ ) // loop on the rows |
---|
| 1312 | { |
---|
| 1313 | for ( point = 0 ; point < rootN ; point++ ) // loop on points in row |
---|
| 1314 | { |
---|
| 1315 | index_src = row * rootN + point; |
---|
| 1316 | c_id_src = index_src / (points_per_cluster); |
---|
| 1317 | c_offset_src = index_src % (points_per_cluster); |
---|
| 1318 | |
---|
| 1319 | index_dst = point * rootN + row; |
---|
| 1320 | c_id_dst = index_dst / (points_per_cluster); |
---|
| 1321 | c_offset_dst = index_dst % (points_per_cluster); |
---|
| 1322 | |
---|
| 1323 | dest[c_id_dst][2*c_offset_dst] = src[c_id_src][2*c_offset_src]; |
---|
| 1324 | dest[c_id_dst][2*c_offset_dst+1] = src[c_id_src][2*c_offset_src+1]; |
---|
| 1325 | } |
---|
| 1326 | } |
---|
| 1327 | } // end Transpose() |
---|
| 1328 | |
---|
[473] | 1329 | ////////////////////////////// |
---|
| 1330 | void Copy( double ** src, // source buffer (array of pointers) |
---|
| 1331 | double ** dest, // destination buffer (array of pointers) |
---|
| 1332 | unsigned int MyFirst, // first row allocated to the thread |
---|
| 1333 | unsigned int MyLast ) // last row allocated to the thread |
---|
[469] | 1334 | { |
---|
[473] | 1335 | unsigned int row; // row index |
---|
| 1336 | unsigned int point; // data point index in a row |
---|
[469] | 1337 | |
---|
[473] | 1338 | unsigned int index; // absolute index in the N points array |
---|
| 1339 | unsigned int c_id; // cluster index |
---|
| 1340 | unsigned int c_offset; // offset in local buffer |
---|
[469] | 1341 | |
---|
| 1342 | // scan all data points allocated to the thread |
---|
| 1343 | for ( row = MyFirst ; row < MyLast ; row++ ) // loop on the rows |
---|
| 1344 | { |
---|
| 1345 | for ( point = 0 ; point < rootN ; point++ ) // loop on points in row |
---|
| 1346 | { |
---|
| 1347 | index = row * rootN + point; |
---|
| 1348 | c_id = index / (points_per_cluster); |
---|
| 1349 | c_offset = index % (points_per_cluster); |
---|
| 1350 | |
---|
| 1351 | dest[c_id][2*c_offset] = src[c_id][2*c_offset]; |
---|
| 1352 | dest[c_id][2*c_offset+1] = src[c_id][2*c_offset+1]; |
---|
| 1353 | } |
---|
| 1354 | } |
---|
| 1355 | } // end Copy() |
---|
| 1356 | |
---|
[473] | 1357 | /////////////////////////////// |
---|
| 1358 | void Reverse( double ** x, |
---|
| 1359 | unsigned int offset_x ) |
---|
[469] | 1360 | { |
---|
[473] | 1361 | unsigned int j, k; |
---|
| 1362 | unsigned int c_id_j; |
---|
| 1363 | unsigned int c_offset_j; |
---|
| 1364 | unsigned int c_id_k; |
---|
| 1365 | unsigned int c_offset_k; |
---|
[469] | 1366 | |
---|
| 1367 | for (k = 0 ; k < rootN ; k++) |
---|
| 1368 | { |
---|
| 1369 | j = BitReverse( k ); |
---|
| 1370 | if (j > k) |
---|
| 1371 | { |
---|
| 1372 | c_id_j = (offset_x + j) / (points_per_cluster); |
---|
| 1373 | c_offset_j = (offset_x + j) % (points_per_cluster); |
---|
| 1374 | c_id_k = (offset_x + k) / (points_per_cluster); |
---|
| 1375 | c_offset_k = (offset_x + k) % (points_per_cluster); |
---|
| 1376 | |
---|
| 1377 | SWAP(x[c_id_j][2*c_offset_j] , x[c_id_k][2*c_offset_k]); |
---|
| 1378 | SWAP(x[c_id_j][2*c_offset_j+1], x[c_id_k][2*c_offset_k+1]); |
---|
| 1379 | } |
---|
| 1380 | } |
---|
| 1381 | } |
---|
| 1382 | |
---|
| 1383 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 1384 | // This function makes the in-place FFT on all points contained in a row |
---|
| 1385 | // (i.e. rootN points) of the x[nclusters][points_per_cluster] array. |
---|
| 1386 | ///////////////////////////////////////////////////////////////////////////// |
---|
[574] | 1387 | void FFTRow( int direction, // 1 direct / -1 inverse |
---|
[473] | 1388 | double * u, // private coefs array |
---|
| 1389 | double ** x, // array of pointers on distributed buffers |
---|
| 1390 | unsigned int offset_x ) // absolute offset in the x array |
---|
[469] | 1391 | { |
---|
[473] | 1392 | unsigned int j; |
---|
| 1393 | unsigned int k; |
---|
| 1394 | unsigned int q; |
---|
| 1395 | unsigned int L; |
---|
| 1396 | unsigned int r; |
---|
| 1397 | unsigned int Lstar; |
---|
[469] | 1398 | double * u1; |
---|
| 1399 | |
---|
[473] | 1400 | unsigned int offset_x1; // index first butterfly input |
---|
| 1401 | unsigned int offset_x2; // index second butterfly output |
---|
[469] | 1402 | |
---|
[473] | 1403 | double omega_r; // real part butterfy coef |
---|
| 1404 | double omega_c; // complex part butterfly coef |
---|
[469] | 1405 | |
---|
[473] | 1406 | double tau_r; |
---|
| 1407 | double tau_c; |
---|
[469] | 1408 | |
---|
[473] | 1409 | double d1_r; // real part first butterfly input |
---|
| 1410 | double d1_c; // imag part first butterfly input |
---|
| 1411 | double d2_r; // real part second butterfly input |
---|
| 1412 | double d2_c; // imag part second butterfly input |
---|
[469] | 1413 | |
---|
[473] | 1414 | unsigned int c_id_1; // cluster index for first butterfly input |
---|
| 1415 | unsigned int c_offset_1; // offset for first butterfly input |
---|
| 1416 | unsigned int c_id_2; // cluster index for second butterfly input |
---|
| 1417 | unsigned int c_offset_2; // offset for second butterfly input |
---|
[469] | 1418 | |
---|
[574] | 1419 | #if DEBUG_ROW |
---|
[469] | 1420 | unsigned int p; |
---|
[628] | 1421 | printf("\n[fft] ROW data in / %d points / offset = %d\n", rootN , offset_x ); |
---|
[574] | 1422 | |
---|
[469] | 1423 | for ( p = 0 ; p < rootN ; p++ ) |
---|
| 1424 | { |
---|
[473] | 1425 | unsigned int index = offset_x + p; |
---|
| 1426 | unsigned int c_id = index / (points_per_cluster); |
---|
| 1427 | unsigned int c_offset = index % (points_per_cluster); |
---|
[469] | 1428 | printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] ); |
---|
| 1429 | } |
---|
| 1430 | printf("\n"); |
---|
| 1431 | #endif |
---|
| 1432 | |
---|
| 1433 | // This makes the rootN input points reordering |
---|
| 1434 | Reverse( x , offset_x ); |
---|
| 1435 | |
---|
[574] | 1436 | #if DEBUG_ROW |
---|
[628] | 1437 | printf("\n[fft] ROW data after reverse / %d points / offset = %d\n", rootN , offset_x ); |
---|
[574] | 1438 | |
---|
[469] | 1439 | for ( p = 0 ; p < rootN ; p++ ) |
---|
| 1440 | { |
---|
[473] | 1441 | unsigned int index = offset_x + p; |
---|
| 1442 | unsigned int c_id = index / (points_per_cluster); |
---|
| 1443 | unsigned int c_offset = index % (points_per_cluster); |
---|
[469] | 1444 | printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] ); |
---|
| 1445 | } |
---|
| 1446 | printf("\n"); |
---|
| 1447 | #endif |
---|
| 1448 | |
---|
| 1449 | // This implements the multi-stages, in place Butterfly network |
---|
| 1450 | for (q = 1; q <= M/2 ; q++) // loop on stages |
---|
| 1451 | { |
---|
| 1452 | L = 1 << q; // number of points per subset for current stage |
---|
| 1453 | r = rootN / L; // number of subsets |
---|
| 1454 | Lstar = L / 2; |
---|
| 1455 | u1 = &u[2 * (Lstar - 1)]; |
---|
| 1456 | for (k = 0; k < r; k++) // loop on the subsets |
---|
| 1457 | { |
---|
| 1458 | offset_x1 = offset_x + (k * L); // index first point |
---|
| 1459 | offset_x2 = offset_x + (k * L + Lstar); // index second point |
---|
| 1460 | |
---|
[574] | 1461 | #if (DEBUG_ROW & 1) |
---|
| 1462 | printf("\n ### q = %d / k = %d / x1 = %d / x2 = %d\n", q , k , offset_x1 , offset_x2 ); |
---|
[469] | 1463 | #endif |
---|
| 1464 | // makes all in-place butterfly(s) for subset |
---|
| 1465 | for (j = 0; j < Lstar; j++) |
---|
| 1466 | { |
---|
| 1467 | // get coef |
---|
| 1468 | omega_r = u1[2*j]; |
---|
| 1469 | omega_c = direction * u1[2*j+1]; |
---|
| 1470 | |
---|
| 1471 | // get d[x1] address and value |
---|
| 1472 | c_id_1 = (offset_x1 + j) / (points_per_cluster); |
---|
| 1473 | c_offset_1 = (offset_x1 + j) % (points_per_cluster); |
---|
| 1474 | d1_r = x[c_id_1][2*c_offset_1]; |
---|
| 1475 | d1_c = x[c_id_1][2*c_offset_1+1]; |
---|
| 1476 | |
---|
| 1477 | // get d[x2] address and value |
---|
| 1478 | c_id_2 = (offset_x2 + j) / (points_per_cluster); |
---|
| 1479 | c_offset_2 = (offset_x2 + j) % (points_per_cluster); |
---|
| 1480 | d2_r = x[c_id_2][2*c_offset_2]; |
---|
| 1481 | d2_c = x[c_id_2][2*c_offset_2+1]; |
---|
| 1482 | |
---|
[574] | 1483 | #if (DEBUG_ROW & 1) |
---|
[469] | 1484 | printf("\n ### d1_in = (%f , %f) / d2_in = (%f , %f) / coef = (%f , %f)\n", |
---|
| 1485 | d1_r , d1_c , d2_r , d2_c , omega_r , omega_c); |
---|
| 1486 | #endif |
---|
| 1487 | // tau = omega * d[x2] |
---|
| 1488 | tau_r = omega_r * d2_r - omega_c * d2_c; |
---|
| 1489 | tau_c = omega_r * d2_c + omega_c * d2_r; |
---|
| 1490 | |
---|
| 1491 | // set new value for d[x1] = d[x1] + omega * d[x2] |
---|
| 1492 | x[c_id_1][2*c_offset_1] = d1_r + tau_r; |
---|
| 1493 | x[c_id_1][2*c_offset_1+1] = d1_c + tau_c; |
---|
| 1494 | |
---|
| 1495 | // set new value for d[x2] = d[x1] - omega * d[x2] |
---|
| 1496 | x[c_id_2][2*c_offset_2] = d1_r - tau_r; |
---|
| 1497 | x[c_id_2][2*c_offset_2+1] = d1_c - tau_c; |
---|
| 1498 | |
---|
[574] | 1499 | #if (DEBUG_ROW & 1) |
---|
[469] | 1500 | printf("\n ### d1_out = (%f , %f) / d2_out = (%f , %f)\n", |
---|
| 1501 | d1_r + tau_r , d1_c + tau_c , d2_r - tau_r , d2_c - tau_c ); |
---|
| 1502 | #endif |
---|
| 1503 | } |
---|
| 1504 | } |
---|
| 1505 | } |
---|
| 1506 | |
---|
[574] | 1507 | #if DEBUG_ROW |
---|
[628] | 1508 | printf("\n[fft] ROW data out / %d points / offset = %d\n", rootN , offset_x ); |
---|
[469] | 1509 | for ( p = 0 ; p < rootN ; p++ ) |
---|
| 1510 | { |
---|
[473] | 1511 | unsigned int index = offset_x + p; |
---|
| 1512 | unsigned int c_id = index / (points_per_cluster); |
---|
| 1513 | unsigned int c_offset = index % (points_per_cluster); |
---|
[469] | 1514 | printf("%f , %f | ", x[c_id][2*c_offset] , x[c_id][2*c_offset+1] ); |
---|
| 1515 | } |
---|
| 1516 | printf("\n"); |
---|
| 1517 | #endif |
---|
| 1518 | |
---|
[574] | 1519 | } // end FFTRow() |
---|
[469] | 1520 | |
---|
[473] | 1521 | /////////////////////////////////////// |
---|
| 1522 | void PrintArray( double ** array, |
---|
| 1523 | unsigned int size ) |
---|
[469] | 1524 | { |
---|
[473] | 1525 | unsigned int i; |
---|
| 1526 | unsigned int c_id; |
---|
| 1527 | unsigned int c_offset; |
---|
[469] | 1528 | |
---|
| 1529 | // float display |
---|
| 1530 | for (i = 0; i < size ; i++) |
---|
| 1531 | { |
---|
| 1532 | c_id = i / (points_per_cluster); |
---|
| 1533 | c_offset = i % (points_per_cluster); |
---|
| 1534 | |
---|
| 1535 | printf(" %f %f |", array[c_id][2*c_offset], array[c_id][2*c_offset+1]); |
---|
| 1536 | |
---|
| 1537 | if ( (i+1) % 4 == 0) printf("\n"); |
---|
| 1538 | } |
---|
| 1539 | printf("\n"); |
---|
| 1540 | } |
---|
| 1541 | |
---|
| 1542 | |
---|
| 1543 | // Local Variables: |
---|
| 1544 | // tab-width: 4 |
---|
| 1545 | // c-basic-offset: 4 |
---|
| 1546 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 1547 | // indent-tabs-mode: nil |
---|
| 1548 | // End: |
---|
| 1549 | |
---|
| 1550 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 1551 | |
---|