forked from seung-lab/znn-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfftw.hpp
263 lines (220 loc) · 7.22 KB
/
fftw.hpp
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
//
// Copyright (C) 2014 Aleksandar Zlateski <[email protected]>
// ----------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef ZNN_FFTW_HPP_INCLUDED
#define ZNN_FFTW_HPP_INCLUDED
#include "types.hpp"
#include "volume_pool.hpp"
#include "fftw_plans.hpp"
#include "utils.hpp"
#include "measure.hpp"
#include <zi/utility/assert.hpp>
#include <zi/concurrency.hpp>
#include <zi/time.hpp>
namespace zi {
namespace znn {
class zfft
{
public:
static complex3d_ptr forward( const boost::shared_ptr<double3d>& in,
vec3i pad = vec3i(0,0,0), bool measure = false )
{
PROFILE_FUNCTION();
double3d_ptr fft_in ;
complex3d_ptr fft_out ;
fftw_plan fft_plan;
if ( pad[0] )
{
fft_in = volume_pool.get_double3d(pad);
fft_out = volume_pool.get_complex3d(pad);
{
zi::class_mutex<zfft>::guard g;
fft_plan = fftw_plan_dft_r2c_3d
( pad[0], pad[1], pad[2],
reinterpret_cast<double*>(fft_in->data()),
reinterpret_cast<fftw_complex*>(fft_out->data()),
measure ? FFTW_MEASURE : FFTW_ESTIMATE );
}
volume_utils::zero_out(fft_in);
(*fft_in)[boost::indices[range(0,pad[0])][range(0,pad[1])][range(0,pad[2])]]
= (*in);
}
else
{
fft_in = volume_pool.get_double3d(in);
fft_out = volume_pool.get_complex3d(in);
{
zi::class_mutex<zfft>::guard g;
fft_plan = fftw_plan_dft_r2c_3d
( fft_in->shape()[0], fft_in->shape()[1], fft_in->shape()[2],
reinterpret_cast<double*>(fft_in->data()),
reinterpret_cast<fftw_complex*>(fft_out->data()),
measure ? FFTW_MEASURE : FFTW_ESTIMATE );
}
(*fft_in) = (*in);
}
fftw_execute(fft_plan);
fftw_destroy_plan(fft_plan);
return fft_out;
}
}; // zfft
class fftw_stats_impl
{
private:
double total_time_;
std::size_t total_ ;
zi::mutex m_ ;
public:
fftw_stats_impl()
: total_time_(0)
, total_(0)
, m_()
{ }
// [kisuklee]
std::size_t get_total_time() const
{
return total_time_;
}
// [kisuklee]
void reset_total_time()
{
total_time_ = 0;
}
void add(double time)
{
zi::mutex::guard g(m_);
++total_;
total_time_ += time;
// if ( total_ % 10000 == 0 )
// {
// std::cout << "Total spent on ffts: " << total_time_ << std::endl;
// }
}
};
namespace {
fftw_stats_impl& fftw_stats = zi::singleton<fftw_stats_impl>::instance();
} // anonymous namespace
class fftw
{
public:
static void forward( const boost::shared_ptr<double3d>& in,
const boost::shared_ptr<complex3d>& out )
{
PROFILE_FUNCTION();
ZI_ASSERT(in->shape()[0]==out->shape()[0]);
ZI_ASSERT(in->shape()[1]==out->shape()[1]);
ZI_ASSERT((in->shape()[2]/2+1)==out->shape()[2]);
fftw_plan plan = fftw_plans.get_forward(
vec3i(in->shape()[0],in->shape()[1],in->shape()[2]));
zi::wall_timer wt;
fftw_execute_dft_r2c(plan,
reinterpret_cast<double*>(in->data()),
reinterpret_cast<fftw_complex*>(out->data()));
fftw_stats.add(wt.elapsed<double>());
}
static void backward( const boost::shared_ptr<complex3d>& in,
const boost::shared_ptr<double3d>& out )
{
PROFILE_FUNCTION();
ZI_ASSERT(in->shape()[0]==out->shape()[0]);
ZI_ASSERT(in->shape()[1]==out->shape()[1]);
ZI_ASSERT((out->shape()[2]/2+1)==in->shape()[2]);
fftw_plan plan = fftw_plans.get_backward(
vec3i(out->shape()[0],out->shape()[1],out->shape()[2]));
zi::wall_timer wt;
fftw_execute_dft_c2r(plan,
reinterpret_cast<fftw_complex*>(in->data()),
reinterpret_cast<double*>(out->data()));
fftw_stats.add(wt.elapsed<double>());
}
static complex3d_ptr forward( const boost::shared_ptr<double3d>& in )
{
PROFILE_FUNCTION();
complex3d_ptr ret = volume_pool.get_complex3d(in);
fftw::forward( in, ret );
return ret;
}
static double3d_ptr backward( const complex3d_ptr& in,
const vec3i& s )
{
PROFILE_FUNCTION();
double3d_ptr ret = volume_pool.get_double3d(s);
fftw::backward( in, ret );
return ret;
}
static complex3d_ptr forward_pad( const double3d_ptr& in,
const vec3i& pad )
{
PROFILE_FUNCTION();
double3d_ptr pin = volume_pool.get_double3d(pad);
volume_utils::zero_pad(pin, in);
return fftw::forward(pin);
}
}; // class fftw
class fft_transform
{
private:
const size_t x, y, z;
const boost::shared_ptr<double3d> inb ;
const boost::shared_ptr<complex3d> outb;
fftw_plan forward_plan;
fftw_plan backward_plan;
public:
static std::size_t r2c_size( std::size_t z )
{
return (z/2)+1;
}
public:
fft_transform( const boost::shared_ptr<double3d>& in,
const boost::shared_ptr<complex3d>& out,
bool measure = true )
: x(in->shape()[0])
, y(in->shape()[1])
, z(in->shape()[2])
{
zi::class_mutex<fft_transform>::guard g;
forward_plan = fftw_plan_dft_r2c_3d
( x,y,z,
reinterpret_cast<double*>(in->data()),
reinterpret_cast<fftw_complex*>(out->data()),
measure ? FFTW_MEASURE : FFTW_ESTIMATE );
backward_plan = fftw_plan_dft_c2r_3d
( x,y,z,
reinterpret_cast<fftw_complex*>(out->data()),
reinterpret_cast<double*>(in->data()),
measure ? FFTW_MEASURE : FFTW_ESTIMATE );
ZI_ASSERT(in->shape()[0]==out->shape()[0]);
ZI_ASSERT(in->shape()[1]==out->shape()[1]);
ZI_ASSERT((in->shape()[2]/2+1)==out->shape()[2]);
}
~fft_transform()
{
zi::class_mutex<fft_transform>::guard g;
fftw_destroy_plan(forward_plan);
fftw_destroy_plan(backward_plan);
}
void forward()
{
fftw_execute(forward_plan);
}
void backward()
{
fftw_execute(backward_plan);
}
};
}} // namespace zi::znn
#endif // ZNN_FFTW_HPP_INCLUDED