Skip to content

Commit

Permalink
Merge pull request #13 from bh107/new_util
Browse files Browse the repository at this point in the history
New util
  • Loading branch information
madsbk authored Aug 20, 2018
2 parents 35d4653 + f60dd1e commit c4931d7
Show file tree
Hide file tree
Showing 55 changed files with 1,078 additions and 1,970 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ python:
before_install:
- pip install .
- pip install twine
- pip install scipy

script:
- python -m benchpress.testing
Expand Down
4 changes: 3 additions & 1 deletion benchpress/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"""
from __future__ import absolute_import
from pkg_resources import get_distribution, DistributionNotFound
from . import util
from . import visualizer
from . import suite_util
from . import argument_handling
from . import time_util
from . import benchmarks
from .benchpress import *

# Set the package version
Expand All @@ -38,4 +38,6 @@ def _suite_schema():
return json.load(f)
except IOError: # readthedocs cannot find "suite_schema.json"
return None


suite_schema = _suite_schema()
11 changes: 11 additions & 0 deletions benchpress/benchmarks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
"""
==================
Benchmark Examples
==================
This submodule of benchpress consist of a broad range of benchmarks in different languages
"""
from __future__ import absolute_import

from . import util as util
4 changes: 0 additions & 4 deletions benchpress/benchmarks/black_scholes/cpp11_bxx/Makefile

This file was deleted.

114 changes: 0 additions & 114 deletions benchpress/benchmarks/black_scholes/cpp11_bxx/src/black_scholes.cpp

This file was deleted.

82 changes: 37 additions & 45 deletions benchpress/benchmarks/black_scholes/python_numpy/black_scholes.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,83 @@
from __future__ import print_function
from benchpress import util
from benchpress.benchmarks import util
import numpy as np
import math

try:
import numpy_force as npf
except ImportError:
import numpy as npf

def model(N, B):
bench = util.Benchmark("Black Scholes", "samples*iterations")


def model(N):
"""Construct pseudo-data representing price samples?"""
# Create the outside of bohrium
S = npf.random.random(N).astype(B.dtype)*4.0 + 58.0 # Price is between 58-62
S = npf.random.random(N).astype(bench.dtype) * 4.0 + 58.0 # Price is between 58-62
S = np.array(S)
return S


def CND(X):
"""
Cumulative normal distribution.
Helper function used by BS(...).
"""

(a1,a2,a3,a4,a5) = (0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429)
(a1, a2, a3, a4, a5) = (0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429)
L = np.absolute(X)
K = 1.0 / (1.0 + 0.2316419 * L)
w = 1.0 - 1.0 / math.sqrt(2*np.pi)*np.exp(-L*L/2.) * \
w = 1.0 - 1.0 / math.sqrt(2 * np.pi) * np.exp(-L * L / 2.) * \
(a1 * K + \
a2 * (K**2) + \
a3 * (K**3) + \
a4 * (K**4) + \
a5 * (K**5))
a2 * (K ** 2) + \
a3 * (K ** 3) + \
a4 * (K ** 4) + \
a5 * (K ** 5))

mask = X<0
w = w * ~mask + (1.0-w)*mask
mask = X < 0
w = w * ~mask + (1.0 - w) * mask

return w


def BS(CallPutFlag, S, X, T, r, v):
"""Black Sholes Function."""

d1 = (np.log(S/X)+(r+v*v/2.)*T)/(v*math.sqrt(T))
d2 = d1-v*math.sqrt(T)
if CallPutFlag=='c':
return S*CND(d1)-X*math.exp(-r*T)*CND(d2)
d1 = (np.log(S / X) + (r + v * v / 2.) * T) / (v * math.sqrt(T))
d2 = d1 - v * math.sqrt(T)
if CallPutFlag == 'c':
return S * CND(d1) - X * math.exp(-r * T) * CND(d2)
else:
return X*math.exp(-r*T)*CND(-d2)-S*CND(-d1)
return X * math.exp(-r * T) * CND(-d2) - S * CND(-d1)

def price(S, I, flag='c', X=65.0, dT=(1.0/365.0), r=0.08, v=0.3, visualize=False):

def price(S, I, flag='c', X=65.0, dT=(1.0 / 365.0), r=0.08, v=0.3):
"""Model price as a function of time."""

T = dT
Ps = []
N = len(S)
for i in range (I):
T = dT
Ps = []
N = len(S)
for i in range(I):
P = np.sum(BS(flag, S, X, T, r, v)) / N
Ps.append(P)
T += dT
if visualize: #NB: this is only for experiments
np.visualize(P, "3d", 0, 0.0, 10)
util.Benchmark().flush()
bench.flush()

return Ps

def main():
B = util.Benchmark()
(N, I) = B.size

if B.inputfn:
S = B.load_array()
else:
S = model(N, B) # Construct pseudo-data

if B.dumpinput:
B.dump_arrays("black_scholes", {'input': S})
def main():
N = bench.args.size[0]
I = bench.args.size[1]

B.start()
R = price(S, I, visualize=B.visualize) # Run the model
B.stop()
B.pprint()
S = model(N) # Construct pseudo-data\

for i in range(len(R)): # Convert to one array
if hasattr(R[i], "copy2numpy"):
R[i] = R[i].copy2numpy()[()]
if B.outputfn:
B.tofile(B.outputfn, {'res':R})
bench.start()
R = price(S, I) # Run the model
bench.stop()
bench.pprint()

if B.verbose:
print (R)

if __name__ == "__main__":
main()
40 changes: 23 additions & 17 deletions benchpress/benchmarks/convolve/python_numpy/convolve.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
from __future__ import print_function
from benchpress import util
import bohrium as bh
from benchpress.benchmarks import util

# if Bohrium is installed we use its convolve else we use the convolve from SciPy
try:
raise ImportError
import bohrium as bh
convolveNd = bh.convolve_scipy
except ImportError:
import scipy
from scipy import signal
convolveNd = signal.convolve

bench = util.Benchmark("Convolution Filter (any dimensional)", "<image-size>*<filter-size>*<ndims>*<niters>")


def main():
"""
Convolve filter (any dimensional)
Parameter: `--size<image-size>*<filter-size>*<ndims>*<niters>`
Parameter: `<image-size>*<filter-size>*<ndims>*<niters>`
where image and filter size is the size of each dimension (not their total size).
"""
B = util.Benchmark()
(image_size, filter_size, ndims, I) = B.size
(image_size, filter_size, ndims, I) = bench.args.size

image = B.random_array((image_size**ndims,)).reshape([image_size]*ndims)
image_filter = B.random_array((filter_size**ndims,)).reshape([filter_size]*ndims)
image = bench.random_array((image_size ** ndims,)).reshape([image_size] * ndims)
image_filter = bench.random_array((filter_size ** ndims,)).reshape([filter_size] * ndims)

B.start()
bench.start()
for _ in range(I):
R = bh.convolve_scipy(image, image_filter)
B.flush()
B.stop()
B.pprint()
if B.outputfn:
B.tofile(B.outputfn, {'res': R})

if B.verbose:
print (R)
R = convolveNd(image, image_filter)
bench.flush()
bench.stop()
bench.save_data({'res': R})
bench.pprint()


if __name__ == "__main__":
Expand Down
25 changes: 11 additions & 14 deletions benchpress/benchmarks/convolve1d/python_numpy/convolve1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
"""

from __future__ import print_function
from benchpress import util
from benchpress.benchmarks import util
import numpy as np

bench = util.Benchmark("Convolution Filter 1D", "<image-size>*<filter-size>*<niters>")


def main():
B = util.Benchmark()
(image_size, filter_size, I) = B.size
(image_size, filter_size, I) = bench.args.size

image = B.random_array((image_size,))
image_filter = B.random_array((filter_size,))
image = bench.random_array((image_size,))
image_filter = bench.random_array((filter_size,))

B.start()
bench.start()
for _ in range(I):
R = np.convolve(image, image_filter)
B.flush()
B.stop()
B.pprint()
if B.outputfn:
B.tofile(B.outputfn, {'res': R})

if B.verbose:
print (R)
bench.flush()
bench.stop()
bench.save_data({'res': R})
bench.pprint()


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit c4931d7

Please sign in to comment.