49867303842ca6d9d6986444eb8cf9796ed8a82c
[alexxy/gromacs.git] / src / contrib / testfft.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include <math.h>
39 #include <stdio.h>
40 #include "typedefs.h"
41 #include "macros.h"
42 #include "smalloc.h"
43 #include "xvgr.h"
44 #include "complex.h"
45 #include "fftgrid.h"
46 #include "mdrun.h"
47
48 void testfft(FILE *fp,t_complex ***grid,int nx,int ny,int nz,gmx_bool bFirst)
49 {
50 #ifdef USE_SGI_FFT
51 #ifdef GMX_DOUBLE
52   static    zomplex   *coeff;
53 #else
54   static    complex   *coeff;
55 #endif  
56   static    int la1,la2;
57 #endif
58   t_complex *cptr;
59   real      *gptr,*fqqq,fg,fac;
60   int       ntot,i,j,k,m,n,ndim[4];
61   int       npppm;
62   
63   ndim[0] = 0;
64   ndim[1] = nx;
65   ndim[2] = ny;
66   ndim[3] = nz;
67   
68   ntot    = nx*ny*nz;
69   cptr    = grid[0][0];
70   fqqq    = &(grid[0][0][0].re);
71   
72 #ifdef USE_SGI_FFT
73   if (bFirst) {
74     fprintf(fp,"Going to use SGI optimized FFT routines.\n");
75 #ifdef GMX_DOUBLE
76     coeff  = zfft3di(nx,ny,nz,NULL);
77 #else
78     coeff  = cfft3di(nx,ny,nz,NULL);
79 #endif
80     bFirst = FALSE;
81   }
82   la1 = nx;
83   la2 = ny;
84 #ifdef GMX_DOUBLE
85   zfft3d(1,nx,ny,nz,(zomplex *)cptr,la1,la2,coeff);
86 #else
87   cfft3d(1,nx,ny,nz,(complex *)cptr,la1,la2,coeff);
88 #endif
89 #else
90   fourn(fqqq-1,ndim,3,1);
91 #endif
92   
93 #ifdef USE_SGI_FFT
94 #ifdef GMX_DOUBLE
95   zfft3d(-1,nx,ny,nz,(zomplex *)cptr,la1,la2,coeff);
96 #else
97   cfft3d(-1,nx,ny,nz,(complex *)cptr,la1,la2,coeff);
98 #endif
99 #else
100   fourn(fqqq-1,ndim,3,-1);
101 #endif
102 }
103
104 void testrft(FILE *fp,real ***grid,int nx,int ny,int nz,gmx_bool bFirst)
105 {
106 #ifdef USE_SGI_FFT
107 #ifdef GMX_DOUBLE
108   static    double *coeff;
109 #else
110   static    float *coeff;
111 #endif
112   static    int la1,la2;
113 #endif
114   real      *cptr;
115   real      *gptr,*fqqq,fg,fac;
116   int       ntot,i,j,k,m,n,ndim[4];
117   int       npppm,job;
118   
119   ndim[0] = 0;
120   ndim[1] = nx;
121   ndim[2] = ny;
122   ndim[3] = nz;
123   
124   ntot    = nx*ny*nz;
125   cptr    = grid[0][0];
126   fqqq    = &(grid[0][0][0]);
127   
128 #ifdef USE_SGI_FFT
129   if (bFirst) {
130     fprintf(fp,"Going to use SGI optimized FFT routines.\n");
131 #ifdef GMX_DOUBLE
132     coeff  = dfft3di(nx,ny,nz,NULL);
133 #else
134     coeff  = sfft3di(nx,ny,nz,NULL);
135 #endif
136     bFirst = FALSE;
137   }
138   job = 1;
139   la1 = nx+2;
140   la2 = ny;
141 #ifdef GMX_DOUBLE
142   dzfft3d(job,nx,ny,nz,cptr,la1,la2,coeff);
143 #else
144   scfft3d(job,nx,ny,nz,cptr,la1,la2,coeff);
145 #endif
146 #else
147   fourn(fqqq-1,ndim,3,1);
148 #endif
149
150   job = -1;
151   
152 #ifdef USE_SGI_FFT
153 #ifdef GMX_DOUBLE
154   zdfft3d(job,nx,ny,nz,cptr,la1,la2,coeff);
155 #else
156   csfft3d(job,nx,ny,nz,cptr,la1,la2,coeff);
157 #endif
158 #else
159   fourn(fqqq-1,ndim,3,-1);
160 #endif
161 }
162
163 int main(int argc,char *argv[])
164 {
165   FILE      *fp;
166   int       nnn[] = { 8, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 40,
167                       45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 100 };
168 #define NNN asize(nnn)
169   int       *niter;
170   int       i,j,n,nit,ntot,n3;
171   double    t,nflop;
172   double    *rt,*ct;
173   t_complex ***g;
174   real      ***h;
175   
176   snew(rt,NNN);
177   snew(ct,NNN);
178   snew(niter,NNN);
179   
180   for(i=0; (i<NNN); i++) {
181     n = nnn[i];
182     fprintf(stderr,"\rReal %d     ",n);
183     if (n < 16)
184       niter[i] = 100;
185     else if (n < 26)
186       niter[i] = 50;
187     else if (n < 51)
188       niter[i] = 10;
189     else
190       niter[i] = 5;
191     nit = niter[i];
192       
193     h   = mk_rgrid(n+2,n,n);
194     start_time();
195     for(j=0; (j<nit); j++) {
196       testrft(stdout,h,n,n,n,(j==0));
197     }
198     update_time();
199     rt[i] = node_time();
200     free_rgrid(h,n,n);
201     
202     fprintf(stderr,"\rComplex %d     ",n);
203     g   = mk_cgrid(n,n,n);
204     start_time();
205     for(j=0; (j<nit); j++) {
206       testfft(stdout,g,n,n,n,(j==0));
207     }
208     update_time();
209     ct[i] = node_time();
210     free_cgrid(g,n,n);
211   }
212   fprintf(stderr,"\n");
213   fp=xvgropen("timing.xvg","FFT timings per grid point","n","t (s)");
214   for(i=0; (i<NNN); i++) {
215     n3 = 2*niter[i]*nnn[i]*nnn[i]*nnn[i];
216     fprintf(fp,"%10d  %10g  %10g\n",nnn[i],rt[i]/n3,ct[i]/n3);
217   }
218   gmx_fio_fclose(fp);
219   
220   return 0;
221 }