from __future__ import division, absolute_import, print_function __copyright__ = """ Copyright (C) 2012 Andreas Kloeckner Copyright (C) 2016, 2017 Matt Wala """ __license__ = """ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import sys import numpy as np import loopy as lp import pyopencl as cl import pyopencl.clmath # noqa import pyopencl.clrandom # noqa import pytest import logging logger = logging.getLogger(__name__) try: import faulthandler except ImportError: pass else: faulthandler.enable() from pyopencl.tools import pytest_generate_tests_for_pyopencl \ as pytest_generate_tests __all__ = [ "pytest_generate_tests", "cl" # 'cl.create_some_context' ] # More things to test. # - scan(a) + scan(b) # - test for badly tagged inames LOOPY_KERNEL_LANGUAGE_VERSION = (2018, 1) @pytest.mark.parametrize("n", [1, 2, 3, 16]) @pytest.mark.parametrize("stride", [1, 2]) def test_sequential_scan(ctx_factory, n, stride): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "[n] -> {[i,j]: 0<=i " "{[i,j]: sweep_lbound<=i {[i]: 0<=i {[i,j,k]: 0<=k {[i]: 0<=i {[i]: 0 <= i < n}", "[i] -> {[j]: 0 <= j <= i}", "[i] -> {[k]: 0 <= k <= i}" ], """ <>tmp[i] = sum(k, 1) out[i] = sum(j, tmp[j]) """) knl = lp.fix_parameters(knl, n=10) knl = lp.tag_inames(knl, dict(i=i_tag, j=j_tag)) knl = lp.realize_reduction(knl, force_scan=True) print(knl) evt, (out,) = knl(queue) print(out) def test_scan_not_triangular(): knl = lp.make_kernel( "{[i,j]: 0<=i<100 and 1<=j<=2*i}", """ a[i] = sum(j, j**2) """ ) with pytest.raises(lp.diagnostic.ReductionIsNotTriangularError): knl = lp.realize_reduction(knl, force_scan=True) @pytest.mark.parametrize("n", [1, 2, 3, 16, 17]) def test_local_parallel_scan(ctx_factory, n): ctx = ctx_factory() queue = cl.CommandQueue(ctx) knl = lp.make_kernel( "[n] -> {[i,j]: 0<=i {[i,j]: 1<=i {[i,j]: 0<=i_ = reduce(segmented(sum), j, arr[j], segflag[j])", [ lp.GlobalArg("arr", np.float32, shape=("n",)), lp.GlobalArg("segflag", np.int32, shape=("n",)), "..." ]) knl = lp.fix_parameters(knl, n=n) knl = lp.tag_inames(knl, dict(i=iname_tag)) knl = lp.realize_reduction(knl, force_scan=True) (evt, (out,)) = knl(queue, arr=arr, segflag=segment_boundaries) check_segmented_scan_output(arr, segment_boundaries_indices, out) if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) else: from py.test.cmdline import main main([__file__]) # vim: foldmethod=marker