|
| 1 | +import pytest |
| 2 | +pytest.importorskip("pyopencl") |
| 3 | + |
| 4 | +import sys |
| 5 | +import petsc4py |
| 6 | +petsc4py.init(sys.argv |
| 7 | + + "-viennacl_backend opencl".split() |
| 8 | + + "-viennacl_opencl_device_type cpu".split()) |
| 9 | +from pyop2 import op2 |
| 10 | +import pyopencl.array as cla |
| 11 | +import numpy as np |
| 12 | +import loopy as lp |
| 13 | +lp.set_caching_enabled(False) |
| 14 | + |
| 15 | + |
| 16 | +def allclose(a, b, rtol=1e-05, atol=1e-08): |
| 17 | + """ |
| 18 | + Prefer this routine over np.allclose(...) to allow pycuda/pyopencl arrays |
| 19 | + """ |
| 20 | + return bool(abs(a - b) < (atol + rtol * abs(b))) |
| 21 | + |
| 22 | + |
| 23 | +def pytest_generate_tests(metafunc): |
| 24 | + if "backend" in metafunc.fixturenames: |
| 25 | + from pyop2.backends.opencl import opencl_backend |
| 26 | + metafunc.parametrize("backend", [opencl_backend]) |
| 27 | + |
| 28 | + |
| 29 | +def test_new_backend_raises_not_implemented_error(): |
| 30 | + from pyop2.backends import AbstractComputeBackend |
| 31 | + unimplemented_backend = AbstractComputeBackend() |
| 32 | + |
| 33 | + attrs = ["GlobalKernel", "Parloop", "Set", "ExtrudedSet", "MixedSet", |
| 34 | + "Subset", "DataSet", "MixedDataSet", "Map", "MixedMap", "Dat", |
| 35 | + "MixedDat", "DatView", "Mat", "Global", "GlobalDataSet", |
| 36 | + "PETScVecType"] |
| 37 | + |
| 38 | + for attr in attrs: |
| 39 | + with pytest.raises(NotImplementedError): |
| 40 | + getattr(unimplemented_backend, attr) |
| 41 | + |
| 42 | + |
| 43 | +def test_dat_with_petscvec_representation(backend): |
| 44 | + op2.set_offloading_backend(backend) |
| 45 | + |
| 46 | + nelems = 9 |
| 47 | + data = np.random.rand(nelems) |
| 48 | + set_ = op2.compute_backend.Set(nelems) |
| 49 | + dset = op2.compute_backend.DataSet(set_, 1) |
| 50 | + dat = op2.compute_backend.Dat(dset, data.copy()) |
| 51 | + |
| 52 | + assert isinstance(dat.data_ro, np.ndarray) |
| 53 | + dat.data[:] *= 3 |
| 54 | + |
| 55 | + with op2.offloading(): |
| 56 | + assert isinstance(dat.data_ro, cla.Array) |
| 57 | + dat.data[:] *= 2 |
| 58 | + |
| 59 | + assert isinstance(dat.data_ro, np.ndarray) |
| 60 | + np.testing.assert_allclose(dat.data_ro, 6*data) |
| 61 | + |
| 62 | + |
| 63 | +def test_dat_not_as_petscvec(backend): |
| 64 | + op2.set_offloading_backend(backend) |
| 65 | + |
| 66 | + nelems = 9 |
| 67 | + data = np.random.randint(low=-10, high=10, |
| 68 | + size=nelems, |
| 69 | + dtype=np.int64) |
| 70 | + set_ = op2.compute_backend.Set(nelems) |
| 71 | + dset = op2.compute_backend.DataSet(set_, 1) |
| 72 | + dat = op2.compute_backend.Dat(dset, data.copy()) |
| 73 | + |
| 74 | + assert isinstance(dat.data_ro, np.ndarray) |
| 75 | + dat.data[:] *= 3 |
| 76 | + |
| 77 | + with op2.offloading(): |
| 78 | + assert isinstance(dat.data_ro, cla.Array) |
| 79 | + dat.data[:] *= 2 |
| 80 | + |
| 81 | + assert isinstance(dat.data_ro, np.ndarray) |
| 82 | + np.testing.assert_allclose(dat.data_ro, 6*data) |
| 83 | + |
| 84 | + |
| 85 | +def test_global_reductions(backend): |
| 86 | + op2.set_offloading_backend(backend) |
| 87 | + |
| 88 | + sum_knl = lp.make_function( |
| 89 | + "{ : }", |
| 90 | + """ |
| 91 | + g[0] = g[0] + x[0] |
| 92 | + """, |
| 93 | + [lp.GlobalArg("g,x", |
| 94 | + dtype="float64", |
| 95 | + shape=lp.auto, |
| 96 | + )], |
| 97 | + name="sum_knl", |
| 98 | + target=lp.CWithGNULibcTarget(), |
| 99 | + lang_version=(2018, 2)) |
| 100 | + |
| 101 | + rng = np.random.default_rng() |
| 102 | + nelems = 4_000 |
| 103 | + data_to_sum = rng.random(nelems) |
| 104 | + |
| 105 | + with op2.offloading(): |
| 106 | + |
| 107 | + set_ = op2.compute_backend.Set(4_000) |
| 108 | + dset = op2.compute_backend.DataSet(set_, 1) |
| 109 | + |
| 110 | + dat = op2.compute_backend.Dat(dset, data_to_sum.copy()) |
| 111 | + glob = op2.compute_backend.Global(1, 0, np.float64, "g") |
| 112 | + |
| 113 | + op2.parloop(op2.Kernel(sum_knl, "sum_knl"), |
| 114 | + set_, |
| 115 | + glob(op2.INC), |
| 116 | + dat(op2.READ)) |
| 117 | + |
| 118 | + assert isinstance(glob.data_ro, cla.Array) |
| 119 | + assert allclose(glob.data_ro[0], data_to_sum.sum()) |
0 commit comments