NFFT  3.3.2
simple_test_threads.c
00001 /*
00002  * Copyright (c) 2002, 2016 Jens Keiner, Stefan Kunis, Daniel Potts
00003  *
00004  * This program is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00012  * details.
00013  *
00014  * You should have received a copy of the GNU General Public License along with
00015  * this program; if not, write to the Free Software Foundation, Inc., 51
00016  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 /* standard headers */
00020 #include <stdio.h>
00021 #include <math.h>
00022 #include <string.h>
00023 #include <stdlib.h>
00024 /* It is important to include complex.h before nfft3.h. */
00025 #include <complex.h>
00026 #include <omp.h>
00027 
00028 #include "nfft3.h" /* NFFT3 header */
00029 
00030 #define __FES__ "E"
00031 #define K(x) ((double) x)
00032 
00033 static void simple_test_nfsft(void)
00034 {
00035   const int N = 4; /* bandwidth/maximum degree */
00036   const int M = 8; /* number of nodes */
00037   nfsft_plan plan; /* transform plan */
00038   int j, k, n; /* loop variables */
00039 
00040   /* precomputation (for fast polynomial transform) */
00041   nfsft_precompute(N,1000.0,0U,0U);
00042 
00043   /* Initialize transform plan using the guru interface. All input and output
00044    * arrays are allocated by nfsft_init_guru(). Computations are performed with
00045    * respect to L^2-normalized spherical harmonics Y_k^n. The array of spherical
00046    * Fourier coefficients is preserved during transformations. The NFFT uses a
00047    * cut-off parameter m = 6. See the NFFT 3 manual for details.
00048    */
00049   nfsft_init_guru(&plan, N, M, NFSFT_MALLOC_X | NFSFT_MALLOC_F |
00050     NFSFT_MALLOC_F_HAT | NFSFT_NORMALIZED | NFSFT_PRESERVE_F_HAT,
00051     PRE_PHI_HUT | PRE_PSI | FFTW_INIT | FFT_OUT_OF_PLACE, 6);
00052 
00053   /* pseudo-random nodes */
00054   for (j = 0; j < plan.M_total; j++)
00055   {
00056     plan.x[2*j]= nfft_drand48() - K(0.5);
00057     plan.x[2*j+1]= K(0.5) * nfft_drand48();
00058   }
00059 
00060   /* precomputation (for NFFT, node-dependent) */
00061   nfsft_precompute_x(&plan);
00062 
00063   /* pseudo-random Fourier coefficients */
00064   for (k = 0; k <= plan.N; k++)
00065     for (n = -k; n <= k; n++)
00066       plan.f_hat[NFSFT_INDEX(k,n,&plan)] =
00067           nfft_drand48() - K(0.5) + _Complex_I*(nfft_drand48() - K(0.5));
00068 
00069   /* Direct transformation, display result. */
00070   nfsft_trafo_direct(&plan);
00071   printf("Vector f (NDSFT):\n");
00072   for (j = 0; j < plan.M_total; j++)
00073     printf("f[%+2d] = %+5.3" __FES__ " %+5.3" __FES__ "*I\n",j,
00074       creal(plan.f[j]), cimag(plan.f[j]));
00075 
00076   printf("\n");
00077 
00078   /* Fast approximate transformation, display result. */
00079   printf("Vector f (NDSFT):\n");
00080   for (j = 0; j < plan.M_total; j++)
00081     printf("f[%+2d] = %+5.3" __FES__ " %+5.3" __FES__ "*I\n",j,
00082       creal(plan.f[j]), cimag(plan.f[j]));
00083 
00084   printf("\n");
00085 
00086   /* Direct adjoint transformation, display result. */
00087   nfsft_adjoint_direct(&plan);
00088   printf("Vector f_hat (NDSFT):\n");
00089   for (k = 0; k <= plan.N; k++)
00090     for (n = -k; n <= k; n++)
00091       fprintf(stdout,"f_hat[%+2d,%+2d] = %+5.3" __FES__ " %+5.3" __FES__ "*I\n",k,n,
00092         creal(plan.f_hat[NFSFT_INDEX(k,n,&plan)]),
00093         cimag(plan.f_hat[NFSFT_INDEX(k,n,&plan)]));
00094 
00095   printf("\n");
00096 
00097   /* Fast approximate adjoint transformation, display result. */
00098   nfsft_adjoint(&plan);
00099   printf("Vector f_hat (NFSFT):\n");
00100   for (k = 0; k <= plan.N; k++)
00101   {
00102     for (n = -k; n <= k; n++)
00103     {
00104       fprintf(stdout,"f_hat[%+2d,%+2d] = %+5.3" __FES__ " %+5.3" __FES__ "*I\n",k,n,
00105         creal(plan.f_hat[NFSFT_INDEX(k,n,&plan)]),
00106         cimag(plan.f_hat[NFSFT_INDEX(k,n,&plan)]));
00107     }
00108   }
00109 
00110   /* Finalize the plan. */
00111   nfsft_finalize(&plan);
00112 
00113   /* Destroy data precomputed for fast polynomial transform. */
00114   nfsft_forget();
00115 }
00116 
00117 int main(void)
00118 {
00119   printf("nthreads = %d\n", nfft_get_num_threads());
00120 
00121   /* init */
00122   fftw_init_threads();
00123 
00124   printf("Computing an NDSFT, an NFSFT, an adjoint NDSFT, and an adjoint NFSFT"
00125     "...\n\n");
00126   simple_test_nfsft();
00127   return EXIT_SUCCESS;
00128 }