Skip to content

Commit

Permalink
adds test_opencl_offloading
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushikcfd committed Jul 14, 2022
1 parent 5caea1b commit 3f834d3
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/unit/test_opencl_offloading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest
pytest.importorskip("pyopencl")

import sys
import petsc4py
petsc4py.init(sys.argv
+ "-viennacl_backend opencl".split()
+ "-viennacl_opencl_device_type cpu".split())
from pyop2 import op2
import pyopencl.array as cla
import numpy as np


def pytest_generate_tests(metafunc):
if "backend" in metafunc.fixturenames:
from pyop2.backends.opencl import opencl_backend
metafunc.parametrize("backend", [opencl_backend])


def test_new_backend_raises_not_implemented_error():
from pyop2.backends import AbstractComputeBackend
unimplemented_backend = AbstractComputeBackend()

attrs = ["GlobalKernel", "Parloop", "Set", "ExtrudedSet", "MixedSet",
"Subset", "DataSet", "MixedDataSet", "Map", "MixedMap", "Dat",
"MixedDat", "DatView", "Mat", "Global", "GlobalDataSet",
"PETScVecType"]

for attr in attrs:
with pytest.raises(NotImplementedError):
getattr(unimplemented_backend, attr)


def test_dat_with_petscvec_representation(backend):
op2.set_offloading_backend(backend)

nelems = 9
data = np.random.rand(nelems)
set_ = op2.compute_backend.Set(nelems)
dset = op2.compute_backend.DataSet(set_, 1)
dat = op2.compute_backend.Dat(dset, data.copy())

assert isinstance(dat.data_ro, np.ndarray)
dat.data[:] *= 3

with op2.offloading():
assert isinstance(dat.data_ro, cla.Array)
dat.data[:] *= 2

assert isinstance(dat.data_ro, np.ndarray)
np.testing.assert_allclose(dat.data_ro, 6*data)


def test_dat_not_as_petscvec(backend):
op2.set_offloading_backend(backend)

nelems = 9
data = np.random.randint(low=-10, high=10,
size=nelems,
dtype=np.int64)
set_ = op2.compute_backend.Set(nelems)
dset = op2.compute_backend.DataSet(set_, 1)
dat = op2.compute_backend.Dat(dset, data.copy())

assert isinstance(dat.data_ro, np.ndarray)
dat.data[:] *= 3

with op2.offloading():
assert isinstance(dat.data_ro, cla.Array)
dat.data[:] *= 2

assert isinstance(dat.data_ro, np.ndarray)
np.testing.assert_allclose(dat.data_ro, 6*data)

0 comments on commit 3f834d3

Please sign in to comment.