-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifiedFFT1D.cpp
More file actions
284 lines (255 loc) · 7.52 KB
/
ModifiedFFT1D.cpp
File metadata and controls
284 lines (255 loc) · 7.52 KB
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
/* --------------------------------------------------------------- */
/* Licensed Materials - Property of IBM */
/* 5724-S84 */
/* (C) Copyright IBM Corp. 2007,2008, All Rights Reserved */
/* US Government Users Restricted Rights - Use, duplication or */
/* disclosure restricted by GSA ADP Schedule Contract with */
/* IBM Corp. */
/* --------------------------------------------------------------- */
/* PROLOG END TAG zYx */
/*
FFT1D_sample.c - a simple routine to drive the fft library
*/
/* NOTE:
* Computing a forward followed by a backward transform (or vice versa) will
* result in the original data multiplied by the size of the transform
* (the product of the dimensions).
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>/* --------------------------------------------------------------- */
/* Licensed Materials - Property of IBM */
/* 5724-S84 */
/* (C) Copyright IBM Corp. 2007,2008, All Rights Reserved */
/* US Government Users Restricted Rights - Use, duplication or */
/* disclosure restricted by GSA ADP Schedule Contract with */
/* IBM Corp. */
/* --------------------------------------------------------------- */
/* PROLOG END TAG zYx */
/*
FFT1D_sample.c - a simple routine to drive the fft library
*/
/* NOTE:
* Computing a forward followed by a backward transform (or vice versa) will
* result in the original data multiplied by the size of the transform
* (the product of the dimensions).
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <libfft.h>
#define HUGE_TLB
#define HUGE_PAGE_SIZE (16*1024*1024) /* 16MB */
// Forward declare.
int test_1D(int numberOfFfts, int sizeOfFfts, int spusToUse, int flavor, int hugepage_flag);
// Main.
int main(int argc, char *argv[])
{
if (argc < 6 || argc > 7)
{
fprintf(stdout, "Usage: %s <function type = c2c, r2c, c2r> <number of ffts> <spus to use> <size of each fft> <hugepage_flag>\n", argv[0]);
exit(1);
}
int numberOfFfts = atoi(argv[2]);
int spusToUse = atoi(argv[3]);
int sizeOfFfts = atoi(argv[4]);
int hugepage_flag = atoi(argv[5]);
int flavor;
if (strcasecmp(argv[1], "c2c") == 0)
{
flavor = FFT_TYPE_C2C;
}
else if (strcasecmp(argv[1], "r2c") == 0)
{
flavor = FFT_TYPE_R2C;
}
else if (strcasecmp(argv[1], "c2r") == 0)
{
flavor = FFT_TYPE_C2R;
}
else
{
fprintf(stdout, "Bad function type.\n");
exit(1);
}
while (true)
{
int res = test_1D(numberOfFfts, sizeOfFfts, spusToUse, flavor, hugepage_flag);
}
return res;
} // end main
// Generate complex numbers as input.
void generateC2Cdata(int numberOfFfts, int sizeOfFfts, float **problems)
{
int i;
for (i=0; i<numberOfFfts; ++i)
{
int j;
for (j=0; j<sizeOfFfts; ++j)
{
problems[i][j*2] = rand() % 1024; // Real
problems[i][j*2+1] = rand() % 1024; // Imag
}
}
}
// Generate packed reals as input.
void generateR2Cdata(int numberOfFfts, int sizeOfFfts, float **problems)
{
int i;
for (i=0; i<numberOfFfts; ++i)
{
int j;
for (j=0; j<sizeOfFfts; ++j)
{
problems[i][j] = rand() % 1024; // Real
}
// Don't care about the elements in the other half of the array,
// since they don't get used.
}
}
// Generate complex conjugates as input.
void generateC2Rdata(int numberOfFfts, int sizeOfFfts, float **problems)
{
int i;
for (i=0; i<numberOfFfts; ++i)
{
problems[i][0] = rand() % 1024; // Real
problems[i][1] = 0; // Imag
int j;
for (j=1; j<(sizeOfFfts+1)/2; ++j)
{
problems[i][j*2] = rand() % 1024; // Real
problems[i][j*2+1] = rand() % 1024; // Imag
// Complex conjugate.
problems[i][(sizeOfFfts-j)*2] = problems[i][j*2]; // Real
problems[i][(sizeOfFfts-j)*2+1] = -problems[i][j*2+1]; // Imag
}
if (!(sizeOfFfts % 2)) // Size is even.
{
problems[i][sizeOfFfts] = rand() % 1024; // Real
problems[i][sizeOfFfts+1] = 0; // Imag
}
}
}
// Allocate space for data and perform the FFT.
int test_1D(int numberOfFfts, int sizeOfFfts, int spusToUse, int flavor, int hugepage_flag)
{
// Allocate storage for input and output data.
void *ptr;
int i;
posix_memalign(&ptr, 128, sizeof(float *) * numberOfFfts);
float **input_data = (float **)ptr;
posix_memalign(&ptr, 128, sizeof(float *) * numberOfFfts);
float **output_data = (float **)ptr;
unsigned int mallocLen = sizeof(float) * 2 * sizeOfFfts; // Real + imaginary
mallocLen += mallocLen % 16;
unsigned int dataLen = 0;
if (hugepage_flag)
/* Using hugepage can significantly reduce the TLB miss thus improve the performance */
{
int fmem;
char *mem_file = "/huge/FFT1D_sample_mem.bin";
if ((fmem = open(mem_file, O_CREAT | O_RDWR, 0755)) == -1)
{
fprintf(stdout, "ERROR: unable to open file %s (errno=%d).\n", mem_file, errno);
return -1;
}
else
{
remove(mem_file);
dataLen = numberOfFfts * mallocLen * 2;
dataLen = ( dataLen + HUGE_PAGE_SIZE-1 ) & ~ (HUGE_PAGE_SIZE-1);
ptr = mmap(0, dataLen, PROT_READ | PROT_WRITE, MAP_PRIVATE, fmem, 0);
if (ptr == MAP_FAILED) {
printf("ERROR: unable to mmap file %s (errno=%d).\n", mem_file, errno);
close (fmem);
return -1;
}
for (i=0; i<numberOfFfts; i++)
{
input_data[i] = (float *)ptr;
/* If the input data are no longer used after computation,
* the input and output data can share the same buffer.
* In this in-place case, TLB miss can be further reduced.
*/
ptr += mallocLen;
output_data[i] = (float *)ptr;
ptr += mallocLen;
}
}
}
else
{
for (i=0; i<numberOfFfts; ++i)
{
posix_memalign(&ptr, 128, mallocLen);
input_data[i] = (float *)ptr;
posix_memalign(&ptr, 128, mallocLen);
output_data[i] = (float *)ptr;
}
}
// Populate input data.
srand(time(NULL));
if (flavor == FFT_TYPE_C2C)
{
generateC2Cdata(numberOfFfts, sizeOfFfts, input_data);
}
else if (flavor == FFT_TYPE_R2C)
{
generateR2Cdata(numberOfFfts, sizeOfFfts, input_data);
}
else if (flavor == FFT_TYPE_C2R)
{
generateC2Rdata(numberOfFfts, sizeOfFfts, input_data);
}
// Start timer.
struct timeval start, end;
gettimeofday(&start, NULL);
// Call library to process.
fft_handle_t handle;
int res = fft_1d_sp_initialize(&handle, spusToUse);
if (res == FFT_RC_SUCCESS)
{
// Perform the transform.
res = fft_1d_sp_perform(handle, numberOfFfts, sizeOfFfts, (void **)input_data, (void **)output_data, 0, flavor);
if (res != FFT_RC_SUCCESS)
{
fprintf(stdout, "FFT failure: %d\n", res);
}
// Cleanup.
fft_1d_sp_terminate(handle);
}
else
{
fprintf(stdout, "FFT failed to initialize: %d\n", res);
}
// Stop timer.
gettimeofday(&end, NULL);
unsigned int elapsed = ((end.tv_sec * 1000000) + end.tv_usec) - ((start.tv_sec * 1000000) + start.tv_usec);
fprintf(stdout, "Calculation time took %u usec.\n", elapsed);
// Cleanup.
if (hugepage_flag)
{
munmap(input_data[0], dataLen);
}
else
{
for (i=0; i<numberOfFfts; ++i)
{
free(input_data[i]);
free(output_data[i]);
}
}
free(input_data);
free(output_data);
return res;
}