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