def test_set_constant_array_errors(mock_4_device_context): context = mock_4_device_context api = API.from_api_id(mock_4_device_context.api.id) other_context = Context.from_criteria(api) other_queue = Queue(other_context.devices[0]) # Contexts don't know about each other and can't interact with stack in a consistent manner. # So we deactivate the other context if we're on CUDA API. if api.id == cuda_api_id(): other_context.deactivate() cm1 = numpy.arange(16).astype(numpy.int32) src = MockDefTemplate(kernels=[ MockKernel('kernel', [], max_total_local_sizes={ 0: 1024, 1: 1024, 2: 1024, 3: 1024 }) ], constant_mem={'cm1': cm1.size * cm1.dtype.itemsize}) queue = Queue(context.devices[0]) if context.api.id == cuda_api_id(): program = Program(context.devices, src, constant_arrays=dict(cm1=cm1)) with pytest.raises( ValueError, match= "The provided queue must belong to the same context as this program uses" ): program.set_constant_array(other_queue, 'cm1', cm1) with pytest.raises(TypeError, match="Unsupported array type"): program.set_constant_array(queue, 'cm1', [1]) with pytest.raises(ValueError, match="Incorrect size of the constant buffer;"): program.set_constant_array(queue, 'cm1', cm1[:8]) with pytest.raises(TypeError, match="Unknown constant array metadata type"): program = Program(context.devices[[0, 1, 2]], src, constant_arrays=dict(cm1=1)) program = Program(context.devices[[0, 1, 2]], src, constant_arrays=dict(cm1=cm1)) queue3 = Queue(context.devices[3]) with pytest.raises( ValueError, match= "The program was not compiled for the device this queue uses"): program.set_constant_array(queue3, 'cm1', cm1) else: with pytest.raises( ValueError, match= "Compile-time constant arrays are only supported by CUDA API"): program = Program(context.devices, src, constant_arrays=dict(cm1=cm1)) program = Program(context.devices, src) with pytest.raises( ValueError, match="Constant arrays are only supported for CUDA API"): program.set_constant_array(queue, 'cm1', cm1) with pytest.raises( ValueError, match= "Compile-time constant arrays are only supported by CUDA API"): sk = StaticKernel(context.devices, src, 'kernel', 1024, constant_arrays=dict(cm1=cm1)) sk = StaticKernel(context.devices, src, 'kernel', 1024) with pytest.raises( ValueError, match="Constant arrays are only supported for CUDA API"): sk.set_constant_array(queue, 'cm1', cm1)
def _test_constant_memory(context, mocked, is_static): cm1 = numpy.arange(16).astype(numpy.int32) cm2 = numpy.arange(16).astype(numpy.int32) * 2 + 1 cm3 = numpy.arange(16).astype(numpy.int32) * 3 + 2 if mocked: kernel = MockKernel('copy_from_cm', [None] if context.api.id == cuda_api_id() else [None, None, None, None], max_total_local_sizes={0: 1024}) src = MockDefTemplate(constant_mem={ 'cm1': cm1.size * cm1.dtype.itemsize, 'cm2': cm2.size * cm2.dtype.itemsize, 'cm3': cm3.size * cm3.dtype.itemsize }, kernels=[kernel]) else: src = SRC_CONSTANT_MEM_STATIC if is_static else SRC_CONSTANT_MEM queue = Queue(context.device) cm1_dev = Array.from_host(queue, cm1) cm2_dev = Array.from_host(queue, cm2) cm3_dev = Array.from_host(queue, cm3) res_dev = Array.empty(context.device, 16, numpy.int32) if context.api.id == cuda_api_id(): # Use different forms of constant array representation constant_arrays = dict( cm1=cm1, # as an array(-like) object cm2=(cm2.shape, cm2.dtype), # as a tuple of shape and dtype cm3=cm3_dev) # as a device array if is_static: copy_from_cm = StaticKernel(context.device, src, 'copy_from_cm', global_size=16, constant_arrays=constant_arrays) copy_from_cm.set_constant_array( queue, 'cm1', cm1_dev) # setting from a device array copy_from_cm.set_constant_array(queue, 'cm2', cm2) # setting from a host array copy_from_cm.set_constant_array( queue, 'cm3', cm3_dev.data) # setting from a host buffer else: program = Program(context.device, src, constant_arrays=constant_arrays) program.set_constant_array(queue, 'cm1', cm1_dev) # setting from a device array program.set_constant_array(queue, 'cm2', cm2) # setting from a host array program.set_constant_array( queue, 'cm3', cm3_dev.data) # setting from a host buffer copy_from_cm = lambda queue, *args: program.kernel.copy_from_cm( queue, 16, None, *args) copy_from_cm(queue, res_dev) else: if is_static: copy_from_cm = StaticKernel(context.device, src, 'copy_from_cm', global_size=16) else: program = Program(context.device, src) copy_from_cm = lambda queue, *args: program.kernel.copy_from_cm( queue, 16, None, *args) copy_from_cm(queue, res_dev, cm1_dev, cm2_dev, cm3_dev) res = res_dev.get(queue) if not mocked: assert (res == cm1 + cm2 + cm3).all()