forked from seung-lab/znn-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fftw_plans.hpp
146 lines (118 loc) · 3.63 KB
/
fftw_plans.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
//
// 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_PLANS_HPP_INCLUDED
#define ZNN_FFTW_PLANS_HPP_INCLUDED
#include "types.hpp"
#include "volume_pool.hpp"
#include <zi/concurrency.hpp>
#include <zi/utility/assert.hpp>
#include <zi/utility/for_each.hpp>
#include <zi/utility/singleton.hpp>
#include <zi/time/time.hpp>
#include <map>
#include <iostream>
namespace zi {
namespace znn {
class fftw_plans_impl
{
private:
zi::mutex m_ ;
std::map<vec3i, fftw_plan> fwd_ ;
std::map<vec3i, fftw_plan> bwd_ ;
double time_ ;
bool threads_ ;
public:
~fftw_plans_impl()
{
FOR_EACH( it, fwd_ )
{
fftw_destroy_plan(it->second);
}
FOR_EACH( it, bwd_ )
{
fftw_destroy_plan(it->second);
}
fftw_cleanup_threads();
}
fftw_plans_impl()
: m_()
, fwd_()
, bwd_()
, time_(0)
, threads_(0)
{
if ( fftw_init_threads() == 0 )
{
throw std::runtime_error("FFT Threads can't be initialized!");
}
else
{
std::cout << "FFT Threads initialized\n";
threads_ = 1;
//fftw_plan_with_nthreads(4);
}
}
fftw_plan get_forward( const vec3i& s )
{
zi::mutex::guard g(m_);
if ( fwd_.count(s) )
{
return fwd_[s];
}
zi::wall_timer wt;
wt.reset();
double3d_ptr in = volume_pool.get_double3d(s);
complex3d_ptr out = volume_pool.get_complex3d(s);
fftw_plan ret = fftw_plan_dft_r2c_3d
( s[0], s[1], s[2],
reinterpret_cast<double*>(in->data()),
reinterpret_cast<fftw_complex*>(out->data()),
FFTW_ESTIMATE );
fwd_[s] = ret;
time_ += wt.elapsed<double>();
std::cout << "Total time spent creating fftw plans: " << time_ << std::endl;
return ret;
}
fftw_plan get_backward( const vec3i& s )
{
zi::mutex::guard g(m_);
if ( bwd_.count(s) )
{
return bwd_[s];
}
zi::wall_timer wt;
wt.reset();
complex3d_ptr in = volume_pool.get_complex3d(s);
double3d_ptr out = volume_pool.get_double3d(s);
fftw_plan ret = fftw_plan_dft_c2r_3d
( s[0], s[1], s[2],
reinterpret_cast<fftw_complex*>(in->data()),
reinterpret_cast<double*>(out->data()),
FFTW_ESTIMATE );
bwd_[s] = ret;
time_ += wt.elapsed<double>();
std::cout << "Total time spent creating fftw plans: " << time_ << std::endl;
return ret;
}
}; // class fftw_plans_impl
namespace {
fftw_plans_impl& fftw_plans =
zi::singleton<fftw_plans_impl>::instance();
} // anonymous namespace
}} // namespace zi::znn
#endif // ZNN_FFTW_PLANS_HPP_INCLUDED