-
Notifications
You must be signed in to change notification settings - Fork 7
/
base_bs_erf.py
157 lines (138 loc) · 4.52 KB
/
base_bs_erf.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
# Copyright (C) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
from __future__ import print_function
import numpy as np
from random import seed, uniform
import sys
try:
import numpy.random_intel as rnd
numpy_ver="Intel"
except:
import numpy.random as rnd
numpy_ver="regular"
try:
from mkl_umath import erf
numpy_ver += "-erf"
except:
from scipy.special import erf
try:
from mkl_umath import invsqrt
numpy_ver += "-invsqrt"
except:
#from numba import jit
invsqrt = lambda x: np.true_divide(1.0, np.sqrt(x))
#invsqrt = jit(['f8(f8)','f8[:](f8[:])'])(invsqrt)
try:
import itimer as it
now = it.itime
get_mops = it.itime_mops_now
except:
from timeit import default_timer
now = default_timer
get_mops = lambda t0, n: n / (1.e6 * (now() - t0))
######################################################
# GLOBAL DECLARATIONS THAT WILL BE USED IN ALL FILES #
######################################################
# make xrange available in python 3
try:
xrange
except NameError:
xrange = range
SEED = 7777777
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
# RISK_FREE = np.float32(0.1)
# VOLATILITY = np.float32(0.2)
# C10 = np.float32(1.)
# C05 = np.float32(.5)
# C025 = np.float32(.25)
TEST_ARRAY_LENGTH = 1024
###############################################
def run(name, alg, sizes=15, step=2, nopt=1024, nparr=True, dask=False, pass_args=False):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--steps', required=False, default=sizes, help="Number of steps")
parser.add_argument('--step', required=False, default=step, help="Factor for each step")
parser.add_argument('--chunk', required=False, default=None, help="Chunk size for Dask (mutually exclusive with --chunks)")
parser.add_argument('--chunks',required=False, default=None, help="Number of chunks for Dask")
parser.add_argument('--size', required=False, default=nopt, help="Initial data size")
parser.add_argument('--repeat',required=False, default=100, help="Iterations inside measured region")
parser.add_argument('--dask', required=False, default="sq", help="Dask scheduler: sq, mt, mp or addr:port of the scheduler")
parser.add_argument('--text', required=False, default="", help="Print with each result")
args = parser.parse_args()
sizes= int(args.steps)
step = int(args.step)
nopt = int(args.size)
chunks=int(args.chunks) if args.chunks else None
chunk= int(args.chunk) if args.chunk else None
assert(not chunks or not chunk, "Only one option can be specified: --chunk or --chunks")
repeat=int(args.repeat)
kwargs={}
print("Using", numpy_ver, "numpy", np.__version__)
if dask:
import dask
import dask.multiprocessing
import dask.array as da
dask_modes = {
"sq": 'synchronous',
"mt": 'threads',
"mp": 'processes'
}
if args.dask in dask_modes:
kwargs = {"schd": dask_modes[args.dask]}
else:
import distributed
kwargs = {"schd": distributed.Client(args.dask)}
name += "-"+args.dask
for i in xrange(sizes):
if pass_args is None:
pass
elif dask:
if chunks:
chunk = int(nopt // chunks)
price = da.random.uniform(S0L, S0H, nopt, chunks=(chunk,))
strike = da.random.uniform(XL, XH, nopt, chunks=(chunk,))
t = da.random.uniform(TL, TH, nopt, chunks=(chunk,))
else:
price, strike, t = rnd.uniform(S0L, S0H, nopt), rnd.uniform(XL, XH, nopt), rnd.uniform(TL, TH, nopt)
if not nparr:
call = [0.0 for i in range(nopt)]
put = [-1.0 for i in range(nopt)]
price=list(price)
strike=list(strike)
t=list(t)
repeat=1 # !!!!! ignore repeat count
if pass_args:
call = np.zeros(nopt, dtype=np.float64)
put = -np.ones(nopt, dtype=np.float64)
iterations = xrange(repeat)
print("ERF: {}: Size: {}".format(name, nopt), end=' ')
# sys.stdout.flush()
if pass_args is None:
alg(nopt, RISK_FREE, VOLATILITY, **kwargs) #warmup
t0 = now()
for _ in iterations:
alg(nopt, RISK_FREE, VOLATILITY, **kwargs)
elif pass_args:
alg(nopt, price, strike, t, RISK_FREE, VOLATILITY, call, put, **kwargs) #warmup
t0 = now()
for _ in iterations:
alg(nopt, price, strike, t, RISK_FREE, VOLATILITY, call, put, **kwargs)
else:
alg(nopt, price, strike, t, RISK_FREE, VOLATILITY, **kwargs) #warmup
t0 = now()
for _ in iterations:
alg(nopt, price, strike, t, RISK_FREE, VOLATILITY, **kwargs)
mops = get_mops(t0, nopt)
print("MOPS:", mops*2*repeat, args.text, flush=True)
nopt *= step
repeat -= step
if repeat < 1:
repeat = 1