Skip to content

Commit 00b839b

Browse files
committed
Added examples
1 parent 10490b9 commit 00b839b

File tree

11 files changed

+168
-95
lines changed

11 files changed

+168
-95
lines changed

.github/workflows/build-sphinx.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
uses: mshick/add-pr-comment@v2.8.1
145145
with:
146146
message: |
147-
View rendered docs @ https://intelpython.github.io/dpnp/pulls/${{ env.PR_NUM }}/index.html
147+
View rendered docs @ https://intelpython.github.io/dpnp/pull/${{ env.PR_NUM }}/index.html
148148
allow-repeats: false
149149

150150
clean:

doc/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ Data Parallel Extension for NumPy*
1818
:caption: Development information
1919

2020
dpnp_backend_api
21-
dpctl

doc/quick_start_guide.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,22 @@ by the source, you will need to run a command as below:
127127
.. code-block:: bash
128128
129129
pytest -s tests
130+
131+
Examples
132+
========
133+
134+
The examples below demonstrates a simple usage of the Data Parallel Extension for NumPy*
135+
136+
.. literalinclude:: ../examples/example_sum.py
137+
:linenos:
138+
:language: python
139+
:lines: 35-
140+
:caption: Calculate sum of an array
141+
142+
.. literalinclude:: ../examples/example_cfd.py
143+
:linenos:
144+
:language: python
145+
:lines: 35-
146+
:caption: Compute follows data paradigm
147+
148+
More examples on how to use ``dpnp`` can be found in ``dpnp/examples``.

examples/example1.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,13 @@
3636
"""
3737

3838

39-
try:
40-
import dpnp
41-
except ImportError:
42-
import os
43-
import sys
44-
45-
root_dir = os.path.join(os.path.dirname(__file__), os.pardir)
46-
sys.path.append(root_dir)
47-
48-
import dpnp
49-
5039
import time
5140

41+
import dpctl
5242
import numpy
5343

44+
import dpnp
45+
5446

5547
def run_dgemm(executor, name, size, test_type, repetition):
5648
x1 = executor.reshape(
@@ -75,15 +67,23 @@ def run_dgemm(executor, name, size, test_type, repetition):
7567
return (min_time, med_time, max_time), result.item(5)
7668

7769

70+
def get_dtypes():
71+
_dtypes_list = [numpy.int32, numpy.int64, numpy.float32]
72+
device = dpctl.select_default_device()
73+
if device.has_aspect_fp64:
74+
_dtypes_list.append(numpy.float64)
75+
return _dtypes_list
76+
77+
7878
if __name__ == "__main__":
7979
test_repetition = 5
80-
for test_type in [numpy.float64, numpy.float32, numpy.int64, numpy.int32]:
80+
for test_type in get_dtypes():
8181
type_name = numpy.dtype(test_type).name
8282
print(
8383
f"...Test data type is {test_type}, each test repetitions {test_repetition}"
8484
)
8585

86-
for size in [16, 32, 64, 128]: # , 256, 512, 1024, 2048, 4096]:
86+
for size in [16, 32, 64, 128, 256, 512, 1024]:
8787
times_python, result_python = run_dgemm(
8888
numpy, "<NumPy>", size, test_type, test_repetition
8989
)
@@ -97,6 +97,6 @@ def run_dgemm(executor, name, size, test_type, repetition):
9797

9898
msg = f"type:{type_name}:N:{size:4}"
9999
msg += f":__NumPy__:{times_python[1]:.3e}:(min:{times_python[0]:.3e}:max:{times_python[2]:.3e})"
100-
msg += f":__SYCL__:{times_sycl[1]:.3e}:(min:{times_sycl[0]:.3e}:max:{times_sycl[2]:.3e})"
100+
msg += f":__DPNP__:{times_sycl[1]:.3e}:(min:{times_sycl[0]:.3e}:max:{times_sycl[2]:.3e})"
101101
msg += f":ratio:{times_python[1]/times_sycl[1]:6.2f}:verification:{verification}"
102102
print(msg)

examples/example10.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,16 @@
2828
"""Example 10.
2929
3030
This example shows simple usage of the DPNP
31-
in combination with dpCtl.
31+
in combination with dpctl.
3232
3333
"""
3434

3535
import time
3636

37-
try:
38-
import dpnp
39-
except ImportError:
40-
import os
41-
import sys
42-
43-
sys.path.insert(0, os.path.abspath("."))
44-
import dpnp
45-
4637
import numpy
4738

39+
import dpnp
40+
4841

4942
def run(executor, size, test_type, repetition):
5043
x = executor.reshape(
@@ -61,9 +54,17 @@ def run(executor, size, test_type, repetition):
6154
return numpy.median(times), result
6255

6356

57+
def get_dtypes():
58+
_dtypes_list = [numpy.int32, numpy.int64, numpy.float32]
59+
device = dpctl.select_default_device()
60+
if device.has_aspect_fp64:
61+
_dtypes_list.append(numpy.float64)
62+
return _dtypes_list
63+
64+
6465
def example():
6566
test_repetition = 5
66-
for test_type in [numpy.float64, numpy.float32, numpy.int64, numpy.int32]:
67+
for test_type in get_dtypes():
6768
type_name = numpy.dtype(test_type).name
6869
print(
6970
f"...Test data type is {type_name}, each test repetitions {test_repetition}"
@@ -80,7 +81,7 @@ def example():
8081
else:
8182
verification = f"({result_dpnp} != {result_numpy})"
8283

83-
msg = f"type:{type_name}:N:{size:4}:NumPy:{time_numpy:.3e}:SYCL:{time_dpnp:.3e}"
84+
msg = f"type:{type_name}:N:{size:4}:NumPy:{time_numpy:.3e}:DPNP:{time_dpnp:.3e}"
8485
msg += f":ratio:{time_numpy/time_dpnp:6.2f}:verification:{verification}"
8586
print(msg)
8687

@@ -90,7 +91,7 @@ def example():
9091
import dpctl
9192

9293
with dpctl.device_context("opencl:gpu") as gpu_queue:
93-
gpu_queue.get_sycl_device().dump_device_info()
94+
gpu_queue.get_sycl_device().print_device_info()
9495
example()
9596

9697
except ImportError:

examples/example2.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,18 @@
3636
"""
3737

