26 use,
intrinsic :: ieee_exceptions
27 use,
intrinsic :: iso_c_binding
56 integer,
parameter :: FINUFFT_TYPE2 = 2
57 integer,
parameter :: FINUFFT_IFLAG = -1
66 real(real64),
public :: norm
70 type(c_ptr) :: plan = c_null_ptr
74 real(real64),
pointer :: xj(:) => null()
75 real(real64),
pointer :: yj(:) => null()
76 real(real64),
pointer :: zj(:) => null()
85 type(c_ptr) function oct_finufft_opts_init(nthreads) bind(c, name=
'oct_finufft_opts_init')
86 import :: c_ptr, c_int
87 integer(c_int),
value :: nthreads
88 end function oct_finufft_opts_init
90 subroutine oct_finufft_opts_end(opts) bind(c, name='oct_finufft_opts_end')
92 type(c_ptr),
value :: opts
93 end subroutine oct_finufft_opts_end
95 integer(c_int) function finufft_makeplan(type_, dim, n_modes, iflag, n_transf, tol, plan, opts) &
96 bind(c, name=
'finufft_makeplan')
97 import :: c_int, c_int64_t, c_double, c_ptr
98 integer(c_int),
value :: type_
99 integer(c_int),
value :: dim
100 integer(c_int64_t),
intent(in) :: n_modes(*)
101 integer(c_int),
value :: iflag
102 integer(c_int),
value :: n_transf
103 real(c_double),
value :: tol
104 type(c_ptr),
intent(out) :: plan
105 type(c_ptr),
value :: opts
106 end function finufft_makeplan
108 integer(c_int) function finufft_setpts(plan, M, xj, yj, zj, N, s, t, u) &
109 bind(c, name=
'finufft_setpts')
110 import :: c_int, c_int64_t, c_ptr
111 type(c_ptr),
value :: plan
112 integer(c_int64_t),
value :: M
113 type(c_ptr),
value :: xj, yj, zj
114 integer(c_int64_t),
value :: N
115 type(c_ptr),
value :: s, t, u
116 end function finufft_setpts
118 integer(c_int) function finufft_execute(plan, cj, fk) bind(c, name='finufft_execute')
119 import :: c_int, c_ptr, c_double_complex
120 type(c_ptr),
value :: plan
121 complex(c_double_complex),
intent(inout) :: cj(*)
122 complex(c_double_complex),
intent(in) :: fk(*)
123 end function finufft_execute
125 integer(c_int) function finufft_execute_adjoint(plan, cj, fk) bind(c, name='finufft_execute_adjoint')
126 import :: c_int, c_ptr, c_double_complex
127 type(c_ptr),
value :: plan
128 complex(c_double_complex),
intent(in) :: cj(*)
129 complex(c_double_complex),
intent(inout) :: fk(*)
130 end function finufft_execute_adjoint
132 integer(c_int) function finufft_destroy(plan) bind(c, name='finufft_destroy')
133 import :: c_int, c_ptr
134 type(c_ptr),
value :: plan
135 end function finufft_destroy
145 type(
finufft_t),
intent(inout) :: finufft
158 call parse_variable(namespace,
'FINUFFTTolerance', 1.0e-8_real64, finufft%tol)
160 if (finufft%tol <=
m_zero)
then
171 type(
finufft_t),
intent(inout) :: finufft
172 real(real64),
intent(in) :: tol
182 subroutine finufft_init(finufft, finufft_options, N, dim, M, nthreads)
183 type(
finufft_t),
intent(inout) :: finufft
184 type(
finufft_t),
intent(in) :: finufft_options
185 integer,
intent(in) :: n(3)
186 integer,
intent(in) :: dim
187 integer,
intent(in) :: m(3)
188 integer,
intent(in),
optional :: nthreads
191 integer(c_int64_t) :: n_modes(3)
192 integer(c_int) :: ier
193 type(c_ptr) :: finufft_opts
194 integer(c_int) :: n_threads
205 finufft%N(dim+1:3) = 1
206 finufft%M(dim+1:3) = 1
208 finufft%tol = finufft_options%tol
211 n_modes(1:3) = int(finufft%N(1:3), c_int64_t)
216 finufft_opts = oct_finufft_opts_init(n_threads)
217 if (.not. c_associated(finufft_opts))
then
218 message(1) =
"Could not allocate the FINUFFT options."
222 ier = finufft_makeplan(int(finufft_type2, c_int), int(dim, c_int), n_modes, &
223 int(finufft_iflag, c_int), 1_c_int, real(finufft%tol, c_double), finufft%plan, finufft_opts)
224 call finufft_check(ier,
'finufft_makeplan')
226 call oct_finufft_opts_end(finufft_opts)
241 type(
finufft_t),
intent(inout) :: finufft
242 real(real64),
contiguous,
intent(in) :: x1(:)
243 real(real64),
contiguous,
optional,
intent(in) :: x2(:)
244 real(real64),
contiguous,
optional,
intent(in) :: x3(:)
246 real(real64),
optional,
intent(in) :: period(:)
248 real(real64) :: v1(1:finufft%m(1)), v2(1:finufft%m(2)), v3(1:finufft%m(3))
249 real(real64) :: inv_spacing, period_(3)
250 integer(c_int64_t) :: nj
251 integer :: i1, i2, i3, jj
253 integer(c_int) :: ier
254 type(c_ptr) :: yj_ptr, zj_ptr
264 if (
present(period)) period_(1:3) = period(1:3)
266 assert(
size(x1) == finufft%M(1))
268 finufft%norm = finufft%norm * inv_spacing
271 if (finufft%dim >= 2)
then
273 assert(
size(x2) == finufft%M(2))
275 finufft%norm = finufft%norm * inv_spacing
279 if (finufft%dim >= 3)
then
281 assert(
size(x3) == finufft%M(3))
283 finufft%norm = finufft%norm * inv_spacing
286 nj = int(finufft%M(1), c_int64_t)*int(finufft%M(2), c_int64_t)*int(finufft%M(3), c_int64_t)
288 safe_deallocate_p(finufft%xj)
289 safe_deallocate_p(finufft%yj)
290 safe_deallocate_p(finufft%zj)
291 safe_allocate(finufft%xj(1:nj))
292 if (finufft%dim >= 2)
then
293 safe_allocate(finufft%yj(1:nj))
295 if (finufft%dim >= 3)
then
296 safe_allocate(finufft%zj(1:nj))
301 do i3 = 1, finufft%M(3)
302 do i2 = 1, finufft%M(2)
303 do i1 = 1, finufft%M(1)
306 if (finufft%dim >= 2) finufft%yj(jj) =
m_two*
m_pi*v2(i2)
307 if (finufft%dim >= 3) finufft%zj(jj) =
m_two*
m_pi*v3(i3)
315 if (finufft%dim >= 2) yj_ptr = c_loc(finufft%yj)
316 if (finufft%dim >= 3) zj_ptr = c_loc(finufft%zj)
319 ier = finufft_setpts(finufft%plan, nj, c_loc(finufft%xj), yj_ptr, zj_ptr, &
320 0_c_int64_t, c_null_ptr, c_null_ptr, c_null_ptr)
321 call finufft_check(ier,
'finufft_setpts')
326 write(
message(1),
'(a)')
"Info: FINUFFT plan precomputed."
337 type(
finufft_t),
intent(inout) :: finufft
338 real(real64),
contiguous,
intent(in) :: xj(:)
339 real(real64),
contiguous,
intent(in) :: yj(:)
340 real(real64),
contiguous,
intent(in) :: zj(:)
341 real(real64),
contiguous,
intent(in) :: period(:)
343 integer(c_int64_t) :: nj
345 integer(c_int) :: ier
352 assert(finufft%dim == 3)
354 nj = int(finufft%M(1), c_int64_t)
355 assert(
size(xj) == nj)
356 assert(
size(yj) == nj)
357 assert(
size(zj) == nj)
361 safe_deallocate_p(finufft%xj)
362 safe_deallocate_p(finufft%yj)
363 safe_deallocate_p(finufft%zj)
364 safe_allocate(finufft%xj(1:nj))
365 safe_allocate(finufft%yj(1:nj))
366 safe_allocate(finufft%zj(1:nj))
373 ier = finufft_setpts(finufft%plan, nj, c_loc(finufft%xj), c_loc(finufft%yj), c_loc(finufft%zj), &
374 0_c_int64_t, c_null_ptr, c_null_ptr, c_null_ptr)
375 call finufft_check(ier,
'finufft_setpts')
387 complex(real64),
contiguous,
intent(in) :: modes(:,:,:)
388 complex(real64),
contiguous,
intent(out) :: values(:)
391 integer(c_int) :: ier
392 logical :: halting(size(ieee_all))
400 call finufft_halting_suspend(halting)
401 ier = finufft_execute(finufft%plan, values, modes)
402 call finufft_halting_restore(halting)
403 call finufft_check(ier,
'finufft_execute')
417 complex(real64),
contiguous,
intent(in) :: values(:)
418 complex(real64),
contiguous,
intent(out) :: modes(:,:,:)
421 integer(c_int) :: ier
422 logical :: halting(size(ieee_all))
430 call finufft_halting_suspend(halting)
431 ier = finufft_execute_adjoint(finufft%plan, values, modes)
432 call finufft_halting_restore(halting)
433 call finufft_check(ier,
'finufft_execute_adjoint')
449 real(real64),
contiguous,
intent(in) :: X(:)
450 real(real64),
contiguous,
intent(out) :: v(:)
451 real(real64),
intent(out) :: inv_spacing
452 real(real64),
intent(in) :: period
454 real(real64) :: length, cc
455 real(real64) :: spacing(size(v) - 1)
460 assert(
size(x) ==
size(v))
465 inv_spacing = real(
size(v), real64)
469 cc = (minval(x) + maxval(x))/
m_two
472 spacing = [(v(i+1) - v(i), i = 1,
size(v)-1)]
473 inv_spacing =
m_one/minval(abs(spacing))
491 do idir = 1, finufft%dim
498 do idir = 1, finufft%dim
515 type(
finufft_t),
intent(inout) :: finufft
518 integer(c_int) :: ier
524 if (c_associated(finufft%plan))
then
525 ier = finufft_destroy(finufft%plan)
526 call finufft_check(ier,
'finufft_destroy')
527 finufft%plan = c_null_ptr
531 safe_deallocate_p(finufft%xj)
532 safe_deallocate_p(finufft%yj)
533 safe_deallocate_p(finufft%zj)
541 subroutine finufft_check(ier, routine)
542 integer(c_int),
intent(in) :: ier
543 character(*),
intent(in) :: routine
547 write(
message(1),
'(3a,i0,a)')
"FINUFFT routine ", trim(routine), &
548 " failed with error code ", ier,
"."
550 end subroutine finufft_check
558 subroutine finufft_halting_suspend(halting)
559 logical,
intent(out) :: halting(:)
562 call ieee_get_halting_mode(ieee_all, halting)
564 call ieee_set_halting_mode(ieee_all, .false.)
567 end subroutine finufft_halting_suspend
571 subroutine finufft_halting_restore(halting)
572 logical,
intent(in) :: halting(:)
576 call ieee_set_halting_mode(ieee_all, halting)
579 end subroutine finufft_halting_restore
584#include "finufft_inc.F90"
587#include "complex.F90"
588#include "finufft_inc.F90"
Non-equispaced FFTs through the C interface of the FINUFFT library.
subroutine, public finufft_write_info(finufft)
subroutine, public finufft_set_nodes(finufft, xj, yj, zj, period)
Hand scattered nodes to FINUFFT, mapping one period of the mode grid onto the [-1/2,...
subroutine, public zfinufft_forward(finufft, in, out)
Evaluate the Fourier modes on the non-equispaced nodes.
subroutine, public zfinufft_backward(finufft, in, out)
Project the non-equispaced nodes back onto the Fourier modes.
subroutine, public finufft_precompute(finufft, X1, X2, X3, period)
Hand the spatial nodes to FINUFFT.
subroutine, public finufft_read_options(finufft, namespace)
subroutine, public finufft_init(finufft, finufft_options, N, dim, M, nthreads)
subroutine, public finufft_end(finufft)
subroutine, public finufft_nodes_to_modes(finufft, values, modes)
Project the scattered nodes onto the Fourier modes.
subroutine, public finufft_modes_to_nodes(finufft, modes, values)
Evaluate the Fourier modes on the scattered nodes.
subroutine finufft_scale_nodes(X, v, inv_spacing, period)
Map the nodes onto [-1/2, 1/2) and return the reciprocal of the smallest scaled spacing....
subroutine, public dfinufft_backward(finufft, in, out)
Project the non-equispaced nodes back onto the Fourier modes.
subroutine, public dfinufft_forward(finufft, in, out)
Evaluate the Fourier modes on the non-equispaced nodes.
subroutine, public finufft_set_tolerance(finufft, tol)
Set the requested tolerance directly, for callers that take it from an input variable of their own ra...
real(real64), parameter, public m_two
real(real64), parameter, public m_zero
real(real64), parameter, public m_pi
some mathematical constants
complex(real64), parameter, public m_z0
real(real64), parameter, public m_epsilon
real(real64), parameter, public m_one
subroutine, public messages_new_line()
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
subroutine, public messages_input_error(namespace, var, details, row, column)
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.