21
21
from reference_black_scholes import ref_python_black_scholes
22
22
23
23
24
- def gen_option_params (n_opts , pl , ph , sl , sh , tl , th , rl , rh , vl , vh , dtype ):
25
- usm_mem = dpctl_mem . MemoryUSMShared ( n_opts * 5 * np .dtype (dtype ).itemsize )
26
- # usm_mem2 = dpctl_mem.MemoryUSMDevice(n_opts * 5 * np.dtype(dtype).itemsize )
24
+ def gen_option_params (n_opts , pl , ph , sl , sh , tl , th , rl , rh , vl , vh , dtype , queue = None ):
25
+ nbytes = n_opts * 5 * np .dtype (dtype ).itemsize
26
+ usm_mem = dpctl_mem .MemoryUSMShared ( nbytes , queue = queue )
27
27
params = np .ndarray (shape = (n_opts , 5 ), buffer = usm_mem , dtype = dtype )
28
28
seed = 1234
29
- bs .populate_params (params , pl , ph , sl , sh , tl , th , rl , rh , vl , vh , seed )
29
+ bs .populate_params (params , pl , ph , sl , sh , tl , th , rl , rh , vl , vh , seed , queue = queue )
30
30
return params
31
31
32
32
@@ -47,38 +47,42 @@ def gen_option_params(n_opts, pl, ph, sl, sh, tl, th, rl, rh, vl, vh, dtype):
47
47
# compute prices in CPython
48
48
X_ref = np .array ([ref_python_black_scholes (* opt ) for opt in opts ], dtype = "d" )
49
49
50
- print (np .allclose (Xgpu , X_ref , atol = 1e-5 ))
50
+ print ("Correctness check: allclose(Xgpu, Xref) == " , np .allclose (Xgpu , X_ref , atol = 1e-5 ))
51
51
52
52
n_opts = 3 * 10 ** 6
53
53
54
54
# compute on CPU sycl device
55
55
import timeit
56
56
57
- for _ in range (3 ):
57
+ cpu_q = dpctl .SyclQueue ("opencl:cpu:0" )
58
+ opts1 = gen_option_params (
59
+ n_opts , 20.0 , 30.0 , 22.0 , 29.0 , 18.0 , 24.0 , 0.01 , 0.05 , 0.01 , 0.05 , "d" , queue = cpu_q
60
+ )
61
+
62
+ gpu_q = dpctl .SyclQueue ("level_zero:gpu:0" )
63
+ opts2 = gen_option_params (
64
+ n_opts , 20.0 , 30.0 , 22.0 , 29.0 , 18.0 , 24.0 , 0.01 , 0.05 , 0.01 , 0.05 , "d" , queue = gpu_q
65
+ )
58
66
59
- dpctl .set_global_queue ("opencl:cpu:0" )
60
- print ("Using : {}" .format (dpctl .get_current_queue ().sycl_device .name ))
67
+ cpu_times = []
68
+ gpu_times = []
69
+ for _ in range (5 ):
61
70
62
71
t0 = timeit .default_timer ()
63
- opts1 = gen_option_params (
64
- n_opts , 20.0 , 30.0 , 22.0 , 29.0 , 18.0 , 24.0 , 0.01 , 0.05 , 0.01 , 0.05 , "d"
65
- )
66
- X1 = bs .black_scholes_price (opts1 )
72
+ X1 = bs .black_scholes_price (opts1 , queue = cpu_q )
67
73
t1 = timeit .default_timer ()
68
74
69
- print ( "Elapsed: {}" . format (t1 - t0 ) )
75
+ cpu_times . append (t1 - t0 )
70
76
71
77
# compute on GPU sycl device
72
- dpctl .set_global_queue ("level_zero:gpu:0" )
73
- print ("Using : {}" .format (dpctl .get_current_queue ().sycl_device .name ))
74
78
75
79
t0 = timeit .default_timer ()
76
- opts2 = gen_option_params (
77
- n_opts , 20.0 , 30.0 , 22.0 , 29.0 , 18.0 , 24.0 , 0.01 , 0.05 , 0.01 , 0.05 , "d"
78
- )
79
- X2 = bs .black_scholes_price (opts2 )
80
+ X2 = bs .black_scholes_price (opts2 , queue = gpu_q )
80
81
t1 = timeit .default_timer ()
81
- print ("Elapsed: {}" .format (t1 - t0 ))
82
+ gpu_times .append (t1 - t0 )
83
+
84
+ print ("Using : {}" .format (cpu_q .sycl_device .name ))
85
+ print ("Wall times : {}" .format (cpu_times ))
82
86
83
- print (np . abs ( opts1 - opts2 ). max ( ))
84
- print (np . abs ( X2 - X1 ). max ( ))
87
+ print ("Using : {}" . format ( gpu_q . sycl_device . name ))
88
+ print ("Wall times : {}" . format ( gpu_times ))
0 commit comments