class TestGetCurrentQueueInMultipleThreads(unittest.TestCase): def test_num_current_queues_outside_with_clause(self): self.assertEqual(dpctl.get_num_activated_queues(), 0) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_num_current_queues_inside_with_clause(self): with dpctl.device_context("opencl:cpu:0"): self.assertEqual(dpctl.get_num_activated_queues(), 1) with dpctl.device_context("opencl:gpu:0"): self.assertEqual(dpctl.get_num_activated_queues(), 2) self.assertEqual(dpctl.get_num_activated_queues(), 0) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_num_current_queues_inside_threads(self): from threading import Thread def SessionThread(self): self.assertEqual(dpctl.get_num_activated_queues(), 0) with dpctl.device_context("opencl:gpu:0"): self.assertEqual(dpctl.get_num_activated_queues(), 1) Session1 = Thread(target=SessionThread(self)) Session2 = Thread(target=SessionThread(self)) with dpctl.device_context("opencl:cpu:0"): self.assertEqual(dpctl.get_num_activated_queues(), 1) Session1.start() Session2.start()
class TestGetCurrentDevice(unittest.TestCase): def test_get_current_device_type_outside_device_ctxt(self): self.assertNotEqual(dpctl.get_current_device_type(), None) def test_get_current_device_type_inside_device_ctxt(self): self.assertNotEqual(dpctl.get_current_device_type(), None) with dpctl.device_context("opencl:gpu:0"): self.assertEqual(dpctl.get_current_device_type(), dpctl.device_type.gpu) self.assertNotEqual(dpctl.get_current_device_type(), None) @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_get_current_device_type_inside_nested_device_ctxt(self): self.assertNotEqual(dpctl.get_current_device_type(), None) with dpctl.device_context("opencl:cpu:0"): self.assertEqual(dpctl.get_current_device_type(), dpctl.device_type.cpu) with dpctl.device_context("opencl:gpu:0"): self.assertEqual(dpctl.get_current_device_type(), dpctl.device_type.gpu) self.assertEqual(dpctl.get_current_device_type(), dpctl.device_type.cpu) self.assertNotEqual(dpctl.get_current_device_type(), None)
def main(): parser = argparse.ArgumentParser(description="Black-Scholes") parser.add_argument("--options", dest="options", type=int, default=10000000) args = parser.parse_args() options = args.options if dpctl.has_gpu_queues(): print("\nScheduling on OpenCL GPU\n") with dpctl.device_context("opencl:gpu") as gpu_queue: run(10) else: print("\nSkip scheduling on OpenCL GPU\n") # if dpctl.has_gpu_queues(dpctl.backend_type.level_zero): # print("\nScheduling on Level Zero GPU\n") # with dpctl.device_context("level0:gpu") as gpu_queue: # run(10) # else: # print("\nSkip scheduling on Level Zero GPU\n") if dpctl.has_cpu_queues(): print("\nScheduling on OpenCL CPU\n") with dpctl.device_context("opencl:cpu") as cpu_queue: run(10) else: print("\nSkip scheduling on OpenCL CPU\n")
class TestSyclQueue(unittest.TestCase): @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_queue_not_equals(self): with dpctl.device_context("opencl:gpu") as gpuQ0: with dpctl.device_context("opencl:cpu") as cpuQ: self.assertFalse(cpuQ.equals(gpuQ0)) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") def test_queue_equals(self): with dpctl.device_context("opencl:gpu") as gpuQ0: with dpctl.device_context("opencl:gpu") as gpuQ1: self.assertTrue(gpuQ0.equals(gpuQ1))
class TestIsInDeviceContext(unittest.TestCase): def test_is_in_device_context_outside_device_ctxt(self): self.assertFalse(dpctl.is_in_device_context()) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") def test_is_in_device_context_inside_device_ctxt(self): with dpctl.device_context("opencl:gpu:0"): self.assertTrue(dpctl.is_in_device_context()) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_is_in_device_context_inside_nested_device_ctxt(self): with dpctl.device_context("opencl:cpu:0"): with dpctl.device_context("opencl:gpu:0"): self.assertTrue(dpctl.is_in_device_context()) self.assertTrue(dpctl.is_in_device_context()) self.assertFalse(dpctl.is_in_device_context())
class TestMemory(unittest.TestCase): @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_memory_create(self): nbytes = 1024 queue = dpctl.get_current_queue() mobj = MemoryUSMShared(nbytes, queue) self.assertEqual(mobj.nbytes, nbytes) def _create_memory(self): nbytes = 1024 queue = dpctl.get_current_queue() mobj = MemoryUSMShared(nbytes, queue) return mobj @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_memory_without_context(self): mobj = self._create_memory() # Without context self.assertEqual(mobj._usm_type(), "shared") @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_memory_cpu_context(self): mobj = self._create_memory() # CPU context with dpctl.device_context("opencl:cpu:0"): # type respective to the context in which # memory was created usm_type = mobj._usm_type() self.assertEqual(usm_type, "shared") current_queue = dpctl.get_current_queue() # type as view from current queue usm_type = mobj._usm_type(current_queue) # type can be unknown if current queue is # not in the same SYCL context self.assertTrue(usm_type in ["unknown", "shared"]) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") def test_memory_gpu_context(self): mobj = self._create_memory() # GPU context with dpctl.device_context("opencl:gpu:0"): usm_type = mobj._usm_type() self.assertEqual(usm_type, "shared") current_queue = dpctl.get_current_queue() usm_type = mobj._usm_type(current_queue) self.assertTrue(usm_type in ["unknown", "shared"]) @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_buffer_protocol(self): mobj = self._create_memory() mv1 = memoryview(mobj) mv2 = memoryview(mobj) self.assertEqual(mv1, mv2)
class TestMemory(unittest.TestCase): @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_memory_create(self): nbytes = 1024 queue = dpctl.get_current_queue() mobj = MemoryUSMShared(nbytes, alignment=64, queue=queue) self.assertEqual(mobj.nbytes, nbytes) self.assertTrue(hasattr(mobj, "__sycl_usm_array_interface__")) @unittest.expectedFailure @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_memory_create_with_np(self): mobj = dpctl.memory.MemoryUSMShared(np.int64(16384)) self.assertTrue(hasattr(mobj, "__sycl_usm_array_interface__")) def _create_memory(self): nbytes = 1024 queue = dpctl.get_current_queue() mobj = MemoryUSMShared(nbytes, alignment=64, queue=queue) return mobj def _create_host_buf(self, nbytes): ba = bytearray(nbytes) for i in range(nbytes): ba[i] = (i % 32) + ord("a") return ba @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_memory_without_context(self): mobj = self._create_memory() # Without context self.assertEqual(mobj.get_usm_type(), "shared") @unittest.skipUnless(dpctl.has_cpu_queues(), "No OpenCL CPU queues available") def test_memory_cpu_context(self): mobj = self._create_memory() # CPU context with dpctl.device_context("opencl:cpu:0"): # type respective to the context in which # memory was created usm_type = mobj.get_usm_type() self.assertEqual(usm_type, "shared") current_queue = dpctl.get_current_queue() # type as view from current queue usm_type = mobj.get_usm_type(current_queue) # type can be unknown if current queue is # not in the same SYCL context self.assertTrue(usm_type in ["unknown", "shared"]) @unittest.skipUnless(dpctl.has_gpu_queues(), "No OpenCL GPU queues available") def test_memory_gpu_context(self): mobj = self._create_memory() # GPU context with dpctl.device_context("opencl:gpu:0"): usm_type = mobj.get_usm_type() self.assertEqual(usm_type, "shared") current_queue = dpctl.get_current_queue() usm_type = mobj.get_usm_type(current_queue) self.assertTrue(usm_type in ["unknown", "shared"]) @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_buffer_protocol(self): mobj = self._create_memory() mv1 = memoryview(mobj) mv2 = memoryview(mobj) self.assertEqual(mv1, mv2) @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_copy_host_roundtrip(self): mobj = self._create_memory() host_src_obj = self._create_host_buf(mobj.nbytes) mobj.copy_from_host(host_src_obj) host_dest_obj = mobj.copy_to_host() del mobj self.assertEqual(host_src_obj, host_dest_obj) @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_zero_copy(self): mobj = self._create_memory() mobj2 = type(mobj)(mobj) self.assertTrue(mobj2.reference_obj is mobj) mobj_data = mobj.__sycl_usm_array_interface__["data"] mobj2_data = mobj2.__sycl_usm_array_interface__["data"] self.assertEqual(mobj_data, mobj2_data) @unittest.skipUnless(dpctl.has_sycl_platforms(), "No SYCL devices except the default host device.") def test_pickling(self): import pickle mobj = self._create_memory() host_src_obj = self._create_host_buf(mobj.nbytes) mobj.copy_from_host(host_src_obj) mobj_reconstructed = pickle.loads(pickle.dumps(mobj)) self.assertEqual(type(mobj), type(mobj_reconstructed), "Pickling should preserve type") self.assertEqual( mobj.tobytes(), mobj_reconstructed.tobytes(), "Pickling should preserve buffer content", ) self.assertNotEqual( mobj._pointer, mobj_reconstructed._pointer, "Pickling/unpickling changes pointer", )