ITK  5.4.0
Insight Toolkit
itkFFTWCommonExtended.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkFFTWCommonExtended_h
19 #define itkFFTWCommonExtended_h
20 
21 #if defined(ITK_USE_FFTWF) || defined(ITK_USE_FFTWD)
22 # if defined(ITK_USE_CUFFTW)
23 # include "cufftw.h"
24 # else
26 # include "fftw3.h"
27 # endif
28 #endif
29 
30 #include <mutex>
31 
32 namespace itk
33 {
34 
35 namespace fftw
36 {
44 template <typename TPixel>
46 {
47  // empty -- only double and float specializations work
48 protected:
49  ComplexToComplexProxy() = default;
50  ~ComplexToComplexProxy() = default;
51 };
54 #if defined(ITK_USE_FFTWF)
55 
56 template <>
58 {
59 public:
60  using PixelType = float;
61  using ComplexType = fftwf_complex;
62  using PlanType = fftwf_plan;
64 
65  static PlanType
66  Plan_dft_c2r_1d(int n, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
67  {
68 # ifndef ITK_USE_CUFFTW
69  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
70  fftwf_plan_with_nthreads(threads);
71 # else
72  (void)threads;
73 # endif
74  PlanType plan = fftwf_plan_dft_c2r_1d(n, in, out, flags);
75  return plan;
76  }
77  static PlanType
78  Plan_dft_c2r_2d(int nx, int ny, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
79  {
80 # ifndef ITK_USE_CUFFTW
81  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
82  fftwf_plan_with_nthreads(threads);
83 # else
84  (void)threads;
85 # endif
86  PlanType plan = fftwf_plan_dft_c2r_2d(nx, ny, in, out, flags);
87  return plan;
88  }
89  static PlanType
90  Plan_dft_c2r_3d(int nx, int ny, int nz, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
91  {
92 # ifndef ITK_USE_CUFFTW
93  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
94  fftwf_plan_with_nthreads(threads);
95 # else
96  (void)threads;
97 # endif
98  PlanType plan = fftwf_plan_dft_c2r_3d(nx, ny, nz, in, out, flags);
99  return plan;
100  }
101  static PlanType
102  Plan_dft_c2r(int rank, const int * n, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
103  {
104 # ifndef ITK_USE_CUFFTW
105  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
106  fftwf_plan_with_nthreads(threads);
107 # else
108  (void)threads;
109 # endif
110  PlanType plan = fftwf_plan_dft_c2r(rank, n, in, out, flags);
111  return plan;
112  }
113 
114  static PlanType
115  Plan_dft_r2c_1d(int n, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
116  {
117 # ifndef ITK_USE_CUFFTW
118  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
119  fftwf_plan_with_nthreads(threads);
120 # else
121  (void)threads;
122 # endif
123  PlanType plan = fftwf_plan_dft_r2c_1d(n, in, out, flags);
124  return plan;
125  }
126  static PlanType
127  Plan_dft_r2c_2d(int nx, int ny, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
128  {
129 # ifndef ITK_USE_CUFFTW
130  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
131  fftwf_plan_with_nthreads(threads);
132 # else
133  (void)threads;
134 # endif
135  PlanType plan = fftwf_plan_dft_r2c_2d(nx, ny, in, out, flags);
136  return plan;
137  }
138  static PlanType
139  Plan_dft_r2c_3d(int nx, int ny, int nz, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
140  {
141 # ifndef ITK_USE_CUFFTW
142  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
143  fftwf_plan_with_nthreads(threads);
144 # else
145  (void)threads;
146 # endif
147  PlanType plan = fftwf_plan_dft_r2c_3d(nx, ny, nz, in, out, flags);
148  return plan;
149  }
150  static PlanType
151  Plan_dft_r2c(int rank, const int * n, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
152  {
153 # ifndef ITK_USE_CUFFTW
154  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
155  fftwf_plan_with_nthreads(threads);
156 # else
157  (void)threads;
158 # endif
159  PlanType plan = fftwf_plan_dft_r2c(rank, n, in, out, flags);
160  return plan;
161  }
162  static PlanType
163  Plan_dft_1d(const int n, ComplexType * in, ComplexType * out, int sign, unsigned int flags, int threads = 1)
164  {
165 # ifndef ITK_USE_CUFFTW
166  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
167  fftwf_plan_with_nthreads(threads);
168 # else
169  (void)threads;
170 # endif
171  PlanType plan = fftwf_plan_dft_1d(n, in, out, sign, flags);
172  return plan;
173  }
174 
175 
176  static void
178  {
179  fftwf_execute(p);
180  }
181  static void
183  {
184  fftwf_destroy_plan(p);
185  }
186 };
187 
188 #endif // USE_FFTWF
189 
190 
191 #if defined(ITK_USE_FFTWD)
192 template <>
194 {
195 public:
196  using PixelType = double;
197  using ComplexType = fftw_complex;
198  using PlanType = fftw_plan;
200 
201  static PlanType
202  Plan_dft_c2r_1d(int n, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
203  {
204 # ifndef ITK_USE_CUFFTW
205  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
206  fftw_plan_with_nthreads(threads);
207 # else
208  (void)threads;
209 # endif
210  PlanType plan = fftw_plan_dft_c2r_1d(n, in, out, flags);
211  return plan;
212  }
213  static PlanType
214  Plan_dft_c2r_2d(int nx, int ny, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
215  {
216 # ifndef ITK_USE_CUFFTW
217  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
218  fftw_plan_with_nthreads(threads);
219 # else
220  (void)threads;
221 # endif
222  PlanType plan = fftw_plan_dft_c2r_2d(nx, ny, in, out, flags);
223  return plan;
224  }
225  static PlanType
226  Plan_dft_c2r_3d(int nx, int ny, int nz, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
227  {
228 # ifndef ITK_USE_CUFFTW
229  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
230  fftw_plan_with_nthreads(threads);
231 # else
232  (void)threads;
233 # endif
234  PlanType plan = fftw_plan_dft_c2r_3d(nx, ny, nz, in, out, flags);
235  return plan;
236  }
237  static PlanType
238  Plan_dft_c2r(int rank, const int * n, ComplexType * in, PixelType * out, unsigned int flags, int threads = 1)
239  {
240 # ifndef ITK_USE_CUFFTW
241  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
242  fftw_plan_with_nthreads(threads);
243 # else
244  (void)threads;
245 # endif
246  PlanType plan = fftw_plan_dft_c2r(rank, n, in, out, flags);
247  return plan;
248  }
249 
250  static PlanType
251  Plan_dft_r2c_1d(int n, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
252  {
253 # ifndef ITK_USE_CUFFTW
254  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
255  fftw_plan_with_nthreads(threads);
256 # else
257  (void)threads;
258 # endif
259  PlanType plan = fftw_plan_dft_r2c_1d(n, in, out, flags);
260  return plan;
261  }
262 
263  static PlanType
264  Plan_dft_r2c_2d(int nx, int ny, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
265  {
266 # ifndef ITK_USE_CUFFTW
267  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
268  fftw_plan_with_nthreads(threads);
269 # else
270  (void)threads;
271 # endif
272  PlanType plan = fftw_plan_dft_r2c_2d(nx, ny, in, out, flags);
273  return plan;
274  }
275 
276  static PlanType
277  Plan_dft_r2c_3d(int nx, int ny, int nz, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
278  {
279 # ifndef ITK_USE_CUFFTW
280  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
281  fftw_plan_with_nthreads(threads);
282 # else
283  (void)threads;
284 # endif
285  PlanType plan = fftw_plan_dft_r2c_3d(nx, ny, nz, in, out, flags);
286  return plan;
287  }
288 
289  static PlanType
290  Plan_dft_r2c(int rank, const int * n, PixelType * in, ComplexType * out, unsigned int flags, int threads = 1)
291  {
292 # ifndef ITK_USE_CUFFTW
293  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
294  fftw_plan_with_nthreads(threads);
295 # else
296  (void)threads;
297 # endif
298  PlanType plan = fftw_plan_dft_r2c(rank, n, in, out, flags);
299  return plan;
300  }
301  static PlanType
302  Plan_dft_1d(const int n, ComplexType * in, ComplexType * out, int sign, unsigned int flags, int threads = 1)
303  {
304 # ifndef ITK_USE_CUFFTW
305  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
306  fftw_plan_with_nthreads(threads);
307 # else
308  (void)threads;
309 # endif
310  PlanType plan = fftw_plan_dft_1d(n, in, out, sign, flags);
311  return plan;
312  }
313 
314  static void
316  {
317  fftw_execute(p);
318  }
319  static void
321  {
322  fftw_destroy_plan(p);
323  }
324 };
325 
326 #endif
327 } // namespace fftw
328 } // namespace itk
329 #endif
itk::fftw::ComplexToComplexProxy< double >::PlanType
fftw_plan PlanType
Definition: itkFFTWCommonExtended.h:198
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_c2r_1d
static PlanType Plan_dft_c2r_1d(int n, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:202
itk::fftw::ComplexToComplexProxy< double >::DestroyPlan
static void DestroyPlan(PlanType p)
Definition: itkFFTWCommonExtended.h:320
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_1d
static PlanType Plan_dft_1d(const int n, ComplexType *in, ComplexType *out, int sign, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:163
itk::fftw::ComplexToComplexProxy< double >::ComplexType
fftw_complex ComplexType
Definition: itkFFTWCommonExtended.h:197
itk::fftw::ComplexToComplexProxy
Definition: itkFFTWCommonExtended.h:45
itk::fftw::ComplexToComplexProxy< float >::DestroyPlan
static void DestroyPlan(PlanType p)
Definition: itkFFTWCommonExtended.h:182
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_c2r_2d
static PlanType Plan_dft_c2r_2d(int nx, int ny, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:78
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_r2c_3d
static PlanType Plan_dft_r2c_3d(int nx, int ny, int nz, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:277
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_c2r
static PlanType Plan_dft_c2r(int rank, const int *n, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:102
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_r2c
static PlanType Plan_dft_r2c(int rank, const int *n, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:151
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_c2r_3d
static PlanType Plan_dft_c2r_3d(int nx, int ny, int nz, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:90
itk::fftw::ComplexToComplexProxy< float >::PlanType
fftwf_plan PlanType
Definition: itkFFTWCommonExtended.h:62
itk::fftw::ComplexToComplexProxy< double >::PixelType
double PixelType
Definition: itkFFTWCommonExtended.h:196
itk::fftw::ComplexToComplexProxy< float >
Definition: itkFFTWCommonExtended.h:57
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_r2c_1d
static PlanType Plan_dft_r2c_1d(int n, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:115
itk::fftw::ComplexToComplexProxy::~ComplexToComplexProxy
~ComplexToComplexProxy()=default
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_r2c_2d
static PlanType Plan_dft_r2c_2d(int nx, int ny, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:127
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_c2r_2d
static PlanType Plan_dft_c2r_2d(int nx, int ny, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:214
itk::fftw::ComplexToComplexProxy< double >::Execute
static void Execute(PlanType p)
Definition: itkFFTWCommonExtended.h:315
itk::fftw::ComplexToComplexProxy< float >::PixelType
float PixelType
Definition: itkFFTWCommonExtended.h:60
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_c2r_3d
static PlanType Plan_dft_c2r_3d(int nx, int ny, int nz, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:226
itk::fftw::ComplexToComplexProxy< double >
Definition: itkFFTWCommonExtended.h:193
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_c2r
static PlanType Plan_dft_c2r(int rank, const int *n, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:238
itk::fftw::ComplexToComplexProxy< float >::ComplexType
fftwf_complex ComplexType
Definition: itkFFTWCommonExtended.h:61
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_r2c_2d
static PlanType Plan_dft_r2c_2d(int nx, int ny, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:264
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::FFTWGlobalConfiguration::GetLockMutex
static std::mutex & GetLockMutex()
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_r2c_1d
static PlanType Plan_dft_r2c_1d(int n, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:251
itkFFTWGlobalConfiguration.h
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_r2c
static PlanType Plan_dft_r2c(int rank, const int *n, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:290
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_c2r_1d
static PlanType Plan_dft_c2r_1d(int n, ComplexType *in, PixelType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:66
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_1d
static PlanType Plan_dft_1d(const int n, ComplexType *in, ComplexType *out, int sign, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:302
itk::fftw::ComplexToComplexProxy::ComplexToComplexProxy
ComplexToComplexProxy()=default
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_r2c_3d
static PlanType Plan_dft_r2c_3d(int nx, int ny, int nz, PixelType *in, ComplexType *out, unsigned int flags, int threads=1)
Definition: itkFFTWCommonExtended.h:139
itk::fftw::ComplexToComplexProxy< float >::Execute
static void Execute(PlanType p)
Definition: itkFFTWCommonExtended.h:177