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