This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrambo.py
More file actions
209 lines (167 loc) · 4.99 KB
/
rambo.py
File metadata and controls
209 lines (167 loc) · 4.99 KB
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
"""
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,
)