-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconv3.cc
204 lines (178 loc) · 4.41 KB
/
conv3.cc
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
#include "Complex.h"
#include "convolution.h"
#include "utils.h"
#include "Array.h"
using namespace std;
using namespace Array;
using namespace fftwpp;
// g++ -g -O3 -DNDEBUG -fomit-frame-pointer -fstrict-aliasing -ffast-math -msse2 -mfpmath=sse conv3.cc fftw++.cc -lfftw3 -march=native
// icpc -O3 -ansi-alias -malign-double -fp-model fast=2 conv3.cc fftw++.cc -lfftw3
// FFTW:
// configure --enable-sse2 CC=icpc CFLAGS="-O3 -ansi-alias -malign-double -fp-model fast=2"
// Number of iterations.
unsigned int N0=10000000;
unsigned int N=0;
unsigned int nx=0;
unsigned int ny=0;
unsigned int nz=0;
unsigned int mx=4;
unsigned int my=4;
unsigned int mz=4;
unsigned int nxp;
unsigned int nyp;
unsigned int nzp;
unsigned int M=1;
bool Direct=false, Implicit=true;
unsigned int outlimit=300;
inline void init(array3<Complex>& f, array3<Complex>& g, unsigned int M=1)
{
unsigned int xstop=nxp;
double factor=1.0/sqrt((double) M);
for(unsigned int s=0; s < M; ++s) {
double S=sqrt(1.0+s);
double ffactor=S*factor;
double gfactor=1.0/S*factor;
for(unsigned int i=0; i < xstop; ++i) {
unsigned int I=s*xstop+i;
for(unsigned int j=0; j < nyp; ++j) {
for(unsigned int k=0; k < mz; ++k) {
f[I][j][k]=ffactor*Complex(i+k,j+k);
g[I][j][k]=gfactor*Complex(2*i+k,j+1+k);
}
}
}
}
}
unsigned int padding(unsigned int m)
{
unsigned int n=3*m-2;
cout << "min padded buffer=" << n << endl;
unsigned int log2n;
// Choose next power of 2 for maximal efficiency.
for(log2n=0; n > ((unsigned int) 1 << log2n); log2n++);
return 1 << log2n;
}
int main(int argc, char* argv[])
{
#ifndef __SSE2__
fftw::effort |= FFTW_NO_SIMD;
#endif
#ifdef __GNUC__
optind=0;
#endif
for (;;) {
int c = getopt(argc,argv,"hdeiptM:N:m:x:y:n:");
if (c == -1) break;
switch (c) {
case 0:
break;
case 'd':
Direct=true;
break;
case 'e':
Implicit=false;
break;
case 'i':
Implicit=true;
break;
case 'p':
Implicit=false;
break;
case 'M':
M=atoi(optarg);
break;
case 'N':
N=atoi(optarg);
break;
case 'm':
mx=my=mz=atoi(optarg);
break;
case 'x':
mx=atoi(optarg);
break;
case 'y':
my=atoi(optarg);
break;
case 'z':
mz=atoi(optarg);
break;
case 'n':
N0=atoi(optarg);
break;
case 'h':
default:
usage(3,false,false);
}
}
nx=padding(mx);
ny=padding(my);
nz=padding(mz);
cout << "nx=" << nx << ", ny=" << ny << ", nz=" << ny << endl;
cout << "mx=" << mx << ", my=" << my << ", mz=" << mz << endl;
if(N == 0) {
N=N0/(nx*ny*nz);
if(N < 10) N=10;
}
cout << "N=" << N << endl;
size_t align=sizeof(Complex);
nxp=2*mx-1;
nyp=2*my-1;
nzp=mz;
unsigned int nxp0=Implicit ? nxp*M : nxp;
array3<Complex> f(nxp0,nyp,nzp,align);
array3<Complex> g(nxp0,nyp,nzp,align);
double *T=new double[N];
if(Implicit) {
ImplicitHConvolution3 C(mx,my,mz,M);
Complex **F=new Complex *[M];
Complex **G=new Complex *[M];
unsigned int mf=nxp*nyp*nzp;
for(unsigned int s=0; s < M; ++s) {
unsigned int smf=s*mf;
F[s]=f+smf;
G[s]=g+smf;
}
for(unsigned int i=0; i < N; ++i) {
init(f,g,M);
seconds();
C.convolve(F,G);
// C.convolve(f,g);
T[i]=seconds();
}
timings("Implicit",T,N);
if(nxp*nyp*mz < outlimit) {
for(unsigned int i=0; i < nxp; ++i) {
for(unsigned int j=0; j < nyp; ++j) {
for(unsigned int k=0; k < mz; ++k)
cout << f[i][j][k] << "\t";
cout << endl;
}
cout << endl;
}
} else cout << f[0][0][0] << endl;
delete [] G;
delete [] F;
}
if(Direct) {
array3<Complex> h(nxp,nyp,mz,align);
array3<Complex> f(nxp,nyp,mz,align);
array3<Complex> g(nxp,nyp,mz,align);
DirectHConvolution3 C(mx,my,mz);
init(f,g);
seconds();
C.convolve(h,f,g);
T[0]=seconds();
timings("Direct",T,1);
if(nxp*nyp*mz < outlimit)
for(unsigned int i=0; i < nxp; ++i) {
for(unsigned int j=0; j < nyp; ++j) {
for(unsigned int k=0; k < mz; ++k)
cout << h[i][j][k] << "\t";
cout << endl;
}
cout << endl;
}
else cout << h[0][0][0] << endl;
}
delete [] T;
}