Skip to content

Commit

Permalink
Merge pull request #335 from IntelPython/cupy_sync_add
Browse files Browse the repository at this point in the history
Adding synchronize to cupy implementations
  • Loading branch information
adarshyoga authored Feb 13, 2024
2 parents 8972feb + ba7c7a6 commit 0646f1e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
10 changes: 6 additions & 4 deletions dpbench/benchmarks/black_scholes/black_scholes_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

import cupy as np
import cupy as cp
from scipy.special import erf


Expand All @@ -14,20 +14,22 @@ def black_scholes(nopt, price, strike, t, rate, volatility, call, put):
S = strike
T = t

a = np.log(P / S)
a = cp.log(P / S)
b = T * mr

z = T * sig_sig_two
c = 0.25 * z
y = np.true_divide(1.0, np.sqrt(z))
y = cp.true_divide(1.0, cp.sqrt(z))

w1 = (a - b + c) * y
w2 = (a - b - c) * y

d1 = 0.5 + 0.5 * erf(w1)
d2 = 0.5 + 0.5 * erf(w2)

Se = np.exp(b) * S
Se = cp.exp(b) * S

call[:] = P * d1 - Se * d2
put[:] = call - P + Se

cp.cuda.stream.get_current_stream().synchronize()
16 changes: 10 additions & 6 deletions dpbench/benchmarks/gpairs/gpairs_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
#
# SPDX-License-Identifier: Apache-2.0

import cupy as np
import cupy as cp


def _gpairs_impl(x1, y1, z1, w1, x2, y2, z2, w2, rbins):
dm = (
np.square(x2 - x1[:, None])
+ np.square(y2 - y1[:, None])
+ np.square(z2 - z1[:, None])
cp.square(x2 - x1[:, None])
+ cp.square(y2 - y1[:, None])
+ cp.square(z2 - z1[:, None])
)
return np.array(
[np.outer(w1, w2)[dm <= rbins[k]].sum() for k in range(len(rbins))]
ret_arr = cp.array(
[cp.outer(w1, w2)[dm <= rbins[k]].sum() for k in range(len(rbins))]
)

cp.cuda.stream.get_current_stream().synchronize()

return ret_arr


def gpairs(nopt, nbins, x1, y1, z1, w1, x2, y2, z2, w2, rbins, results):
results[:] = _gpairs_impl(x1, y1, z1, w1, x2, y2, z2, w2, rbins)
8 changes: 5 additions & 3 deletions dpbench/benchmarks/l2_norm/l2_norm_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#
# SPDX-License-Identifier: Apache-2.0

import cupy as np
import cupy as cp


def l2_norm(a, d):
sq = np.square(a)
sq = cp.square(a)
sum = sq.sum(axis=1)
d[:] = np.sqrt(sum)
d[:] = cp.sqrt(sum)

cp.cuda.stream.get_current_stream().synchronize()
16 changes: 9 additions & 7 deletions dpbench/benchmarks/pairwise_distance/pairwise_distance_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#
# SPDX-License-Identifier: Apache-2.0

import cupy as np
import cupy as cp


def pairwise_distance(X1, X2, D):
x1 = np.sum(np.square(X1), axis=1)
x2 = np.sum(np.square(X2), axis=1)
np.dot(X1, X2.T, D)
x1 = cp.sum(cp.square(X1), axis=1)
x2 = cp.sum(cp.square(X2), axis=1)
cp.dot(X1, X2.T, D)
D *= -2
x3 = x1.reshape(x1.size, 1)
np.add(D, x3, D)
np.add(D, x2, D)
np.sqrt(D, D)
cp.add(D, x3, D)
cp.add(D, x2, D)
cp.sqrt(D, D)

cp.cuda.stream.get_current_stream().synchronize()
13 changes: 8 additions & 5 deletions dpbench/benchmarks/pca/pca_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
#
# SPDX-License-Identifier: Apache-2.0

import cupy as np
import cupy as cp


def pca(data, dims_rescaled_data=2):
# mean center the data
data -= data.mean(axis=0)

# calculate the covariance matrix
v = np.cov(data, rowvar=False, dtype=data.dtype)
v = cp.cov(data, rowvar=False, dtype=data.dtype)

# calculate eigenvectors & eigenvalues of the covariance matrix
evalues, evectors = np.linalg.eigh(v)
evalues, evectors = cp.linalg.eigh(v)

# sort eigenvalues and eigenvectors in decreasing order
idx = np.argsort(evalues)[::-1]
idx = cp.argsort(evalues)[::-1]
evectors = evectors[:, idx]
evalues = evalues[idx]

Expand All @@ -25,7 +25,10 @@ def pca(data, dims_rescaled_data=2):
evectors = evectors[:, :dims_rescaled_data]

# carry out the transformation on the data using eigenvectors
tdata = np.dot(evectors.T, data.T).T
tdata = cp.dot(evectors.T, data.T).T

cp.cuda.stream.get_current_stream().synchronize()

# return the transformed data, eigenvalues, and eigenvectors

return tdata, evalues, evectors
14 changes: 8 additions & 6 deletions dpbench/benchmarks/rambo/rambo_cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#
# SPDX-License-Identifier: Apache-2.0

import cupy as np
import cupy as cp


def rambo(nevts, nout, C1, F1, Q1, output):
C = 2.0 * C1 - 1.0
S = np.sqrt(1 - np.square(C))
F = 2.0 * np.pi * F1
Q = -np.log(Q1)
S = cp.sqrt(1 - cp.square(C))
F = 2.0 * cp.pi * F1
Q = -cp.log(Q1)

output[:, :, 0] = Q
output[:, :, 1] = Q * S * np.sin(F)
output[:, :, 2] = Q * S * np.cos(F)
output[:, :, 1] = Q * S * cp.sin(F)
output[:, :, 2] = Q * S * cp.cos(F)
output[:, :, 3] = Q * C

cp.cuda.stream.get_current_stream().synchronize()

0 comments on commit 0646f1e

Please sign in to comment.