ITK  6.0.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, [[maybe_unused]] 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 # endif
72  PlanType plan = fftwf_plan_dft_c2r_1d(n, in, out, flags);
73  return plan;
74  }
75  static PlanType
77  int ny,
78  ComplexType * in,
79  PixelType * out,
80  unsigned int flags,
81  [[maybe_unused]] int threads = 1)
82  {
83 # ifndef ITK_USE_CUFFTW
84  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
85  fftwf_plan_with_nthreads(threads);
86 # endif
87  PlanType plan = fftwf_plan_dft_c2r_2d(nx, ny, in, out, flags);
88  return plan;
89  }
90  static PlanType
92  int ny,
93  int nz,
94  ComplexType * in,
95  PixelType * out,
96  unsigned int flags,
97  [[maybe_unused]] int threads = 1)
98  {
99 # ifndef ITK_USE_CUFFTW
100  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
101  fftwf_plan_with_nthreads(threads);
102 # endif
103  PlanType plan = fftwf_plan_dft_c2r_3d(nx, ny, nz, in, out, flags);
104  return plan;
105  }
106  static PlanType
107  Plan_dft_c2r(int rank,
108  const int * n,
109  ComplexType * in,
110  PixelType * out,
111  unsigned int flags,
112  [[maybe_unused]] int threads = 1)
113  {
114 # ifndef ITK_USE_CUFFTW
115  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
116  fftwf_plan_with_nthreads(threads);
117 # endif
118  PlanType plan = fftwf_plan_dft_c2r(rank, n, in, out, flags);
119  return plan;
120  }
121 
122  static PlanType
123  Plan_dft_r2c_1d(int n, PixelType * in, ComplexType * out, unsigned int flags, [[maybe_unused]] int threads = 1)
124  {
125 # ifndef ITK_USE_CUFFTW
126  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
127  fftwf_plan_with_nthreads(threads);
128 # endif
129  PlanType plan = fftwf_plan_dft_r2c_1d(n, in, out, flags);
130  return plan;
131  }
132  static PlanType
134  int ny,
135  PixelType * in,
136  ComplexType * out,
137  unsigned int flags,
138  [[maybe_unused]] int threads = 1)
139  {
140 # ifndef ITK_USE_CUFFTW
141  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
142  fftwf_plan_with_nthreads(threads);
143 # endif
144  PlanType plan = fftwf_plan_dft_r2c_2d(nx, ny, in, out, flags);
145  return plan;
146  }
147  static PlanType
149  int ny,
150  int nz,
151  PixelType * in,
152  ComplexType * out,
153  unsigned int flags,
154  [[maybe_unused]] int threads = 1)
155  {
156 # ifndef ITK_USE_CUFFTW
157  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
158  fftwf_plan_with_nthreads(threads);
159 # endif
160  PlanType plan = fftwf_plan_dft_r2c_3d(nx, ny, nz, in, out, flags);
161  return plan;
162  }
163  static PlanType
164  Plan_dft_r2c(int rank,
165  const int * n,
166  PixelType * in,
167  ComplexType * out,
168  unsigned int flags,
169  [[maybe_unused]] int threads = 1)
170  {
171 # ifndef ITK_USE_CUFFTW
172  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
173  fftwf_plan_with_nthreads(threads);
174 # endif
175  PlanType plan = fftwf_plan_dft_r2c(rank, n, in, out, flags);
176  return plan;
177  }
178  static PlanType
179  Plan_dft_1d(const int n,
180  ComplexType * in,
181  ComplexType * out,
182  int sign,
183  unsigned int flags,
184  [[maybe_unused]] int threads = 1)
185  {
186 # ifndef ITK_USE_CUFFTW
187  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
188  fftwf_plan_with_nthreads(threads);
189 # endif
190  PlanType plan = fftwf_plan_dft_1d(n, in, out, sign, flags);
191  return plan;
192  }
193 
194 
195  static void
197  {
198  fftwf_execute(p);
199  }
200  static void
202  {
203  fftwf_destroy_plan(p);
204  }
205 };
206 
207 #endif // USE_FFTWF
208 
209 
210 #if defined(ITK_USE_FFTWD)
211 template <>
213 {
214 public:
215  using PixelType = double;
216  using ComplexType = fftw_complex;
217  using PlanType = fftw_plan;
219 
220  static PlanType
221  Plan_dft_c2r_1d(int n, ComplexType * in, PixelType * out, unsigned int flags, [[maybe_unused]] int threads = 1)
222  {
223 # ifndef ITK_USE_CUFFTW
224  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
225  fftw_plan_with_nthreads(threads);
226 # endif
227  PlanType plan = fftw_plan_dft_c2r_1d(n, in, out, flags);
228  return plan;
229  }
230  static PlanType
232  int ny,
233  ComplexType * in,
234  PixelType * out,
235  unsigned int flags,
236  [[maybe_unused]] int threads = 1)
237  {
238 # ifndef ITK_USE_CUFFTW
239  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
240  fftw_plan_with_nthreads(threads);
241 # endif
242  PlanType plan = fftw_plan_dft_c2r_2d(nx, ny, in, out, flags);
243  return plan;
244  }
245  static PlanType
247  int ny,
248  int nz,
249  ComplexType * in,
250  PixelType * out,
251  unsigned int flags,
252  [[maybe_unused]] int threads = 1)
253  {
254 # ifndef ITK_USE_CUFFTW
255  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
256  fftw_plan_with_nthreads(threads);
257 # endif
258  PlanType plan = fftw_plan_dft_c2r_3d(nx, ny, nz, in, out, flags);
259  return plan;
260  }
261  static PlanType
262  Plan_dft_c2r(int rank,
263  const int * n,
264  ComplexType * in,
265  PixelType * out,
266  unsigned int flags,
267  [[maybe_unused]] int threads = 1)
268  {
269 # ifndef ITK_USE_CUFFTW
270  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
271  fftw_plan_with_nthreads(threads);
272 # endif
273  PlanType plan = fftw_plan_dft_c2r(rank, n, in, out, flags);
274  return plan;
275  }
276 
277  static PlanType
278  Plan_dft_r2c_1d(int n, PixelType * in, ComplexType * out, unsigned int flags, [[maybe_unused]] int threads = 1)
279  {
280 # ifndef ITK_USE_CUFFTW
281  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
282  fftw_plan_with_nthreads(threads);
283 # endif
284  PlanType plan = fftw_plan_dft_r2c_1d(n, in, out, flags);
285  return plan;
286  }
287 
288  static PlanType
290  int ny,
291  PixelType * in,
292  ComplexType * out,
293  unsigned int flags,
294  [[maybe_unused]] int threads = 1)
295  {
296 # ifndef ITK_USE_CUFFTW
297  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
298  fftw_plan_with_nthreads(threads);
299 # endif
300  PlanType plan = fftw_plan_dft_r2c_2d(nx, ny, in, out, flags);
301  return plan;
302  }
303 
304  static PlanType
306  int ny,
307  int nz,
308  PixelType * in,
309  ComplexType * out,
310  unsigned int flags,
311  [[maybe_unused]] int threads = 1)
312  {
313 # ifndef ITK_USE_CUFFTW
314  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
315  fftw_plan_with_nthreads(threads);
316 # endif
317  PlanType plan = fftw_plan_dft_r2c_3d(nx, ny, nz, in, out, flags);
318  return plan;
319  }
320 
321  static PlanType
322  Plan_dft_r2c(int rank,
323  const int * n,
324  PixelType * in,
325  ComplexType * out,
326  unsigned int flags,
327  [[maybe_unused]] int threads = 1)
328  {
329 # ifndef ITK_USE_CUFFTW
330  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
331  fftw_plan_with_nthreads(threads);
332 # endif
333  PlanType plan = fftw_plan_dft_r2c(rank, n, in, out, flags);
334  return plan;
335  }
336  static PlanType
337  Plan_dft_1d(const int n,
338  ComplexType * in,
339  ComplexType * out,
340  int sign,
341  unsigned int flags,
342  [[maybe_unused]] int threads = 1)
343  {
344 # ifndef ITK_USE_CUFFTW
345  const std::lock_guard<FFTWGlobalConfiguration::MutexType> lockGuard(FFTWGlobalConfiguration::GetLockMutex());
346  fftw_plan_with_nthreads(threads);
347 # endif
348  PlanType plan = fftw_plan_dft_1d(n, in, out, sign, flags);
349  return plan;
350  }
351 
352  static void
354  {
355  fftw_execute(p);
356  }
357  static void
359  {
360  fftw_destroy_plan(p);
361  }
362 };
363 
364 #endif
365 } // namespace fftw
366 } // namespace itk
367 #endif
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:289
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:133
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_r2c
static PlanType Plan_dft_r2c(int rank, const int *n, PixelType *in, ComplexType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:322
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:246
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:76
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:91
itk::fftw::ComplexToComplexProxy< double >::PlanType
fftw_plan PlanType
Definition: itkFFTWCommonExtended.h:217
itk::fftw::ComplexToComplexProxy< double >::DestroyPlan
static void DestroyPlan(PlanType p)
Definition: itkFFTWCommonExtended.h:358
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:231
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_r2c
static PlanType Plan_dft_r2c(int rank, const int *n, PixelType *in, ComplexType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:164
itk::fftw::ComplexToComplexProxy< double >::ComplexType
fftw_complex ComplexType
Definition: itkFFTWCommonExtended.h:216
itk::fftw::ComplexToComplexProxy
Definition: itkFFTWCommonExtended.h:45
itk::fftw::ComplexToComplexProxy< float >::DestroyPlan
static void DestroyPlan(PlanType p)
Definition: itkFFTWCommonExtended.h:201
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_c2r
static PlanType Plan_dft_c2r(int rank, const int *n, ComplexType *in, PixelType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:262
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_c2r
static PlanType Plan_dft_c2r(int rank, const int *n, ComplexType *in, PixelType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:107
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_r2c_1d
static PlanType Plan_dft_r2c_1d(int n, PixelType *in, ComplexType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:123
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_c2r_1d
static PlanType Plan_dft_c2r_1d(int n, ComplexType *in, PixelType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:221
itk::fftw::ComplexToComplexProxy< float >::PlanType
fftwf_plan PlanType
Definition: itkFFTWCommonExtended.h:62
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_r2c_1d
static PlanType Plan_dft_r2c_1d(int n, PixelType *in, ComplexType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:278
itk::fftw::ComplexToComplexProxy< double >::PixelType
double PixelType
Definition: itkFFTWCommonExtended.h:215
itk::fftw::ComplexToComplexProxy< float >
Definition: itkFFTWCommonExtended.h:57
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_1d
static PlanType Plan_dft_1d(const int n, ComplexType *in, ComplexType *out, int sign, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:179
itk::fftw::ComplexToComplexProxy::~ComplexToComplexProxy
~ComplexToComplexProxy()=default
itk::fftw::ComplexToComplexProxy< double >::Plan_dft_1d
static PlanType Plan_dft_1d(const int n, ComplexType *in, ComplexType *out, int sign, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:337
itk::fftw::ComplexToComplexProxy< double >::Execute
static void Execute(PlanType p)
Definition: itkFFTWCommonExtended.h:353
itk::fftw::ComplexToComplexProxy< float >::PixelType
float PixelType
Definition: itkFFTWCommonExtended.h:60
itk::fftw::ComplexToComplexProxy< double >
Definition: itkFFTWCommonExtended.h:212
itk::fftw::ComplexToComplexProxy< float >::ComplexType
fftwf_complex ComplexType
Definition: itkFFTWCommonExtended.h:61
itk::fftw::ComplexToComplexProxy< float >::Plan_dft_c2r_1d
static PlanType Plan_dft_c2r_1d(int n, ComplexType *in, PixelType *out, unsigned int flags, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:66
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:305
itk::FFTWGlobalConfiguration::GetLockMutex
static std::mutex & GetLockMutex()
itkFFTWGlobalConfiguration.h
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, [[maybe_unused]] int threads=1)
Definition: itkFFTWCommonExtended.h:148
itk::fftw::ComplexToComplexProxy::ComplexToComplexProxy
ComplexToComplexProxy()=default
itk::fftw::ComplexToComplexProxy< float >::Execute
static void Execute(PlanType p)
Definition: itkFFTWCommonExtended.h:196