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