diff --git a/docs/sources/programming_dpep.rst b/docs/sources/programming_dpep.rst index e2da421..650d2c5 100644 --- a/docs/sources/programming_dpep.rst +++ b/docs/sources/programming_dpep.rst @@ -37,8 +37,6 @@ to execute your `Numpy*`_ script on GPU usually requires changing just a few lin :caption: Your first NumPy code running on GPU :name: ex_01_hello_dpnp -.. command-output:: python ./01-hello_dpnp.py - :cwd: ../../examples In this example ``np.asarray()`` creates an array on the default `SYCL*`_ device, which is ``"gpu"`` on systems with integrated or discrete GPU (it is ``"cpu"`` on systems that do not have GPU). @@ -57,8 +55,6 @@ In the following example we create the array ``x`` on the GPU device, and perfor :caption: Select device type while creating array :name: ex_02_dpnp_device -.. command-output:: python ./02-dpnp_device.py - :cwd: ../../examples Data Parallel Extension for Numba - numba-dpex ********************************************** @@ -69,3 +65,9 @@ SIMD instructions, and schedules those in a way that exploits maximum instructio The ``numba-dpex`` extension allows to compile and offload data parallel regions to any data parallel device. It takes just a few lines to modify your CPU `Numba*`_ script to run on GPU. +.. literalinclude:: ../../examples/03-dpnp2numba-dpex.py + :language: python + :lines: 27- + :caption: Compile dpnp code with numba-dpex + :name: ex_03_dpnp2numba_dpex + diff --git a/examples/01-hello_dpnp.py b/examples/01-hello_dpnp.py index 71852fa..8f3a7bd 100644 --- a/examples/01-hello_dpnp.py +++ b/examples/01-hello_dpnp.py @@ -28,3 +28,6 @@ x = np.asarray([1, 2, 3]) y = np.sum(x) + +print(y.shape) # Must be 0-dimensional array +print(y) # Expect 6 diff --git a/examples/02-dpnp_device.py b/examples/02-dpnp_device.py index a004b1d..16fa2dd 100644 --- a/examples/02-dpnp_device.py +++ b/examples/02-dpnp_device.py @@ -33,3 +33,4 @@ print("GPU device is not available") y = np.sum(x) +print(y) # Expect 6 diff --git a/examples/03-dpnp2numba-dpex.py b/examples/03-dpnp2numba-dpex.py new file mode 100644 index 0000000..f788b53 --- /dev/null +++ b/examples/03-dpnp2numba-dpex.py @@ -0,0 +1,41 @@ +# ***************************************************************************** +# Copyright (c) 2022, Intel Corporation All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ***************************************************************************** + +import dpnp as np +from numba_dpex import njit + + +@njit(parallel=True, fastmath=True) +def sum(x): + return np.sum(x) + + +x = np.empty(3) +try: + x = np.asarray([1, 2, 3], device="gpu") +except: + print("GPU device is not available") +