3838

39-
try:
40-
import dpnp
41-
except ImportError:
42-
import os
43-
import sys
44-
45-
root_dir = os.path.join(os.path.dirname(__file__), os.pardir)
46-
sys.path.append(root_dir)
47-
48-
import dpnp
49-
5039
import time
5140

5241
import numpy
5342

54-
common_function_one_input = numpy.sin
55-
"""
56-
Fixed third party function
57-
"""
58-
59-
60-
def get_package_specific_input_data_type(input_type, size):
61-
return input_type.arange(size)
43+
import dpnp
6244

6345

64-
def run_third_party_function(input, repetition):
46+
def run_third_party_function(xp, input, repetition):
6547
times = []
6648
for _ in range(repetition):
6749
start_time = time.time()
68-
result = common_function_one_input(input)
50+
result = xp.sin(input)
6951
end_time = time.time()
7052
times.append(end_time - start_time)
7153

@@ -75,18 +57,19 @@ def run_third_party_function(input, repetition):
7557

7658
if __name__ == "__main__":
7759
test_repetition = 5
78-
for input_type in [numpy, dpnp]:
79-
type_name = input_type.__name__
60+
for xp in [numpy, dpnp]:
61+
type_name = xp.__name__
8062
print(
8163
f"...Test data type is {type_name}, each test repetitions {test_repetition}"
8264
)
8365

84-
for size in [2048, 4096, 8192, 16384, 32768, 65536]:
85-
input_data = get_package_specific_input_data_type(input_type, size)
66+
for size in range(20, 25):
67+
size = 2**size
68+
input_data = xp.arange(size)
8669
result_time, result = run_third_party_function(
87-
input_data, test_repetition
70+
xp, input_data, test_repetition
8871
)
8972

9073
print(
91-
f"type:{type_name}:N:{size:6}:Time:{result_time:.3e}:result:{result:.3e}"
74+
f"type:{type_name}:N:{size:6}:Time:{result_time:.3e}:result:{result}"
9275
)

