Skip to content

Commit

Permalink
Updating all kernel examples into new launch parameter syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
chudur-budur committed Jan 30, 2023
1 parent 64b81a9 commit 33f524f
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 42 deletions.
3 changes: 2 additions & 1 deletion numba_dpex/examples/kernel/atomic_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dpnp as np

import numba_dpex as ndpex
from numba_dpex.core.kernel_interface.utils import Range


@ndpex.kernel
Expand All @@ -20,7 +21,7 @@ def main():
print("Using device ...")
print(a.device)

atomic_reduction[N](a)
atomic_reduction[Range(N)](a)
print("Reduction sum =", a[0])

print("Done...")
Expand Down
5 changes: 4 additions & 1 deletion numba_dpex/examples/kernel/black_scholes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dpnp as np

import numba_dpex as ndpx
from numba_dpex.core.kernel_interface.utils import Range

# Stock price range
S0L = 10.0
Expand Down Expand Up @@ -94,7 +95,9 @@ def main():
print("Using device ...")
print(price.device)

kernel_black_scholes[NOPT](price, strike, t, rate, volatility, call, put)
kernel_black_scholes[Range(NOPT)](
price, strike, t, rate, volatility, call, put
)

print("Call:", call)
print("Put:", put)
Expand Down
11 changes: 6 additions & 5 deletions numba_dpex/examples/kernel/device_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numba_dpex as ndpex
from numba_dpex import float32, int32, int64
from numba_dpex.core.kernel_interface.utils import Range

# Array size
N = 10
Expand Down Expand Up @@ -69,7 +70,7 @@ def test1():

print("A=", a)
try:
a_kernel_function[N](a, b)
a_kernel_function[Range(N)](a, b)
except Exception as err:
print(err)
print("B=", b)
Expand All @@ -87,7 +88,7 @@ def test2():

print("A=", a)
try:
a_kernel_function_int32[N](a, b)
a_kernel_function_int32[Range(N)](a, b)
except Exception as err:
print(err)
print("B=", b)
Expand All @@ -105,7 +106,7 @@ def test3():

print("A=", a)
try:
a_kernel_function_int32_float32[N](a, b)
a_kernel_function_int32_float32[Range(N)](a, b)
except Exception as err:
print(err)
print("B=", b)
Expand All @@ -119,7 +120,7 @@ def test3():

print("A=", a)
try:
a_kernel_function_int32_float32[N](a, b)
a_kernel_function_int32_float32[Range(N)](a, b)
except Exception as err:
print(err)
print("B=", b)
Expand All @@ -134,7 +135,7 @@ def test3():

print("A=", a)
try:
a_kernel_function_int32_float32[N](a, b)
a_kernel_function_int32_float32[Range(N)](a, b)
except Exception as err:
print(err)
print("B=", b)
Expand Down
11 changes: 8 additions & 3 deletions numba_dpex/examples/kernel/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from numpy.testing import assert_almost_equal

import numba_dpex as ndpex
from numba_dpex.core.kernel_interface.utils import NdRange, Range

# Interpolation domain
XLO = 10.0
Expand Down Expand Up @@ -114,9 +115,13 @@ def main():

print("Using device ...")
print(xp.device)
global_range = (N_POINTS // N_POINTS_PER_WORK_ITEM,)
local_range = (LOCAL_SIZE,)
kernel_polynomial[global_range, local_range](xp, yp, COEFFICIENTS)
global_range = Range(
N_POINTS // N_POINTS_PER_WORK_ITEM,
)
local_range = Range(
LOCAL_SIZE,
)
kernel_polynomial[NdRange(global_range, local_range)](xp, yp, COEFFICIENTS)

# Copy results back to the host
nyp = np.asnumpy(yp)
Expand Down
7 changes: 4 additions & 3 deletions numba_dpex/examples/kernel/kernel_private_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from numba import float32

import numba_dpex
from numba_dpex.core.kernel_interface.utils import NdRange, Range


def private_memory():
Expand Down Expand Up @@ -39,9 +40,9 @@ def private_memory_kernel(A):
print("Using device ...")
device.print_device_info()

global_range = (N,)
local_range = (N,)
private_memory_kernel[global_range, local_range](arr)
global_range = Range(N)
local_range = Range(N)
private_memory_kernel[NdRange(global_range, local_range)](arr)

arr_out = dpt.asnumpy(arr)
np.testing.assert_allclose(orig * 2, arr_out)
Expand Down
9 changes: 5 additions & 4 deletions numba_dpex/examples/kernel/kernel_specialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
InvalidKernelSpecializationError,
MissingSpecializationError,
)
from numba_dpex.core.kernel_interface.utils import Range

