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 (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 )
2727 params = np .ndarray (shape = (n_opts , 5 ), buffer = usm_mem , dtype = dtype )
2828 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 )
3030 return params
3131
3232
@@ -47,38 +47,42 @@ def gen_option_params(n_opts, pl, ph, sl, sh, tl, th, rl, rh, vl, vh, dtype):
4747# compute prices in CPython
4848X_ref = np .array ([ref_python_black_scholes (* opt ) for opt in opts ], dtype = "d" )
4949
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 ))
5151
5252n_opts = 3 * 10 ** 6
5353
5454# compute on CPU sycl device
5555import timeit
5656
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+ )
5866
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 ):
6170
6271 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 )
6773 t1 = timeit .default_timer ()
6874
69- print ( "Elapsed: {}" . format (t1 - t0 ) )
75+ cpu_times . append (t1 - t0 )
7076
7177 # 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 ))
7478
7579 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 )
8081 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 ))
8286
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