|
| 1 | +"""Benchmark and compare mie_nojit and mie_jit backend performance.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from time import perf_counter |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | + |
| 9 | +from miepython import mie_jit, mie_nojit |
| 10 | + |
| 11 | +RUN_BENCH = os.environ.get("MIEPYTHON_RUN_MIE_SPEED", "0") == "1" |
| 12 | + |
| 13 | + |
| 14 | +def _median_runtime(func, repeats=3): |
| 15 | + """Return median runtime and final function output.""" |
| 16 | + timings = [] |
| 17 | + last = None |
| 18 | + for _ in range(repeats): |
| 19 | + t0 = perf_counter() |
| 20 | + last = func() |
| 21 | + timings.append(perf_counter() - t0) |
| 22 | + return float(np.median(timings)), last |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.skipif(not RUN_BENCH, reason="Set MIEPYTHON_RUN_MIE_SPEED=1 to run mie backend speed benchmark") |
| 26 | +def test_mie_backend_speed_compare(): |
| 27 | + """Compare runtime and output agreement for no-JIT and JIT Mie backends.""" |
| 28 | + rng = np.random.default_rng(12345) |
| 29 | + |
| 30 | + n_particles = 4000 |
| 31 | + refr = rng.uniform(1.2, 2.0, n_particles) |
| 32 | + refi = np.exp(rng.uniform(np.log(1e-4), np.log(5e-1), n_particles)) |
| 33 | + xvals = np.exp(rng.uniform(np.log(5e-2), np.log(50), n_particles)) |
| 34 | + mvals = refr - 1j * refi |
| 35 | + |
| 36 | + mu = np.linspace(-1.0, 1.0, 361) |
| 37 | + m_ref = 1.5 - 0.05j |
| 38 | + x_ref = 12.0 |
| 39 | + |
| 40 | + # Warm up numba kernels so benchmark times reflect steady-state runtime. |
| 41 | + mie_jit._single_sphere_nb(np.complex128(mvals[0]), float(xvals[0]), 0, True) |
| 42 | + mie_jit._S1_S2_nb(np.complex128(m_ref), float(x_ref), mu, 0) |
| 43 | + |
| 44 | + def run_single_nojit(): |
| 45 | + out = np.empty((4, n_particles), dtype=np.float64) |
| 46 | + for i in range(n_particles): |
| 47 | + out[:, i] = mie_nojit._single_sphere_py(mvals[i], float(xvals[i]), 0, True) |
| 48 | + return out |
| 49 | + |
| 50 | + def run_single_jit(): |
| 51 | + out = np.empty((4, n_particles), dtype=np.float64) |
| 52 | + for i in range(n_particles): |
| 53 | + out[:, i] = mie_jit._single_sphere_nb(np.complex128(mvals[i]), float(xvals[i]), 0, True) |
| 54 | + return out |
| 55 | + |
| 56 | + def run_s12_nojit(): |
| 57 | + return mie_nojit._S1_S2_py(m_ref, x_ref, mu, 0) |
| 58 | + |
| 59 | + def run_s12_jit(): |
| 60 | + return mie_jit._S1_S2_nb(np.complex128(m_ref), float(x_ref), mu, 0) |
| 61 | + |
| 62 | + t_single_nojit, single_nojit = _median_runtime(run_single_nojit, repeats=3) |
| 63 | + t_single_jit, single_jit = _median_runtime(run_single_jit, repeats=3) |
| 64 | + |
| 65 | + t_s12_nojit, s12_nojit = _median_runtime(run_s12_nojit, repeats=5) |
| 66 | + t_s12_jit, s12_jit = _median_runtime(run_s12_jit, repeats=5) |
| 67 | + |
| 68 | + np.testing.assert_allclose(single_nojit, single_jit, rtol=1e-10, atol=1e-12) |
| 69 | + np.testing.assert_allclose(s12_nojit[0], s12_jit[0], rtol=1e-10, atol=1e-12) |
| 70 | + np.testing.assert_allclose(s12_nojit[1], s12_jit[1], rtol=1e-10, atol=1e-12) |
| 71 | + |
| 72 | + print(f"single_sphere nojit median: {t_single_nojit:.4f} s") |
| 73 | + print(f"single_sphere jit median: {t_single_jit:.4f} s") |
| 74 | + print(f"single_sphere speedup: {t_single_nojit / t_single_jit:.2f}x") |
| 75 | + print(f"S1_S2 nojit median: {t_s12_nojit:.4f} s") |
| 76 | + print(f"S1_S2 jit median: {t_s12_jit:.4f} s") |
| 77 | + print(f"S1_S2 speedup: {t_s12_nojit / t_s12_jit:.2f}x") |
| 78 | + |
| 79 | + assert t_single_nojit > 0.0 |
| 80 | + assert t_single_jit > 0.0 |
| 81 | + assert t_s12_nojit > 0.0 |
| 82 | + assert t_s12_jit > 0.0 |
0 commit comments