Skip to content

Commit f64bd0f

Browse files
authored
Modify test cases and examples to use level zero queue. (#179)
1 parent c158f3f commit f64bd0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2859
-3616
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include MANIFEST.in
22
include README.md setup.py LICENSE
33

44
recursive-include numba_dppy *.cl
5+
recursive-include numba_dppy *.spir
56

67
include versioneer.py
78
include numba_dppy/_version.py

numba_dppy/dpnp_glue/dpnp_array_creations_impl.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ def dpnp_zeros_like_impl(a, dtype=None):
6464
void dpnp_initval_c(void* result1, void* value, size_t size)
6565
6666
"""
67-
res_dtype = dtype or a.dtype
68-
if dtype:
69-
name_dtype = res_dtype.dtype.name
70-
else:
67+
res_dtype = dtype
68+
if dtype == types.none:
69+
res_dtype = a.dtype
7170
name_dtype = res_dtype.name
71+
else:
72+
name_dtype = res_dtype.dtype.name
7273

7374
sig = signature(ret_type, types.voidptr, types.voidptr, types.intp)
7475
dpnp_func = dpnp_ext.dpnp_func("dpnp_" + name, [name_dtype, "NONE"], sig)
@@ -77,7 +78,7 @@ def dpnp_zeros_like_impl(a, dtype=None):
7778

7879
def dpnp_impl(a, dtype=None):
7980
b = np.zeros(1, dtype=res_dtype)
80-
out = np.arange(a.size, dtype=res_dtype)
81+
out = np.zeros(a.shape, dtype=res_dtype)
8182
common_impl(a, b, out, dpnp_func, PRINT_DEBUG)
8283
return out
8384

@@ -98,11 +99,12 @@ def dpnp_ones_like_impl(a, dtype=None):
9899
void dpnp_initval_c(void* result1, void* value, size_t size)
99100
100101
"""
101-
res_dtype = dtype or a.dtype
102-
if dtype:
103-
name_dtype = res_dtype.dtype.name
104-
else:
102+
res_dtype = dtype
103+
if dtype == types.none:
104+
res_dtype = a.dtype
105105
name_dtype = res_dtype.name
106+
else:
107+
name_dtype = res_dtype.dtype.name
106108

107109
sig = signature(ret_type, types.voidptr, types.voidptr, types.intp)
108110
dpnp_func = dpnp_ext.dpnp_func("dpnp_" + name, [name_dtype, "NONE"], sig)
@@ -111,7 +113,7 @@ def dpnp_ones_like_impl(a, dtype=None):
111113

112114
def dpnp_impl(a, dtype=None):
113115
b = np.ones(1, dtype=res_dtype)
114-
out = np.arange(a.size, dtype=res_dtype)
116+
out = np.ones(a.shape, dtype=res_dtype)
115117
common_impl(a, b, out, dpnp_func, PRINT_DEBUG)
116118
return out
117119

@@ -139,13 +141,14 @@ def dpnp_full_like_impl(a, b):
139141
PRINT_DEBUG = dpnp_lowering.DEBUG
140142

141143
def dpnp_impl(a, b):
142-
out = np.arange(a.size, dtype=res_dtype)
144+
out = np.ones(a.shape, dtype=res_dtype)
143145
common_impl(a, b, out, dpnp_func, PRINT_DEBUG)
144146
return out
145147

146148
return dpnp_impl
147149

148150

151+
# TODO: This implementation is incorrect
149152
@overload(stubs.dpnp.full)
150153
def dpnp_full_impl(a, b):
151154
name = "full"

numba_dppy/dpnp_glue/dpnp_array_ops_impl.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,13 @@ def dpnp_cumprod_impl(a):
9191
dpnp_func = dpnp_ext.dpnp_func("dpnp_" + name, [a.dtype.name, "NONE"], sig)
9292

9393
PRINT_DEBUG = dpnp_lowering.DEBUG
94+
if a.dtype == types.Integer:
95+
ret_dtype = np.int64
96+
else:
97+
ret_dtype = a.dtype
9498

9599
def dpnp_impl(a):
96-
out = np.arange(a.size, dtype=a.dtype)
100+
out = np.arange(a.size, dtype=ret_dtype)
97101
common_impl(a, out, dpnp_func, PRINT_DEBUG)
98102

99103
return out

numba_dppy/dpnp_glue/dpnp_linalgimpl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,15 @@ def dpnp_matrix_power_impl(a, n):
346346
size_t size_n, size_t size_k)
347347
"""
348348

349+
PRINT_DEBUG = dpnp_lowering.DEBUG
350+
349351
def dpnp_impl(a, n):
350352
if n < 0:
351353
raise ValueError("n < 0 is not supported for np.linalg.matrix_power(a, n)")
352354

353355
if n == 0:
356+
if PRINT_DEBUG:
357+
print("dpnp implementation")
354358
return np.identity(a.shape[0], a.dtype)
355359

356360
result = a

numba_dppy/dpnp_glue/dpnp_statisticsimpl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def dpnp_mean_impl(a):
175175
types.intp,
176176
)
177177
dpnp_func = dpnp_ext.dpnp_func("dpnp_" + name, [a.dtype.name, "NONE"], sig)
178+
PRINT_DEBUG = dpnp_lowering.DEBUG
178179

179180
res_dtype = np.float64
180181
if a.dtype == types.float32:

numba_dppy/dppy_passes.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,46 +98,58 @@ def run_pass(self, state):
9898
).value
9999
if (
100100
isinstance(call_node, ir.Expr)
101+
and call_node.op == "getattr"
101102
and call_node.attr == "array"
102103
):
103-
arg = None
104-
# at first look in keyword arguments to get the shape, which has to be
105-
# constant
106-
if expr.kws:
107-
for _arg in expr.kws:
108-
if _arg[0] == "shape":
109-
arg = _arg[1]
110-
111-
if not arg:
112-
arg = expr.args[0]
113-
114-
error = False
115-
# arg can be one constant or a tuple of constant items
116-
arg_type = func_ir.get_definition(arg.name)
117-
if isinstance(arg_type, ir.Expr):
118-
# we have a tuple
119-
for item in arg_type.items:
104+
# let's check if it is from numba_dppy.local
105+
attr_node = block.find_variable_assignment(
106+
call_node.value.name
107+
).value
108+
if (
109+
isinstance(attr_node, ir.Expr)
110+
and attr_node.op == "getattr"
111+
and attr_node.attr == "local"
112+
):
113+
114+
arg = None
115+
# at first look in keyword arguments to get the shape, which has to be
116+
# constant
117+
if expr.kws:
118+
for _arg in expr.kws:
119+
if _arg[0] == "shape":
120+
arg = _arg[1]
121+
122+
if not arg:
123+
arg = expr.args[0]
124+
125+
error = False
126+
# arg can be one constant or a tuple of constant items
127+
arg_type = func_ir.get_definition(arg.name)
128+
if isinstance(arg_type, ir.Expr):
129+
# we have a tuple
130+
for item in arg_type.items:
131+
if not isinstance(
132+
func_ir.get_definition(item.name),
133+
ir.Const,
134+
):
135+
error = True
136+
break
137+
138+
else:
120139
if not isinstance(
121-
func_ir.get_definition(item.name), ir.Const
140+
func_ir.get_definition(arg.name), ir.Const
122141
):
123142
error = True
124143
break
125144

126-
else:
127-
if not isinstance(
128-
func_ir.get_definition(arg.name), ir.Const
129-
):
130-
error = True
131-
break
132-
133-
if error:
134-
warnings.warn_explicit(
135-
"The size of the Local memory has to be constant",
136-
errors.NumbaError,
137-
state.func_id.filename,
138-
state.func_id.firstlineno,
139-
)
140-
raise
145+
if error:
146+
warnings.warn_explicit(
147+
"The size of the Local memory has to be constant",
148+
errors.NumbaError,
149+
state.func_id.filename,
150+
state.func_id.firstlineno,
151+
)
152+
raise
141153

142154
if config.DEBUG or config.DUMP_IR:
143155
name = state.func_ir.func_id.func_qualname
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2021 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import numpy as np
16+
import math
17+
import time
18+
import numba_dppy, numba_dppy as dppy
19+
import unittest
20+
import dpctl
21+
22+
23+
RISKFREE = 0.02
24+
VOLATILITY = 0.30
25+
26+
A1 = 0.31938153
27+
A2 = -0.356563782
28+
A3 = 1.781477937
29+
A4 = -1.821255978
30+
A5 = 1.330274429
31+
RSQRT2PI = 0.39894228040143267793994605993438
32+
33+
34+
def randfloat(rand_var, low, high):
35+
return (1.0 - rand_var) * low + rand_var * high
36+
37+
38+
OPT_N = 400
39+
iterations = 2
40+
41+
stockPrice = randfloat(np.random.random(OPT_N), 5.0, 30.0)
42+
optionStrike = randfloat(np.random.random(OPT_N), 1.0, 100.0)
43+
optionYears = randfloat(np.random.random(OPT_N), 0.25, 10.0)
44+
callResult = np.zeros(OPT_N)
45+
putResult = -np.ones(OPT_N)
46+
47+
48+
@dppy.kernel
49+
def black_scholes_dppy(callResult, putResult, S, X, T, R, V):
50+
i = dppy.get_global_id(0)
51+
if i >= S.shape[0]:
52+
return
53+
sqrtT = math.sqrt(T[i])
54+
d1 = (math.log(S[i] / X[i]) + (R + 0.5 * V * V) * T[i]) / (V * sqrtT)
55+
d2 = d1 - V * sqrtT
56+
57+
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d1))
58+
cndd1 = (
59+
RSQRT2PI
60+
* math.exp(-0.5 * d1 * d1)
61+
* (K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))
62+
)
63+
if d1 > 0:
64+
cndd1 = 1.0 - cndd1
65+
66+
K = 1.0 / (1.0 + 0.2316419 * math.fabs(d2))
67+
cndd2 = (
68+
RSQRT2PI
69+
* math.exp(-0.5 * d2 * d2)
70+
* (K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))))
71+
)
72+
if d2 > 0:
73+
cndd2 = 1.0 - cndd2
74+
75+
expRT = math.exp((-1.0 * R) * T[i])
76+
callResult[i] = S[i] * cndd1 - X[i] * expRT * cndd2
77+
putResult[i] = X[i] * expRT * (1.0 - cndd2) - S[i] * (1.0 - cndd1)
78+
79+
80+
blockdim = 512, 1
81+
griddim = int(math.ceil(float(OPT_N) / blockdim[0])), 1
82+
83+
with dpctl.device_context("level0:gpu") as gpu_queue:
84+
time1 = time.time()
85+
for i in range(iterations):
86+
black_scholes_dppy[blockdim, griddim](
87+
callResult,
88+
putResult,
89+
stockPrice,
90+
optionStrike,
91+
optionYears,
92+
RISKFREE,
93+
VOLATILITY,
94+
)
95+
96+
print("callResult : ", callResult)
97+
print("putResult : ", putResult)

numba_dppy/examples/blacksholes_njit.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (c) 2017 Intel Corporation
33
# SPDX-License-Identifier: BSD-2-Clause
44
#
5-
5+
import dpctl
66
import numba
77
import numpy as np
88
import math
@@ -16,7 +16,7 @@ def cndf2(inp):
1616
return out
1717

1818

19-
@numba.njit(parallel={"offload": True}, fastmath=True)
19+
@numba.njit(parallel=True, fastmath=True)
2020
def blackscholes(sptprice, strike, rate, volatility, timev):
2121
logterm = np.log(sptprice / strike)
2222
powterm = 0.5 * volatility * volatility
@@ -52,9 +52,24 @@ def main():
5252
args = parser.parse_args()
5353
options = args.options
5454

55-
run(10)
56-
print("options = ", options)
57-
run(options)
55+
if dpctl.has_gpu_queues():
56+
print("\nScheduling on OpenCL GPU\n")
57+
with dpctl.device_context("opencl:gpu") as gpu_queue:
58+
run(10)
59+
else:
60+
print("\nSkip scheduling on OpenCL GPU\n")
61+
# if dpctl.has_gpu_queues(dpctl.backend_type.level_zero):
62+
# print("\nScheduling on Level Zero GPU\n")
63+
# with dpctl.device_context("level0:gpu") as gpu_queue:
64+
# run(10)
65+
# else:
66+
# print("\nSkip scheduling on Level Zero GPU\n")
67+
if dpctl.has_cpu_queues():
68+
print("\nScheduling on OpenCL CPU\n")
69+
with dpctl.device_context("opencl:cpu") as cpu_queue:
70+
run(10)
71+
else:
72+
print("\nSkip scheduling on OpenCL CPU\n")
5873

5974

6075
if __name__ == "__main__":

numba_dppy/examples/pa_examples/test1-2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def f1(a, b):
3232
print("a:", a, hex(a.ctypes.data))
3333
print("b:", b, hex(b.ctypes.data))
3434

35-
with dpctl.device_context("opencl:gpu:0"):
35+
with dpctl.device_context("level0:gpu:0"):
3636
c = f1(a, b)
3737

3838
print("BIG RESULT c:", c, hex(c.ctypes.data))

0 commit comments

Comments
 (0)