# Similar to Numba, numba-dpex supports eager compilation of functions. The
# following examples demonstrate the feature for numba_dpex.kernel and presents
Expand Down Expand Up @@ -38,7 +39,7 @@ def data_parallel_sum(a, b, c):
b = dpt.ones(1024, dtype=dpt.int64)
c = dpt.zeros(1024, dtype=dpt.int64)

data_parallel_sum[1024](a, b, c)
data_parallel_sum[Range(1024)](a, b, c)

npc = dpt.asnumpy(c)
npc_expected = np.full(1024, 2, dtype=np.int64)
Expand All @@ -65,7 +66,7 @@ def data_parallel_sum2(a, b, c):
b = dpt.ones(1024, dtype=dpt.int64)
c = dpt.zeros(1024, dtype=dpt.int64)

data_parallel_sum2[1024](a, b, c)
data_parallel_sum2[Range(1024)](a, b, c)

npc = dpt.asnumpy(c)
npc_expected = np.full(1024, 2, dtype=np.int64)
Expand All @@ -76,7 +77,7 @@ def data_parallel_sum2(a, b, c):
b = dpt.ones(1024, dtype=dpt.float32)
c = dpt.zeros(1024, dtype=dpt.float32)

data_parallel_sum2[1024](a, b, c)
data_parallel_sum2[Range(1024)](a, b, c)

npc = dpt.asnumpy(c)
npc_expected = np.full(1024, 2, dtype=np.float32)
Expand All @@ -94,7 +95,7 @@ def data_parallel_sum2(a, b, c):
c = dpt.zeros(1024, dtype=dpt.int32)

try:
data_parallel_sum[1024](a, b, c)
data_parallel_sum[Range(1024)](a, b, c)
except MissingSpecializationError as mse:
print(mse)

Expand Down
7 changes: 4 additions & 3 deletions numba_dpex/examples/kernel/matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np

import numba_dpex as dpex
from numba_dpex.core.kernel_interface.utils import NdRange, Range


@dpex.kernel
Expand All @@ -30,13 +31,13 @@ def gemm(a, b, c):
Y = 16
global_size = X, X

griddim = X, X
blockdim = Y, Y
griddim = Range(X, X)
blockdim = Range(Y, Y)


def driver(a, b, c):
# Invoke the kernel
gemm[griddim, blockdim](a, b, c)
gemm[NdRange(griddim, blockdim)](a, b, c)


def main():
Expand Down
7 changes: 4 additions & 3 deletions numba_dpex/examples/kernel/pairwise_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np

import numba_dpex as dpex
from numba_dpex.core.kernel_interface.utils import NdRange, Range

