/
fft-inc.f90
628 lines (492 loc) · 16.7 KB
/
fft-inc.f90
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#if (PREC==2)
#define RP DRP
#define CP DCP
#define fftw_execute_complex fftw_execute_dft
#define fftw_mpi_execute_complex fftw_mpi_execute_dft
#define fftw_execute_real fftw_execute_r2r
#define pfft_execute_gen pfft_execute
#else
#define RP SRP
#define CP SCP
#define fftw_execute_complex fftwf_execute_dft
#define fftw_mpi_execute_complex fftwf_mpi_execute_dft
#define fftw_execute_real fftwf_execute_r2r
#define pfft_execute_gen pfftf_execute
#endif
use iso_c_binding
use PoisFFT_Precisions
use fftw3
#ifdef MPI
use pfft
#endif
implicit none
integer, parameter :: FFT_Complex = 0
integer, parameter :: FFT_RealEven00 = FFTW_REDFT00
integer, parameter :: FFT_RealEven01 = FFTW_REDFT01
integer, parameter :: FFT_RealEven10 = FFTW_REDFT10
integer, parameter :: FFT_RealEven11 = FFTW_REDFT11
integer, parameter :: FFT_RealOdd00 = FFTW_RODFT00
integer, parameter :: FFT_RealOdd01 = FFTW_RODFT01
integer, parameter :: FFT_RealOdd10 = FFTW_RODFT10
integer, parameter :: FFT_RealOdd11 = FFTW_RODFT11
integer, parameter :: FFT_DISTRIBUTED_FFTW = 1
integer, parameter :: FFT_DISTRIBUTED_PFFT = 2
type PoisFFT_Plan1D
type(c_ptr) :: planptr = c_null_ptr
logical :: planowner = .false.
logical :: distributed = .false.
integer(c_int) :: dir
end type PoisFFT_Plan1D
type PoisFFT_Plan2D
type(c_ptr) :: planptr = c_null_ptr
logical :: planowner = .false.
logical :: distributed = .false.
integer :: method = FFT_DISTRIBUTED_PFFT
integer(c_int) :: dir
end type PoisFFT_Plan2D
type PoisFFT_Plan3D
type(c_ptr) :: planptr = c_null_ptr
logical :: planowner = .false.
logical :: distributed = .false.
integer(c_int) :: dir
end type PoisFFT_Plan3D
type mpi_vars_1d
!MPI communicator for the exchange
integer :: comm = -1
!our rank in comm
integer :: rank
!number of processes in comm
integer :: np
! s for dimensions of send buffers, r for receive buffers
! nx, nz are the dimensions of individual parts which will be sent to or received from other processes in comm
! displs are the displacements (offsets) of individual pieces in the whole 1D buffer
! counts are the the number of elements in each piece
! sumrnzs(i) is the sum of rnzs in pieces 1..i-1
integer,dimension(:),allocatable :: snxs, snzs, &
rnxs, rnzs, &
sdispls, scounts, &
rdispls, rcounts, &
sumrnzs
!contiguous 3D buffer for locally transposed RHS
real(RP), allocatable :: tmp1(:,:,:)
!1D buffer for globally transposed blocks of tmp1 from different processes after MPI_Alltoallv
real(RP), allocatable :: tmp2(:)
!3D buffer for contiguous 1D rows on which 1D FFT can be performed locally,
! constructed by local reordering of tmp2
real(RP), allocatable :: rwork(:,:,:)
! global indexes corresponding to the given y or z line in the rwork array.
integer, allocatable :: glob_i(:,:) ! value of global i, (:,:) local indexes of rwork
end type
type PoisFFT_Solver1D
real(RP) :: Lx
integer(c_int) :: nxyz(1)
integer(c_int) :: nx
integer(c_int) :: gnx
integer(c_int) :: offx = 0 !offset from global index
integer(c_size_t) :: cnt
integer(c_size_t) :: gcnt
real(RP) :: norm_factor
integer(c_int), dimension(2) :: BCs
real(RP), allocatable, dimension(:) :: denomx
integer :: approximation = 0
type(PoisFFT_Plan1D) :: forward, backward
complex(CP), dimension(:), &
#ifndef NO_CONTIGUOUS
contiguous, &
#endif
pointer :: cwork => null()
real(RP), dimension(:), &
#ifndef NO_CONTIGUOUS
contiguous, &
#endif
pointer :: rwork => null()
logical :: mpi_transpose_needed = .false.
integer :: nthreads = 1
type(mpi_vars_1D) :: mpi
end type PoisFFT_Solver1D
type mpi_vars_2d
integer :: rank
integer :: np
integer :: comm = -1
integer :: comm_dim = 2
end type
type PoisFFT_Solver2D
real(RP) :: Lx, Ly
integer(c_int) :: nxyz(2)
integer(c_int) :: nx, ny
integer(c_int) :: gnx, gny
integer(c_int) :: offx = 0, offy = 0 !offsets from global indexes
integer(c_size_t) :: cnt
integer(c_size_t) :: gcnt
real(RP) :: norm_factor
integer(c_int), dimension(4) :: BCs
real(RP), allocatable, dimension(:) :: denomx, denomy
integer :: approximation = 0
type(PoisFFT_Plan2D) :: forward, backward
complex(CP), dimension(:,:), &
#ifndef NO_CONTIGUOUS
contiguous, &
#endif
pointer :: cwork => null()
real(RP), dimension(:,:), &
#ifndef NO_CONTIGUOUS
contiguous, &
#endif
pointer :: rwork => null()
integer :: nthreads = 1
type(mpi_vars_2D) :: mpi
end type PoisFFT_Solver2D
type mpi_vars_3D
integer :: comm = -1
end type
type PoisFFT_Solver3D
real(RP) :: Lx, Ly, Lz
integer(c_int) :: nxyz(3)
integer(c_int) :: nx, ny, nz
integer(c_int) :: gnx, gny, gnz
integer(c_int) :: offx = 0, offy = 0, offz = 0 !offsets from global indexes
integer(c_size_t) :: cnt
integer(c_size_t) :: gcnt
real(RP) :: norm_factor
integer(c_int), dimension(6) :: BCs
real(RP), allocatable, dimension(:) :: denomx, denomy, denomz
integer :: approximation = 0
type(PoisFFT_Plan3D) :: forward, backward
complex(CP), dimension(:,:,:), &
#ifndef NO_CONTIGUOUS
contiguous, &
#endif
pointer :: cwork => null()
real(RP), dimension(:,:,:), &
#ifndef NO_CONTIGUOUS
contiguous, &
#endif
pointer :: rwork => null()
integer :: nthreads = 1
!will be used in splitting for some boundary conditions
type(PoisFFT_Solver1D),dimension(:),allocatable :: Solvers1D
type(PoisFFT_Solver2D),dimension(:),allocatable :: Solvers2D
type(mpi_vars_3D) :: mpi
end type PoisFFT_Solver3D
type, extends(PoisFFT_Solver3D) :: PoisFFT_Solver3D_nonuniform_Z
real(RP) :: z_start, z_end
real(RP), allocatable :: z(:), z_u(:)
real(RP), allocatable :: mat_a(:), mat_b(:), mat_c(:) !tridiagonal matrix elements, variable in z
end type
interface deallocate_fftw
module procedure deallocate_fftw_1D_complex
module procedure deallocate_fftw_1D_real
module procedure deallocate_fftw_2D_complex
module procedure deallocate_fftw_2D_real
module procedure deallocate_fftw_3D_complex
module procedure deallocate_fftw_3D_real
end interface
interface allocate_fftw_real
module procedure allocate_fftw_1D_real
module procedure allocate_fftw_2D_real
module procedure allocate_fftw_3D_real
end interface
interface allocate_fftw_complex
module procedure allocate_fftw_1D_complex
module procedure allocate_fftw_2D_complex
module procedure allocate_fftw_3D_complex
end interface
interface Execute
module procedure PoisFFT_Plan1D_Execute_Real
module procedure PoisFFT_Plan1D_Execute_Complex
module procedure PoisFFT_Plan2D_Execute_Real
module procedure PoisFFT_Plan2D_Execute_Complex
module procedure PoisFFT_Plan3D_Execute_Real
module procedure PoisFFT_Plan3D_Execute_Complex
end interface
#ifdef MPI
interface Execute_MPI
module procedure PoisFFT_Plan1D_Execute_MPI
module procedure PoisFFT_Plan2D_Execute_MPI
module procedure PoisFFT_Plan3D_Execute_MPI
end interface
#endif
interface Finalize
module procedure PoisFFT_Plan1D_Finalize
module procedure PoisFFT_Plan2D_Finalize
module procedure PoisFFT_Plan3D_Finalize
end interface
interface PoisFFT_Plan1D
module procedure PoisFFT_Plan1D_New
end interface
interface PoisFFT_Plan2D
module procedure PoisFFT_Plan2D_New
end interface
interface PoisFFT_Plan3D
module procedure PoisFFT_Plan3D_New
end interface
contains
function PoisFFT_Plan1D_New(D, plantypes, distributed) result(plan)
#define dimensions 1
#include "plan_new-inc.f90"
#undef dimensions
end function
function PoisFFT_Plan2D_New(D, plantypes, distributed) result(plan)
#define dimensions 2
#include "plan_new-inc.f90"
#undef dimensions
end function
function PoisFFT_Plan3D_New(D, plantypes, distributed) result(plan)
#define dimensions 3
#include "plan_new-inc.f90"
#undef dimensions
end function
subroutine allocate_fftw_1D_complex(D)
#define dimensions 1
#define realcomplex 2
#include "allocate_fftw-inc.f90"
#undef dimensions
#undef realcomplex
end subroutine
subroutine allocate_fftw_1D_real(D)
#define dimensions 1
#define realcomplex 1
#include "allocate_fftw-inc.f90"
#undef dimensions
#undef realcomplex
end subroutine
subroutine allocate_fftw_2D_complex(D)
#define dimensions 2
#define realcomplex 2
#include "allocate_fftw-inc.f90"
#undef dimensions
#undef realcomplex
end subroutine
subroutine allocate_fftw_2D_real(D)
#define dimensions 2
#define realcomplex 1
#include "allocate_fftw-inc.f90"
#undef dimensions
#undef realcomplex
end subroutine
subroutine allocate_fftw_3D_complex(D)
#define dimensions 3
#define realcomplex 2
#include "allocate_fftw-inc.f90"
#undef dimensions
#undef realcomplex
end subroutine
subroutine allocate_fftw_3D_real(D)
#define dimensions 3
#define realcomplex 1
#include "allocate_fftw-inc.f90"
#undef dimensions
#undef realcomplex
end subroutine
subroutine PoisFFT_Plan1D_Execute_Complex(plan, data)
type(PoisFFT_Plan1D), intent(in) :: plan
complex(CP), dimension(:) &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
call fftw_execute_complex(plan%planptr, data, data)
end subroutine
subroutine PoisFFT_Plan1D_Execute_Real(plan, data)
type(PoisFFT_Plan1D), intent(in) :: plan
real(RP), dimension(:) &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
call fftw_execute_real(plan%planptr, data, data)
end subroutine
subroutine PoisFFT_Plan2D_Execute_Complex(plan, data)
type(PoisFFT_Plan2D), intent(in) :: plan
complex(CP), dimension(:,:) &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
call fftw_execute_complex(plan%planptr, data, data)
end subroutine
subroutine PoisFFT_Plan2D_Execute_Real(plan, data)
type(PoisFFT_Plan2D), intent(in) :: plan
real(RP), dimension(:,:) &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
call fftw_execute_real(plan%planptr, data, data)
end subroutine
subroutine PoisFFT_Plan3D_Execute_Complex(plan, data)
type(PoisFFT_Plan3D), intent(in) :: plan
complex(CP), dimension(:,:,:) &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
call fftw_execute_complex(plan%planptr, data, data)
end subroutine
subroutine PoisFFT_Plan3D_Execute_Real(plan, data)
type(PoisFFT_Plan3D), intent(in) :: plan
real(RP), dimension(:,:,:) &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
call fftw_execute_real(plan%planptr, data, data)
end subroutine
#ifdef MPI
subroutine PoisFFT_Plan1D_Execute_MPI(plan)
type(PoisFFT_Plan1D), intent(in) :: plan
call pfft_execute_gen(plan%planptr)
end subroutine
subroutine PoisFFT_Plan2D_Execute_MPI(plan, data)
type(PoisFFT_Plan2D), intent(in) :: plan
complex(CP), dimension(:,:), optional &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
if (plan%method==FFT_DISTRIBUTED_FFTW) then
if (present(data)) then
call fftw_mpi_execute_complex(plan%planptr, data, data)
else
stop "Error, missing `data` argument in a call to Execute_MPI with FFTW."
end if
else
call pfft_execute_gen(plan%planptr)
end if
end subroutine
subroutine PoisFFT_Plan3D_Execute_MPI(plan)
type(PoisFFT_Plan3D), intent(in) :: plan
call pfft_execute_gen(plan%planptr)
end subroutine
#endif
subroutine deallocate_fftw_1D_complex(data)
complex(CP), dimension(:), pointer &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
type(c_ptr) :: p
p = c_loc(data(1))
call fftw_free(p)
nullify(data)
end subroutine deallocate_fftw_1D_complex
subroutine deallocate_fftw_1D_real(data)
real(RP), dimension(:), pointer &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
type(c_ptr) :: p
p = c_loc(data(1))
call fftw_free(p)
nullify(data)
end subroutine deallocate_fftw_1D_real
subroutine deallocate_fftw_2D_complex(data)
complex(CP), dimension(:,:), pointer &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
type(c_ptr) :: p
p = c_loc(data(1,1))
call fftw_free(p)
nullify(data)
end subroutine deallocate_fftw_2D_complex
subroutine deallocate_fftw_2D_real(data)
real(RP), dimension(:,:), pointer &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
type(c_ptr) :: p
p = c_loc(data(1,1))
call fftw_free(p)
nullify(data)
end subroutine deallocate_fftw_2D_real
subroutine deallocate_fftw_3D_complex(data)
complex(CP), dimension(:,:,:), pointer &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
type(c_ptr) :: p
p = c_loc(data(1,1,1))
call fftw_free(p)
nullify(data)
end subroutine deallocate_fftw_3D_complex
subroutine deallocate_fftw_3D_real(data)
real(RP), dimension(:,:,:), pointer &
#ifndef NO_CONTIGUOUS
, contiguous &
#endif
:: data
type(c_ptr) :: p
p = c_loc(data(1,1,1))
call fftw_free(p)
nullify(data)
end subroutine deallocate_fftw_3D_real
subroutine PoisFFT_Plan1D_Finalize(plan)
type(PoisFFT_Plan1D) :: plan
if (c_associated(plan%planptr).and.plan%planowner) &
call fftw_destroy_plan(plan%planptr)
plan%planptr = c_null_ptr
end subroutine PoisFFT_Plan1D_Finalize
subroutine PoisFFT_Plan2D_Finalize(plan)
type(PoisFFT_Plan2D) :: plan
if (c_associated(plan%planptr).and.plan%planowner) then
#ifdef MPI
if (plan%distributed.and.plan%method==FFT_DISTRIBUTED_PFFT) then
call pfft_destroy_plan(plan%planptr)
else
call fftw_destroy_plan(plan%planptr)
end if
#else
call fftw_destroy_plan(plan%planptr)
#endif
end if
plan%planptr = c_null_ptr
end subroutine PoisFFT_Plan2D_Finalize
subroutine PoisFFT_Plan3D_Finalize(plan)
type(PoisFFT_Plan3D) :: plan
if (c_associated(plan%planptr).and.plan%planowner) then
#ifdef MPI
if (plan%distributed) then
call pfft_destroy_plan(plan%planptr)
else
call fftw_destroy_plan(plan%planptr)
end if
#else
call fftw_destroy_plan(plan%planptr)
#endif
end if
plan%planptr = c_null_ptr
end subroutine PoisFFT_Plan3D_Finalize
subroutine PoisFFT_InitThreads(nthreads) !instructs fftw to plan to use nthreads threads
integer, intent(in) :: nthreads
#if defined(_OPENMP) || defined(ENABLE_PTHREADS)
integer(c_int) :: error
error = fftw_init_threads()
if (error==0) then
write(*,*) "Error when initializing FFTW for threads."
else
call fftw_plan_with_nthreads(int(nthreads,c_int))
end if
#endif
end subroutine PoisFFT_InitThreads
#ifdef MPI
subroutine PoisFFT_PFFT_Init
call pfft_init()
end subroutine
subroutine PoisFFT_PFFT_InitThreads(nthreads) !instructs PFFT and fftw to plan to use nthreads threads
integer, intent(in) :: nthreads
#if defined(_OPENMP) || defined(ENABLE_PTHREADS)
call pfft_plan_with_nthreads(int(nthreads,c_int))
#endif
end subroutine PoisFFT_PFFT_InitThreads
#endif
#undef RP
#undef CP
#undef fftw_execute_complex
#undef fftw_execute_real
#undef pfft_execute_gen
#undef fftw_mpi_execute_complex