/
fft_jp.c
266 lines (253 loc) · 5.86 KB
/
fft_jp.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
/*
* fft_jp.c
*
* A parallel fft implementation written as a final project
* for IAM751 Spring 2012.
*
* Author: Jeffrey Picard (jpicardnh@gmail.com)
*/
/* Standard headers */
#include <stdlib.h>
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <mpi.h>
/* My headers */
#include "fft_jp.h"
/*
* fft
*
* Takes an input vector, output vector and a number of points
* Performs a Fast Fourier Transform on the input vector
* and stores it in the output vector.
*/
void fft( vector *in, vector *out, int n )
{
int k;
if( n == 1 )
VEC( out, 0 ) = VEC( in, 0 );
else
{
complex double wn = cexp( -2*M_PI*I / n );
vector *x, *y, *p, *q;
create_vector( &x, n/2 );
create_vector( &y, n/2 );
create_vector( &p, n/2 );
create_vector( &q, n/2 );
for( k = 0; k < n/2; k++ )
{
VEC( x, k ) = VEC( in, 2*k );
VEC( y, k ) = VEC( in, 2*k + 1 );
}
fft( x, p, n/2 );
fft( y, q, n/2 );
for( k = 0; k < n; k++ )
{
VEC( out, k ) = VEC( p, k % (n/2) ) + cpow(wn,k)*VEC( q, k % (n/2) );
}
destroy_vector( x );
destroy_vector( y );
destroy_vector( p );
destroy_vector( q );
}
}
/*
* fft_mpi
*
* Takes an input vector, output vector and a number of points
* Performs a Fast Fourier Transform on the input vector
* and stores it in the output vector.
*
* Uses MPI for parallelization
*/
void fft_mpi( vector *in, vector *out, int n )
{
int rank, size;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
fprintf( stderr, "%d/%d\n", rank, size );
int k;
if( n == 1 )
VEC( out, 0 ) = VEC( in, 0 );
else
{
complex double wn = cexp( -2*M_PI*I / n );
vector *x, *y, *p, *q;
create_vector( &x, n/2 );
create_vector( &y, n/2 );
create_vector( &p, n/2 );
create_vector( &q, n/2 );
for( k = 0; k < n/2; k++ )
{
//x[k] = in[2*k]
//y[k] = in[2*k + 1]
VEC( x, k ) = VEC( in, 2*k );
VEC( y, k ) = VEC( in, 2*k + 1 );
}
fft( x, p, n/2 );
fft( y, q, n/2 );
for( k = 0; k < n; k++ )
{
//out[k] = p[ k % (n/2) ] + pow(w,k)*q[k % (n/2) ];
VEC( out, k ) = VEC( p, k % (n/2) ) + cpow(wn,k)*VEC( q, k % (n/2) );
}
destroy_vector( x );
destroy_vector( y );
destroy_vector( p );
destroy_vector( q );
}
}
/*
* fft_bin
*
* Takes an input vector, output vector and a number of points
* Performs a Fast Fourier Transform on the input vector
* and stores it in the output vector.
*
* Uses the binary exchange algorithm
*/
void fft_bin( vector *in, vector *out, int n )
{
int rank, size;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
fprintf( stderr, "%d/%d\n", rank, size );
int k;
if( n == 1 )
VEC( out, 0 ) = VEC( in, 0 );
else
{
complex double wn = cexp( -2*M_PI*I / n );
vector *x, *y, *p, *q;
create_vector( &x, n/2 );
create_vector( &y, n/2 );
create_vector( &p, n/2 );
create_vector( &q, n/2 );
for( k = 0; k < n/2; k++ )
{
//x[k] = in[2*k]
//y[k] = in[2*k + 1]
VEC( x, k ) = VEC( in, 2*k );
VEC( y, k ) = VEC( in, 2*k + 1 );
}
fft( x, p, n/2 );
fft( y, q, n/2 );
for( k = 0; k < n; k++ )
{
//out[k] = p[ k % (n/2) ] + pow(w,k)*q[k % (n/2) ];
VEC( out, k ) = VEC( p, k % (n/2) ) + cpow(wn,k)*VEC( q, k % (n/2) );
}
destroy_vector( x );
destroy_vector( y );
destroy_vector( p );
destroy_vector( q );
}
}
/*
* vec_fill_sine
*
* Takes a vector and a scalar and fills the vector
* with sine values.
*/
void vec_fill_sine( vector *v, val_type scale )
{
int i;
for( i = 0; i < v->n; i++ )
VEC( v, i ) = sin( i * scale / v->n );
}
/*
* vec_fill_cosine
*
* Takes a vector and a scalar and fills the vector
* with cosine values.
*/
void vec_fill_cosine( vector *v, const int n, val_type scale )
{
int i;
for( i = 0; i < n; i++ )
VEC( v, i ) = cos( i * scale / n );
}
/*
* vec_fill_grid
*
* Takes a vector and a scalar and fills the vector
* to be an evenly spaced grid from 0 ... scale.
*/
void vec_fill_grid( vector *v, val_type scale )
{
int i;
for( i = 0; i < v->n; i++ )
VEC( v, i ) = (double)i / (double)v->n;
}
/*
* vec_fill_grid_mpi
*
* Takes a vector and a scalar and fills the vector
* to be an evenly spaced grid from 0 ... scale.
* Scaling with the rank from MPI.
*/
void vec_fill_grid_mpi( vector *v, val_type scale, int rank, int size )
{
int i;
for( i = 0; i < v->n; i++ )
VEC( v, i ) = (double)((rank*size)+i) / (double)v->n;
//VEC( v, i ) = ((double)i / (double)v->n) * (double)(rank+1);
}
/*
* create_vector
*
* Takes a pointer to a pointer to a vector and a size.
* Allocates space for the vector struct and the number
* of specified values.
*/
void create_vector( vector **v, int n )
{
(*v) = malloc( sizeof(vector) );
if( !*v )
EXIT_WITH_PERROR("malloc failed in create_vector: ")
(*v)->vals = calloc( n, sizeof(val_type) );
if( !(*v)->vals )
EXIT_WITH_PERROR("malloc failed in create_vector: ")
(*v)->n = n;
}
/*
* destroy_vector
*
* Takes a vector pointer and frees all of its
* associated memory, including the struct.
*/
void destroy_vector( vector *v )
{
if( !v ) return;
free( v->vals );
free( v );
}
/*
* write_vector
*
* Takes a FILE*, and a vector and writes it
*/
void write_vector( FILE *fp, vector *v )
{
int i;
fprintf( fp, "\n" );
for( i = 0; i < v->n; i++ )
fprintf( fp, "%g ", VEC( v, i ) );
fprintf( fp, "\n" );
}
/*
* write_data
*
* Takes a FILE*, a data vector, a grid vector
* a size and a mode.
* mode 0: write real values
* mode 1: write complex values
* mode 2: write both real and comples values
*/
void write_data( FILE *fp, vector *v, vector *grid,
const int n, int mode )
{
int i;
for( i = 0; i < n; i++ )
fprintf( fp, "%g %g\n", creal(VEC( grid, i )), creal(VEC( v, i )) );
}