parser = argparse.ArgumentParser(
description="Program to compute pairwise distance"
Expand All @@ -25,9 +26,9 @@
args = parser.parse_args()

# Global work size is equal to the number of points
global_size = (args.n,)
global_size = Range(args.n)
# Local Work size is optional
local_size = (args.l,)
local_size = Range(args.l)

X = np.random.random((args.n, args.d)).astype(np.single)
D = np.empty((args.n, args.n), dtype=np.single)
Expand Down Expand Up @@ -65,7 +66,7 @@ def driver():

for repeat in range(args.r):
start = time()
pairwise_distance[global_size, local_size](
pairwise_distance[NdRange(global_size, local_size)](
x_ndarray, d_ndarray, X.shape[0], X.shape[1]
)
end = time()
Expand Down
3 changes: 2 additions & 1 deletion numba_dpex/examples/kernel/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dpnp as np

import numba_dpex as ndpx
from numba_dpex.core.kernel_interface.utils import Range

# 1D array size
N = 64
Expand Down Expand Up @@ -56,7 +57,7 @@ def main():

print("Using device ...")
print(arr.device)
kernel_hillis_steele_scan[N](arr)
kernel_hillis_steele_scan[Range(N)](arr)

# the output should be [0, 1, 3, 6, ...]
arr_np = np.asnumpy(arr)
Expand Down
5 changes: 3 additions & 2 deletions numba_dpex/examples/kernel/select_device_for_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np

import numba_dpex
from numba_dpex.core.kernel_interface.utils import NdRange, Range

"""
We support passing arrays of two types to a @numba_dpex.kernel decorated
Expand Down Expand Up @@ -86,7 +87,7 @@ def select_device_ndarray(N):
default_device = dpctl.select_default_device()

with numba_dpex.offload_to_sycl_device(default_device.filter_string):
sum_kernel[(N,), (1,)](a, b, got)
sum_kernel[NdRange(Range(N), Range(1))](a, b, got)

expected = a + b

Expand All @@ -110,7 +111,7 @@ def select_device_SUAI(N):

# Users don't need to specify where the computation will
# take place. It will be inferred from data.
sum_kernel[(N,), (1,)](da, db, dc)
sum_kernel[NdRange(Range(N), Range(1))](da, db, dc)

dc.usm_data.copy_to_host(got.reshape((-1)).view("|u1"))

Expand Down
7 changes: 4 additions & 3 deletions numba_dpex/examples/kernel/sum_reduction_ocl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from numba import int32

import numba_dpex as dpex
from numba_dpex.core.kernel_interface.utils import NdRange, Range


@dpex.kernel
Expand Down Expand Up @@ -49,9 +50,9 @@ def sum_reduce(A):

partial_sums = dpt.zeros(nb_work_groups, dtype=A.dtype, device=A.device)

gs = (global_size,)
ls = (work_group_size,)
sum_reduction_kernel[gs, ls](A, partial_sums)
gs = Range(global_size)
ls = Range(work_group_size)
sum_reduction_kernel[NdRange(gs, ls)](A, partial_sums)

final_sum = 0
# calculate the final sum in HOST
Expand Down
3 changes: 2 additions & 1 deletion numba_dpex/examples/kernel/vector_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy.testing as testing

import numba_dpex as ndpx
from numba_dpex.core.kernel_interface.utils import Range


# Data parallel kernel implementing vector sum
Expand All @@ -18,7 +19,7 @@ def kernel_vector_sum(a, b, c):
# Utility function for printing and testing
def driver(a, b, c, global_size):

kernel_vector_sum[global_size](a, b, c)
kernel_vector_sum[Range(global_size)](a, b, c)

a_np = dpnp.asnumpy(a) # Copy dpnp array a to NumPy array a_np
b_np = dpnp.asnumpy(b) # Copy dpnp array b to NumPy array b_np
Expand Down
14 changes: 2 additions & 12 deletions numba_dpex/examples/kernel/vector_sum2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ def data_parallel_sum(a, b, c):


def driver(a, b, c, global_size):
data_parallel_sum[global_size, dpex.DEFAULT_LOCAL_SIZE](a, b, c)


def driver_with_range(a, b, c, global_size):
ranges = Range(*global_size)
data_parallel_sum[ranges](a, b, c)
data_parallel_sum[global_size](a, b, c)


def main():
# Array dimensions
X = 8
Y = 8
global_size = X, Y
global_size = Range(X, Y)

a = np.arange(X * Y, dtype=np.float32).reshape(X, Y)
b = np.arange(X * Y, dtype=np.float32).reshape(X, Y)
Expand All @@ -59,11 +54,6 @@ def main():
c_out = dpt.asnumpy(c_dpt)
assert np.allclose(c, c_out)

print("Running kernel with the new lanuch parameter syntax ...")
driver_with_range(a_dpt, b_dpt, c_dpt, global_size)
c_out = dpt.asnumpy(c_dpt)
assert np.allclose(c, c_out)

print("Done...")


Expand Down

0 comments on commit 33f524f

Please sign in to comment.