-
Notifications
You must be signed in to change notification settings - Fork 2
Benchmark rambo #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c4aa251
Refactor random module and add tests
AllanZyne ffc8cd4
use "SHARPY::is_contiguous" to check strided and simpliy wait code
AllanZyne b1d6227
remove np alias in test_setget.py
AllanZyne 4aefe8a
add doc for rambo.py & change unpack in idtr.cpp
AllanZyne ec7206a
add "sharpy.random" package
AllanZyne b061efc
fix test_random.py crash
AllanZyne 2cdff42
Merge branch 'main' into benchmark_rambo
AllanZyne efc164c
update imex version
AllanZyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
""" | ||
Rambo benchmark | ||
|
||
Examples: | ||
|
||
# run 1000 iterations of 10 events and 100 outputs on sharpy backend | ||
python rambo.py -nevts 10 -nout 100 -b sharpy -i 1000 | ||
|
||
# MPI parallel run | ||
mpiexec -n 3 python rambo.py -nevts 64 -nout 64 -b sharpy -i 1000 | ||
|
||
""" | ||
|
||
import argparse | ||
import time as time_mod | ||
|
||
import numpy | ||
|
||
import sharpy | ||
|
||
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 sp_rambo(sp, sp_C1, sp_F1, sp_Q1, sp_output, nevts, nout): | ||
sp_C = 2.0 * sp_C1 - 1.0 | ||
sp_S = sp.sqrt(1 - sp.square(sp_C)) | ||
sp_F = 2.0 * sp.pi * sp_F1 | ||
sp_Q = -sp.log(sp_Q1) | ||
|
||
sp_output[:, :, 0] = sp.reshape(sp_Q, (nevts, nout, 1)) | ||
sp_output[:, :, 1] = sp.reshape( | ||
sp_Q * sp_S * sp.sin(sp_F), (nevts, nout, 1) | ||
) | ||
sp_output[:, :, 2] = sp.reshape( | ||
sp_Q * sp_S * sp.cos(sp_F), (nevts, nout, 1) | ||
) | ||
sp_output[:, :, 3] = sp.reshape(sp_Q * sp_C, (nevts, nout, 1)) | ||
|
||
sharpy.sync() | ||
|
||
|
||
def np_rambo(np, C1, F1, Q1, output, nevts, nout): | ||
C = 2.0 * C1 - 1.0 | ||
S = np.sqrt(1 - np.square(C)) | ||
F = 2.0 * np.pi * F1 | ||
Q = -np.log(Q1) | ||
|
||
output[:, :, 0] = Q | ||
output[:, :, 1] = Q * S * np.sin(F) | ||
output[:, :, 2] = Q * S * np.cos(F) | ||
output[:, :, 3] = Q * C | ||
|
||
|
||
def initialize(np, nevts, nout, seed, dtype): | ||
np.random.seed(seed) | ||
C1 = np.random.rand(nevts, nout) | ||
F1 = np.random.rand(nevts, nout) | ||
Q1 = np.random.rand(nevts, nout) * np.random.rand(nevts, nout) | ||
return (C1, F1, Q1, np.zeros((nevts, nout, 4), dtype)) | ||
|
||
|
||
def run(nevts, nout, backend, iterations, datatype): | ||
if backend == "sharpy": | ||
import sharpy as np | ||
from sharpy import fini, init, sync | ||
|
||
rambo = sp_rambo | ||
|
||
init(False) | ||
elif backend == "numpy": | ||
import numpy as np | ||
|
||
if comm is not None: | ||
assert ( | ||
comm.Get_size() == 1 | ||
), "Numpy backend only supports serial execution." | ||
|
||
fini = sync = lambda x=None: None | ||
rambo = np_rambo | ||
else: | ||
raise ValueError(f'Unknown backend: "{backend}"') | ||
|
||
dtype = { | ||
"f32": np.float32, | ||
"f64": np.float64, | ||
}[datatype] | ||
|
||
info(f"Using backend: {backend}") | ||
info(f"Number of events: {nevts}") | ||
info(f"Number of outputs: {nout}") | ||
info(f"Datatype: {datatype}") | ||
|
||
seed = 7777 | ||
C1, F1, Q1, output = initialize(np, nevts, nout, seed, dtype) | ||
sync() | ||
|
||
# verify | ||
if backend == "sharpy": | ||
sp_rambo(sharpy, C1, F1, Q1, output, nevts, nout) | ||
# sync() !! not work here? | ||
np_C1 = sharpy.to_numpy(C1) | ||
np_F1 = sharpy.to_numpy(F1) | ||
np_Q1 = sharpy.to_numpy(Q1) | ||
np_output = numpy.zeros((nevts, nout, 4)) | ||
np_rambo(numpy, np_C1, np_F1, np_Q1, np_output, nevts, nout) | ||
assert numpy.allclose(sharpy.to_numpy(output), np_output) | ||
|
||
def eval(): | ||
tic = time_mod.perf_counter() | ||
rambo(np, C1, F1, Q1, output, nevts, nout) | ||
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) | ||
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") | ||
|
||
fini() | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Run rambo benchmark", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
parser.add_argument( | ||
"-nevts", | ||
"--num_events", | ||
type=int, | ||
default=10, | ||
help="Number of events to evaluate.", | ||
) | ||
parser.add_argument( | ||
"-nout", | ||
"--num_outputs", | ||
type=int, | ||
default=10, | ||
help="Number of outputs to evaluate.", | ||
) | ||
|
||
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() | ||
nevts, nout = args.num_events, args.num_outputs | ||
run( | ||
nevts, | ||
nout, | ||
args.backend, | ||
args.iterations, | ||
args.datatype, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
|
||
import sharpy as sp | ||
from sharpy import float64 | ||
from sharpy.numpy import fromfunction | ||
|
||
|
||
def uniform(low, high, size, device="", team=1): | ||
data = np.random.uniform(low, high, size) | ||
if len(data.shape) == 0: | ||
sp_data = sp.full((), data[()], device=device, team=team) | ||
return sp_data | ||
return fromfunction( | ||
lambda *index: data[index], | ||
data.shape, | ||
dtype=float64, | ||
device=device, | ||
team=team, | ||
) | ||
|
||
|
||
def rand(*shape, device="", team=1): | ||
data = np.random.rand(*shape) | ||
if isinstance(data, float): | ||
return data | ||
return fromfunction( | ||
lambda *index: data[index], | ||
data.shape, | ||
dtype=float64, | ||
device=device, | ||
team=team, | ||
) | ||
|
||
|
||
def seed(s): | ||
np.random.seed(s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.