| 1 |
from array import array |
| 2 |
import math |
| 3 |
|
| 4 |
import pyperf |
| 5 |
from six.moves import xrange |
| 6 |
|
| 7 |
|
| 8 |
class Array2D(object): |
| 9 |
|
| 10 |
def __init__(self, w, h, data=None): |
| 11 |
self.width = w |
| 12 |
self.height = h |
| 13 |
self.data = array("d", [0]) * (w * h) |
| 14 |
if data is not None: |
| 15 |
self.setup(data) |
| 16 |
|
| 17 |
def _idx(self, x, y): |
| 18 |
return y * self.width + x |
| 19 |
|
| 20 |
if 0 <= x < self.width and 0 <= y < self.height: |
| 21 |
return y * self.width + x |
| 22 |
raise IndexError |
| 23 |
|
| 24 |
def __getitem__(self, x_y): |
| 25 |
(x, y) = x_y |
| 26 |
return self.data[self._idx(x, y)] |
| 27 |
|
| 28 |
def __setitem__(self, x_y, val): |
| 29 |
(x, y) = x_y |
| 30 |
self.data[self._idx(x, y)] = val |
| 31 |
|
| 32 |
def setup(self, data): |
| 33 |
for y in xrange(self.height): |
| 34 |
for x in xrange(self.width): |
| 35 |
self[x, y] = data[y][x] |
| 36 |
return self |
| 37 |
|
| 38 |
def indexes(self): |
| 39 |
for y in xrange(self.height): |
| 40 |
for x in xrange(self.width): |
| 41 |
yield x, y |
| 42 |
|
| 43 |
def copy_data_from(self, other): |
| 44 |
self.data[:] = other.data[:] |
| 45 |
|
| 46 |
|
| 47 |
class Random(object): |
| 48 |
MDIG = 32 |
| 49 |
ONE = 1 |
| 50 |
m1 = (ONE << (MDIG - 2)) + ((ONE << (MDIG - 2)) - ONE) |
| 51 |
m2 = ONE << MDIG // 2 |
| 52 |
dm1 = 1.0 / float(m1) |
| 53 |
|
| 54 |
def __init__(self, seed): |
| 55 |
self.initialize(seed) |
| 56 |
self.left = 0.0 |
| 57 |
self.right = 1.0 |
| 58 |
self.width = 1.0 |
| 59 |
self.haveRange = False |
| 60 |
|
| 61 |
def initialize(self, seed): |
| 62 |
|
| 63 |
self.seed = seed |
| 64 |
seed = abs(seed) |
| 65 |
jseed = min(seed, self.m1) |
| 66 |
if jseed % 2 == 0: |
| 67 |
jseed -= 1 |
| 68 |
k0 = 9069 % self.m2 |
| 69 |
k1 = 9069 / self.m2 |
| 70 |
j0 = jseed % self.m2 |
| 71 |
j1 = jseed / self.m2 |
| 72 |
self.m = array("d", [0]) * 17 |
| 73 |
for iloop in xrange(17): |
| 74 |
jseed = j0 * k0 |
| 75 |
j1 = (jseed / self.m2 + j0 * k1 + j1 * k0) % (self.m2 / 2) |
| 76 |
j0 = jseed % self.m2 |
| 77 |
self.m[iloop] = j0 + self.m2 * j1 |
| 78 |
self.i = 4 |
| 79 |
self.j = 16 |
| 80 |
|
| 81 |
def nextDouble(self): |
| 82 |
I, J, m = self.i, self.j, self.m |
| 83 |
k = m[I] - m[J] |
| 84 |
if k < 0: |
| 85 |
k += self.m1 |
| 86 |
self.m[J] = k |
| 87 |
|
| 88 |
if I == 0: |
| 89 |
I = 16 |
| 90 |
else: |
| 91 |
I -= 1 |
| 92 |
self.i = I |
| 93 |
|
| 94 |
if J == 0: |
| 95 |
J = 16 |
| 96 |
else: |
| 97 |
J -= 1 |
| 98 |
self.j = J |
| 99 |
|
| 100 |
if self.haveRange: |
| 101 |
return self.left + self.dm1 * float(k) * self.width |
| 102 |
else: |
| 103 |
return self.dm1 * float(k) |
| 104 |
|
| 105 |
def RandomMatrix(self, a): |
| 106 |
for x, y in a.indexes(): |
| 107 |
a[x, y] = self.nextDouble() |
| 108 |
return a |
| 109 |
|
| 110 |
def RandomVector(self, n): |
| 111 |
return array("d", [self.nextDouble() for i in xrange(n)]) |
| 112 |
|
| 113 |
|
| 114 |
def copy_vector(vec): |
| 115 |
|
| 116 |
vec2 = array("d") |
| 117 |
vec2[:] = vec[:] |
| 118 |
return vec2 |
| 119 |
|
| 120 |
|
| 121 |
class ArrayList(Array2D): |
| 122 |
|
| 123 |
def __init__(self, w, h, data=None): |
| 124 |
self.width = w |
| 125 |
self.height = h |
| 126 |
self.data = [array("d", [0]) * w for y in xrange(h)] |
| 127 |
if data is not None: |
| 128 |
self.setup(data) |
| 129 |
|
| 130 |
def __getitem__(self, idx): |
| 131 |
if isinstance(idx, tuple): |
| 132 |
return self.data[idx[1]][idx[0]] |
| 133 |
else: |
| 134 |
return self.data[idx] |
| 135 |
|
| 136 |
def __setitem__(self, idx, val): |
| 137 |
if isinstance(idx, tuple): |
| 138 |
self.data[idx[1]][idx[0]] = val |
| 139 |
else: |
| 140 |
self.data[idx] = val |
| 141 |
|
| 142 |
def copy_data_from(self, other): |
| 143 |
for l1, l2 in zip(self.data, other.data): |
| 144 |
l1[:] = l2 |
| 145 |
|
| 146 |
|
| 147 |
def SOR_execute(omega, G, cycles, Array): |
| 148 |
for p in xrange(cycles): |
| 149 |
for y in xrange(1, G.height - 1): |
| 150 |
for x in xrange(1, G.width - 1): |
| 151 |
G[x, y] = ( |
| 152 |
omega |
| 153 |
* 0.25 |
| 154 |
* (G[x, y - 1] + G[x, y + 1] + G[x - 1, y] + G[x + 1, y]) |
| 155 |
+ (1.0 - omega) * G[x, y] |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
def bench_SOR(loops, n, cycles, Array): |
| 160 |
range_it = xrange(loops) |
| 161 |
t0 = pyperf.perf_counter() |
| 162 |
|
| 163 |
for _ in range_it: |
| 164 |
G = Array(n, n) |
| 165 |
SOR_execute(1.25, G, cycles, Array) |
| 166 |
|
| 167 |
return pyperf.perf_counter() - t0 |
| 168 |
|
| 169 |
|
| 170 |
def SparseCompRow_matmult(M, y, val, row, col, x, num_iterations): |
| 171 |
range_it = xrange(num_iterations) |
| 172 |
t0 = pyperf.perf_counter() |
| 173 |
|
| 174 |
for _ in range_it: |
| 175 |
for r in xrange(M): |
| 176 |
sa = 0.0 |
| 177 |
for i in xrange(row[r], row[r + 1]): |
| 178 |
sa += x[col[i]] * val[i] |
| 179 |
y[r] = sa |
| 180 |
|
| 181 |
return pyperf.perf_counter() - t0 |
| 182 |
|
| 183 |
|
| 184 |
def bench_SparseMatMult(cycles, N, nz): |
| 185 |
x = array("d", [0]) * N |
| 186 |
y = array("d", [0]) * N |
| 187 |
|
| 188 |
nr = nz // N |
| 189 |
anz = nr * N |
| 190 |
val = array("d", [0]) * anz |
| 191 |
col = array("i", [0]) * nz |
| 192 |
row = array("i", [0]) * (N + 1) |
| 193 |
|
| 194 |
row[0] = 0 |
| 195 |
for r in xrange(N): |
| 196 |
rowr = row[r] |
| 197 |
step = r // nr |
| 198 |
row[r + 1] = rowr + nr |
| 199 |
if step < 1: |
| 200 |
step = 1 |
| 201 |
for i in xrange(nr): |
| 202 |
col[rowr + i] = i * step |
| 203 |
|
| 204 |
return SparseCompRow_matmult(N, y, val, row, col, x, cycles) |
| 205 |
|
| 206 |
|
| 207 |
def MonteCarlo(Num_samples): |
| 208 |
rnd = Random(113) |
| 209 |
under_curve = 0 |
| 210 |
for count in xrange(Num_samples): |
| 211 |
x = rnd.nextDouble() |
| 212 |
y = rnd.nextDouble() |
| 213 |
if x * x + y * y <= 1.0: |
| 214 |
under_curve += 1 |
| 215 |
return float(under_curve) / Num_samples * 4.0 |
| 216 |
|
| 217 |
|
| 218 |
def bench_MonteCarlo(loops, Num_samples): |
| 219 |
range_it = xrange(loops) |
| 220 |
t0 = pyperf.perf_counter() |
| 221 |
|
| 222 |
for _ in range_it: |
| 223 |
MonteCarlo(Num_samples) |
| 224 |
|
| 225 |
return pyperf.perf_counter() - t0 |
| 226 |
|
| 227 |
|
| 228 |
def LU_factor(A, pivot): |
| 229 |
M, N = A.height, A.width |
| 230 |
minMN = min(M, N) |
| 231 |
for j in xrange(minMN): |
| 232 |
jp = j |
| 233 |
t = abs(A[j][j]) |
| 234 |
for i in xrange(j + 1, M): |
| 235 |
ab = abs(A[i][j]) |
| 236 |
if ab > t: |
| 237 |
jp = i |
| 238 |
t = ab |
| 239 |
pivot[j] = jp |
| 240 |
|
| 241 |
if A[jp][j] == 0: |
| 242 |
raise Exception("factorization failed because of zero pivot") |
| 243 |
|
| 244 |
if jp != j: |
| 245 |
A[j], A[jp] = A[jp], A[j] |
| 246 |
|
| 247 |
if j < M - 1: |
| 248 |
recp = 1.0 / A[j][j] |
| 249 |
for k in xrange(j + 1, M): |
| 250 |
A[k][j] *= recp |
| 251 |
|
| 252 |
if j < minMN - 1: |
| 253 |
for ii in xrange(j + 1, M): |
| 254 |
for jj in xrange(j + 1, N): |
| 255 |
A[ii][jj] -= A[ii][j] * A[j][jj] |
| 256 |
|
| 257 |
|
| 258 |
def LU(lu, A, pivot): |
| 259 |
lu.copy_data_from(A) |
| 260 |
LU_factor(lu, pivot) |
| 261 |
|
| 262 |
|
| 263 |
def bench_LU(cycles, N): |
| 264 |
rnd = Random(7) |
| 265 |
A = rnd.RandomMatrix(ArrayList(N, N)) |
| 266 |
lu = ArrayList(N, N) |
| 267 |
pivot = array("i", [0]) * N |
| 268 |
range_it = xrange(cycles) |
| 269 |
t0 = pyperf.perf_counter() |
| 270 |
|
| 271 |
for _ in range_it: |
| 272 |
LU(lu, A, pivot) |
| 273 |
|
| 274 |
return pyperf.perf_counter() - t0 |
| 275 |
|
| 276 |
|
| 277 |
def int_log2(n): |
| 278 |
k = 1 |
| 279 |
log = 0 |
| 280 |
while k < n: |
| 281 |
k *= 2 |
| 282 |
log += 1 |
| 283 |
if n != 1 << log: |
| 284 |
raise Exception("FFT: Data length is not a power of 2: %s" % n) |
| 285 |
return log |
| 286 |
|
| 287 |
|
| 288 |
def FFT_num_flops(N): |
| 289 |
return (5.0 * N - 2) * int_log2(N) + 2 * (N + 1) |
| 290 |
|
| 291 |
|
| 292 |
def FFT_transform_internal(N, data, direction): |
| 293 |
n = N // 2 |
| 294 |
bit = 0 |
| 295 |
dual = 1 |
| 296 |
if n == 1: |
| 297 |
return |
| 298 |
|
| 299 |
logn = int_log2(n) |
| 300 |
if N == 0: |
| 301 |
return |
| 302 |
FFT_bitreverse(N, data) |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
bit = 0 |
| 307 |
while bit < logn: |
| 308 |
w_real = 1.0 |
| 309 |
w_imag = 0.0 |
| 310 |
theta = 2.0 * direction * math.pi / (2.0 * float(dual)) |
| 311 |
s = math.sin(theta) |
| 312 |
t = math.sin(theta / 2.0) |
| 313 |
s2 = 2.0 * t * t |
| 314 |
for b in range(0, n, 2 * dual): |
| 315 |
i = 2 * b |
| 316 |
j = 2 * (b + dual) |
| 317 |
wd_real = data[j] |
| 318 |
wd_imag = data[j + 1] |
| 319 |
data[j] = data[i] - wd_real |
| 320 |
data[j + 1] = data[i + 1] - wd_imag |
| 321 |
data[i] += wd_real |
| 322 |
data[i + 1] += wd_imag |
| 323 |
for a in xrange(1, dual): |
| 324 |
tmp_real = w_real - s * w_imag - s2 * w_real |
| 325 |
tmp_imag = w_imag + s * w_real - s2 * w_imag |
| 326 |
w_real = tmp_real |
| 327 |
w_imag = tmp_imag |
| 328 |
for b in range(0, n, 2 * dual): |
| 329 |
i = 2 * (b + a) |
| 330 |
j = 2 * (b + a + dual) |
| 331 |
z1_real = data[j] |
| 332 |
z1_imag = data[j + 1] |
| 333 |
wd_real = w_real * z1_real - w_imag * z1_imag |
| 334 |
wd_imag = w_real * z1_imag + w_imag * z1_real |
| 335 |
data[j] = data[i] - wd_real |
| 336 |
data[j + 1] = data[i + 1] - wd_imag |
| 337 |
data[i] += wd_real |
| 338 |
data[i + 1] += wd_imag |
| 339 |
bit += 1 |
| 340 |
dual *= 2 |
| 341 |
|
| 342 |
|
| 343 |
def FFT_bitreverse(N, data): |
| 344 |
n = N // 2 |
| 345 |
nm1 = n - 1 |
| 346 |
j = 0 |
| 347 |
for i in range(nm1): |
| 348 |
ii = i << 1 |
| 349 |
jj = j << 1 |
| 350 |
k = n >> 1 |
| 351 |
if i < j: |
| 352 |
tmp_real = data[ii] |
| 353 |
tmp_imag = data[ii + 1] |
| 354 |
data[ii] = data[jj] |
| 355 |
data[ii + 1] = data[jj + 1] |
| 356 |
data[jj] = tmp_real |
| 357 |
data[jj + 1] = tmp_imag |
| 358 |
while k <= j: |
| 359 |
j -= k |
| 360 |
k >>= 1 |
| 361 |
j += k |
| 362 |
|
| 363 |
|
| 364 |
def FFT_transform(N, data): |
| 365 |
FFT_transform_internal(N, data, -1) |
| 366 |
|
| 367 |
|
| 368 |
def FFT_inverse(N, data): |
| 369 |
n = N / 2 |
| 370 |
norm = 0.0 |
| 371 |
FFT_transform_internal(N, data, +1) |
| 372 |
norm = 1 / float(n) |
| 373 |
for i in xrange(N): |
| 374 |
data[i] *= norm |
| 375 |
|
| 376 |
|
| 377 |
def bench_FFT(loops, N, cycles): |
| 378 |
twoN = 2 * N |
| 379 |
init_vec = Random(7).RandomVector(twoN) |
| 380 |
range_it = xrange(loops) |
| 381 |
t0 = pyperf.perf_counter() |
| 382 |
|
| 383 |
for _ in range_it: |
| 384 |
x = copy_vector(init_vec) |
| 385 |
for i in xrange(cycles): |
| 386 |
FFT_transform(twoN, x) |
| 387 |
FFT_inverse(twoN, x) |
| 388 |
|
| 389 |
return pyperf.perf_counter() - t0 |
| 390 |
|
| 391 |
|
| 392 |
def add_cmdline_args(cmd, args): |
| 393 |
if args.benchmark: |
| 394 |
cmd.append(args.benchmark) |
| 395 |
|
| 396 |
|
| 397 |
BENCHMARKS = { |
| 398 |
|
| 399 |
"sor": (bench_SOR, 100, 10, Array2D), |
| 400 |
"sparse_mat_mult": (bench_SparseMatMult, 1000, 50 * 1000), |
| 401 |
"monte_carlo": ( |
| 402 |
bench_MonteCarlo, |
| 403 |
100 * 1000, |
| 404 |
), |
| 405 |
"lu": ( |
| 406 |
bench_LU, |
| 407 |
100, |
| 408 |
), |
| 409 |
"fft": (bench_FFT, 1024, 50), |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
if __name__ == "__main__": |
| 414 |
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args) |
| 415 |
runner.argparser.add_argument("benchmark", nargs="?", choices=sorted(BENCHMARKS)) |
| 416 |
|
| 417 |
args = runner.parse_args() |
| 418 |
if args.benchmark: |
| 419 |
benchmarks = (args.benchmark,) |
| 420 |
else: |
| 421 |
benchmarks = sorted(BENCHMARKS) |
| 422 |
|
| 423 |
for bench in benchmarks: |
| 424 |
name = "scimark_%s" % bench |
| 425 |
print(name) |
| 426 |
args = BENCHMARKS[bench] |
| 427 |
(args[0])(10, *args[1:]) |
| 428 |
|