Skip to content

Commit 27c0778

Browse files
author
Diptorup Deb
committed
Restructure dpnp offload section.
1 parent ea1d569 commit 27c0778

File tree

6 files changed

+132
-221
lines changed

6 files changed

+132
-221
lines changed

docs/source/experimental/index.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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:

docs/source/ext_links.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
.. _Intel VTune Profiler: https://www.intel.com/content/www/us/en/developer/tools/oneapi/vtune-profiler.html
2626
.. _Intel Advisor: https://www.intel.com/content/www/us/en/developer/tools/oneapi/advisor.html
2727
.. _oneMKL: https://www.intel.com/content/www/us/en/docs/oneapi/programming-guide/2023-2/intel-oneapi-math-kernel-library-onemkl.html
28+
.. _oneDPL: https://www.intel.com/content/www/us/en/developer/tools/oneapi/dpc-library.html#gs.5izf63
2829
.. _UXL: https://uxlfoundation.org/
2930
.. _oneAPI GPU optimization guide: https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2024-0/general-purpose-computing-on-gpu.html

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Data Parallel Extension for Numba*
4848
programming_model
4949
user_guide/index
5050
autoapi/index
51+
experimental/index
5152
useful_links
5253

5354
.. toctree::

docs/source/user_guide/config.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Configuration Options for ``numba-dpex``
44
========================================
55

6-
``numba-dpex`` provides a set of environment variables and flags for configuring different aspects of the compilation, debugging and execution of programs. The configuration flags of ``numba-dpex`` are mostly inherited from those of Numba*. They are defined in :file:`numba_dpex/core/config.py`.
6+
``numba-dpex`` provides a set of environment variables and flags for configuring
7+
different aspects of the compilation, debugging and execution of programs. The
8+
configuration flags of ``numba-dpex`` are mostly inherited from those of Numba*.
9+
They are defined in :file:`numba_dpex/core/config.py`.
710

811
.. note::
912
In order to enable/disable each of the configuration flags, a ``NUMBA_DPEX``

docs/source/user_guide/dpnp_offload.rst

Lines changed: 0 additions & 219 deletions
This file was deleted.

docs/source/user_guide/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ Tutorials
88
:maxdepth: 2
99

1010
kernel_programming/index
11-
dpnp_offload
1211
debugging/index
1312
config

0 commit comments

Comments
 (0)