-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblack_scholes.py
329 lines (268 loc) · 7.52 KB
/
black_scholes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
The Black-Scholes program computes the price of a portfolio of
options using partial differential equations.
The entire computation performed by Black-Scholes is data-parallel
where each option can be priced independent of other options.
Examples:
# run 1 million options, by default times 10 iterations
python black_scholes.py -n 1000000
# run 10 iterations of M16Gb problem size
python black_scholes.py -i 10 -p M16Gb
# use float32 data type
python black_scholes.py -p M16Gb -d f32
# MPI parallel run
mpiexec -n 4 python black_scholes.py -p M16Gb
# use numpy backend (serial only)
python black_scholes.py -p M16Gb -b numpy
"""
import argparse
import os
import time as time_mod
from functools import partial
import numpy
try:
import mpi4py
mpi4py.rc.finalize = False
from mpi4py import MPI
comm_rank = MPI.COMM_WORLD.Get_rank()
comm = MPI.COMM_WORLD
except ImportError:
comm_rank = 0
comm = None
def info(s):
if comm_rank == 0:
print(s)
def naive_erf(x):
"""
Error function (erf) implementation
Adapted from formula 7.1.26 in
Abramowitz and Stegun, "Handbook of Mathematical Functions", 1965.
"""
y = numpy.abs(x)
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.421413741
a4 = -1.453152027
a5 = 1.061405429
p = 0.3275911
t = 1.0 / (1.0 + p * y)
f = (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t
return numpy.sign(x) * (1.0 - f * numpy.exp(-y * y))
def initialize(create_full, nopt, seed, dtype):
"""
Initialize arrays.
Input
---------
np:
Numpy-like array module
nopt: int
number of options
seed: double
random generator seed
dtype:
data type
Output
------
price, strike, t, call, put: array of shape (nopt, )
data arrays
rate, volatility: float
rate and volatility parameters
"""
# S0L = 10.0
# S0H = 50.0
# XL = 10.0
# XH = 50.0
# TL = 1.0
# TH = 2.0
RISK_FREE = 0.1
VOLATILITY = 0.2
# random initialization
# random.seed(seed)
# price = random.uniform(S0L, S0H, nopt)
# strike = random.uniform(XL, XH, nopt)
# t = random.uniform(TL, TH, nopt)
# constant values
price = create_full((nopt,), 49.81959369901096, dtype)
strike = create_full((nopt,), 40.13264789835957, dtype)
t = create_full((nopt,), 1.8994311692123782, dtype)
# parameters
rate = RISK_FREE
volatility = VOLATILITY
# output arrays
call = create_full((nopt,), 0.0, dtype) # 16.976097804669887
put = create_full((nopt,), 0.0, dtype) # 0.34645174725098116
return (price, strike, t, rate, volatility, call, put)
def black_scholes(np, erf, nopt, price, strike, t, rate, volatility, call, put):
"""
Evaluate the Black-Scholes equation.
Input
---------
np:
Numpy-like array module
erf:
erf function implementation
nopt: int
number of options
price, strike, t: array of shape (nopt, )
vectors representing different components of portfolio
rate, volatility: float
scalars used for price computation
Output
-------
call, put: array of shape (nopt, )
output vectors
"""
mr = -rate
sig_sig_two = volatility * volatility * 2
P = price
S = strike
T = t
a = np.log(P / S)
b = T * mr
z = T * sig_sig_two
c = 0.25 * z
y = 1.0 / np.sqrt(z)
w1 = (a - b + c) * y
w2 = (a - b - c) * y
d1 = 0.5 + 0.5 * erf(w1)
d2 = 0.5 + 0.5 * erf(w2)
Se = np.exp(b) * S
r = P * d1 - Se * d2
call[:] = r
put[:] = r - P + Se
def run(nopt, backend, iterations, datatype):
if backend == "sharpy":
import sharpy as np
from sharpy import fini, init, sync
device = os.getenv("SHARPY_DEVICE", "")
create_full = partial(np.full, device=device)
erf = np.erf
init(False)
elif backend == "numpy":
import numpy as np
erf = naive_erf
if comm is not None:
assert (
comm.Get_size() == 1
), "Numpy backend only supports serial execution."
create_full = np.full
fini = sync = lambda x=None: None
else:
raise ValueError(f'Unknown backend: "{backend}"')
dtype = {
"f32": np.float32,
"f64": np.float64,
}[datatype]
seed = 777777
info(f"Using backend: {backend}")
info(f"Number of options: {nopt}")
info(f"Datatype: {datatype}")
# initialize
args = initialize(create_full, nopt, seed, dtype)
sync()
def eval():
tic = time_mod.perf_counter()
black_scholes(np, erf, nopt, *args)
sync()
toc = time_mod.perf_counter()
return toc - tic
# warm-up run
t_warm = eval()
# evaluate
info(f"Running {iterations} iterations")
time_list = []
for i in range(iterations):
time_list.append(eval())
# get max time over mpi ranks
if comm is not None:
t_warm = comm.allreduce(t_warm, MPI.MAX)
time_list = comm.allreduce(time_list, MPI.MAX)
t_min = numpy.min(time_list)
t_max = numpy.max(time_list)
t_med = numpy.median(time_list)
perf_rate = nopt / t_med / 1e6 # million options per second
init_overhead = t_warm - t_med
if backend == "sharpy":
info(f"Estimated initialization overhead: {init_overhead:.5f} s")
info(f"Min. duration: {t_min:.5f} s")
info(f"Max. duration: {t_max:.5f} s")
info(f"Median duration: {t_med:.5f} s")
info(f"Median rate: {perf_rate:.5f} Mopts/s")
# verify
if device:
# FIXME gpu.memcpy to host requires identity layout
# FIXME reduction on gpu
# call = args[-2].to_device()
# put = args[-1].to_device()
pass
else:
call = args[-2]
put = args[-1]
expected_call = 16.976097804669887
expected_put = 0.34645174725098116
call_value = float(call[0])
put_value = float(put[0])
assert numpy.allclose(call_value, expected_call)
assert numpy.allclose(put_value, expected_put)
info("SUCCESS")
fini()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run Black-Scholes benchmark",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-p",
"--preset",
type=str,
help="Use a preset problem size.",
choices=["S", "M", "L", "M16Gb"],
default="S",
)
parser.add_argument(
"-n",
"--noptions",
type=int,
default=-1,
help="Number of of options to evaluate. If set, overrides --preset.",
)
parser.add_argument(
"-b",
"--backend",
type=str,
default="sharpy",
choices=["sharpy", "numpy"],
help="Backend to use.",
)
parser.add_argument(
"-i",
"--iterations",
type=int,
default=10,
help="Number of iterations to run.",
)
parser.add_argument(
"-d",
"--datatype",
type=str,
default="f64",
choices=["f32", "f64"],
help="Datatype for model state variables",
)
args = parser.parse_args()
# dpbench preset sizes
preset_dict = {
"S": 524288,
"M": 134217728,
"L": 268435456,
"M16Gb": 67108864,
}
nopt = args.noptions
if nopt < 1:
nopt = preset_dict[args.preset]
run(
nopt,
args.backend,
args.iterations,
args.datatype,
)