forked from RudolfWeeber/espresso-virtual-sites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft.c
1532 lines (1353 loc) · 52.1 KB
/
fft.c
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the ESPResSo distribution (http://www.espresso.mpg.de).
// It is therefore subject to the ESPResSo license agreement which you accepted upon receiving the distribution
// and by which you are legally bound while utilizing this file in any form or way.
// There is NO WARRANTY, not even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// You should have received a copy of that license along with this program;
// if not, refer to http://www.espresso.mpg.de/license.html where its current version can be found, or
// write to Max-Planck-Institute for Polymer Research, Theory Group, PO Box 3148, 55021 Mainz, Germany.
// Copyright (c) 2002-2009; all rights reserved unless otherwise stated.
/** \file fft.c
*
* Routines, row decomposition, data structures and communication for the 3D-FFT.
*
* For more information about FFT usage, see \ref fft.h "fft.h".
*/
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "utils.h"
#ifdef ELP3M
#if FFTW == 3
# include <fftw3.h>
#else
# include <fftw.h>
# include <rfftw.h>
#endif
#include "communication.h"
#include "grid.h"
#ifdef NPT
#include "pressure.h"
#endif
#include "fft.h"
#include "p3m.h"
/************************************************
* DEFINES
************************************************/
/* MPI tags for the fft communications: */
/** Tag for communication in fft_init() */
#define REQ_FFT_INIT 300
/** Tag for communication in forw_grid_comm() */
#define REQ_FFT_FORW 301
/** Tag for communication in back_grid_comm() */
#define REQ_FFT_BACK 302
#if FFTW == 3
/* Tag for wisdom file I/O */
# define FFTW_FAILURE 0
#endif
/************************************************
* variables
************************************************/
#ifdef ELECTROSTATICS
int fft_init_tag=0;
fft_forw_plan fft_plan[4];
/** Information for Back FFTs (see fft_plan). */
fft_back_plan fft_back[4];
/** Maximal size of the communication buffers. */
static int max_comm_size=0;
/** Maximal local mesh size. */
static int max_mesh_size=0;
/** send buffer. */
static double *send_buf = NULL;
/** receive buffer. */
static double *recv_buf = NULL;
/** Buffer for receive data. */
static double *data_buf = NULL;
/** Complex data pointers. */
static fftw_complex *c_data;
static fftw_complex *c_data_buf;
#endif
#ifdef MAGNETOSTATICS
int Dfft_init_tag=0;
fft_forw_plan Dfft_plan[4];
/** Information for Back FFTs (see fft_plan). */
fft_back_plan Dfft_back[4];
static int Dmax_comm_size=0;
/** Maximal local mesh size. */
static int Dmax_mesh_size=0;
/** send buffer. */
static double *Dsend_buf = NULL;
/** receive buffer. */
static double *Drecv_buf = NULL;
/** Buffer for receive data. */
static double *Ddata_buf = NULL;
/** Complex data pointers. */
static fftw_complex *Dc_data;
static fftw_complex *Dc_data_buf;
#endif
/** \name Privat Functions */
/************************************************************/
/*@{*/
/** This ugly function does the bookkepping which nodes have to
* communicate to each other, when you change the node grid.
* Changing the domain decomposition requieres communication. This
* function finds (hopefully) the best way to do this. As input it
* needs the two grids (grid1, grid2) and a linear list (node_list1)
* with the node identities for grid1. The linear list (node_list2)
* for the second grid is calculated. For the communication group of
* the calling node it calculates a list (group) with the node
* identities and the positions (pos1, pos2) of that nodes in grid1
* and grid2. The return value is the size of the communication
* group. It gives -1 if the two grids do not fit to each other
* (grid1 and grid2 have to be component wise multiples of each
* other. see e.g. \ref calc_2d_grid in \ref grid.c for how to do
* this.).
*
* \param grid1 The node grid you start with (Input).
* \param grid2 The node grid you want to have (Input).
* \param node_list1 Linear node index list for grid1 (Input).
* \param node_list2 Linear node index list for grid2 (Output).
* \param group communication group (node identity list) for the calling node (Output).
* \param pos positions of the nodes in in grid2 (Output).
* \param my_pos position of this_node in grid2.
* \return Size of the communication group (Output of course!). */
int find_comm_groups(int grid1[3], int grid2[3], int *node_list1, int *node_list2,
int *group, int *pos, int *my_pos);
/** Calculate the local fft mesh. Calculate the local mesh (loc_mesh)
* of a node at position (n_pos) in a node grid (n_grid) for a global
* mesh of size (mesh) and a mesh offset (mesh_off (in mesh units))
* and store also the first point (start) of the local mesh.
*
* \return size number of mesh points in local mesh.
* \param n_pos Position of the node in n_grid.
* \param n_grid node grid.
* \param mesh global mesh dimensions.
* \param mesh_off global mesh offset (see \ref p3m_struct).
* \param loc_mesh local mesh dimension (output).
* \param start first point of local mesh in global mesh (output).
*/
int calc_local_mesh(int n_pos[3], int n_grid[3], int mesh[3], double mesh_off[3],
int loc_mesh[3], int start[3]);
/** Calculate a send (or recv.) block for grid communication during a
* decomposition change. Calculate the send block specification
* (block = lower left corner and upper right corner) which a node at
* position (pos1) in the actual node grid (grid1) has to send to
* another node at position (pos2) in the desired node grid
* (grid2). The global mesh, subject to communication, is specified
* via its size (mesh) and its mesh offset (mesh_off (in mesh
* units)).
*
* For the calculation of a receive block you have to change the arguments in the following way: <br>
* pos1 - position of receiving node in the desired node grid. <br>
* grid1 - desired node grid. <br>
* pos2 - position of the node you intend to receive the data from in the actual node grid. <br>
* grid2 - actual node grid. <br>
*
* \return size of the send block.
* \param pos1 Position of send node in grid1.
* \param grid1 node grid 1.
* \param pos2 Position of recv node in grid2.
* \param grid2 node grid 2.
* \param mesh global mesh dimensions.
* \param mesh_off global mesh offset (see \ref p3m_struct).
* \param block send block specification.
*/
int calc_send_block(int pos1[3], int grid1[3], int pos2[3], int grid2[3],
int mesh[3], double mesh_off[3], int block[6]);
#ifdef ELECTROSTATICS
/** communicate the grid data according to the given fft_forw_plan.
* \param plan communication plan (see \ref fft_forw_plan).
* \param in input mesh.
* \param out output mesh.
*/
void forw_grid_comm(fft_forw_plan plan, double *in, double *out);
/** communicate the grid data according to the given fft_forw_plan/fft_bakc_plan.
* \param plan_f communication plan (see \ref fft_forw_plan).
* \param plan_b additional back plan (see \ref fft_back_plan).
* \param in input mesh.
* \param out output mesh.
*/
void back_grid_comm(fft_forw_plan plan_f, fft_back_plan plan_b, double *in, double *out);
#endif
#ifdef MAGNETOSTATICS
/** communicate the grid data according to the given fft_forw_plan.
* \param plan communication plan (see \ref fft_forw_plan).
* \param in input mesh.
* \param out output mesh.
*/
void Dforw_grid_comm(fft_forw_plan plan, double *in, double *out);
/** communicate the grid data according to the given fft_forw_plan/fft_bakc_plan.
* \param plan_f communication plan (see \ref fft_forw_plan).
* \param plan_b additional back plan (see \ref fft_back_plan).
* \param in input mesh.
* \param out output mesh.
*/
void Dback_grid_comm(fft_forw_plan plan_f, fft_back_plan plan_b, double *in, double *out);
#endif
/** Debug function to print fft_forw_plan structure.
* \param pl fft/communication plan (see \ref fft_forw_plan).
*/
void print_fft_plan(fft_forw_plan pl);
/*@}*/
/************************************************************/
void fft_pre_init()
{
#if defined(ELECTROSTATICS) || defined(MAGNETOSTATICS)
int i;
#endif
#ifdef ELECTROSTATICS
for(i=0;i<4;i++) {
fft_plan[i].group = malloc(1*n_nodes*sizeof(int));
fft_plan[i].send_block = NULL;
fft_plan[i].send_size = NULL;
fft_plan[i].recv_block = NULL;
fft_plan[i].recv_size = NULL;
}
#endif
#ifdef MAGNETOSTATICS
for(i=0;i<4;i++) {
Dfft_plan[i].group = malloc(1*n_nodes*sizeof(int));
Dfft_plan[i].send_block = NULL;
Dfft_plan[i].send_size = NULL;
Dfft_plan[i].recv_block = NULL;
Dfft_plan[i].recv_size = NULL;
}
#endif
}
#ifdef ELECTROSTATICS
int fft_init(double **data, int *ca_mesh_dim, int *ca_mesh_margin, int *ks_pnum)
{
int i,j;
/* helpers */
int mult[3];
int n_grid[4][3]; /* The four node grids. */
int my_pos[4][3]; /* The position of this_node in the node grids. */
int *n_id[4]; /* linear node identity lists for the node grids. */
int *n_pos[4]; /* positions of nodes in the node grids. */
/* FFTW WISDOM stuff. */
char wisdom_file_name[255];
FILE *wisdom_file;
#if FFTW == 3
int wisdom_status;
#else
fftw_status wisdom_status;
#endif
FFT_TRACE(fprintf(stderr,"%d: fft_init():\n",this_node));
max_comm_size=0; max_mesh_size=0;
for(i=0;i<4;i++) {
n_id[i] = malloc(1*n_nodes*sizeof(int));
n_pos[i] = malloc(3*n_nodes*sizeof(int));
}
/* === node grids === */
/* real space node grid (n_grid[0]) */
for(i=0;i<3;i++) {
n_grid[0][i] = node_grid[i];
my_pos[0][i] = node_pos[i];
}
for(i=0;i<n_nodes;i++) {
n_id[0][i] = i;
get_grid_pos(i,&(n_pos[0][3*i+0]),&(n_pos[0][3*i+1]),&(n_pos[0][3*i+2]),
n_grid[0]);
}
/* FFT node grids (n_grid[1 - 3]) */
calc_2d_grid(n_nodes,n_grid[1]);
/* resort n_grid[1] dimensions if necessary */
fft_plan[1].row_dir = map_3don2d_grid(n_grid[0], n_grid[1], mult);
fft_plan[0].n_permute = 0;
for(i=1;i<4;i++) fft_plan[i].n_permute = (fft_plan[1].row_dir+i)%3;
for(i=0;i<3;i++) {
n_grid[2][i] = n_grid[1][(i+1)%3];
n_grid[3][i] = n_grid[1][(i+2)%3];
}
fft_plan[2].row_dir = (fft_plan[1].row_dir-1)%3;
fft_plan[3].row_dir = (fft_plan[1].row_dir-2)%3;
/* === communication groups === */
/* copy local mesh off real space charge assignment grid */
for(i=0;i<3;i++) fft_plan[0].new_mesh[i] = ca_mesh_dim[i];
for(i=1; i<4;i++) {
fft_plan[i].g_size=find_comm_groups(n_grid[i-1], n_grid[i], n_id[i-1], n_id[i],
fft_plan[i].group, n_pos[i], my_pos[i]);
if(fft_plan[i].g_size==-1) {
/* try permutation */
j = n_grid[i][(fft_plan[i].row_dir+1)%3];
n_grid[i][(fft_plan[i].row_dir+1)%3] = n_grid[i][(fft_plan[i].row_dir+2)%3];
n_grid[i][(fft_plan[i].row_dir+2)%3] = j;
fft_plan[i].g_size=find_comm_groups(n_grid[i-1], n_grid[i], n_id[i-1], n_id[i],
fft_plan[i].group, n_pos[i], my_pos[i]);
if(fft_plan[i].g_size==-1) {
fprintf(stderr,"%d: INTERNAL ERROR: find_comm_groups error\n", this_node);
errexit();
}
}
fft_plan[i].send_block = (int *)realloc(fft_plan[i].send_block, 6*fft_plan[i].g_size*sizeof(int));
fft_plan[i].send_size = (int *)realloc(fft_plan[i].send_size, 1*fft_plan[i].g_size*sizeof(int));
fft_plan[i].recv_block = (int *)realloc(fft_plan[i].recv_block, 6*fft_plan[i].g_size*sizeof(int));
fft_plan[i].recv_size = (int *)realloc(fft_plan[i].recv_size, 1*fft_plan[i].g_size*sizeof(int));
fft_plan[i].new_size = calc_local_mesh(my_pos[i], n_grid[i], p3m.mesh,
p3m.mesh_off, fft_plan[i].new_mesh,
fft_plan[i].start);
permute_ifield(fft_plan[i].new_mesh,3,-(fft_plan[i].n_permute));
permute_ifield(fft_plan[i].start,3,-(fft_plan[i].n_permute));
fft_plan[i].n_ffts = fft_plan[i].new_mesh[0]*fft_plan[i].new_mesh[1];
/* === send/recv block specifications === */
for(j=0; j<fft_plan[i].g_size; j++) {
int k, node;
/* send block: this_node to comm-group-node i (identity: node) */
node = fft_plan[i].group[j];
fft_plan[i].send_size[j]
= calc_send_block(my_pos[i-1], n_grid[i-1], &(n_pos[i][3*node]), n_grid[i],
p3m.mesh, p3m.mesh_off, &(fft_plan[i].send_block[6*j]));
permute_ifield(&(fft_plan[i].send_block[6*j]),3,-(fft_plan[i-1].n_permute));
permute_ifield(&(fft_plan[i].send_block[6*j+3]),3,-(fft_plan[i-1].n_permute));
if(fft_plan[i].send_size[j] > max_comm_size)
max_comm_size = fft_plan[i].send_size[j];
/* First plan send blocks have to be adjusted, since the CA grid
may have an additional margin outside the actual domain of the
node */
if(i==1) {
for(k=0;k<3;k++)
fft_plan[1].send_block[6*j+k ] += ca_mesh_margin[2*k];
}
/* recv block: this_node from comm-group-node i (identity: node) */
fft_plan[i].recv_size[j]
= calc_send_block(my_pos[i], n_grid[i], &(n_pos[i-1][3*node]), n_grid[i-1],
p3m.mesh,p3m.mesh_off,&(fft_plan[i].recv_block[6*j]));
permute_ifield(&(fft_plan[i].recv_block[6*j]),3,-(fft_plan[i].n_permute));
permute_ifield(&(fft_plan[i].recv_block[6*j+3]),3,-(fft_plan[i].n_permute));
if(fft_plan[i].recv_size[j] > max_comm_size)
max_comm_size = fft_plan[i].recv_size[j];
}
for(j=0;j<3;j++) fft_plan[i].old_mesh[j] = fft_plan[i-1].new_mesh[j];
if(i==1)
fft_plan[i].element = 1;
else {
fft_plan[i].element = 2;
for(j=0; j<fft_plan[i].g_size; j++) {
fft_plan[i].send_size[j] *= 2;
fft_plan[i].recv_size[j] *= 2;
}
}
/* DEBUG */
for(j=0;j<n_nodes;j++) {
/* MPI_Barrier(MPI_COMM_WORLD); */
if(j==this_node) FFT_TRACE(print_fft_plan(fft_plan[i]));
}
}
/* Factor 2 for complex fields */
max_comm_size *= 2;
max_mesh_size = (ca_mesh_dim[0]*ca_mesh_dim[1]*ca_mesh_dim[2]);
for(i=1;i<4;i++)
if(2*fft_plan[i].new_size > max_mesh_size) max_mesh_size = 2*fft_plan[i].new_size;
FFT_TRACE(fprintf(stderr,"%d: max_comm_size = %d, max_mesh_size = %d\n",
this_node,max_comm_size,max_mesh_size));
/* === pack function === */
for(i=1;i<4;i++) {
fft_plan[i].pack_function = pack_block_permute2;
FFT_TRACE(fprintf(stderr,"%d: forw plan[%d] permute 2 \n",this_node,i));
}
(*ks_pnum)=6;
if(fft_plan[1].row_dir==2) {
fft_plan[1].pack_function = pack_block;
FFT_TRACE(fprintf(stderr,"%d: forw plan[%d] permute 0 \n",this_node,1));
(*ks_pnum)=4;
}
else if(fft_plan[1].row_dir==1) {
fft_plan[1].pack_function = pack_block_permute1;
FFT_TRACE(fprintf(stderr,"%d: forw plan[%d] permute 1 \n",this_node,1));
(*ks_pnum)=5;
}
/* Factor 2 for complex numbers */
send_buf = (double *)realloc(send_buf, max_comm_size*sizeof(double));
recv_buf = (double *)realloc(recv_buf, max_comm_size*sizeof(double));
(*data) = (double *)realloc((*data), max_mesh_size*sizeof(double));
data_buf = (double *)realloc(data_buf, max_mesh_size*sizeof(double));
if(!(*data) || !data_buf || !recv_buf || !send_buf) {
fprintf(stderr,"%d: Could not allocate FFT data arays\n",this_node);
errexit();
}
c_data = (fftw_complex *) (*data);
c_data_buf = (fftw_complex *) data_buf;
/* === FFT Routines (Using FFTW / RFFTW package)=== */
for(i=1;i<4;i++) {
fft_plan[i].dir = FFTW_FORWARD;
/* FFT plan creation.
Attention: destroys contents of c_data/data and c_data_buf/data_buf. */
wisdom_status = FFTW_FAILURE;
sprintf(wisdom_file_name,"fftw3_1d_wisdom_forw_n%d.file",
fft_plan[i].new_mesh[2]);
if( (wisdom_file=fopen(wisdom_file_name,"r"))!=NULL ) {
wisdom_status = fftw_import_wisdom_from_file(wisdom_file);
fclose(wisdom_file);
}
if(fft_init_tag==1) fftw_destroy_plan(fft_plan[i].fft_plan);
#if FFTW == 3
//printf("fft_plan[%d].n_ffts=%d\n",i,fft_plan[i].n_ffts);
fft_plan[i].fft_plan =
fftw_plan_many_dft(1,&fft_plan[i].new_mesh[2],fft_plan[i].n_ffts,
c_data,NULL,1,fft_plan[i].new_mesh[2],
c_data,NULL,1,fft_plan[i].new_mesh[2],
fft_plan[i].dir,FFTW_PATIENT);
#else
fft_plan[i].fft_plan =
fftw_create_plan_specific(fft_plan[i].new_mesh[2], fft_plan[i].dir,
FFTW_MEASURE | FFTW_IN_PLACE | FFTW_USE_WISDOM,
c_data, 1,c_data_buf, 1);
#endif
if( wisdom_status == FFTW_FAILURE &&
(wisdom_file=fopen(wisdom_file_name,"w"))!=NULL ) {
fftw_export_wisdom_to_file(wisdom_file);
fclose(wisdom_file);
}
#if FFTW == 3
fft_plan[i].fft_function = fftw_execute;
#else
fft_plan[i].fft_function = fftw;
#endif
}
/* === The BACK Direction === */
/* this is needed because slightly different functions are used */
for(i=1;i<4;i++) {
fft_back[i].dir = FFTW_BACKWARD;
wisdom_status = FFTW_FAILURE;
sprintf(wisdom_file_name,"fftw3_1d_wisdom_back_n%d.file",
fft_plan[i].new_mesh[2]);
if( (wisdom_file=fopen(wisdom_file_name,"r"))!=NULL ) {
wisdom_status = fftw_import_wisdom_from_file(wisdom_file);
fclose(wisdom_file);
}
if(fft_init_tag==1) fftw_destroy_plan(fft_back[i].fft_plan);
#if FFTW == 3
fft_back[i].fft_plan =
fftw_plan_many_dft(1,&fft_plan[i].new_mesh[2],fft_plan[i].n_ffts,
c_data,NULL,1,fft_plan[i].new_mesh[2],
c_data,NULL,1,fft_plan[i].new_mesh[2],
fft_back[i].dir,FFTW_PATIENT);
#else
fft_back[i].fft_plan =
fftw_create_plan_specific(fft_plan[i].new_mesh[2], fft_back[i].dir,
FFTW_MEASURE | FFTW_IN_PLACE | FFTW_USE_WISDOM,
c_data, 1,c_data_buf, 1);
#endif
if( wisdom_status == FFTW_FAILURE &&
(wisdom_file=fopen(wisdom_file_name,"w"))!=NULL ) {
fftw_export_wisdom_to_file(wisdom_file);
fclose(wisdom_file);
}
#if FFTW == 3
fft_back[i].fft_function = fftw_execute;
#else
fft_back[i].fft_function = fftw;
#endif
fft_back[i].pack_function = pack_block_permute1;
FFT_TRACE(fprintf(stderr,"%d: back plan[%d] permute 1 \n",this_node,i));
}
if(fft_plan[1].row_dir==2) {
fft_back[1].pack_function = pack_block;
FFT_TRACE(fprintf(stderr,"%d: back plan[%d] permute 0 \n",this_node,1));
}
else if(fft_plan[1].row_dir==1) {
fft_back[1].pack_function = pack_block_permute2;
FFT_TRACE(fprintf(stderr,"%d: back plan[%d] permute 2 \n",this_node,1));
}
fft_init_tag=1;
/* free(data); */
for(i=0;i<4;i++) { free(n_id[i]); free(n_pos[i]); }
return max_mesh_size;
}
void fft_perform_forw(double *data)
{
int i;
/* int m,n,o; */
/* ===== first direction ===== */
FFT_TRACE(fprintf(stderr,"%d: fft_perform_forw: dir 1:\n",this_node));
c_data = (fftw_complex *) data;
c_data_buf = (fftw_complex *) data_buf;
/* communication to current dir row format (in is data) */
forw_grid_comm(fft_plan[1], data, data_buf);
/*
fprintf(stderr,"%d: start grid \n",this_node);
i=0;
for(m=0;m<8;m++) {
for(n=0;n<8;n++) {
for(o=0;o<8;o++) {
fprintf(stderr,"%.3f ",data_buf[i++]);
}
fprintf(stderr,"\n");
}
fprintf(stderr,"\n");
}
*/
/* complexify the real data array (in is data_buf) */
for(i=0;i<fft_plan[1].new_size;i++) {
data[2*i] = data_buf[i]; /* real value */
data[(2*i)+1] = 0; /* complex value */
}
/* perform FFT (in/out is data)*/
#if FFTW == 3
fftw_execute_dft(fft_plan[1].fft_plan,c_data,c_data);
#else
fft_plan[1].fft_function(fft_plan[1].fft_plan, fft_plan[1].n_ffts,
c_data, 1, fft_plan[1].new_mesh[2],
c_data_buf, 1, fft_plan[1].new_mesh[2]);
#endif
/* ===== second direction ===== */
FFT_TRACE(fprintf(stderr,"%d: fft_perform_forw: dir 2:\n",this_node));
/* communication to current dir row format (in is data) */
forw_grid_comm(fft_plan[2], data, data_buf);
/* perform FFT (in/out is data_buf)*/
#if FFTW == 3
fftw_execute_dft(fft_plan[2].fft_plan,c_data_buf,c_data_buf);
#else
fft_plan[2].fft_function(fft_plan[2].fft_plan, fft_plan[2].n_ffts,
c_data_buf, 1, fft_plan[2].new_mesh[2],
c_data, 1, fft_plan[2].new_mesh[2]);
#endif
/* ===== third direction ===== */
FFT_TRACE(fprintf(stderr,"%d: fft_perform_forw: dir 3:\n",this_node));
/* communication to current dir row format (in is data_buf) */
forw_grid_comm(fft_plan[3], data_buf, data);
/* perform FFT (in/out is data)*/
#if FFTW == 3
fftw_execute_dft(fft_plan[3].fft_plan,c_data,c_data);
#else
fft_plan[3].fft_function(fft_plan[3].fft_plan, fft_plan[3].n_ffts,
c_data, 1, fft_plan[3].new_mesh[2],
c_data_buf, 1, fft_plan[3].new_mesh[2]);
#endif
//print_global_fft_mesh(fft_plan[3],data,1,0);
/* REMARK: Result has to be in data. */
}
void fft_perform_back(double *data)
{
int i;
//The next 4 lines were added by Vincent:
c_data = (fftw_complex *) data;
c_data_buf = (fftw_complex *) data_buf;
/* ===== third direction ===== */
FFT_TRACE(fprintf(stderr,"%d: fft_perform_back: dir 3:\n",this_node));
/* perform FFT (in is data) */
#if FFTW == 3
fftw_execute_dft(fft_back[3].fft_plan,c_data,c_data);
#else
fft_back[3].fft_function(fft_back[3].fft_plan, fft_plan[3].n_ffts,
c_data, 1, fft_plan[3].new_mesh[2],
c_data_buf, 1, fft_plan[3].new_mesh[2]);
#endif
/* communicate (in is data)*/
back_grid_comm(fft_plan[3],fft_back[3],data,data_buf);
/* ===== second direction ===== */
FFT_TRACE(fprintf(stderr,"%d: fft_perform_back: dir 2:\n",this_node));
/* perform FFT (in is data_buf) */
#if FFTW == 3
fftw_execute_dft(fft_back[2].fft_plan,c_data_buf,c_data_buf);
#else
fft_back[2].fft_function(fft_back[2].fft_plan, fft_plan[2].n_ffts,
c_data_buf, 1, fft_plan[2].new_mesh[2],
c_data, 1, fft_plan[2].new_mesh[2]);
#endif
/* communicate (in is data_buf) */
back_grid_comm(fft_plan[2],fft_back[2],data_buf,data);
/* ===== first direction ===== */
FFT_TRACE(fprintf(stderr,"%d: fft_perform_back: dir 1:\n",this_node));
/* perform FFT (in is data) */
#if FFTW == 3
fftw_execute_dft(fft_back[1].fft_plan,c_data,c_data);
#else
fft_back[1].fft_function(fft_back[1].fft_plan, fft_plan[1].n_ffts,
c_data, 1, fft_plan[1].new_mesh[2],
c_data_buf, 1, fft_plan[1].new_mesh[2]);
#endif
/* throw away the (hopefully) empty complex component (in is data)*/
for(i=0;i<fft_plan[1].new_size;i++) {
data_buf[i] = data[2*i]; /* real value */
//Vincent:
if (data[2*i+1]>1e-5) {
printf("Complex value is not zero (i=%d,data=%g)!!!\n",i,data[2*i+1]);
if (i>100) exit(-1);
}
}
/* communicate (in is data_buf) */
back_grid_comm(fft_plan[1],fft_back[1],data_buf,data);
/* REMARK: Result has to be in data. */
}
#endif
#ifdef MAGNETOSTATICS
int Dfft_init(double **Ddata, int *Dca_mesh_dim, int *Dca_mesh_margin, int *ks_pnum)
{
int i,j;
/* helpers */
int mult[3];
int n_grid[4][3]; /* The four node grids. */
int my_pos[4][3]; /* The position of this_node in the node grids. */
int *n_id[4]; /* linear node identity lists for the node grids. */
int *n_pos[4]; /* positions of nodes in the node grids. */
/* FFTW WISDOM stuff. */
char wisdom_file_name[255];
FILE *wisdom_file;
#if FFTW == 3
int wisdom_status;
#else
fftw_status wisdom_status;
#endif
FFT_TRACE(fprintf(stderr,"%d: dipolar Dfft_init():\n",this_node));
Dmax_comm_size=0; Dmax_mesh_size=0;
for(i=0;i<4;i++) {
n_id[i] = malloc(1*n_nodes*sizeof(int));
n_pos[i] = malloc(3*n_nodes*sizeof(int));
}
/* === node grids === */
/* real space node grid (n_grid[0]) */
for(i=0;i<3;i++) {
n_grid[0][i] = node_grid[i];
my_pos[0][i] = node_pos[i];
}
for(i=0;i<n_nodes;i++) {
n_id[0][i] = i;
get_grid_pos(i,&(n_pos[0][3*i+0]),&(n_pos[0][3*i+1]),&(n_pos[0][3*i+2]),
n_grid[0]);
}
/* FFT node grids (n_grid[1 - 3]) */
calc_2d_grid(n_nodes,n_grid[1]);
/* resort n_grid[1] dimensions if necessary */
Dfft_plan[1].row_dir = map_3don2d_grid(n_grid[0], n_grid[1], mult);
Dfft_plan[0].n_permute = 0;
for(i=1;i<4;i++) Dfft_plan[i].n_permute = (Dfft_plan[1].row_dir+i)%3;
for(i=0;i<3;i++) {
n_grid[2][i] = n_grid[1][(i+1)%3];
n_grid[3][i] = n_grid[1][(i+2)%3];
}
Dfft_plan[2].row_dir = (Dfft_plan[1].row_dir-1)%3;
Dfft_plan[3].row_dir = (Dfft_plan[1].row_dir-2)%3;
/* === communication groups === */
/* copy local mesh off real space charge assignment grid */
for(i=0;i<3;i++) Dfft_plan[0].new_mesh[i] = Dca_mesh_dim[i];
for(i=1; i<4;i++) {
Dfft_plan[i].g_size=find_comm_groups(n_grid[i-1], n_grid[i], n_id[i-1], n_id[i],
Dfft_plan[i].group, n_pos[i], my_pos[i]);
if(Dfft_plan[i].g_size==-1) {
/* try permutation */
j = n_grid[i][(Dfft_plan[i].row_dir+1)%3];
n_grid[i][(Dfft_plan[i].row_dir+1)%3] = n_grid[i][(Dfft_plan[i].row_dir+2)%3];
n_grid[i][(Dfft_plan[i].row_dir+2)%3] = j;
Dfft_plan[i].g_size=find_comm_groups(n_grid[i-1], n_grid[i], n_id[i-1], n_id[i],
Dfft_plan[i].group, n_pos[i], my_pos[i]);
if(Dfft_plan[i].g_size==-1) {
fprintf(stderr,"%d: dipolar INTERNAL ERROR: find_comm_groups error\n", this_node);
errexit();
}
}
Dfft_plan[i].send_block = (int *)realloc(Dfft_plan[i].send_block, 6*Dfft_plan[i].g_size*sizeof(int));
Dfft_plan[i].send_size = (int *)realloc(Dfft_plan[i].send_size, 1*Dfft_plan[i].g_size*sizeof(int));
Dfft_plan[i].recv_block = (int *)realloc(Dfft_plan[i].recv_block, 6*Dfft_plan[i].g_size*sizeof(int));
Dfft_plan[i].recv_size = (int *)realloc(Dfft_plan[i].recv_size, 1*Dfft_plan[i].g_size*sizeof(int));
Dfft_plan[i].new_size = calc_local_mesh(my_pos[i], n_grid[i], p3m.Dmesh,
p3m.Dmesh_off, Dfft_plan[i].new_mesh,
Dfft_plan[i].start);
permute_ifield(Dfft_plan[i].new_mesh,3,-(Dfft_plan[i].n_permute));
permute_ifield(Dfft_plan[i].start,3,-(Dfft_plan[i].n_permute));
Dfft_plan[i].n_ffts = Dfft_plan[i].new_mesh[0]*Dfft_plan[i].new_mesh[1];
/* === send/recv block specifications === */
for(j=0; j<Dfft_plan[i].g_size; j++) {
int k, node;
/* send block: this_node to comm-group-node i (identity: node) */
node = Dfft_plan[i].group[j];
Dfft_plan[i].send_size[j]
= calc_send_block(my_pos[i-1], n_grid[i-1], &(n_pos[i][3*node]), n_grid[i],
p3m.Dmesh, p3m.Dmesh_off, &(Dfft_plan[i].send_block[6*j]));
permute_ifield(&(Dfft_plan[i].send_block[6*j]),3,-(Dfft_plan[i-1].n_permute));
permute_ifield(&(Dfft_plan[i].send_block[6*j+3]),3,-(Dfft_plan[i-1].n_permute));
if(Dfft_plan[i].send_size[j] > Dmax_comm_size)
Dmax_comm_size = Dfft_plan[i].send_size[j];
/* First plan send blocks have to be adjusted, since the CA grid
may have an additional margin outside the actual domain of the
node */
if(i==1) {
for(k=0;k<3;k++)
Dfft_plan[1].send_block[6*j+k ] += Dca_mesh_margin[2*k];
}
/* recv block: this_node from comm-group-node i (identity: node) */
Dfft_plan[i].recv_size[j]
= calc_send_block(my_pos[i], n_grid[i], &(n_pos[i-1][3*node]), n_grid[i-1],
p3m.Dmesh,p3m.Dmesh_off,&(Dfft_plan[i].recv_block[6*j]));
permute_ifield(&(Dfft_plan[i].recv_block[6*j]),3,-(Dfft_plan[i].n_permute));
permute_ifield(&(Dfft_plan[i].recv_block[6*j+3]),3,-(Dfft_plan[i].n_permute));
if(Dfft_plan[i].recv_size[j] > Dmax_comm_size)
Dmax_comm_size = Dfft_plan[i].recv_size[j];
}
for(j=0;j<3;j++) Dfft_plan[i].old_mesh[j] = Dfft_plan[i-1].new_mesh[j];
if(i==1)
Dfft_plan[i].element = 1;
else {
Dfft_plan[i].element = 2;
for(j=0; j<Dfft_plan[i].g_size; j++) {
Dfft_plan[i].send_size[j] *= 2;
Dfft_plan[i].recv_size[j] *= 2;
}
}
/* DEBUG */
for(j=0;j<n_nodes;j++) {
/* MPI_Barrier(MPI_COMM_WORLD); */
if(j==this_node) FFT_TRACE(print_fft_plan(Dfft_plan[i]));
}
}
/* Factor 2 for complex fields */
Dmax_comm_size *= 2;
Dmax_mesh_size = (Dca_mesh_dim[0]*Dca_mesh_dim[1]*Dca_mesh_dim[2]);
for(i=1;i<4;i++)
if(2*Dfft_plan[i].new_size > Dmax_mesh_size) Dmax_mesh_size = 2*Dfft_plan[i].new_size;
FFT_TRACE(fprintf(stderr,"%d: Dmax_comm_size = %d, Dmax_mesh_size = %d\n",
this_node,Dmax_comm_size,Dmax_mesh_size));
/* === pack function === */
for(i=1;i<4;i++) {
Dfft_plan[i].pack_function = pack_block_permute2;
FFT_TRACE(fprintf(stderr,"%d: forw plan[%d] permute 2 \n",this_node,i));
}
(*ks_pnum)=6;
if(Dfft_plan[1].row_dir==2) {
Dfft_plan[1].pack_function = pack_block;
FFT_TRACE(fprintf(stderr,"%d: forw plan[%d] permute 0 \n",this_node,1));
(*ks_pnum)=4;
}
else if(Dfft_plan[1].row_dir==1) {
Dfft_plan[1].pack_function = pack_block_permute1;
FFT_TRACE(fprintf(stderr,"%d: forw plan[%d] permute 1 \n",this_node,1));
(*ks_pnum)=5;
}
/* Factor 2 for complex numbers */
Dsend_buf = (double *)realloc(Dsend_buf, Dmax_comm_size*sizeof(double));
Drecv_buf = (double *)realloc(Drecv_buf, Dmax_comm_size*sizeof(double));
(*Ddata) = (double *)realloc((*Ddata), Dmax_mesh_size*sizeof(double));
Ddata_buf = (double *)realloc(Ddata_buf, Dmax_mesh_size*sizeof(double));
if(!(*Ddata) || !Ddata_buf || !Drecv_buf || !Dsend_buf) {
fprintf(stderr,"%d: Could not allocate FFT data arays\n",this_node);
errexit();
}
Dc_data = (fftw_complex *) (*Ddata);
Dc_data_buf = (fftw_complex *) Ddata_buf;
/* === FFT Routines (Using FFTW / RFFTW package)=== */
for(i=1;i<4;i++) {
Dfft_plan[i].dir = FFTW_FORWARD;
/* FFT plan creation.
Attention: destroys contents of c_data/data and c_data_buf/data_buf. */
wisdom_status = FFTW_FAILURE;
sprintf(wisdom_file_name,"Dfftw3_1d_wisdom_forw_n%d.file",
Dfft_plan[i].new_mesh[2]);
if( (wisdom_file=fopen(wisdom_file_name,"r"))!=NULL ) {
wisdom_status = fftw_import_wisdom_from_file(wisdom_file);
fclose(wisdom_file);
}
if(Dfft_init_tag==1) fftw_destroy_plan(Dfft_plan[i].fft_plan);
#if FFTW == 3
//printf("Dfft_plan[%d].n_ffts=%d\n",i,Dfft_plan[i].n_ffts);
Dfft_plan[i].fft_plan =
fftw_plan_many_dft(1,&Dfft_plan[i].new_mesh[2],Dfft_plan[i].n_ffts,
Dc_data,NULL,1,Dfft_plan[i].new_mesh[2],
Dc_data,NULL,1,Dfft_plan[i].new_mesh[2],
Dfft_plan[i].dir,FFTW_PATIENT);
#else
Dfft_plan[i].fft_plan =
fftw_create_plan_specific(Dfft_plan[i].new_mesh[2], Dfft_plan[i].dir,
FFTW_MEASURE | FFTW_IN_PLACE | FFTW_USE_WISDOM,
Dc_data, 1,Dc_data_buf, 1);
#endif
if( wisdom_status == FFTW_FAILURE &&
(wisdom_file=fopen(wisdom_file_name,"w"))!=NULL ) {
fftw_export_wisdom_to_file(wisdom_file);
fclose(wisdom_file);
}
#if FFTW == 3
Dfft_plan[i].fft_function = fftw_execute;
#else
Dfft_plan[i].fft_function = fftw;
#endif
}
/* === The BACK Direction === */
/* this is needed because slightly different functions are used */
for(i=1;i<4;i++) {
Dfft_back[i].dir = FFTW_BACKWARD;
wisdom_status = FFTW_FAILURE;
sprintf(wisdom_file_name,"Dfftw3_1d_wisdom_back_n%d.file",
Dfft_plan[i].new_mesh[2]);
if( (wisdom_file=fopen(wisdom_file_name,"r"))!=NULL ) {
wisdom_status = fftw_import_wisdom_from_file(wisdom_file);
fclose(wisdom_file);
}
if(Dfft_init_tag==1) fftw_destroy_plan(Dfft_back[i].fft_plan);
#if FFTW == 3
Dfft_back[i].fft_plan =
fftw_plan_many_dft(1,&Dfft_plan[i].new_mesh[2],Dfft_plan[i].n_ffts,
Dc_data,NULL,1,Dfft_plan[i].new_mesh[2],
Dc_data,NULL,1,Dfft_plan[i].new_mesh[2],
Dfft_back[i].dir,FFTW_PATIENT);
#else
Dfft_back[i].fft_plan =
fftw_create_plan_specific(Dfft_plan[i].new_mesh[2], Dfft_back[i].dir,
FFTW_MEASURE | FFTW_IN_PLACE | FFTW_USE_WISDOM,
Dc_data, 1,Dc_data_buf, 1);
#endif
if( wisdom_status == FFTW_FAILURE &&
(wisdom_file=fopen(wisdom_file_name,"w"))!=NULL ) {
fftw_export_wisdom_to_file(wisdom_file);
fclose(wisdom_file);
}
#if FFTW == 3
Dfft_back[i].fft_function = fftw_execute;
#else
Dfft_back[i].fft_function = fftw;
#endif
Dfft_back[i].pack_function = pack_block_permute1;
FFT_TRACE(fprintf(stderr,"%d: back plan[%d] permute 1 \n",this_node,i));
}
if(Dfft_plan[1].row_dir==2) {
Dfft_back[1].pack_function = pack_block;
FFT_TRACE(fprintf(stderr,"%d: back plan[%d] permute 0 \n",this_node,1));
}
else if(Dfft_plan[1].row_dir==1) {
Dfft_back[1].pack_function = pack_block_permute2;
FFT_TRACE(fprintf(stderr,"%d: back plan[%d] permute 2 \n",this_node,1));
}
Dfft_init_tag=1;
/* free(data); */
for(i=0;i<4;i++) { free(n_id[i]); free(n_pos[i]); }
return Dmax_mesh_size;
}
void Dfft_perform_forw(double *Ddata)
{
int i;
/* int m,n,o; */
/* ===== first direction ===== */
FFT_TRACE(fprintf(stderr,"%d: dipolar fft_perform_forw: dir 1:\n",this_node));
Dc_data = (fftw_complex *) Ddata;
Dc_data_buf = (fftw_complex *) Ddata_buf;
/* communication to current dir row format (in is data) */
Dforw_grid_comm(Dfft_plan[1], Ddata, Ddata_buf);
/*
fprintf(stderr,"%d: start grid \n",this_node);
i=0;
for(m=0;m<8;m++) {
for(n=0;n<8;n++) {
for(o=0;o<8;o++) {
fprintf(stderr,"%.3f ",data_buf[i++]);
}
fprintf(stderr,"\n");
}
fprintf(stderr,"\n");
}
*/
/* complexify the real data array (in is data_buf) */
for(i=0;i<Dfft_plan[1].new_size;i++) {
Ddata[2*i] = Ddata_buf[i]; /* real value */
Ddata[(2*i)+1] = 0; /* complex value */
}
/* perform FFT (in/out is data)*/
#if FFTW == 3
fftw_execute_dft(Dfft_plan[1].fft_plan,Dc_data,Dc_data);
#else
Dfft_plan[1].fft_function(Dfft_plan[1].fft_plan, Dfft_plan[1].n_ffts,
Dc_data, 1, Dfft_plan[1].new_mesh[2],
Dc_data_buf, 1, Dfft_plan[1].new_mesh[2]);
#endif
/* ===== second direction ===== */
FFT_TRACE(fprintf(stderr,"%d: dipolar fft_perform_forw: dir 2:\n",this_node));
/* communication to current dir row format (in is data) */
Dforw_grid_comm(Dfft_plan[2], Ddata, Ddata_buf);
/* perform FFT (in/out is data_buf)*/
#if FFTW == 3
fftw_execute_dft(Dfft_plan[2].fft_plan,Dc_data_buf,Dc_data_buf);
#else
Dfft_plan[2].fft_function(Dfft_plan[2].fft_plan, Dfft_plan[2].n_ffts,
Dc_data_buf, 1, Dfft_plan[2].new_mesh[2],
Dc_data, 1, Dfft_plan[2].new_mesh[2]);
#endif
/* ===== third direction ===== */
FFT_TRACE(fprintf(stderr,"%d: dipolar fft_perform_forw: dir 3:\n",this_node));
/* communication to current dir row format (in is data_buf) */
Dforw_grid_comm(Dfft_plan[3], Ddata_buf, Ddata);
/* perform FFT (in/out is data)*/
#if FFTW == 3
fftw_execute_dft(Dfft_plan[3].fft_plan,Dc_data,Dc_data);
#else
Dfft_plan[3].fft_function(Dfft_plan[3].fft_plan, Dfft_plan[3].n_ffts,
Dc_data, 1, Dfft_plan[3].new_mesh[2],
Dc_data_buf, 1, Dfft_plan[3].new_mesh[2]);
#endif
//print_global_fft_mesh(Dfft_plan[3],data,1,0);
/* REMARK: Result has to be in data. */
}
void Dfft_perform_back(double *Ddata)
{
int i;
Dc_data = (fftw_complex *) Ddata;
Dc_data_buf = (fftw_complex *) Ddata_buf;
/* ===== third direction ===== */
FFT_TRACE(fprintf(stderr,"%d: dipolar fft_perform_back: dir 3:\n",this_node));
/* perform FFT (in is data) */
#if FFTW == 3
fftw_execute_dft(Dfft_back[3].fft_plan,Dc_data,Dc_data);
#else
Dfft_back[3].fft_function(Dfft_back[3].fft_plan, Dfft_plan[3].n_ffts,