Skip to content

Bohrium 100 times slower than numpy, time "unaccounted for"  #633

@jamesavery

Description

@jamesavery

When running with Bohrium or bh107, a crazy amount of time is being spent in "unaccounted for". The following code runs in 0.27 seconds in pure Numpy, but takes 25 seconds with both bohrium and bh107, despite only doing Bohrium-friendly operations, and the generated kernels being efficient. Where does the time go, and how do we fix this?

Switch between the backends by setting backend to "numpy", "bohrium", or "bh107".

#!/usr/bin/python
from time import time
import numpy  as np
import sys

backend = "numpy"
if backend == "bohrium":
    import bohrium as bh
    def to_np(A): return A.copy2numpy()
    def bh_flush(): bh.flush()
    def linspace(*args): return bh.linspace(*args)
elif backend == "bh107":
    import bh107 as bh
    def to_np(A): return A.copy2numpy()
    def bh_flush(): bh.flush()
    def linspace(*args): return bh.array(np.linspace(*args)) # No linspace in bh107
else:
    bh = np
    def to_np(A):   return A
    def bh_flush(): return
    def linspace(*args): return np.linspace(*args)    

# -------- SET UP SIMULATION PARAMETERS --------
n      = 2000
g      = 9.81
h0     = 10 
dx     = 0.5
dt     = 0.01

# -------- ALLOCATE MEMORY --------
dtype=np.float64
u          = bh.zeros(n,dtype=dtype)
eta        = bh.zeros(n,dtype=dtype)
h          = bh.zeros(n,dtype=dtype)

# -------- THE ACTUAL CODE --------
def Dx0(f,dx): return (f[2:]-f[1:-1])*(1/dx)  # d/dx on Grid 0
def Dx1(f,dx): return (f[1:-1]-f[:-2])*(1/dx) # d/dx on Grid 1

def simulation_step_naive():
#    eta_x    =  (eta[2:]-eta[1:-1])*(1/dx) # bh107 can't multiply from left
    eta_x    =  Dx0(eta,dx)
    u[1:-1]    += eta_x*dt*(-g)     # Forward-Euler time integration
    bh_flush()

    h        = eta+h0
    h_x      = Dx0(h,dx)
    u_x      = Dx1(u,dx)
    eta_t    = u[1:-1]*h_x*-1 - u_x*h[1:-1];  # Unary minus doesn't work in bh107
    
    eta[1:-1]  += eta_t*dt      # Forward-Euler time integratio
    bh_flush()


# -------- INITIALIZE AND RUN --------    
xs = linspace(0,(n-1)*dx,n)
eta[:] = bh.exp((xs-500)**2/(2*50**2)*-1)

bh_flush(); T0 = time()
for i in range(10000):
    simulation_step_naive()

bh_flush(); T1 = time()
print(round(T1-T0,4),"seconds")

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions