/* L-14 MCS 572 Fri 23 Sep 2016 : fftw_timing.c.
 * With this program we time the execution of the FFTW
 * (the fastest Fourier Transform in the West) to random inputs.
 * On the command line, the user can provide the dimension
 * and the number of times to compute a DFT on that sequence. */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <fftw3.h>

void random_sequence ( int n, double *re, double *im );
/* 
 * Returns a random sequence of n complex numbers
 * with real parts in re and imaginary parts in im. */

int main ( int argc, char *argv[] )
{
   int n,m;
   if(argc > 1)
      n = atoi(argv[1]);
   else
      printf("please provide dimension on command line\n");
   m = (argc > 2) ? atoi(argv[2]) : 1;

   double *x, *y;
   x = (double*)calloc(n,sizeof(double));
   y = (double*)calloc(n,sizeof(double));
   random_sequence(n,x,y);

   fftw_complex *in,*out;
   in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*n);
   out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*n);

   fftw_plan p;
   p = fftw_plan_dft_1d(n,in,out,FFTW_FORWARD,FFTW_ESTIMATE);
   clock_t tstart,tstop;
   tstart = clock();
   int i;
   for(i=0; i<m; i++) fftw_execute(p);
   tstop = clock();
   printf("%d iterations took %.3f seconds\n",
          m,(tstop-tstart)/((double) CLOCKS_PER_SEC));

   return 0;
}

void random_sequence ( int n, double *re, double *im )
{
   int i;
   for(i=0; i<n; i++) 
   {
      re[i] = ((double) rand())/RAND_MAX;
      im[i] = ((double) rand())/RAND_MAX;
   }
}
