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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#define BZ_DISABLE_RESTRICT
#define BZ_ARRAY_2D_NEW_STENCIL_TILING
#include <blitz/array.h>
#include <blitz/timer.h>
#ifdef BZ_HAVE_STD
#include <fstream>
#else
#include <fstream.h>
#endif
BZ_USING_NAMESPACE(blitz)
#if defined(BZ_FORTRAN_SYMBOLS_WITH_TRAILING_UNDERSCORES)
#define echo_f90 echo_f90_
#define echo_f77 echo_f77_
#define echo_f90_tuned echo_f90_tuned_
#define echo_f77tuned echo_f77tuned_
#elif defined(BZ_FORTRAN_SYMBOLS_WITH_DOUBLE_TRAILING_UNDERSCORES)
#define echo_f90 echo_f90__
#define echo_f77 echo_f77__
#define echo_f90_tuned echo_f90_tuned__
#define echo_f77tuned echo_f77tuned__
#elif defined(BZ_FORTRAN_SYMBOLS_CAPS)
#define echo_f90 ECHO_F90
#define echo_f77 ECHO_F77
#define echo_f90_tuned ECHO_F90_TUNED
#define echo_f77tuned ECHO_F77TUNED
#endif
extern "C" {
void echo_f90(int& N, int& niters, float& check);
void echo_f77(int& N, int& niters, float& check);
void echo_f90_tuned(int& N, int& niters, float& check);
void echo_f77tuned(int& N, int& niters, float& check);
}
float echo_BlitzInterlacedCycled(int N, int niters);
float echo_BlitzCycled(int N, int niters);
float echo_BlitzRaw(int N, int niters);
float echo_BlitzStencil(int N, int niters);
int main()
{
Timer timer;
int N = 650;
int niters = 60;
float check;
cout << "Acoustic 2D Benchmark" << endl << endl;
double Mflops = (N-2)*(N-2) * 9.0 * niters / 1.0e+6;
timer.start();
check = echo_BlitzRaw(N, niters);
timer.stop();
cout << "Blitz++ (raw): " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
timer.start();
check = echo_BlitzStencil(N, niters);
timer.stop();
cout << "Blitz++ (stencil): " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
#if 0
timer.start();
check = echo_BlitzInterlaced(N, niters, c);
timer.stop();
cout << "Blitz++ (interlaced): " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
#endif
timer.start();
check = echo_BlitzCycled(N, niters);
timer.stop();
cout << "Blitz++ (cycled): " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
timer.start();
check = echo_BlitzInterlacedCycled(N, niters);
timer.stop();
cout << "Blitz++ (interlaced & cycled): " << timer.elapsedSeconds()
<< " s check = " << check
<< " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
#ifdef FORTRAN_90
timer.start();
echo_f90(N, niters, check);
timer.stop();
cout << "Fortran 90: " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
timer.start();
echo_f90_tuned(N, niters, check);
timer.stop();
cout << "Fortran 90 (tuned): " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
#endif
timer.start();
echo_f77(N, niters, check);
timer.stop();
cout << "Fortran 77: " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
timer.start();
echo_f77tuned(N, niters, check);
timer.stop();
cout << "Fortran 77 (tuned): " << timer.elapsedSeconds() << " s check = "
<< check << " Mflops = " << (Mflops/timer.elapsedSeconds())
<< endl << endl;
return 0;
}
void checkArray(Array<float,2>& A, int N)
{
float check = 0.0;
for (int i=0; i < N; ++i)
for (int j=0; j < N; ++j)
check += ((i+1)*N + j + 1) * A(i,j);
cout << "Array check: " << check << endl;
}
void setInitialConditions(Array<float,2>& c, Array<float,2>& P1,
Array<float,2>& P2, Array<float,2>& P3, int N);
float echo_BlitzRaw(int N, int niters)
{
Array<float,2> P1(N,N), P2(N,N), P3(N,N), c(N,N);
Range I(1,N-2), J(1,N-2);
setInitialConditions(c, P1, P2, P3, N);
checkArray(P2, N);
checkArray(c, N);
for (int iter=0; iter < niters; ++iter)
{
P3(I,J) = (2-4*c(I,J)) * P2(I,J)
+ c(I,J)*(P2(I-1,J) + P2(I+1,J) + P2(I,J-1) + P2(I,J+1))
- P1(I,J);
P1 = P2;
P2 = P3;
}
#if 0
ofstream ofs("testecho.m");
ofs << "A = [";
for (int i=0; i < N; ++i)
{
for (int j=0; j < N; ++j)
{
ofs << int(8192*P2(i,j)+1024*c(i,j)) << " ";
}
if (i < N-1)
ofs << ";" << endl;
}
ofs << "];" << endl;
#endif
return P1(N/2-1,(7*N)/8-1);
}
float echo_BlitzCycled(int N, int niters)
{
Array<float,2> P1(N,N), P2(N,N), P3(N,N), c(N,N);
Range I(1,N-2), J(1,N-2);
setInitialConditions(c, P1, P2, P3, N);
checkArray(P2, N);
checkArray(c, N);
for (int iter=0; iter < niters; ++iter)
{
P3(I,J) = (2-4*c(I,J)) * P2(I,J)
+ c(I,J)*(P2(I-1,J) + P2(I+1,J) + P2(I,J-1) + P2(I,J+1))
- P1(I,J);
cycleArrays(P1,P2,P3);
}
return P1(N/2-1,(7*N)/8-1);
}
float echo_BlitzInterlacedCycled(int N, int niters)
{
Array<float,2> P1, P2, P3, c;
allocateArrays(shape(N,N), P1, P2, P3, c);
Range I(1,N-2), J(1,N-2);
setInitialConditions(c, P1, P2, P3, N);
checkArray(P2, N);
checkArray(c, N);
for (int iter=0; iter < niters; ++iter)
{
P3(I,J) = (2-4*c(I,J)) * P2(I,J)
+ c(I,J)*(P2(I-1,J) + P2(I+1,J) + P2(I,J-1) + P2(I,J+1))
- P1(I,J);
cycleArrays(P1,P2,P3);
}
return P1(N/2-1,(7*N)/8-1);
}
BZ_DECLARE_STENCIL4(acoustic2D,P1,P2,P3,c)
P3 = 2 * P2 + c * Laplacian2D(P2) - P1;
BZ_STENCIL_END
float echo_BlitzStencil(int N, int niters)
{
Array<float,2> P1, P2, P3, c;
allocateArrays(shape(N,N), P1, P2, P3, c);
setInitialConditions(c, P1, P2, P3, N);
checkArray(P2, N);
checkArray(c, N);
for (int iter=0; iter < niters; ++iter)
{
applyStencil(acoustic2D(), P1, P2, P3, c);
cycleArrays(P1,P2,P3);
}
return P1(N/2-1,(7*N)/8-1);
}
void setInitialConditions(Array<float,2>& c, Array<float,2>& P1,
Array<float,2>& P2, Array<float,2>& P3, int N)
{
// Set the velocity field
c = 0.2;
// Solid block with which the pulse collides
int blockLeft = 0;
int blockRight = int(2*N/5.0-1);
int blockTop = int(N/3-1);
int blockBottom = int(2*N/3.0-1);
c(Range(blockTop,blockBottom),Range(blockLeft,blockRight)) = 0.5;
// Channel directing the pulse leftwards
int channelLeft = int(4*N/5.0-1);
int channelRight = N-1;
int channel1Height = int(3*N/8.0-1);
int channel2Height = int(5*N/8.0-1);
c(channel1Height,Range(channelLeft,channelRight)) = 0.0;
c(channel2Height,Range(channelLeft,channelRight)) = 0.0;
// Initial pressure distribution: gaussian pulse inside the channel
BZ_USING_NAMESPACE(blitz::tensor)
int cr = int(N/2-1);
int cc = int(7.0*N/8.0-1);
float s2 = 64.0 * 9.0 / pow2(N/2.0);
cout << "cr = " << cr << " cc = " << cc << " s2 = " << s2 << endl;
P1 = 0.0;
P2 = exp(-(pow2(i-cr)+pow2(j-cc)) * s2);
P3 = 0.0;
}
|