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