2121from reference_black_scholes import ref_python_black_scholes
2222
2323
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 )
2729 params = np .ndarray (shape = (n_opts , 5 ), buffer = usm_mem , dtype = dtype )
2830 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+ )
3034 return params
3135
3236
@@ -47,38 +51,44 @@ def gen_option_params(n_opts, pl, ph, sl, sh, tl, th, rl, rh, vl, vh, dtype):
4751# compute prices in CPython
4852X_ref = np .array ([ref_python_black_scholes (* opt ) for opt in opts ], dtype = "d" )
4953
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+ )
5157
5258n_opts = 3 * 10 ** 6
5359
5460# compute on CPU sycl device
5561import timeit
5662
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+ )
5872
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 ):
6176
6277 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 )
6779 t1 = timeit .default_timer ()
6880
69- print ( "Elapsed: {}" . format (t1 - t0 ) )
81+ cpu_times . append (t1 - t0 )
7082
7183 # 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 ))
7484
7585 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 )
8087 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 ))
8292
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