|
| 1 | +.. _index: |
| 2 | +.. include:: ./../ext_links.txt |
| 3 | + |
| 4 | +Experimental Features |
| 5 | +===================== |
| 6 | + |
| 7 | +Numba-dpex includes various experimental features that are not yet suitable for |
| 8 | +everyday production usage, but are included as an engineering preview. |
| 9 | +The most prominent experimental features currently included in numba-dpex are |
| 10 | +listed in this section. |
| 11 | + |
| 12 | + |
| 13 | +Compiling and Offloading ``dpnp`` statements |
| 14 | +-------------------------------------------- |
| 15 | + |
| 16 | +Data Parallel Extension for NumPy* (`dpnp`_) is a drop-in NumPy* replacement |
| 17 | +library built using the oneAPI software stack including `oneMKL`_, `oneDPL`_ and |
| 18 | +`SYCL*`_. numba-dpex has experimental support for compiling a subset of dpnp |
| 19 | +functions. The feature is enabled by the :py:func:`numba_dpex.dpjit` decorator. |
| 20 | + |
| 21 | +An example of a supported usage of dpnp in numba-dpex is provided in the |
| 22 | +following code snippet: |
| 23 | + |
| 24 | +.. code-block:: python |
| 25 | +
|
| 26 | + import dpnp |
| 27 | + from numba_dpex import dpjit |
| 28 | +
|
| 29 | +
|
| 30 | + @dpjit |
| 31 | + def foo(): |
| 32 | + a = dpnp.ones(1024, device="gpu") |
| 33 | + return dpnp.sqrt(a) |
| 34 | +
|
| 35 | +
|
| 36 | + a = foo() |
| 37 | + print(a) |
| 38 | + print(type(a)) |
| 39 | +
|
| 40 | +
|
| 41 | +Offloading ``prange`` loops |
| 42 | +--------------------------- |
| 43 | + |
| 44 | +numba-dpex supports using the ``numba.prange`` statements with |
| 45 | +``dpnp.ndarray`` objects. All such ``prange`` loops are offloaded as kernels and |
| 46 | +executed on a device inferred using the compute follows data programming model. |
| 47 | +The next examples shows using a ``prange`` loop. |
| 48 | + |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + import dpnp |
| 53 | + from numba_dpex import dpjit, prange |
| 54 | +
|
| 55 | +
|
| 56 | + @dpjit |
| 57 | + def foo(): |
| 58 | + x = dpnp.ones(1024, device="gpu") |
| 59 | + o = dpnp.empty_like(a) |
| 60 | + for i in prange(x.shape[0]): |
| 61 | + o[i] = x[i] * x[i] |
| 62 | + return o |
| 63 | +
|
| 64 | +
|
| 65 | + c = foo() |
| 66 | + print(c) |
| 67 | + print(type(c)) |
| 68 | +
|
| 69 | +
|
| 70 | +``prange`` loop statements can also be used to write reduction loops as |
| 71 | +demonstrated by the following naive pairwise distance computation. |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + from numba_dpex import dpjit, prange |
| 76 | + import dpnp |
| 77 | + import dpctl |
| 78 | +
|
| 79 | +
|
| 80 | + @dpjit |
| 81 | + def pairwise_distance(X1, X2, D): |
| 82 | + """Naïve pairwise distance impl - take an array representing M points in N |
| 83 | + dimensions, and return the M x M matrix of Euclidean distances |
| 84 | +
|
| 85 | + Args: |
| 86 | + X1 : Set of points |
| 87 | + X2 : Set of points |
| 88 | + D : Outputted distance matrix |
| 89 | + """ |
| 90 | + # Size of inputs |
| 91 | + X1_rows = X1.shape[0] |
| 92 | + X2_rows = X2.shape[0] |
| 93 | + X1_cols = X1.shape[1] |
| 94 | +
|
| 95 | + float0 = X1.dtype.type(0.0) |
| 96 | +
|
| 97 | + # Outermost parallel loop over the matrix X1 |
| 98 | + for i in prange(X1_rows): |
| 99 | + # Loop over the matrix X2 |
| 100 | + for j in range(X2_rows): |
| 101 | + d = float0 |
| 102 | + # Compute exclidean distance |
| 103 | + for k in range(X1_cols): |
| 104 | + tmp = X1[i, k] - X2[j, k] |
| 105 | + d += tmp * tmp |
| 106 | + # Write computed distance to distance matrix |
| 107 | + D[i, j] = dpnp.sqrt(d) |
| 108 | +
|
| 109 | +
|
| 110 | + q = dpctl.SyclQueue() |
| 111 | + X1 = dpnp.ones((10, 2), sycl_queue=q) |
| 112 | + X2 = dpnp.zeros((10, 2), sycl_queue=q) |
| 113 | + D = dpnp.empty((10, 2), sycl_queue=q) |
| 114 | +
|
| 115 | + pairwise_distance(X1, X2, D) |
| 116 | + print(D) |
| 117 | +
|
| 118 | +
|
| 119 | +Kernel fusion |
| 120 | +------------- |
| 121 | + |
| 122 | +.. ``numba-dpex`` can identify each NumPy* (or ``dpnp``) array expression as a |
| 123 | +.. data-parallel kernel and fuse them together to generate a single SYCL kernel. |
| 124 | +.. The kernel is automatically offloaded to the specified device where the fusion |
| 125 | +.. operation is invoked. Here is a simple example of a Black-Scholes formula |
| 126 | +.. computation where kernel fusion occurs at different ``dpnp`` math functions: |
0 commit comments