-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistributed_fft3d.cc
281 lines (261 loc) · 9.6 KB
/
distributed_fft3d.cc
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
#include <iostream>
#include <cstring>
#include <stdexcept>
#include "multi_array_offsets.h"
#include "distributed_fft3d.h"
Distributed_fft3d::Distributed_fft3d(std::vector<int > const & shape,
Commxx_sptr comm_sptr, int planner_flags,
std::string const& wisdom_filename) :
uppers(0), lengths(0), shape(shape), comm_sptr(comm_sptr)
{
if (comm_sptr->get_size() / 2 > shape[0] / 2) {
throw std::runtime_error(
"Distributed_fft3d: (number of processors)/2 must be <= shape[0]/2");
}
std::vector<int > padded_shape(get_padded_shape_real());
int padded_shape2_complex = padded_shape[2] / 2;
//n.b. : we aren't using the wisdom_filename yet
fftw_mpi_init();
ptrdiff_t local_nx, local_x_start;
ptrdiff_t fftw_local_size = fftw_mpi_local_size_3d(shape[0], shape[1],
shape[2], comm_sptr->get(), &local_nx, &local_x_start);
local_size_real = local_nx * padded_shape[1] * padded_shape[2];
local_size_complex = local_nx * padded_shape[1] * padded_shape2_complex;
if (local_nx == 0) {
have_local_data = false;
} else {
have_local_data = true;
}
// MEDIUM HACK. fftw often (always?) returns a local size that is
// impossibly small. Adjust to the smallest possible size.
if (local_size_real > fftw_local_size) {
fftw_local_size = local_size_real;
}
// GREAT BIG HACK. fftw sometimes crashes when memory_fudge_factor = 1. The test
// program test_distributed_fft3d_mpi fails when memory_fudge_factor = 1 and
// numproc = 3. This is either a bug in fftw3, or a failing of the documentation.
// The precise value "2" is just a guess.
const int memory_fudge_factor = 2;
data = (double *) fftw_malloc(sizeof(double) * fftw_local_size
* memory_fudge_factor);
workspace = (fftw_complex *) fftw_malloc(sizeof(double) * fftw_local_size
* memory_fudge_factor);
plan = fftw_mpi_plan_dft_r2c_3d(shape[0], shape[1], shape[2], data,
workspace, comm_sptr->get(), planner_flags);
inv_plan = fftw_mpi_plan_dft_c2r_3d(shape[0], shape[1], shape[2],
workspace, data, comm_sptr->get(), planner_flags);
lower = local_x_start;
upper = lower + local_nx;
}
Commxx_sptr
Distributed_fft3d::get_comm_sptr()
{
return comm_sptr;
}
Commxx &
Distributed_fft3d::get_comm()
{
return *comm_sptr;
}
int
Distributed_fft3d::get_lower() const
{
return lower;
}
int
Distributed_fft3d::get_upper() const
{
return upper;
}
void
Distributed_fft3d::calculate_uppers_lengths()
{
if (uppers.empty()) {
int size = comm_sptr->get_size();
uppers.resize(size);
lengths.resize(size);
if (size == 1) {
uppers[0] = upper;
} else {
MPI_Allgather((void*) (&upper), 1, MPI_INT, (void*) (&uppers[0]),
1, MPI_INT, comm_sptr->get());
}
lengths[0] = uppers[0] * shape[1] * shape[2];
for (int i = 1; i < size; ++i) {
if (uppers[i] == 0) {
uppers[i] = uppers[i - 1];
}
lengths[i] = (uppers[i] - uppers[i - 1]) * shape[1] * shape[2];
}
}
}
std::vector<int > const&
Distributed_fft3d::get_uppers()
{
calculate_uppers_lengths();
return uppers;
}
std::vector<int > const&
Distributed_fft3d::get_lengths()
{
calculate_uppers_lengths();
return lengths;
}
std::vector<int > const&
Distributed_fft3d::get_shape() const
{
return shape;
}
std::vector<int >
Distributed_fft3d::get_padded_shape_real() const
{
std::vector<int > retval(shape);
retval[2] = 2 * (shape[2] / 2 + 1);
return retval;
}
std::vector<int >
Distributed_fft3d::get_padded_shape_complex() const
{
std::vector<int > retval(shape);
retval[2] = shape[2] / 2 + 1;
return retval;
}
void
Distributed_fft3d::transform(MArray3d_ref & in, MArray3dc_ref & out)
{
if (have_local_data) {
if (in.index_bases()[0] > lower) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible first index offset in input array");
}
if (static_cast<int >(in.index_bases()[0] + in.shape()[0]) < upper) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible first dimension of input array");
}
if (static_cast<int >(in.shape()[1]) != get_padded_shape_real()[1]) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible second dimension of input array");
}
if (static_cast<int >(in.shape()[2]) != get_padded_shape_real()[2]) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible third dimension of input array");
}
if (static_cast<int >(out.index_bases()[0]) > lower) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible first index offset in output array");
}
if (static_cast<int >(out.index_bases()[0] + out.shape()[0]) < upper) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible first dimension of output array");
}
if (static_cast<int >(out.shape()[1])
!= get_padded_shape_complex()[1]) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible second dimension of output array");
}
if (static_cast<int >(out.shape()[2])
!= get_padded_shape_complex()[2]) {
throw std::runtime_error(
"Distributed_fft3d::transform found an incompatible third dimension of output array");
}
}
#ifdef USE_FFTW2
if (have_local_data) {
memcpy((void*) data,(void*) multi_array_offset(in, lower, 0, 0),
local_size_real * sizeof(double));
}
rfftwnd_mpi(plan, 1, data, workspace, FFTW_NORMAL_ORDER);
if (have_local_data) {
memcpy((void* ) multi_array_offset(out, lower, 0, 0),
(void*) (data),
local_size_complex * sizeof(std::complex<double >));
}
#else
if (have_local_data) {
memcpy((void*) data, (void*) multi_array_offset(in, lower, 0, 0),
local_size_real * sizeof(double));
}
fftw_execute(plan);
if (have_local_data) {
memcpy((void*) multi_array_offset(out, lower, 0, 0),
(void*) (workspace), local_size_complex * sizeof(std::complex<
double >));
}
#endif //USE_FFTW2
}
void
Distributed_fft3d::inv_transform(MArray3dc_ref & in, MArray3d_ref & out)
{
if (have_local_data) {
if (in.index_bases()[0] > lower) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible first index offset in input array");
}
if (static_cast<int >(in.index_bases()[0] + in.shape()[0]) < upper) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible first dimension of input array");
}
if (static_cast<int >(in.shape()[1]) != get_padded_shape_complex()[1]) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible second dimension of input array");
}
if (static_cast<int >(in.shape()[2]) != get_padded_shape_complex()[2]) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible third dimension of input array");
}
if (static_cast<int >(out.index_bases()[0]) > lower) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible first index offset in output array");
}
if (static_cast<int >(out.index_bases()[0] + out.shape()[0]) < upper) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible first dimension of output array");
}
if (static_cast<int >(out.shape()[1]) != get_padded_shape_real()[1]) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible second dimension of output array");
}
if (static_cast<int >(out.shape()[2]) != get_padded_shape_real()[2]) {
throw std::runtime_error(
"Distributed_fft3d::inv_transform found an incompatible third dimension of output array");
}
}
#ifdef USE_FFTW2
if (have_local_data) {
memcpy( (void*) data, (void*) multi_array_offset(in, lower, 0, 0),
local_size_complex * sizeof(std::complex<double >));
}
rfftwnd_mpi(inv_plan, 1, data, workspace, FFTW_NORMAL_ORDER);
if (have_local_data) {
memcpy( (void*) multi_array_offset(out, lower, 0, 0),
(void*) data, local_size_real * sizeof(double));
}
#else
if (have_local_data) {
memcpy((void*) workspace, (void*) multi_array_offset(in, lower, 0, 0),
local_size_complex * sizeof(std::complex<double >));
}
fftw_execute(inv_plan);
if (have_local_data) {
memcpy((void*) multi_array_offset(out, lower, 0, 0), (void*) data,
local_size_real * sizeof(double));
}
#endif //USE_FFTW2
}
double
Distributed_fft3d::get_roundtrip_normalization() const
{
return 1.0 / (shape[0] * shape[1] * shape[2]);
}
Distributed_fft3d::~Distributed_fft3d()
{
#ifdef USE_FFTW2
fftw_free(data);
fftw_free(workspace);
#else
fftw_destroy_plan(plan);
fftw_destroy_plan(inv_plan);
fftw_free(data);
fftw_free(workspace);
#endif //USE_FFTW2
}