[768] | 1 | #include "machdefs.h"
|
---|
| 2 |
|
---|
[460] | 3 | #include <math.h>
|
---|
| 4 | #include <iostream.h>
|
---|
| 5 |
|
---|
[768] | 6 | #include "nbrandom.h"
|
---|
[705] | 7 | #include "fftpserver.h"
|
---|
[715] | 8 | #include "fftmserver.h"
|
---|
[768] | 9 | #include "fftwserver.h"
|
---|
| 10 | #include "ntoolsinit.h"
|
---|
[460] | 11 |
|
---|
[705] | 12 | #include "timing.h"
|
---|
[460] | 13 |
|
---|
[705] | 14 |
|
---|
[768] | 15 | static bool inp_typ_random = false ; // true -> random input
|
---|
| 16 |
|
---|
[705] | 17 | template <class T>
|
---|
| 18 | inline T module(complex<T> c)
|
---|
[460] | 19 | {
|
---|
[705] | 20 | return (sqrt(c.real()*c.real()+c.imag()*c.imag()));
|
---|
[460] | 21 | }
|
---|
| 22 |
|
---|
[715] | 23 | // Max Matrix print elts
|
---|
| 24 | static int nprt = 2;
|
---|
| 25 | static int nprtfc = 8;
|
---|
| 26 |
|
---|
[705] | 27 | template <class T>
|
---|
[715] | 28 | void TestFFTPack(T seuil, int num)
|
---|
[460] | 29 | {
|
---|
[705] | 30 | int i;
|
---|
| 31 | T fact = 1./num;
|
---|
[460] | 32 |
|
---|
[705] | 33 | TVector< complex<T> > inc(num), bkc(num), difc(num);
|
---|
| 34 | TVector< T > in(num), ino(num), bk(num),dif(num);
|
---|
| 35 | TVector< complex<T> > outc(num);
|
---|
[715] | 36 |
|
---|
| 37 | cout << " DBG/1 outc " << outc.NElts() << endl;
|
---|
| 38 | outc.ReSize(32);
|
---|
| 39 | cout << " DBG/2 outc " << outc.NElts() << endl;
|
---|
| 40 | outc.ReSize(10);
|
---|
| 41 | cout << " DBG/3 outc " << outc.NElts() << endl;
|
---|
| 42 | outc.ReSize(48);
|
---|
| 43 | cout << " DBG/4 outc " << outc.NElts() << endl;
|
---|
| 44 |
|
---|
[768] | 45 | if (inp_typ_random)
|
---|
| 46 | for (int i=0; i<num ; i++){
|
---|
| 47 | ino[i] = in[i] = GauRnd(0., 1.);
|
---|
| 48 | inc[i] = complex<T> (in[i], 0.);
|
---|
| 49 | }
|
---|
| 50 | else for (int i=0; i<num ; i++){
|
---|
[715] | 51 | ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num)
|
---|
| 52 | + 2*sin(4*M_PI*(double)i/(double)num);
|
---|
[705] | 53 | inc[i] = complex<T> (in[i], 0.);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[460] | 56 |
|
---|
[705] | 57 | cout << "Input / L = " << num << endl;
|
---|
[715] | 58 | in.Print(0,0,nprt,0,nprt);
|
---|
[460] | 59 | cout << endl;
|
---|
| 60 |
|
---|
[705] | 61 | cout << " >>>> Testing FFTPackServer " << endl;
|
---|
| 62 | FFTPackServer fftp;
|
---|
| 63 | cout << " Testing FFTPackServer " << endl;
|
---|
| 64 | fftp.fftf(in.NElts(), in.Data());
|
---|
[715] | 65 | // in /= (num/2.);
|
---|
[705] | 66 | cout << " fftp.fftf(in.NElts(), in.Data()) FORWARD: " << endl;
|
---|
[715] | 67 | in.Print(0,0,nprtfc,0,nprtfc);
|
---|
[705] | 68 | cout << endl;
|
---|
| 69 | fftp.fftb(in.NElts(), in.Data());
|
---|
| 70 | cout << " fftp.fftb(in.NElts(), in.Data()) BACKWARD: " << endl;
|
---|
[715] | 71 | in.Print(0,0,nprt,0,nprt);
|
---|
[705] | 72 | cout << endl;
|
---|
[715] | 73 | dif = ino-in;
|
---|
| 74 | cout << " dif , NElts= " << dif.NElts() << endl;
|
---|
| 75 | dif.Print(0,0,nprt,0,nprt);
|
---|
[460] | 76 |
|
---|
[715] | 77 | int ndiff = 0;
|
---|
| 78 | T maxdif=0., vdif;
|
---|
| 79 | for(i=0; i<num; i++) {
|
---|
| 80 | vdif = fabs(dif(i));
|
---|
| 81 | if (vdif > seuil) ndiff++;
|
---|
| 82 | if (vdif > maxdif) maxdif = vdif;
|
---|
| 83 | }
|
---|
| 84 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
|
---|
| 85 | << " MaxDiff= " << maxdif << endl;
|
---|
[705] | 86 |
|
---|
[715] | 87 | }
|
---|
| 88 |
|
---|
| 89 | template <class T>
|
---|
| 90 | void TestFFTS(T seuil, FFTServerInterface & ffts, int num)
|
---|
| 91 | {
|
---|
| 92 |
|
---|
| 93 | cout <<" ===> TestFFTS " << ffts.getInfo() << " ArrSz= " << num << endl;
|
---|
| 94 | int i;
|
---|
| 95 |
|
---|
| 96 | T fact = 1.;
|
---|
| 97 |
|
---|
| 98 | TVector< complex<T> > inc(num), bkc(num), difc(num);
|
---|
| 99 | TVector< T > in(num), ino(num), bk(num),dif(num);
|
---|
| 100 | TVector< complex<T> > outc(num);
|
---|
| 101 |
|
---|
| 102 | for (int i=0; i<num ; i++){
|
---|
| 103 | ino[i] = in[i] = 0.5 + cos(2*M_PI*(double)i/(double)num)
|
---|
| 104 | + 2*sin(4*M_PI*(double)i/(double)num);
|
---|
| 105 | inc[i] = complex<T> (in[i], 0.);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | cout << " Testing FFTServer " << ffts.getInfo() << endl;
|
---|
| 110 |
|
---|
| 111 | cout << "Input / L = " << num << endl;
|
---|
| 112 | in.Print(0,0,nprt,0,nprt);
|
---|
| 113 | cout << endl;
|
---|
| 114 |
|
---|
| 115 | int ndiff = 0;
|
---|
| 116 |
|
---|
[705] | 117 | cout << "\n ---- Testing FFT(T, complex<T>) ---- " << endl;
|
---|
| 118 | ffts.FFTForward(in, outc);
|
---|
[715] | 119 | cout << " FourierCoefs , NElts= " << outc.NElts() << endl;
|
---|
| 120 | outc.Print(0,0,nprtfc,0,nprtfc);
|
---|
[705] | 121 |
|
---|
| 122 | ffts.FFTBackward(outc, bk);
|
---|
[715] | 123 | cout << " Backward , NElts= " << bk.NElts() << endl;
|
---|
| 124 | bk.Print(0,0,nprt,0,nprt);
|
---|
[705] | 125 |
|
---|
| 126 | dif = bk*fact - in;
|
---|
[715] | 127 | cout << " Difference , NElts= " << dif.NElts() << endl;
|
---|
| 128 | dif.Print(0,0,nprt,0,nprt);
|
---|
[705] | 129 |
|
---|
[715] | 130 | ndiff = 0;
|
---|
| 131 | T maxdif=0., vdif;
|
---|
| 132 | for(i=0; i<num; i++) {
|
---|
| 133 | vdif = fabs(dif(i));
|
---|
| 134 | if (vdif > seuil) ndiff++;
|
---|
| 135 | if (vdif > maxdif) maxdif = vdif;
|
---|
| 136 | }
|
---|
| 137 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
|
---|
| 138 | << " MaxDiff= " << maxdif << endl;
|
---|
[705] | 139 |
|
---|
| 140 | cout << "\n ---- Testing FFT(complex<T>, complex<T>) ---- " << endl;
|
---|
| 141 | ffts.FFTForward(inc, outc);
|
---|
[715] | 142 | cout << " FourierCoef , NElts= " << outc.NElts() << endl;
|
---|
| 143 | outc.Print(0,0,nprtfc,0,nprtfc);
|
---|
[705] | 144 |
|
---|
| 145 | ffts.FFTBackward(outc, bkc);
|
---|
[715] | 146 | cout << " Backward , NElts= " << bkc.NElts() << endl;
|
---|
| 147 | bkc.Print(0,0,nprt,0,nprt);
|
---|
[705] | 148 |
|
---|
| 149 | difc = bkc*complex<T>(fact,0.) - inc;
|
---|
[715] | 150 | cout << " Difference , NElts= " << difc.NElts() << endl;
|
---|
| 151 | difc.Print(0,0,nprt,0,nprt);
|
---|
[705] | 152 |
|
---|
[715] | 153 | ndiff = 0;
|
---|
| 154 | maxdif=0., vdif;
|
---|
| 155 | for(i=0; i<num; i++) {
|
---|
| 156 | vdif = fabs(module(difc(i)));
|
---|
| 157 | if (vdif > seuil) ndiff++;
|
---|
| 158 | if (vdif > maxdif) maxdif = vdif;
|
---|
| 159 | }
|
---|
| 160 | cout << " Difference, Seuil= " << seuil << " NDiff= " << ndiff
|
---|
| 161 | << " MaxDiff= " << maxdif << endl;
|
---|
[460] | 162 | }
|
---|
[705] | 163 |
|
---|
| 164 |
|
---|
[715] | 165 |
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | int main(int narg, char* arg[])
|
---|
[705] | 169 | {
|
---|
| 170 |
|
---|
[768] | 171 | SophyaInit();
|
---|
[705] | 172 | InitTim(); // Initializing the CPU timer
|
---|
| 173 |
|
---|
[715] | 174 | if (narg < 4) {
|
---|
[768] | 175 | cout << "tfft/ args error - \n Usage tfft size px/Px/Mx/Wx f/d [NPrtFC NPrt] \n"
|
---|
| 176 | << " p=FFTPackTest P=FFTPack, M=FFTMayer, W= FFTWServer"
|
---|
| 177 | << " x=0 -> Random input - f=float, d=double " << endl;
|
---|
[715] | 178 | exit(0);
|
---|
| 179 | }
|
---|
[705] | 180 |
|
---|
[715] | 181 | FFTPackServer fftp;
|
---|
| 182 | FFTMayerServer fftm;
|
---|
| 183 |
|
---|
[768] | 184 | FFTWServer fftw;
|
---|
| 185 |
|
---|
| 186 | inp_typ_random = false;
|
---|
| 187 | if (arg[2][1] == '0') inp_typ_random = true;
|
---|
| 188 |
|
---|
[715] | 189 | int sz = atoi(arg[1]);
|
---|
| 190 | if (narg > 4) nprtfc = atoi(arg[4]);
|
---|
| 191 | if (narg > 5) nprt = atoi(arg[4]);
|
---|
| 192 |
|
---|
| 193 | if (sz < 2) sz = 2;
|
---|
| 194 | FFTServerInterface * ffts;
|
---|
| 195 | if (*arg[2] == 'M') ffts = fftm.Clone();
|
---|
[768] | 196 | else if (*arg[2] == 'W') ffts = fftw.Clone();
|
---|
[715] | 197 | else ffts = fftp.Clone();
|
---|
| 198 |
|
---|
| 199 | float fs = 1.e-4;
|
---|
| 200 | double ds = 1.e-6;
|
---|
| 201 |
|
---|
[705] | 202 | try {
|
---|
[715] | 203 | if (*arg[3] == 'd') {
|
---|
| 204 | cout << "\n ========================================== \n"
|
---|
| 205 | << " ------ Testing FFTServer for double ----- \n"
|
---|
| 206 | << " ============================================ " << endl;
|
---|
| 207 | if (*arg[2] == 'p') TestFFTPack(ds, sz);
|
---|
| 208 | else TestFFTS(ds, *ffts, sz);
|
---|
| 209 | }
|
---|
| 210 | else {
|
---|
| 211 | cout << "\n ========================================= \n"
|
---|
| 212 | << " ------ Testing FFTServer for float ----- \n"
|
---|
| 213 | << " =========================================== " << endl;
|
---|
| 214 | if (*arg[2] == 'p') TestFFTPack(fs, sz);
|
---|
| 215 | else TestFFTS(fs, *ffts, sz);
|
---|
| 216 | }
|
---|
[705] | 217 | }
|
---|
| 218 | catch(PThrowable exc ) {
|
---|
[715] | 219 | cerr << "TestFFT-main() , Catched exception: \n" << exc.Msg() << endl;
|
---|
[705] | 220 | }
|
---|
| 221 | catch(std::exception ex) {
|
---|
| 222 | cerr << "TestFFT-main() , Catched exception ! " << (string)(ex.what()) << endl;
|
---|
| 223 | }
|
---|
[715] | 224 |
|
---|
[705] | 225 | /*
|
---|
| 226 | catch(...) {
|
---|
| 227 | cerr << "TestFFT-main() , Catched ... exception ! " << endl;
|
---|
| 228 | }
|
---|
| 229 | */
|
---|
| 230 | PrtTim("End of tfft ");
|
---|
[715] | 231 | delete ffts;
|
---|
[705] | 232 | }
|
---|