Interplay with the Data Parallel Control Library

Data Parallel Control Library provides API to manage specific SYCL* resources for SYCL-based Python packages.

An example below demonstrates how the Data Parallel Extension for NumPy* can be easily combined with the device management interface provided by dpctl package.

 1    import dpctl
 2    import dpnp
 3
 4    d = dpctl.select_cpu_device()
 5    x = dpnp.array([1, 2, 3], device=d)
 6    s = dpnp.sum(x)
 7
 8    y = dpnp.linspace(0, dpnp.pi, num=10**6, device="gpu")
 9    f = 1 + y * dpnp.sin(y)
10
11    # locate argument where function attains global maximum
12    max_arg = x[dpnp.argmax(f)]
13    max_val = dpnp.max(f)

For more information please refer to Data Parallel Control Library documentation.

Example

 1import time
 2
 3import numpy
 4
 5import dpnp
 6
 7
 8def run(executor, size, test_type, repetition):
 9    x = executor.reshape(
10        executor.arange(size * size, dtype=test_type), (size, size)
11    )
12
13    times = []
14    for _ in range(repetition):
15        start_time = time.perf_counter()
16        result = executor.sum(x)
17        end_time = time.perf_counter()
18        times.append(end_time - start_time)
19
20    return numpy.median(times), result
21
22
23def get_dtypes():
24    _dtypes_list = [numpy.int32, numpy.int64, numpy.float32]
25    device = dpctl.select_default_device()
26    if device.has_aspect_fp64:
27        _dtypes_list.append(numpy.float64)
28    return _dtypes_list
29
30
31def example():
32    test_repetition = 5
33    for test_type in get_dtypes():
34        type_name = numpy.dtype(test_type).name
35        print(
36            f"...Test data type is {type_name}, each test repetitions {test_repetition}"
37        )
38
39        for size in [64, 128, 256, 512, 1024, 2048, 4096]:
40            time_numpy, result_numpy = run(
41                numpy, size, test_type, test_repetition
42            )
43            time_dpnp, result_dpnp = run(dpnp, size, test_type, test_repetition)
44
45            if result_dpnp == result_numpy:
46                verification = True
47            else:
48                verification = f"({result_dpnp} != {result_numpy})"
49
50            msg = f"type:{type_name}:N:{size:4}:NumPy:{time_numpy:.3e}:DPNP:{time_dpnp:.3e}"
51            msg += f":ratio:{time_numpy/time_dpnp:6.2f}:verification:{verification}"
52            print(msg)
53
54
55if __name__ == "__main__":
56    import dpctl
57
58    dpctl.select_default_device().print_device_info()
59    example()