This repository has been archived by the owner on Oct 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cooleyTukey.cpp
404 lines (342 loc) · 16 KB
/
cooleyTukey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <iostream>
#include <cmath>
#include "cooleyTukey.h"
#include "fftutils.h"
using namespace std;
/* Functions Implementation*/
void transpose2(double *data3DRr,double *data3DRi, int xR, int yR, int off)
{
int xxx, var1 = 0, var2 = 0;
double temp1,temp2;
for (xxx = 0; xxx <= xR * yR - 1; xxx++) { // for each position
var1 = xxx;
var2 = 0;
do {
var2++;
var1 = (var1 % yR) * xR + var1 / yR;
} while (var1 > xxx);
if (var1 < xxx || var2 == 1) continue;
var1 = xxx;
temp1 = data3DRr[(off+var1)];
temp2 = data3DRi[(off+var1)];
do {
var2 = (var1 % yR) * xR + var1 / yR;
data3DRr[(off+var1)] = (var2 == xxx) ? temp1 : data3DRr[(off+var2)];
data3DRi[(off+var1)] = (var2 == xxx) ? temp2 : data3DRi[(off+var2)];
var1 = var2;
} while (var1 > xxx);
}
}
void transpose3(const unsigned offset, double *data3DRr, double *data3DRi, int zR, int yR, int xR, int off)
{
double *slice = (double *) malloc(sizeof(double)*yR*zR);
double *slice1 = (double *) malloc(sizeof(double)*yR*zR);
int counter;
int newX, newY, newZ;
int nX = xR, nY = zR;//, nZ = yR;
for (newZ = 0; newZ < xR; newZ++) { // yR
counter = 0;
for (newY = 0; newY < yR; newY++) {
for (newX = zR - 1; newX >= 0; newX--) {
slice[counter] = data3DRr[(offset + off + newZ + newY * xR + newX * xR * yR)];
slice1[counter] = data3DRi[(offset + off + newZ + newY * xR + newX * xR * yR)];
counter++;
}
}
transpose2(slice,slice1, zR, yR, 0);
counter = 0;
for (newY = zR - 1; newY >= 0; newY--) {
for (newX = 0; newX < yR; newX++) {
//data3DRr[(offset + off + newZ + newY * xR + newX * xR * yR)] = slice[counter];
//data3DRi[(offset + off + newZ + newY * xR + newX * xR * yR)] = slice1[counter];
data3DRr[(offset + off + newZ + newY * nX + newX * nX * nY)] = slice[counter];
data3DRi[(offset + off + newZ + newY * nX + newX * nX * nY)] = slice1[counter];
counter++;
}
}
}
free(slice);
free(slice1);
}
void convolveCPU(const unsigned offset1,int ASPAN){
//if (useCpu == 0) return;
for (int iz = 0; iz < xRange * yRange * zRange; iz++) {
hrhVecI[(iz+offset1)] = hrRaVec[(0*ASPAN+iz+offset1)] * hrRmVecI[iz+offset1] - hiRaVec[(0*ASPAN+iz+offset1)] * hiRmVecI[iz+offset1] + // XX
hrRaVec[(1*ASPAN+iz+offset1)] * hrRmVecJ[iz+offset1] - hiRaVec[(1*ASPAN+iz+offset1)] * hiRmVecJ[iz+offset1] + // XY
hrRaVec[(2*ASPAN+iz+offset1)] * hrRmVecK[iz+offset1] - hiRaVec[(2*ASPAN+iz+offset1)] * hiRmVecK[iz+offset1] ; // XZ
hihVecI[(iz+offset1)] = hiRaVec[(0*ASPAN+iz+offset1)] * hrRmVecI[iz+offset1] + hrRaVec[(0*ASPAN+iz+offset1)] * hiRmVecI[iz+offset1] + // XX
hiRaVec[(1*ASPAN+iz+offset1)] * hrRmVecJ[iz+offset1] + hrRaVec[(1*ASPAN+iz+offset1)] * hiRmVecJ[iz+offset1] + // XY
hiRaVec[(2*ASPAN+iz+offset1)] * hrRmVecK[iz+offset1] + hrRaVec[(2*ASPAN+iz+offset1)] * hiRmVecK[iz+offset1]; // XZ
hrhVecJ[(iz+offset1)] = hrRaVec[(1*ASPAN+iz+offset1)] * hrRmVecI[iz+offset1] - hiRaVec[(1*ASPAN+iz+offset1)] * hiRmVecI[iz+offset1] + // YX
hrRaVec[(3*ASPAN+iz+offset1)] * hrRmVecJ[iz+offset1] - hiRaVec[(3*ASPAN+iz+offset1)] * hiRmVecJ[iz+offset1] + // YY
hrRaVec[(4*ASPAN+iz+offset1)] * hrRmVecK[iz+offset1] - hiRaVec[(4*ASPAN+iz+offset1)] * hiRmVecK[iz+offset1] ; // YZ
hihVecJ[(iz+offset1)] = hiRaVec[(1*ASPAN+iz+offset1)] * hrRmVecI[iz+offset1] + hrRaVec[(1*ASPAN+iz+offset1)] * hiRmVecI[iz+offset1] + // YX
hiRaVec[(3*ASPAN+iz+offset1)] * hrRmVecJ[iz+offset1] + hrRaVec[(3*ASPAN+iz+offset1)] * hiRmVecJ[iz+offset1] + // YY
hiRaVec[(4*ASPAN+iz+offset1)] * hrRmVecK[iz+offset1] + hrRaVec[(4*ASPAN+iz+offset1)] * hiRmVecK[iz+offset1]; // YZ
hrhVecK[(iz+offset1)] = hrRaVec[(2*ASPAN+iz+offset1)] * hrRmVecI[iz] - hiRaVec[(2*ASPAN+iz+offset1)] * hiRmVecI[iz+offset1] + // ZX
hrRaVec[(4*ASPAN+iz+offset1)] * hrRmVecJ[iz+offset1] - hiRaVec[(4*ASPAN+iz+offset1)] * hiRmVecJ[iz+offset1] + // ZY
hrRaVec[(5*ASPAN+iz+offset1)] * hrRmVecK[iz+offset1] - hiRaVec[(5*ASPAN+iz+offset1)] * hiRmVecK[iz+offset1] ; // ZZ
hihVecK[(iz+offset1)] = hiRaVec[(2*ASPAN+iz+offset1)] * hrRmVecI[iz+offset1] + hrRaVec[(2*ASPAN+iz+offset1)] * hiRmVecI[iz+offset1] + // ZX
hiRaVec[(4*ASPAN+iz+offset1)] * hrRmVecJ[iz+offset1] + hrRaVec[(4*ASPAN+iz+offset1)] * hiRmVecJ[iz+offset1] + // ZY
hiRaVec[(5*ASPAN+iz+offset1)] * hrRmVecK[iz+offset1] + hrRaVec[(5*ASPAN+iz+offset1)] * hiRmVecK[iz+offset1]; // ZZ
}
if (_show_result){
printf("CONVOLUTION hVecI\n");
printf("XRange=:%d\t YRange=:%d\t ZRange=:%d\t \n", xRange,yRange,zRange);
printMe(0,hrhVecI, hihVecI, zRange, yRange, xRange, 0 );
/*printf("CONVOLUTION hVecJ\n");
printMe(0,hrhVecJ, hihVecJ, zRange, yRange, xRange, 0 );
printf("CONVOLUTION hVecK\n");
printMe(0,hrhVecK, hihVecK, zRange, yRange, xRange, 0 );*/
//show_results=0;
}
}
void cooleyTukeyCpu3DFFT(const unsigned offset, const unsigned N, const unsigned size,double *data3DFr,double *data3DFi,double *data3DRr,double *data3DRi,int ASPAN_offset, int show_results, int fft_type,int xRange,int yRange,int zRange){
if (size == 0) return;
//if (useCpu == 0) return;
int planeStart=0;
int tHolder = 0;
//reinitializing them when the ranges are different
/* TO CHECK
if(fft_type !=1){
xRange = initialxRange;
yRange = initialyRange;
zRange = initialzRange;
}
*/
if (_show_result) {
printf("XRange=:%d\t YRange=:%d\t ZRange=:%d\t \n", xRange,yRange,zRange);
printf("Start\n");
printMe(offset,data3DFr, data3DFi, zRange, yRange, xRange, ASPAN_offset );
}
//************************** X TRANSFORM STARTS ********************************************//
cooleyTukeyCpu(offset, xRange, size,data3DFr,data3DFi,data3DRr,data3DRi,ASPAN_offset,show_results,fft_type,0,0,0);
if (_show_result){
printf("X-TRANSFORM \n");
printMe(offset,data3DRr, data3DRi, zRange, yRange, xRange, ASPAN_offset );
//show_results=0;
}
//************************** X TRANSFORM ENDS ********************************************//
//************************** XY PLANE TRANSPOSE STARTS *******************************************//
if (yRange > 1)
{
for (int z = 0; z < zRange; z++){
planeStart = offset + ASPAN_offset + z * xRange * yRange;
transpose2(data3DRr,data3DRi, xRange, yRange, planeStart);
}
if (_show_result){
printf("Transpose XY \n");
printMe(offset,data3DRr, data3DRi, zRange, yRange, xRange, ASPAN_offset );
//show_results=0;
}
}
//************************** XY PLANE TRANSPOSE ENDS *******************************************//
//************************** Y TRANSFORM STARTS ********************************************//
if (yRange > 1){
// N=yRange; //xRange=yRange;
// and yRange=xRange;
tHolder = xRange; xRange = yRange; yRange = tHolder;
if (_show_result){
printf("Y-TRANSFORM \n");
printf("XRange=:%d\t YRange=:%d\t ZRange=:%d\t \n", xRange,yRange,zRange);
}
cooleyTukeyCpu(offset, xRange, size,data3DRr,data3DRi,data3DFr,data3DFi,ASPAN_offset,show_results,fft_type,0,0,0);
if (_show_result){
printf("Y-TRANSFORM \n");
printMe(offset,data3DFr, data3DFi, zRange, yRange, xRange, ASPAN_offset );
}
}
//************************** Y TRANSFORM ENDS ********************************************//
//************************** YZ PLANE TRANSPOSE STARTS *******************************************//
if (zRange > 1){
transpose3(offset,data3DFr, data3DFi, zRange, yRange, xRange, ASPAN_offset);
tHolder = zRange; zRange = yRange; yRange = tHolder;
if (_show_result){
printf("Transpose YZ \n");
printf("XRange=:%d\t YRange=:%d\t ZRange=:%d\t \n", xRange,yRange,zRange);
printMe(offset,data3DFr, data3DFi, zRange, yRange, xRange, ASPAN_offset );
//show_results=0;
}
}
//************************** YZ PLANE TRANSPOSE ENDS *******************************************//
//************************** ZX PLANE TRANSPOSE STARTS *******************************************//
if (zRange > 1)
{
for (int z = 0; z < zRange; z++){
planeStart = offset + ASPAN_offset + z * xRange * yRange;
transpose2(data3DFr, data3DFi, xRange, yRange, planeStart);
}
tHolder = xRange; xRange = yRange; yRange = tHolder;
if (_show_result){
printf("Transpose ZX \n");
printf("XRange=:%d\t YRange=:%d\t ZRange=:%d\t \n", xRange,yRange,zRange);
printMe(offset,data3DFr, data3DFi, zRange, yRange, xRange, ASPAN_offset );
}
}
//************************** ZX PLANE TRANSPOSE ENDS *******************************************//
//************************** Z TRANSFORM STARTS ********************************************//
if (zRange > 1)
{
cooleyTukeyCpu(offset, xRange, size,data3DFr,data3DFi,data3DRr,data3DRi,ASPAN_offset,show_results,fft_type,0,0,0);
// normalization in inverse fft starts
if (fft_type == 1) {
for (int z = 0; z < zRange; z++) {
int planeStart = z * xRange * yRange;
for (int y = yRange-1; y >= 0; y--) {
int yStart = y * xRange;
for (int x = 0; x < xRange; x++) {
data3DRr[(offset+ASPAN_offset+planeStart+yStart+x)] = data3DRr[(offset+ASPAN_offset+planeStart+yStart+x)]/(xRange*yRange*zRange);
data3DRi[(offset+ASPAN_offset+planeStart+yStart+x)] = data3DRi[(offset+ASPAN_offset+planeStart+yStart+x)]/(xRange*yRange*zRange);
}
}
}
}
// normalization in inverse fft ends
if (_show_result){
printf("Z-TRANSFORM \n");
printMe(offset,data3DRr, data3DRi, zRange, yRange, xRange, ASPAN_offset );
//system("rm plotMe && touch plotMe");
//show_results=0;
}
}
//************************** Z TRANSFORM ENDS ********************************************//
// printField(hVecI, hVecJ, hVecK, zRange, yRange, xRange);
}
// Inplace version of rearrange function
void cooleyTukeyCpu(const unsigned offset, const unsigned N, const unsigned size,double *data3DFr,double *data3DFi,double *data3DRr,double *data3DRi,int ASPAN_offset, int show_results, int fft_type, int full, int red, int reduction)
{
//printf("red = %d \n", red);
//printf("xRange=%d \n",N);
if (size == 0) return;
//if (useCpu == 0) return;
const unsigned powN = (unsigned)log2(N);
//const double start = omp_get_wtime();
int red_off=0;
//TODO:: set the number of threads
// #pragma omp parallel for
//*********************************3dFFT bit revrsal for N or aVec Starts*******************************
// start of bit reversal see chapter printed page No. 465
int red2=red*N;
for (int i = 0; i < (int)size; ++i) {
// for reduction
if(i !=0 && reduction==1 && i % (red*N) ==0 )
{
if(red % 2==0 )
i =i + ((full-red)*N);
else if(i % red2 ==0 && red % 2!=0)
{
i =i + ((full-red)*N);
red2+=(red*N)+((full-red)*N);
}
}
unsigned int lIndex = i % N;
unsigned int lPosition = 0;
unsigned int lReverse= 0;
while(lIndex) {
lReverse = lReverse << 1;
lReverse += lIndex %2;
lIndex = lIndex>>1;
lPosition++;
}
if (lPosition < powN) {
lReverse = lReverse << (powN- lPosition); //<< is a shift operator
}
uint lTempReverse = lReverse + (i / N) * N + offset;
data3DRr[lTempReverse + ASPAN_offset] = data3DFr[i + offset + ASPAN_offset];
data3DRi[lTempReverse + ASPAN_offset] = data3DFi[i + offset + ASPAN_offset];
}
//*********************************3dFFT bit revrsal for N or aVec Ends*******************************
/* Some Complex Arithmetic for reference
* (a+bi) + (c+di) = (a+c) + (b+d)i
* (a+bi) - (c+di) = (a-c) + (b-d)i
* (a+bi) * (c+di) = (ac - bd) + (bc + ad)i
* (a+bi) / (c+di) = [ (ac + bd) / (c^2 + d^2) ] + [ (bc - ad) / (c^2 + d^2) ]i
*/
//double pi = acos(-1); // value of pi .. never use 22/7 again
const double twopi = 2 * 3.14159265358979323846;
double cs;
double sn;
/* removed not used vars [ki]
double norm = 1.0/(xRange*yRange*zRange); //N; // 'normalisation' factor (xRange*yRange*zRange);
*/
int red1=red;
red_off=0;
for (unsigned i = 0; i < size / N; ++i) { //Number of FFTS // In kernel it is = unsigned int lIndex = lThread %n;
// printf("red = %d \n", red);
// for reduction
if(i !=0 && reduction==1 && i % red==0 )
{
if(red % 2==0){
i = i + (full-red);
}
else if(red % 2!=0 && i==red){
i = i + (full-red);
red1+=red+(full-red);
}
// if(i>size / N)
// return;
}
if(i !=0 && reduction==1 && i % red1==0 && red % 2!=0 && (i-1) > red)
{
i = i + (full-red);//(full)-(red);
red1+=red+(full-red);
// if(i>size / N)
// return;
}
for (unsigned p = 0; p < powN ; ++p ) { // for number of stages // In kernel it is = for(Iter =0 ,nIter =1;Iter<(powN);Iter ++,nIter*=2)
// if(p==0){
const unsigned powP = (unsigned)pow(2.0, (double)p);
//#pragma omp parallel for
for (int k = 0; k < (int)N / 2; ++k) { // for butter fly calculation // In kernel it is = thread id i.e addr i.e not N/2 rather size/2 and lThread % n makes it N/2.
const unsigned indexAdd = i * N + (k / powP) * 2 * powP + k % powP + offset; // getting index // offset is the location of data in real memory starting point
const unsigned indexMult = indexAdd + powP;
const unsigned kk = k % powP;
if (fft_type == 0) {
cs = cos(twopi * kk / ( 2 * powP)); // coefficent calcultaions Wn // at stage 0, 2*powP = 2 and kk= 0 (means W at index 0 and index 1 are same with opposite sign) and at stage =1, 2*powP = 4 and kk = 0 and 2
sn = sin(twopi * kk / ( 2 * powP)); // coefficent calcultaions Wn
}else if (fft_type == 1) {
cs = cos(twopi * kk / ( 2 * powP)); // coefficent calcultaions Wn // at stage 0, 2*powP = 2 and kk= 0 (means W at index 0 and index 1 are same with opposite sign) and at stage =1, 2*powP = 4 and kk = 0 and 2
sn = -1*sin(twopi * kk / ( 2 * powP)); // coefficent calcultaions Wn
}
const double addReal = data3DRr[indexAdd + ASPAN_offset];// +i%yRange+yFRange
const double addImag = data3DRi[indexAdd + ASPAN_offset];
// complex numbers multiplication start// W * x(1) // tempReal = W * x(1) and addReal = x(0), and indexAdd = 0 and indexMult = 1
const double tempReal = cs * data3DRr[indexMult + ASPAN_offset] +
sn * data3DRi[indexMult + ASPAN_offset];
const double tempImag = cs * data3DRi[indexMult + ASPAN_offset] -
sn * data3DRr[indexMult + ASPAN_offset];
// complex numbers multiplication end
// Complex numbers addition start
data3DRr[indexAdd + ASPAN_offset] = addReal + tempReal; // X(0) = x(0) + W * x(1)
data3DRi[indexAdd + ASPAN_offset] = addImag + tempImag;
// Complex numbers addition end
// Complex numbers subtraction start
data3DRr[indexMult + ASPAN_offset] = addReal - tempReal; // X(1) = x(0) - W * x(1)
data3DRi[indexMult + ASPAN_offset] = addImag - tempImag;
// Complex numbers subtraction end
/* if(fft_type == 1 && p==powN-1) //if last stage
{
// Complex numbers addition start
data3DRr[indexAdd + ASPAN_offset] *= norm; // X(0) = x(0) + W * x(1)
data3DRi[indexAdd + ASPAN_offset] *= norm;
// Complex numbers addition end
// Complex numbers subtraction start
data3DRr[indexMult + ASPAN_offset] *= norm; // X(1) = x(0) - W * x(1)
data3DRi[indexMult + ASPAN_offset] *= norm;
// Complex numbers subtraction end
}*/
}
// }
}
// for reduction
}
//const double end = omp_get_wtime();
//totalcputime+=end - start;
// cout << "CPU Time: " << end - start << endl;
/*
if (print) {
printResult(size,data3DRr,data3DRi);
}
*/
}
/* Functions Implementation end*/