def test_context_equals(): try: ctx1 = dpctl.SyclContext("gpu") ctx0 = dpctl.SyclContext("gpu") except ValueError: pytest.skip() assert ctx0 == ctx1
def test_context_multi_device(): try: d = dpctl.SyclDevice("cpu") except ValueError: pytest.skip() if d.default_selector_score < 0: pytest.skip() n = d.max_compute_units n1 = n // 2 n2 = n - n1 if n1 == 0 or n2 == 0: pytest.skip() d1, d2 = d.create_sub_devices(partition=(n1, n2)) ctx = dpctl.SyclContext((d1, d2)) assert ctx.device_count == 2 assert type(repr(ctx)) is str q1 = dpctl.SyclQueue(ctx, d1) q2 = dpctl.SyclQueue(ctx, d2) import dpctl.memory as dpmem shmem_1 = dpmem.MemoryUSMShared(256, queue=q1) shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2) shmem_2.copy_from_device(shmem_1) # create context for single sub-device ctx1 = dpctl.SyclContext(d1) q1 = dpctl.SyclQueue(ctx1, d1) shmem_1 = dpmem.MemoryUSMShared(256, queue=q1) cap = ctx1._get_capsule() cap2 = ctx1._get_capsule() del ctx1 del cap2 # exercise deleter of non-renamed capsule ctx2 = dpctl.SyclContext(cap) q2 = dpctl.SyclQueue(ctx2, d1) shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2) shmem_2.copy_from_device(shmem_1)
def test_context_not_equals(): try: ctx_gpu = dpctl.SyclContext("gpu") except ValueError: pytest.skip() try: ctx_cpu = dpctl.SyclContext("cpu") except ValueError: pytest.skip() assert ctx_cpu != ctx_gpu
def test_context_not_equals(): """ Test if context equality fails when devices in different contexts are compared. """ try: ctx_gpu = dpctl.SyclContext("gpu") except ValueError: pytest.skip() try: ctx_cpu = dpctl.SyclContext("cpu") except ValueError: pytest.skip() assert ctx_cpu != ctx_gpu assert hash(ctx_cpu) != hash(ctx_gpu)
def create_subdevice_queue(): """ Partition a CPU sycl device into sub-devices. Create a multi-device sycl context. """ cpu_d = dpctl.SyclDevice("cpu") cpu_count = cpu_d.max_compute_units sub_devs = cpu_d.create_sub_devices(partition=cpu_count // 2) multidevice_ctx = dpctl.SyclContext(sub_devs) # create a SyclQueue for each sub-device, using commont # multi-device context q0, q1 = [dpctl.SyclQueue(multidevice_ctx, d) for d in sub_devs] # for each sub-device allocate 26 bytes m0 = dpctl.memory.MemoryUSMDevice(26, queue=q0) m1 = dpctl.memory.MemoryUSMDevice(26, queue=q1) # populate m0 with host data of spaces hostmem = bytearray(b" " * 26) # copy spaces into m1 m1.copy_from_host(hostmem) for i in range(26): hostmem[i] = ord("a") + i # copy character sequence into m0 m0.copy_from_host(hostmem) # from from m0 to m1. Due to using multi-device context, # copying can be done directly m1.copy_from_device(m0) return bytes(m1.copy_to_host())
def test_invalid_filter_selectors(invalid_filter): """ An invalid filter string should always be caught and a SyclQueueCreationError raised. """ with pytest.raises(ValueError): dpctl.SyclContext(invalid_filter)
def test_address_of(): """ Test if the address_of method returns an int value. """ ctx = dpctl.SyclContext() assert ctx.addressof_ref() is not None assert isinstance(ctx.addressof_ref(), int)
def test_asarray_input_validation(): with pytest.raises(TypeError): # copy keyword is not of right type dpt.asarray([1], copy="invalid") with pytest.raises(TypeError): # order keyword is not valid dpt.asarray([1], order=1) with pytest.raises(TypeError): # dtype is not valid dpt.asarray([1], dtype="invalid") with pytest.raises(ValueError): # unexpected value of order dpt.asarray([1], order="Z") with pytest.raises(TypeError): # usm_type is of wrong type dpt.asarray([1], usm_type=dict()) with pytest.raises(ValueError): # usm_type has wrong value dpt.asarray([1], usm_type="mistake") with pytest.raises(TypeError): # sycl_queue type is not right dpt.asarray([1], sycl_queue=dpctl.SyclContext()) with pytest.raises(ValueError): # sequence is not rectangular dpt.asarray([[1], 2])
def test_context_not_equals2(): """ Test if comparing a SyclContext object to some random Python object is correctly handled and returns False. """ ctx = dpctl.SyclContext() assert ctx != "some context"
def test_hashing_of_context(): """ Test that a :class:`dpctl.SyclContext` object can be used as a dictionary key. """ ctx_dict = {dpctl.SyclContext(): "default_context"} assert ctx_dict
def test_ctxt_creation_from_filter(valid_filter): """ Test SyclContext creation using a filter selector string. """ try: dpctl.SyclContext(valid_filter) except ValueError: pytest.skip("Failed to create context with supported filter")
def test_context_can_be_used_in_queue(valid_filter): try: ctx = dpctl.SyclContext(valid_filter) except ValueError: pytest.skip() devs = ctx.get_devices() assert len(devs) == ctx.device_count for d in devs: dpctl.SyclQueue(ctx, d)
def test_constructor_many_arg(): with pytest.raises(TypeError): dpctl.SyclQueue(None, None, None, None) with pytest.raises(TypeError): dpctl.SyclQueue(None, None) ctx = dpctl.SyclContext() with pytest.raises(TypeError): dpctl.SyclQueue(ctx, None) with pytest.raises(TypeError): dpctl.SyclQueue(ctx)
def test_context_can_be_used_in_queue2(valid_filter): try: d = dpctl.SyclDevice(valid_filter) except ValueError: pytest.skip() if d.default_selector_score < 0: # skip test for devices rejected by default selector pytest.skip() ctx = dpctl.SyclContext(d) dpctl.SyclQueue(ctx, d)
def test_get_execution_queue_nonequiv(): try: q = dpctl.SyclQueue("cpu") d1, d2 = q.sycl_device.create_sub_devices(partition=[1, 1]) ctx = dpctl.SyclContext([q.sycl_device, d1, d2]) q1 = dpctl.SyclQueue(ctx, d1) q2 = dpctl.SyclQueue(ctx, d2) except dpctl.SyclQueueCreationError: pytest.skip("Queue could not be create for default device") exec_q = dpctl.utils.get_execution_queue((q, q1, q2)) assert exec_q is None
def test_valid_filter_selectors(valid_filter, check): """ Tests if we can create a SyclDevice using a supported filter selector string. """ device = None try: ctx = dpctl.SyclContext(valid_filter) device = ctx.get_devices() except ValueError: pytest.skip("Failed to create context with supported filter") check(device[0])
def create_queue_from_subdevice_multidevice_context(): """ Create a queue from a sub-device. """ cpu_d = dpctl.SyclDevice("opencl:cpu:0") sub_devs = cpu_d.create_sub_devices(partition=4) ctx = dpctl.SyclContext(sub_devs) q = dpctl.SyclQueue(ctx, sub_devs[0]) # a single-device context is created automatically print( "Number of devices in SyclContext " "associated with the queue: ", q.sycl_context.device_count, )
def test_standard_selectors(device_selector, check): """ Tests if the standard SYCL device_selectors are able to select a device. """ try: device = device_selector() if device.default_selector_score < 0: pytest.skip() ctx = dpctl.SyclContext(device) devs = ctx.get_devices() assert len(devs) == 1 check(devs[0]) except ValueError: pytest.skip()
def test_constructor_inconsistent_ctx_dev(): try: q = dpctl.SyclQueue("cpu") except dpctl.SyclQueueCreationError: pytest.skip("Failed to create CPU queue") cpuD = q.sycl_device n_eu = cpuD.max_compute_units n_half = n_eu // 2 try: d0, d1 = cpuD.create_sub_devices(partition=[n_half, n_eu - n_half]) except Exception: pytest.skip("Could not create CPU sub-devices") ctx = dpctl.SyclContext(d0) with pytest.raises(dpctl.SyclQueueCreationError): dpctl.SyclQueue(ctx, d1)
def test_cpython_api(): import ctypes import sys ctx = dpctl.SyclContext() mod = sys.modules[ctx.__class__.__module__] # get capsule storign get_context_ref function ptr ctx_ref_fn_cap = mod.__pyx_capi__["get_context_ref"] # construct Python callable to invoke "get_context_ref" cap_ptr_fn = ctypes.pythonapi.PyCapsule_GetPointer cap_ptr_fn.restype = ctypes.c_void_p cap_ptr_fn.argtypes = [ctypes.py_object, ctypes.c_char_p] ctx_ref_fn_ptr = cap_ptr_fn( ctx_ref_fn_cap, b"DPCTLSyclContextRef (struct PySyclContextObject *)" ) callable_maker = ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object) get_context_ref_fn = callable_maker(ctx_ref_fn_ptr) r2 = ctx.addressof_ref() r1 = get_context_ref_fn(ctx) assert r1 == r2
def test_context_multi_device(): try: d = dpctl.SyclDevice("cpu") except ValueError: pytest.skip() if d.default_selector_score < 0: pytest.skip() n = d.max_compute_units n1 = n // 2 n2 = n - n1 if n1 == 0 or n2 == 0: pytest.skip() d1, d2 = d.create_sub_devices(partition=(n1, n2)) ctx = dpctl.SyclContext((d1, d2)) assert ctx.device_count == 2 q1 = dpctl.SyclQueue(ctx, d1) q2 = dpctl.SyclQueue(ctx, d2) import dpctl.memory as dpmem shmem_1 = dpmem.MemoryUSMShared(256, queue=q1) shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2) shmem_2.copy_from_device(shmem_1)
def test_name(): """ Test if a __name__ method is defined for SyclContext. """ ctx = dpctl.SyclContext() assert ctx.__name__ == "SyclContext"
def test_memory_without_context(): mobj = _create_memory() # Without context assert mobj.get_usm_type() == "shared" assert mobj.get_usm_type(syclobj=dpctl.SyclContext()) == "shared"
def test_multi_device_different_platforms(): devs = dpctl.get_devices() # all devices if len(devs) > 1: with pytest.raises(ValueError): dpctl.SyclContext(devs)
def test_invalid_capsule(): cap = create_invalid_capsule() with pytest.raises(ValueError): dpctl.SyclContext(cap)
def test_context_repr(): ctx = dpctl.SyclContext() assert type(ctx.__repr__()) is str
def test_repr(): """ Test if a __repr__ method is defined for SyclContext. """ ctx = dpctl.SyclContext() assert ctx.__repr__ is not None