From c7bbe640d7c034005c6d5cb59522104bdbc08970 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Fri, 3 Jun 2016 09:29:39 -0500 Subject: [PATCH] Adjust test_enqueue_copy to pytest, PEP8 --- test/test_enqueue_copy.py | 351 +++++++++++++++++++------------------- 1 file changed, 180 insertions(+), 171 deletions(-) diff --git a/test/test_enqueue_copy.py b/test/test_enqueue_copy.py index a8c2ceae..6e760ea7 100644 --- a/test/test_enqueue_copy.py +++ b/test/test_enqueue_copy.py @@ -24,183 +24,192 @@ THE SOFTWARE. """ import numpy as np -import numpy.random - -import unittest import pyopencl as cl +import pytest + +from pyopencl.tools import ( # noqa + pytest_generate_tests_for_pyopencl as pytest_generate_tests) + def generate_slice(start, shape): return tuple([slice(start[i], start[i]+shape[i]) for i in range(len(start))]) -class EnqueueCopyRectTest(unittest.TestCase): + +def test_enqueue_copy_rect_2d(ctx_factory, honor_skip=True): """ - Tests to/from device copy of sub-arrays. + Test 2D sub-array (slice) copy. """ - - def setUp(self): - """ - Initialise context and command-queue. - """ - self.context = cl.create_some_context(interactive=False) - self.queue = cl.CommandQueue(self.context) - - def test2d(self): - """ - Test 2D sub-array (slice) copy. - """ - ary_in_shp = 256, 128 # Entire array shape from which sub-array copied to device - sub_ary_shp = 128, 96 # Sub-array shape to be copied to device - ary_in_origin = 20,13 # Sub-array origin - ary_in_slice = generate_slice(ary_in_origin, sub_ary_shp) - - ary_out_origin = 11,19 # Origin of sub-array copy from device to host-array - ary_out_shp = 512, 256 # Entire host-array shape copy sub-array from device to host - ary_out_slice = generate_slice(ary_out_origin, sub_ary_shp) - - buf_in_origin = 7,3 # Origin of sub-array in device buffer - buf_in_shp = 300, 200 # shape of device buffer - - buf_out_origin = 31,17 # Origin of 2nd device buffer - buf_out_shp = 300, 400 # shape of 2nd device buffer - - # Create host array of random values. - h_ary_in = \ - np.array( - np.random.randint( - 0, - 256, - np.product(ary_in_shp) - ), - dtype=np.uint8 - ).reshape(ary_in_shp) - - # Create device buffers - d_in_buf = cl.Buffer(self.context, cl.mem_flags.READ_ONLY, size=np.product(buf_in_shp)) - d_out_buf = cl.Buffer(self.context, cl.mem_flags.READ_ONLY, size=np.product(buf_out_shp)) - - # Copy sub-array (rectangular buffer) from host to device - cl.enqueue_copy( - self.queue, - d_in_buf, - h_ary_in, - buffer_origin=buf_in_origin[::-1], - host_origin=ary_in_origin[::-1], - region=sub_ary_shp[::-1], - buffer_pitches = (buf_in_shp[-1],), - host_pitches = (ary_in_shp[-1],) - ) - # Copy sub-array (rectangular buffer) from device-buffer to device-buffer - cl.enqueue_copy( - self.queue, - d_out_buf, - d_in_buf, - src_origin=buf_in_origin[::-1], - dst_origin=buf_out_origin[::-1], - region=sub_ary_shp[::-1], - src_pitches = (buf_in_shp[-1],), - dst_pitches = (buf_out_shp[-1],) - ) - - # Create zero-initialised array to receive sub-array from device - h_ary_out = np.zeros(ary_out_shp, dtype=h_ary_in.dtype) - - # Copy sub-array (rectangular buffer) from device to host-array. - cl.enqueue_copy( - self.queue, - h_ary_out, - d_out_buf, - buffer_origin=buf_out_origin[::-1], - host_origin=ary_out_origin[::-1], - region=sub_ary_shp[::-1], - buffer_pitches = (buf_out_shp[-1],), - host_pitches = (ary_out_shp[-1],) - ) - self.queue.finish() - - # Check that the sub-array copied to device is - # the same as the sub-array received from device. - self.assertTrue( - np.all(h_ary_in[ary_in_slice] == h_ary_out[ary_out_slice]) - ) - - def test3d(self): - """ - Test 3D sub-array (slice) copy. - """ - ary_in_shp = 256, 128, 31 # Entire array shape from which sub-array copied to device - sub_ary_shp = 128, 96, 20 # Sub-array shape to be copied to device - ary_in_origin = 20,13, 7 # Sub-array origin - ary_in_slice = generate_slice(ary_in_origin, sub_ary_shp) - - ary_out_origin = 11,19, 14 # Origin of sub-array copy from device to host-array - ary_out_shp = 192, 256, 128 # Entire host-array shape copy sub-array from device to host - ary_out_slice = generate_slice(ary_out_origin, sub_ary_shp) - - buf_in_origin = 7, 3, 6 # Origin of sub-array in device buffer - buf_in_shp = 300, 200, 30 # shape of device buffer - - buf_out_origin = 31,17,3 # Origin of 2nd device buffer - buf_out_shp = 300, 400, 40 # shape of 2nd device buffer - - # Create host array of random values. - h_ary_in = \ - np.array( - np.random.randint( - 0, - 256, - np.product(ary_in_shp) - ), - dtype=np.uint8 - ).reshape(ary_in_shp) - - # Create device buffers - d_in_buf = cl.Buffer(self.context, cl.mem_flags.READ_ONLY, size=np.product(buf_in_shp)) - d_out_buf = cl.Buffer(self.context, cl.mem_flags.READ_ONLY, size=np.product(buf_out_shp)) - - # Copy sub-array (rectangular buffer) from host to device - cl.enqueue_copy( - self.queue, - d_in_buf, - h_ary_in, - buffer_origin=buf_in_origin[::-1], - host_origin=ary_in_origin[::-1], - region=sub_ary_shp[::-1], - buffer_pitches = (buf_in_shp[-1],buf_in_shp[-1]*buf_in_shp[-2]), - host_pitches = (ary_in_shp[-1],ary_in_shp[-1]*ary_in_shp[-2]) - ) - # Copy sub-array (rectangular buffer) from device-buffer to device-buffer - cl.enqueue_copy( - self.queue, - d_out_buf, - d_in_buf, - src_origin=buf_in_origin[::-1], - dst_origin=buf_out_origin[::-1], - region=sub_ary_shp[::-1], - src_pitches = (buf_in_shp[-1],buf_in_shp[-1]*buf_in_shp[-2]), - dst_pitches = (buf_out_shp[-1],buf_out_shp[-1]*buf_out_shp[-2]) - ) - - # Create zero-initialised array to receive sub-array from device - h_ary_out = np.zeros(ary_out_shp, dtype=h_ary_in.dtype) - - # Copy sub-array (rectangular buffer) from device to host-array. - cl.enqueue_copy( - self.queue, - h_ary_out, - d_out_buf, - buffer_origin=buf_out_origin[::-1], - host_origin=ary_out_origin[::-1], - region=sub_ary_shp[::-1], - buffer_pitches = (buf_out_shp[-1],buf_out_shp[-1]*buf_out_shp[-2]), - host_pitches = (ary_out_shp[-1],ary_out_shp[-1]*ary_out_shp[-2]) - ) - self.queue.finish() - - # Check that the sub-array copied to device is - # the same as the sub-array received from device. - self.assertTrue( - np.all(h_ary_in[ary_in_slice] == h_ary_out[ary_out_slice]) - ) + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + + if honor_skip and ctx.devices[0].platform.name == "Portable Computing Language": + pytest.skip("POCL's rectangular copies crash") + + ary_in_shp = 256, 128 # Entire array shape from which sub-array copied to device + sub_ary_shp = 128, 96 # Sub-array shape to be copied to device + ary_in_origin = 20, 13 # Sub-array origin + ary_in_slice = generate_slice(ary_in_origin, sub_ary_shp) + + ary_out_origin = 11, 19 # Origin of sub-array copy from device to host-array + ary_out_shp = 512, 256 # Entire host-array shape copy sub-array device->host + ary_out_slice = generate_slice(ary_out_origin, sub_ary_shp) + + buf_in_origin = 7, 3 # Origin of sub-array in device buffer + buf_in_shp = 300, 200 # shape of device buffer + + buf_out_origin = 31, 17 # Origin of 2nd device buffer + buf_out_shp = 300, 400 # shape of 2nd device buffer + + # Create host array of random values. + h_ary_in = \ + np.array( + np.random.randint( + 0, + 256, + np.product(ary_in_shp) + ), + dtype=np.uint8 + ).reshape(ary_in_shp) + + # Create device buffers + d_in_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY, size=np.product(buf_in_shp)) + d_out_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY, size=np.product(buf_out_shp)) + + # Copy sub-array (rectangular buffer) from host to device + cl.enqueue_copy( + queue, + d_in_buf, + h_ary_in, + buffer_origin=buf_in_origin[::-1], + host_origin=ary_in_origin[::-1], + region=sub_ary_shp[::-1], + buffer_pitches=(buf_in_shp[-1],), + host_pitches=(ary_in_shp[-1],) + ) + # Copy sub-array (rectangular buffer) from device-buffer to device-buffer + cl.enqueue_copy( + queue, + d_out_buf, + d_in_buf, + src_origin=buf_in_origin[::-1], + dst_origin=buf_out_origin[::-1], + region=sub_ary_shp[::-1], + src_pitches=(buf_in_shp[-1],), + dst_pitches=(buf_out_shp[-1],) + ) + + # Create zero-initialised array to receive sub-array from device + h_ary_out = np.zeros(ary_out_shp, dtype=h_ary_in.dtype) + + # Copy sub-array (rectangular buffer) from device to host-array. + cl.enqueue_copy( + queue, + h_ary_out, + d_out_buf, + buffer_origin=buf_out_origin[::-1], + host_origin=ary_out_origin[::-1], + region=sub_ary_shp[::-1], + buffer_pitches=(buf_out_shp[-1],), + host_pitches=(ary_out_shp[-1],) + ) + queue.finish() + + # Check that the sub-array copied to device is + # the same as the sub-array received from device. + assert np.all(h_ary_in[ary_in_slice] == h_ary_out[ary_out_slice]) + + +def test_enqueue_copy_rect_3d(ctx_factory, honor_skip=False): + """ + Test 3D sub-array (slice) copy. + """ + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + + if honor_skip and ctx.devices[0].platform.name == "Portable Computing Language": + pytest.skip("POCL's rectangular copies crash") + + ary_in_shp = 256, 128, 31 # array shape from which sub-array copied to device + sub_ary_shp = 128, 96, 20 # Sub-array shape to be copied to device + ary_in_origin = 20, 13, 7 # Sub-array origin + ary_in_slice = generate_slice(ary_in_origin, sub_ary_shp) + + ary_out_origin = 11, 19, 14 # Origin of sub-array copy from device to host-array + ary_out_shp = 192, 256, 128 # Entire host-array shape copy sub-array dev->host + ary_out_slice = generate_slice(ary_out_origin, sub_ary_shp) + + buf_in_origin = 7, 3, 6 # Origin of sub-array in device buffer + buf_in_shp = 300, 200, 30 # shape of device buffer + + buf_out_origin = 31, 17, 3 # Origin of 2nd device buffer + buf_out_shp = 300, 400, 40 # shape of 2nd device buffer + + # Create host array of random values. + h_ary_in = \ + np.array( + np.random.randint( + 0, + 256, + np.product(ary_in_shp) + ), + dtype=np.uint8 + ).reshape(ary_in_shp) + + # Create device buffers + d_in_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY, size=np.product(buf_in_shp)) + d_out_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY, size=np.product(buf_out_shp)) + + # Copy sub-array (rectangular buffer) from host to device + cl.enqueue_copy( + queue, + d_in_buf, + h_ary_in, + buffer_origin=buf_in_origin[::-1], + host_origin=ary_in_origin[::-1], + region=sub_ary_shp[::-1], + buffer_pitches=(buf_in_shp[-1], buf_in_shp[-1]*buf_in_shp[-2]), + host_pitches=(ary_in_shp[-1], ary_in_shp[-1]*ary_in_shp[-2]) + ) + # Copy sub-array (rectangular buffer) from device-buffer to device-buffer + cl.enqueue_copy( + queue, + d_out_buf, + d_in_buf, + src_origin=buf_in_origin[::-1], + dst_origin=buf_out_origin[::-1], + region=sub_ary_shp[::-1], + src_pitches=(buf_in_shp[-1], buf_in_shp[-1]*buf_in_shp[-2]), + dst_pitches=(buf_out_shp[-1], buf_out_shp[-1]*buf_out_shp[-2]) + ) + + # Create zero-initialised array to receive sub-array from device + h_ary_out = np.zeros(ary_out_shp, dtype=h_ary_in.dtype) + + # Copy sub-array (rectangular buffer) from device to host-array. + cl.enqueue_copy( + queue, + h_ary_out, + d_out_buf, + buffer_origin=buf_out_origin[::-1], + host_origin=ary_out_origin[::-1], + region=sub_ary_shp[::-1], + buffer_pitches=(buf_out_shp[-1], buf_out_shp[-1]*buf_out_shp[-2]), + host_pitches=(ary_out_shp[-1], ary_out_shp[-1]*ary_out_shp[-2]) + ) + queue.finish() + + # Check that the sub-array copied to device is + # the same as the sub-array received from device. + assert np.array_equal(h_ary_in[ary_in_slice], h_ary_out[ary_out_slice]) + if __name__ == "__main__": - unittest.main() + # make sure that import failures get reported, instead of skipping the tests. + import pyopencl # noqa + + import sys + if len(sys.argv) > 1: + exec(sys.argv[1]) + else: + from py.test.cmdline import main + main([__file__]) -- GitLab