Skip to content

Commit fa9de07

Browse files
authored
Merge pull request #310 from IntelPython/cupy_impls
Cupy implementations of dpbench workloads
2 parents 07f59c9 + 409279d commit fa9de07

File tree

11 files changed

+187
-0
lines changed

11 files changed

+187
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import cupy as np
6+
from scipy.special import erf
7+
8+
9+
def black_scholes(nopt, price, strike, t, rate, volatility, call, put):
10+
mr = -rate
11+
sig_sig_two = volatility * volatility * 2
12+
13+
P = price
14+
S = strike
15+
T = t
16+
17+
a = np.log(P / S)
18+
b = T * mr
19+
20+
z = T * sig_sig_two
21+
c = 0.25 * z
22+
y = np.true_divide(1.0, np.sqrt(z))
23+
24+
w1 = (a - b + c) * y
25+
w2 = (a - b - c) * y
26+
27+
d1 = 0.5 + 0.5 * erf(w1)
28+
d2 = 0.5 + 0.5 * erf(w2)
29+
30+
Se = np.exp(b) * S
31+
32+
call[:] = P * d1 - Se * d2
33+
put[:] = call - P + Se
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import cupy as np
6+
7+
8+
def _gpairs_impl(x1, y1, z1, w1, x2, y2, z2, w2, rbins):
9+
dm = (
10+
np.square(x2 - x1[:, None])
11+
+ np.square(y2 - y1[:, None])
12+
+ np.square(z2 - z1[:, None])
13+
)
14+
return np.array(
15+
[np.outer(w1, w2)[dm <= rbins[k]].sum() for k in range(len(rbins))]
16+
)
17+
18+
19+
def gpairs(nopt, nbins, x1, y1, z1, w1, x2, y2, z2, w2, rbins, results):
20+
results[:] = _gpairs_impl(x1, y1, z1, w1, x2, y2, z2, w2, rbins)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import cupy as np
6+
7+
8+
def l2_norm(a, d):
9+
sq = np.square(a)
10+
sum = sq.sum(axis=1)
11+
d[:] = np.sqrt(sum)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import cupy as np
6+
7+
8+
def pairwise_distance(X1, X2, D):
9+
x1 = np.sum(np.square(X1), axis=1)
10+
x2 = np.sum(np.square(X2), axis=1)
11+
np.dot(X1, X2.T, D)
12+
D *= -2
13+
x3 = x1.reshape(x1.size, 1)
14+
np.add(D, x3, D)
15+
np.add(D, x2, D)
16+
np.sqrt(D, D)

dpbench/benchmarks/pca/pca_cupy.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import cupy as np
6+
7+
8+
def pca(data, dims_rescaled_data=2):
9+
# mean center the data
10+
data -= data.mean(axis=0)
11+
12+
# calculate the covariance matrix
13+
v = np.cov(data, rowvar=False, dtype=data.dtype)
14+
15+
# calculate eigenvectors & eigenvalues of the covariance matrix
16+
evalues, evectors = np.linalg.eigh(v)
17+
18+
# sort eigenvalues and eigenvectors in decreasing order
19+
idx = np.argsort(evalues)[::-1]
20+
evectors = evectors[:, idx]
21+
evalues = evalues[idx]
22+
23+
# select the first n eigenvectors (n is desired dimension
24+
# of rescaled data array, or dims_rescaled_data)
25+
evectors = evectors[:, :dims_rescaled_data]
26+
27+
# carry out the transformation on the data using eigenvectors
28+
tdata = np.dot(evectors.T, data.T).T
29+
30+
# return the transformed data, eigenvalues, and eigenvectors
31+
return tdata, evalues, evectors
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import cupy as np
6+
7+
8+
def rambo(nevts, nout, C1, F1, Q1, output):
9+
C = 2.0 * C1 - 1.0
10+
S = np.sqrt(1 - np.square(C))
11+
F = 2.0 * np.pi * F1
12+
Q = -np.log(Q1)
13+
14+
output[:, :, 0] = Q
15+
output[:, :, 1] = Q * S * np.sin(F)
16+
output[:, :, 2] = Q * S * np.cos(F)
17+
output[:, :, 3] = Q * C
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
[framework]
6+
simple_name = "cupy"
7+
full_name = "cupy"
8+
prefix = "cp"
9+
postfix = "cupy"
10+
class = "CupyFramework"
11+
arch = "gpu"
12+
13+
[[framework.postfixes]]
14+
impl_postfix = "cupy"
15+
description = "cupy"

dpbench/infrastructure/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
store_results,
1515
)
1616
from .frameworks import (
17+
CupyFramework,
1718
DpcppFramework,
1819
DpnpFramework,
1920
Framework,
@@ -39,6 +40,7 @@
3940
"NumbaDpexFramework",
4041
"NumbaMlirFramework",
4142
"DpnpFramework",
43+
"CupyFramework",
4244
"DpcppFramework",
4345
"create_connection",
4446
"create_results_table",

dpbench/infrastructure/frameworks/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
from .cupy_framework import CupyFramework
56
from .dpcpp_framework import DpcppFramework
67
from .dpnp_framework import DpnpFramework
78
from .fabric import build_framework, build_framework_map
@@ -15,6 +16,7 @@
1516
"NumbaFramework",
1617
"NumbaDpexFramework",
1718
"DpnpFramework",
19+
"CupyFramework",
1820
"DpcppFramework",
1921
"NumbaMlirFramework",
2022
"build_framework",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2022 - 2023 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from typing import Callable
6+
7+
import dpbench.config as cfg
8+
9+
from .framework import Framework
10+
11+
12+
class CupyFramework(Framework):
13+
"""A class for reading and processing framework information."""
14+
15+
def __init__(self, fname: str = None, config: cfg.Framework = None):
16+
"""Reads framework information.
17+
:param fname: The framework name.
18+
"""
19+
20+
super().__init__(fname, config)
21+
22+
def copy_to_func(self) -> Callable:
23+
"""Returns the copy-method that should be used
24+
for copying the benchmark arguments."""
25+
26+
def _copy_to_func_impl(ref_array):
27+
import cupy
28+
29+
return cupy.asarray(ref_array)
30+
31+
return _copy_to_func_impl
32+
33+
def copy_from_func(self) -> Callable:
34+
"""Returns the copy-method that should be used
35+
for copying the benchmark arguments."""
36+
import cupy
37+
38+
return cupy.asnumpy

0 commit comments

Comments
 (0)