DPCtl Usage
DPCtl provides API to manage specific SYCL resources for SYCL-based Python packages. DPNP uses DPCtl as a global SYCL queue manager. Below code illustrates simple usage of DPNP in combination with dpCtl.
1import dpctl
2import dpnp as np
3
4with dpctl.device_context("opencl:gpu"):
5 x = np.array([1, 2, 3])
6 s = np.sum(x)
For more information please refer to DPCtl’s documentation.
Example
1import time
2
3try:
4 import dpnp
5except ImportError:
6 import os
7 import sys
8
9 sys.path.insert(0, os.path.abspath('.'))
10 import dpnp
11
12import numpy
13
14
15def run(executor, size, test_type, repetition):
16 x = executor.reshape(executor.arange(size * size, dtype=test_type), (size, size))
17
18 times = []
19 for _ in range(repetition):
20 start_time = time.perf_counter()
21 result = executor.sum(x)
22 end_time = time.perf_counter()
23 times.append(end_time - start_time)
24
25 return numpy.median(times), result
26
27
28def example():
29 test_repetition = 5
30 for test_type in [numpy.float64, numpy.float32, numpy.int64, numpy.int32]:
31 type_name = numpy.dtype(test_type).name
32 print(f"...Test data type is {type_name}, each test repetitions {test_repetition}")
33
34 for size in [64, 128, 256, 512, 1024, 2048, 4096]:
35 time_numpy, result_numpy = run(numpy, size, test_type, test_repetition)
36 time_dpnp, result_dpnp = run(dpnp, size, test_type, test_repetition)
37
38 if result_dpnp == result_numpy:
39 verification = True
40 else:
41 verification = f"({result_dpnp} != {result_numpy})"
42
43 msg = f"type:{type_name}:N:{size:4}:NumPy:{time_numpy:.3e}:SYCL:{time_dpnp:.3e}"
44 msg += f":ratio:{time_numpy/time_dpnp:6.2f}:verification:{verification}"
45 print(msg)
46
47
48if __name__ == "__main__":
49 try:
50 import dpctl
51
52 with dpctl.device_context("opencl:gpu") as gpu_queue:
53 gpu_queue.get_sycl_device().dump_device_info()
54 example()
55
56 except ImportError:
57 example()