def test_max_total_local_sizes(mock_backend): mock_backend.add_devices( ["Device1", "Device2 - tag", "Device3 - tag", "Device4"]) api = API.from_api_id(mock_backend.api_id) context = Context.from_criteria(api, devices_num=2, device_include_masks=["tag"]) # Providing max_total_local_sizes for all possible devices to make sure # only the ones corresponding to the context will get picked up kernel = MockKernel('test', max_total_local_sizes={ 0: 64, 1: 1024, 2: 512, 3: 128 }) src = MockDefTemplate(kernels=[kernel]) program = Program(context.devices, src) # The indices here correspond to the devices in the context, not in the platform assert program.kernel.test.max_total_local_sizes == { context.devices[0]: 1024, context.devices[1]: 512 }
def test_from_criteria(mock_backend_pyopencl): backend = mock_backend_pyopencl backend.add_platform_with_devices('foo-bar', ['Device1']) backend.add_platform_with_devices('bar-baz', ['Device2']) backend.add_platform_with_devices('foo-baz', [ 'foo-bar', 'foo-baz-1', 'bar-baz', 'foo-baz-1', 'foo-baz-2', 'foo-baz-2', 'foo-baz-3' ]) api = API.from_api_id(backend.api_id) context = Context.from_criteria(api, devices_num=2, platform_include_masks=['foo'], platform_exclude_masks=['bar'], device_include_masks=['foo'], device_exclude_masks=['bar'], unique_devices_only=True) assert context.platform.name == 'foo-baz' assert [device.name for device in context.devices] == ['foo-baz-1', 'foo-baz-2']
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 make_context(backend, devices_num): api_id = backend.api_id backend.add_devices(['Device' + str(i) for i in range(devices_num)]) api = API.from_api_id(api_id) return Context.from_criteria(api, devices_num=devices_num)