-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfft_mpi.c
347 lines (269 loc) · 9.63 KB
/
fft_mpi.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <complex.h>
#include <mpi.h>
#include <math.h>
#include "pgm.h"
#include "cshift.h"
#define PI 3.14159265358979323846
typedef double complex cplx;
// Function to convert a matrix in form of a vector
// mat = matrix
// width = number of columns
// height = number of rows
// return = vector
cplx* mat2vet(cplx** mat, int width, int height){
cplx *v = (cplx*)malloc(height * width * sizeof(cplx));
for(int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
v[i * width + j] = mat[i][j];
}
}
return v;
}
// Function to convert a vector in form of a matrix
// v = vector
// width = number of columns
// height = number of rows
// return = matrix
cplx** vet2mat(cplx* v, int width, int height){
cplx **mat = (cplx**)malloc(height * sizeof(cplx*));
for(int i = 0; i < height; i++){
mat[i] = (cplx*)malloc(width * sizeof(cplx));
for (int j = 0; j < width; j++){
mat[i][j] = v[i * width + j];
}
}
return mat;
}
// Function to calculate the next power of 2
// num = number to calculate the next power of 2
// return = next power of 2
int nextPowerOf2(int num) {
int power = 1;
while (power < num) {
power *= 2;
}
return power;
}
// Function to check if a number is a power of 2
// x = number to check
// return = 1 if x is a power of 2, 0 otherwise
int is_power_of_two(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
// Function to perform zero padding on an image
// image = image to pad
// return = padded image
pgm_t zeroPadding(const pgm_t image) {
pgm_t paddedImage;
int newWidth = nextPowerOf2(image.width);
int newHeight = nextPowerOf2(image.height);
// If the image is not square, pad the image to make it square
if (newWidth != newHeight) {
newWidth = newHeight = (newWidth > newHeight) ? newWidth : newHeight;
}
// Allocate memory for the padded image
paddedImage.data = (cplx**)malloc(newHeight * sizeof(cplx*));
for (int i = 0; i < newHeight; i++) {
paddedImage.data[i] = (cplx*)calloc(newWidth, sizeof(cplx));
}
// Copy the image into the padded image
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
paddedImage.data[i][j] = image.data[i][j];
}
}
// Copy the info of the image into the padded image
paddedImage.width = newWidth;
paddedImage.height = newHeight;
paddedImage.max = image.max;
strcpy(paddedImage.type, image.type);
return paddedImage;
}
// Function to perform Cooley-Tukey FFT
// x = input vector
// N = size of the input vector
// Forwards if inverse = 0, backwards if inverse = 1
void cooley_tukey_fft(cplx x[], int N, int inverse) {
// Bit-reversal permutation
int i, j, k;
for (i = 1, j = N / 2; i < N - 1; i++) {
if (i < j) {
cplx temp = x[i];
x[i] = x[j];
x[j] = temp;
}
k = N / 2;
while (k <= j) {
j -= k;
k /= 2;
}
j += k;
}
// Iterative FFT or IFFT
double sign = (inverse) ? 1.0 : -1.0; // Sign for IFFT
for (int s = 1; s <= log2(N); s++) {
int m = 1 << s; // Subproblem size
cplx omega_m = cexp(sign * I * 2.0 * PI / m);
for (int k = 0; k < N; k += m) {
cplx omega = 1.0;
for (int j = 0; j < m / 2; j++) {
cplx t = omega * x[k + j + m / 2];
cplx u = x[k + j];
x[k + j] = u + t;
x[k + j + m / 2] = u - t;
omega *= omega_m;
}
}
}
}
// Function to transpose a matrix in form of a vector
// v = vector
// width = number of columns
// height = number of rows
// return = transposed vector
cplx* transpose(cplx* v, int width, int height){
cplx *tmp = (cplx*)malloc(height * width * sizeof(cplx));
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
tmp[j * height + i] = v[i * width + j];
}
}
return tmp;
}
int main(int argc, char** argv) {
double start_time = MPI_Wtime();
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
pgm_t img;
cplx *v_send;
cplx *v_revc;
int len_info[5];
if(rank == 0){
img = pgm_read(argv[1]);
len_info[3] = img.width;
len_info[4] = img.height;
// Check if padding is needed
if(!is_power_of_two(img.width*img.height) || img.width != img.height){
img = zeroPadding(img);
}
len_info[0] = img.width;
len_info[1] = img.height;
len_info[2] = img.max;
// Allocate memory for the send vector
v_send = (cplx*)malloc(img.width * img.height * sizeof(cplx));
// Convert image to vector
v_send = mat2vet(img.data, img.width, img.height);
}
// Broadcast the usefull length information
MPI_Bcast(len_info, 5, MPI_INT, 0, MPI_COMM_WORLD);
// Find the number of rows per processor
int rows_per_processor = len_info[1] / size;
int remainder = len_info[1] % size;
// Find the number of processors that will receive an extra row
int processors_with_extra_rows = (rank < remainder) ? 1 : 0;
// Find the number of rows to send to this processor
int my_num_rows = rows_per_processor + processors_with_extra_rows;
// Calculate the displacements for MPI_Scatterv
int* displacements = (int*)malloc(size * sizeof(int));
int* recvcounts = (int*)malloc(size * sizeof(int));
// Calculate the displacements and the recvcounts
int displacement = 0;
for (int i = 0; i < size; i++) {
recvcounts[i] = rows_per_processor * len_info[0];
if (i < remainder) {
recvcounts[i] += len_info[0]; // Distribute remaining rows
}
displacements[i] = displacement;
displacement += recvcounts[i];
}
// Allocate memory for the received vector
v_revc = (cplx*)malloc(my_num_rows * len_info[0] * sizeof(cplx));
// Scatter the data
MPI_Scatterv(v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
//#################### Start 2D FFT ####################
// Perform 1D FFT
for(int i = 0; i < my_num_rows; i++)
{
cooley_tukey_fft(v_revc + i * len_info[0], len_info[0], 0);
}
// Gather the data
MPI_Gatherv(v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
if(rank == 0){
// Transpose
v_send = transpose(v_send, len_info[0], len_info[1]);
}
// Scatter the data
MPI_Scatterv(v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
// Perform 1D FFT
for(int i = 0; i < my_num_rows; i++)
{
cooley_tukey_fft(v_revc + i * len_info[0], len_info[0], 0);
}
//#################### End 2D FFT ####################
// Gather the data
MPI_Gatherv(v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
if(rank == 0){
// Print the FFT image
img.data = vet2mat(fftshift(v_send, len_info[0], len_info[1]), len_info[0], len_info[1]);
// Write FFT image
pgm_write_fft(img, "fft.pgm", "");
v_send = ifftshift(mat2vet(img.data, len_info[0], len_info[1]), len_info[0], len_info[1]);
}
// Scatter the data
MPI_Scatterv(v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
//#################### Start 2D FFT ####################
// Perform 1D FFT
for(int i = 0; i < my_num_rows; i++)
{
cooley_tukey_fft(v_revc + i * len_info[0], len_info[0], 1);
}
// Gather the data
MPI_Gatherv(v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
if(rank == 0){
// Transpose
v_send = transpose(v_send, len_info[0], len_info[1]);
}
// Scatter the data
MPI_Scatterv(v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
// Perform 1D FFT
for(int i = 0; i < my_num_rows; i++)
{
cooley_tukey_fft(v_revc + i * len_info[0], len_info[0], 1);
}
//#################### End 2D FFT ####################
//(missing the division by the number of elements, we will do it after the gather)
// Gather the data
MPI_Gatherv(v_revc, my_num_rows * len_info[0], MPI_C_DOUBLE_COMPLEX, v_send, recvcounts, displacements, MPI_C_DOUBLE_COMPLEX, 0, MPI_COMM_WORLD);
if(rank == 0){
// Divide by the number of elements
for(int i=0; i<len_info[0]*len_info[1]; i++){
v_send[i] /= (double)(len_info[0]*len_info[1]);
}
img.data = vet2mat(v_send, len_info[0], len_info[1]);
free(v_send);
img.data = realloc(img.data, len_info[4] * sizeof(cplx*));
for (int i = 0; i < len_info[4]; i++) {
img.data[i] = realloc(img.data[i], len_info[3] * sizeof(cplx));
}
img.width = len_info[3];
img.height = len_info[4];
// Write the ifft
pgm_write(img, "ifft.pgm", "");
free(img.data);
}
free(v_revc);
free(displacements);
free(recvcounts);
double end_time = MPI_Wtime();
if(rank == 0){
printf("Time: %.10lf\n", end_time - start_time);
}
MPI_Finalize();
return 0;
}