Skip to content

Commit 52eb727

Browse files
AvijitBag07AvijitBag07
authored andcommitted
DPNP_GettingStarted
Signed-off-by: AvijitBag07 <[email protected]>
1 parent a6b9e6e commit 52eb727

File tree

5 files changed

+464
-0
lines changed

5 files changed

+464
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright Intel Corporation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Intel® Python Data Parallel Extension for NumPy Getting Started Sample
2+
3+
The `Intel® Python DPNP Getting Started` sample code shows how to find conjugate gradient using the Intel Python API powered by the [Intel® Python DPNP - Data Parallel Extension for NumPy](https://github.com/IntelPython/dpnp).
4+
5+
| Area | Description
6+
| :--- | :---
7+
| Category | Getting Started
8+
| What you will learn | DPNP programming model for Intel GPU
9+
| Time to complete | 60 minutes
10+
>**Note**: This sample is migrated from Cupy Python sample. See the [ConjugateGradient](https://github.com/cupy/cupy/blob/main/examples/cg/cg.py) sample in the cupy-samples GitHub.
11+
12+
13+
## Purpose
14+
The Data Parallel Extension for NumPy* (dpnp package) - a library that implements a subset of NumPy* that can be executed on any data parallel device. The subset is a drop-in replacement of core NumPy* functions and numerical data types.
15+
16+
The DPNP is used to offload python code to INTEL GPU's. This is very similar to CUPY API [Comparsion_list](https://intelpython.github.io/dpnp/reference/comparison.html#).
17+
18+
19+
## Prerequisites
20+
21+
| Optimized for | Description
22+
| :--- | :---
23+
| OS | Ubuntu* 22.04 (or newer)
24+
| Hardware | Intel® Gen9 <br>Intel® Gen11 <br>Intel® Data Center GPU Max
25+
| Software | Intel® Python Data Parallel Extension for NumPy (DPNP)
26+
> **Note**: [Intel® Python DPNP - Data Parallel Extension for NumPy](https://github.com/IntelPython/dpnp).
27+
28+
## Key Implementation Details
29+
30+
- This get-started sample code is implemented for Intel GPUs using Python language. The example assumes the user has the latest DPNP installed in the environment, similar to what is delivered with the installation of the [Intel® Distribution for Python*](https://www.intel.com/content/www/us/en/developer/tools/oneapi/distribution-python-download.html).
31+
32+
## Environment Setup
33+
34+
You will need to download and install the following toolkits, tools, and components to use the sample.
35+
36+
**1. Intel Python**
37+
38+
39+
Required Intel Python package: DPNP (Select Intel® Distribution for Python*: Offline on [Get Intel® Distribution for Python*](https://www.intel.com/content/www/us/en/developer/tools/oneapi/distribution-python-download.html) to install)
40+
41+
42+
**2. (Offline Installer) Update the Intel Python base environment**
43+
44+
Load python env:
45+
```
46+
source $PYTHON_INSTALL/env/vars.sh
47+
```
48+
49+
**3. (Offline Installer) Check the DPNP version**
50+
51+
```
52+
python -c "import dpnp; print(dpnp.__version__)"
53+
```
54+
Note: if the version is 0.15.0 or more continue, otherwise need to upgrade the dpnp version
55+
56+
**4. Clone the GitHub repository**
57+
<!-- for oneapi-samples: git clone https://github.com/oneapi-src/oneAPI-samples.git
58+
cd oneAPI-samples/DirectProgramming/<samples-folder>/<individual-sample-folder> -->
59+
<!-- for migrated samples - provide git clone command for individual repo and cd to sample dir -->
60+
```
61+
git clone https://github.com/oneapi-src/oneAPI-samples.git
62+
cd oneAPI-samples/DirectProgramming/Python/DPNP_GettingStarted
63+
```
64+
65+
66+
## Run the Sample
67+
>**Note**: Before running the sample, make sure Intel Python is installed.
68+
69+
1. Change to the sample directory.
70+
2. Build the program.
71+
```
72+
$ python cg.py
73+
```
74+
75+
## License
76+
77+
Code samples are licensed under the MIT license. See
78+
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt)
79+
for details.
80+
81+
Third party program Licenses can be found here:
82+
[third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt)
83+
84+
*Other names and brands may be claimed as the property of others. [Trademarks](https://www.intel.com/content/www/us/en/legal/trademarks.html)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import argparse
2+
import time
3+
import dpctl
4+
import dpctl.tensor as dpt
5+
import dpnp
6+
import numpy as np
7+
8+
9+
def fit_cpu(A, b, tol, max_iter):
10+
# Note that this function works even tensors 'A' and 'b' are NumPy or dpnp
11+
# arrays.
12+
x = np.zeros_like(b, dtype=np.float64)
13+
r0 = b - np.dot(A, x)
14+
p = r0
15+
for i in range(max_iter):
16+
a = np.inner(r0, r0) / np.inner(p, np.dot( A, p))
17+
x += (a * p)
18+
r1 = r0 - a * np.dot(A, p)
19+
if np.linalg.norm(r1) < tol:
20+
return x
21+
b = np.inner(r1, r1) / np.inner(r0, r0)
22+
p = r1 + b * p
23+
r0 = r1
24+
print('Failed to converge. Increase max-iter or tol.')
25+
return x
26+
27+
def fit(A, b, tol, max_iter):
28+
# Note that this function works even tensors 'A' and 'b' are NumPy or dpnp
29+
# arrays.
30+
x = dpnp.zeros_like(b, dtype=dpnp.float64)
31+
r0 = b - dpnp.dot(A, x)
32+
p = r0
33+
for i in range(max_iter):
34+
a = dpnp.inner(r0, r0) / dpnp.inner(p, dpnp.dot(A, p))
35+
x += a * p
36+
r1 = r0 - a * dpnp.dot(A, p)
37+
if dpnp.linalg.norm(r1) < tol:
38+
return x
39+
b = dpnp.inner(r1, r1) / dpnp.inner(r0, r0)
40+
p = r1 + b * p
41+
r0 = r1
42+
print('Failed to converge. Increase max-iter or tol.')
43+
return x
44+
45+
46+
def run(gpu_id, tol, max_iter):
47+
"""CuPy Conjugate gradient example
48+
49+
Solve simultaneous linear equations, Ax = b.
50+
'A' and 'x' are created randomly and 'b' is computed by 'Ax' at first.
51+
Then, 'x' is computed from 'A' and 'b' in two ways, namely with CPU and
52+
GPU. To evaluate the accuracy of computation, the Euclidean distances
53+
between the answer 'x' and the reconstructed 'x' are computed.
54+
55+
"""
56+
for repeat in range(3):
57+
print('Trial: %d' % repeat)
58+
# Create the large symmetric matrix 'A'.
59+
N = 10000
60+
A = np.random.random((N,N))
61+
A = (A @ A.T).astype(np.float64)
62+
x_ans = np.random.random((N)).astype(np.float64)
63+
b = np.dot(A, x_ans)
64+
65+
print('Running CPU...')
66+
start = time.time()
67+
x_cpu = fit_cpu(A, b, tol, max_iter)
68+
print(np.linalg.norm(x_cpu - x_ans))
69+
end = time.time()
70+
print('%s: %f sec' % ("CPU", end - start))
71+
72+
a_dpt = dpnp.asarray(A, dtype=dpnp.float64)
73+
b_dpt = dpnp.asarray(b, dtype=dpnp.float64)
74+
75+
print('Running GPU...')
76+
start = time.time()
77+
x_gpu = fit(a_dpt, b_dpt, tol, max_iter)
78+
79+
print(np.linalg.norm(dpnp.asnumpy(x_gpu) - x_ans))
80+
end = time.time()
81+
print('%s: %f sec' % ("GPU", end - start))
82+
83+
print()
84+
85+
86+
if __name__ == '__main__':
87+
parser = argparse.ArgumentParser()
88+
parser.add_argument('--gpu-id', '-g', default=0, type=int,
89+
help='ID of GPU.')
90+
parser.add_argument('--tol', '-t', default=0.1, type=float,
91+
help='tolerance to stop iteration')
92+
parser.add_argument('--max-iter', '-m', default=5000, type=int,
93+
help='number of iterations')
94+
args = parser.parse_args()
95+
run(args.gpu_id, args.tol, args.max_iter)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"guid": "B8E1196A-83B1-4972-8D87-E15F14CAFC82",
3+
"name": "Intel® Python Data Parallel Extension for NumPy",
4+
"categories": ["Toolkit/oneAPI DirectProgramming/Python/DPNP_GettingStarted"],
5+
"description": "This sample code shows how to find conjugate gradient using the Intel Python API powered by the dpnp",
6+
"builder": ["cli"],
7+
"languages": [{"python":{}}],
8+
"dependencies": ["intelpython"],
9+
"os":["linux"],
10+
"targetDevice": ["GPU","CPU"],
11+
"ciTests": {
12+
"linux": [
13+
{
14+
"env": [
15+
"source /intel/oneapi/intelpython/env/activate"
16+
],
17+
"id": "idp_intel_dpnp_cg_py",
18+
"steps": [
19+
"python cg.py"
20+
]
21+
}
22+
]
23+
},
24+
"expertise": "Code Optimization"
25+
}

0 commit comments

Comments
 (0)