examples/example6.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@
3232
dpnp.random.randn
3333
"""
3434

35-
36-
try:
37-
import dpnp
38-
except ImportError:
39-
import os
40-
import sys
41-
42-
root_dir = os.path.join(os.path.dirname(__file__), os.pardir)
43-
sys.path.append(root_dir)
44-
45-
import dpnp
35+
import dpnp
4636

4737
if __name__ == "__main__":
4838
# TODO

examples/example7.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# THE POSSIBILITY OF SUCH DAMAGE.
2626
# *****************************************************************************
2727

28-
"""Example 1.
28+
"""Example 7.
2929
3030
This example shows simple usage of the DPNP
3131
to calculate square matrix multiplication
@@ -35,22 +35,13 @@
3535
3636
"""
3737

38-
39-
try:
40-
import dpnp
41-
except ImportError:
42-
import os
43-
import sys
44-
45-
root_dir = os.path.join(os.path.dirname(__file__), os.pardir)
46-
sys.path.append(root_dir)
47-
48-
import dpnp
49-
5038
import time
5139

40+
import dpctl
5241
import numpy
5342

43+
import dpnp
44+
5445

5546
def run_function(executor, name, size, test_type, repetition):
5647
x = executor.reshape(
@@ -69,15 +60,23 @@ def run_function(executor, name, size, test_type, repetition):
6960
return execution_time, result
7061

7162

63+
def get_dtypes():
64+
_dtypes_list = [numpy.int32, numpy.int64, numpy.float32]
65+
device = dpctl.select_default_device()
66+
if device.has_aspect_fp64:
67+
_dtypes_list.append(numpy.float64)
68+
return _dtypes_list
69+
70+
7271
if __name__ == "__main__":
7372
test_repetition = 5
74-
for test_type in [numpy.float64, numpy.float32, numpy.int64, numpy.int32]:
73+
for test_type in get_dtypes():
7574
type_name = numpy.dtype(test_type).name
7675
print(
7776
f"...Test data type is {test_type}, each test repetitions {test_repetition}"
7877
)
7978

80-
for size in [16, 32, 64, 128, 256, 512, 1024, 2048, 4096]:
79+
for size in [256, 512, 1024, 2048, 4096, 8192]:
8180
time_python, result_python = run_function(
8281
numpy, "<NumPy>", size, test_type, test_repetition
8382
)
@@ -90,6 +89,6 @@ def run_function(executor, name, size, test_type, repetition):
9089
else:
9190
verification = f"({result_mkl} != {result_python})"
9291

93-
msg = f"type:{type_name}:N:{size:4}:NumPy:{time_python:.3e}:SYCL:{time_mkl:.3e}"
92+
msg = f"type:{type_name}:N:{size:4}:NumPy:{time_python:.3e}:DPNP:{time_mkl:.3e}"
9493
msg += f":ratio:{time_python/time_mkl:6.2f}:verification:{verification}"
9594
print(msg)

examples/example_bs.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,10 @@
3232
3333
"""
3434

35-
try:
36-
import dpnp as np
37-
except ImportError:
38-
import os
39-
import sys
40-
41-
root_dir = os.path.join(os.path.dirname(__file__), os.pardir)
42-
sys.path.append(root_dir)
43-
44-
import dpnp as np
45-
35+
import dpnp as np
4636

4737
SIZE = 2**8
48-
DTYPE = np.float64
38+
DTYPE = np.default_float_type()
4939

5040
SEED = 7777777
5141
PL, PH = 10.0, 50.0

examples/example_cfd.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# cython: language_level=3
2+
# -*- coding: utf-8 -*-
3+
# *****************************************************************************
4+
# Copyright (c) 2023, Intel Corporation
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
# - Redistributions of source code must retain the above copyright notice,
10+
# this list of conditions and the following disclaimer.
11+
# - Redistributions in binary form must reproduce the above copyright notice,
12+
# this list of conditions and the following disclaimer in the documentation
13+
# and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25+
# THE POSSIBILITY OF SUCH DAMAGE.
26+
# *****************************************************************************
27+
28+
"""Example CFD.
29+
30+
This example shows how DPNP follows compute follows data paradigm
31+
32+
"""
33+
34+
import dpnp as np
35+
36+
x = np.empty(3)
37+
try:
38+
x = np.asarray([1, 2, 3], device="gpu")
39+
except Exception:
40+
print("GPU device is not available")
41+
42+
print("Array x allocated on the device:", x.device)
43+
44+
y = np.sum(x)
45+
46+
print("Result y is located on the device:", y.device) # The same device as x
47+
print("Shape of y is:", y.shape) # 0-dimensional array
48+
print("y=", y) # Expect 6

0 commit comments

Comments
 (0)