Debian Codegraph @

scripts/benchmark-hoomd.py

raw symbols SCIP
1 # Copyright (c) 2016-2026 The Regents of the University of Michigan
2 # Part of GSD, released under the BSD 2-Clause License.
3
4 """Benchmark GSD HOOMD file read/write."""
5
6 import math
7 import os
8 import random
9 import sys
10 import time
11 from subprocess import PIPE, call
12
13 import gsd.fl
14 import numpy
15
16 import gsd.hoomd
17 import gsd.pygsd
18
19 # import logging
20 # logging.basicConfig(level=logging.DEBUG)
21
22
23 def write_frame(file, frame_idx, position, orientation):
24 """Write a frame to the file."""
25 frame = gsd.hoomd.Frame()
26 frame.particles.N = position.shape[0]
27 frame.configuration.step = frame_idx * 10
28 position[0][0] = frame_idx
29 orientation[0][0] = frame_idx
30 frame.particles.position = position
31 frame.particles.orientation = orientation
32 file.append(frame)
33
34
35 def read_frame(file, frame_idx, position, orientation):
36 """Read a frame from the file."""
37 frame = file[frame_idx] # noqa
38
39
40 def write_file(file, nframes, N, position, orientation):
41 """Write a whole file."""
42 steps = compute_actual_size(N, nframes) / (250 * 1024**2)
43 step = int(nframes / steps)
44 if step == 0:
45 step = 1
46
47 for i in range(0, nframes):
48 if i % step == 0:
49 print(i, '/', nframes, file=sys.stderr, flush=True)
50
51 write_frame(file, i, position, orientation)
52
53
54 def read_sequential_file(file, nframes, nframes_read, N, position, orientation):
55 """Read the file sequentially."""
56 steps = compute_actual_size(N, nframes) / (250 * 1024**2)
57 step = int(nframes / steps)
58 if step == 0:
59 step = 1
60
61 for i in range(0, nframes_read):
62 if i % step == 0:
63 print(i, '/', nframes, file=sys.stderr, flush=True)
64
65 read_frame(file, i, position, orientation)
66
67
68 def read_random_file(file, nframes, nframes_read, N, position, orientation):
69 """Read the file in random order."""
70 steps = compute_actual_size(N, nframes) / (250 * 1024**2)
71 step = int(nframes / steps)
72 if step == 0:
73 step = 1
74
75 frames = list(range(0, nframes))
76 random.shuffle(frames)
77
78 for i, f in enumerate(frames[:nframes_read]):
79 if i % step == 0:
80 print(i, '/', nframes, file=sys.stderr, flush=True)
81 read_frame(file, f, position, orientation)
82
83
84 def compute_nframes(N, size):
85 """Compute the number of frames to write to the file."""
86 bytes_per_frame = (3 + 4) * 4 * N
87 return math.ceil(size / bytes_per_frame)
88
89
90 def compute_actual_size(N, nframes):
91 """Compute the actual size of the file."""
92 bytes_per_frame = (3 + 4) * 4 * N
93 return nframes * bytes_per_frame
94
95
96 # Run all benchmarks with the given options
97
98
99 def run_benchmarks(N, size):
100 """Run all the benchmarks."""
101 bmark_read_size = 0.25 * 1024**3
102
103 timings = {}
104 rng = numpy.random.default_rng()
105 position = rng.random((N, 3)).astype('float32')
106 orientation = rng.random((N, 4)).astype('float32')
107
108 nframes = compute_nframes(N, size)
109 actual_size = compute_actual_size(N, nframes)
110 nframes_read = int(nframes * bmark_read_size / actual_size)
111 bmark_read_size = compute_actual_size(N, nframes_read)
112 if nframes_read > nframes:
113 nframes_read = nframes
114 bmark_read_size = actual_size
115
116 # first, write the file and time how long it takes
117 print('Writing file: ', file=sys.stderr, flush=True)
118
119 # if the file size is small, write it once to warm up the disk
120 if size < 64 * 1024**3:
121 with gsd.hoomd.open(name='test.gsd', mode='w') as hf:
122 write_file(hf, nframes, N, position, orientation)
123
124 # write it again and time this one
125 with gsd.hoomd.open(name='test.gsd', mode='w') as hf:
126 start = time.time()
127 write_file(hf, nframes, N, position, orientation)
128
129 # ensure that all writes to disk are completed and drop file system cache
130 call(['sudo', '/bin/sync'])
131 call(['sudo', '/sbin/sysctl', 'vm.drop_caches=3'], stdout=PIPE)
132
133 end = time.time()
134
135 timings['write'] = actual_size / 1024**2 / (end - start)
136
137 # time how long it takes to open the file
138 print('Opening file... ', file=sys.stderr, flush=True, end='')
139 start = time.time()
140 with gsd.hoomd.open(name='test.gsd', mode='r') as hf:
141 end = time.time()
142
143 print(end - start, 's', file=sys.stderr, flush=True)
144
145 timings['open_time'] = end - start
146
147 # Read the file sequentially and measure the time taken
148 print('Sequential read file:', file=sys.stderr, flush=True)
149 start = time.time()
150 read_sequential_file(hf, nframes, nframes_read, N, position, orientation)
151 end = time.time()
152
153 timings['seq_read'] = bmark_read_size / 1024**2 / (end - start)
154
155 # drop the file system cache
156 call(['sudo', '/bin/sync'])
157 call(['sudo', '/sbin/sysctl', 'vm.drop_caches=3'], stdout=PIPE)
158
159 # Read the file randomly and measure the time taken
160 print('Random read file:', file=sys.stderr, flush=True)
161 start = time.time()
162 read_random_file(hf, nframes, nframes_read, N, position, orientation)
163 end = time.time()
164
165 timings['random_read'] = bmark_read_size / 1024**2 / (end - start)
166 timings['random_read_time'] = (end - start) / nframes_read / 1e-3
167
168 os.unlink('test.gsd')
169 return timings
170
171
172 def run_sweep(size, size_str):
173 """Run a single sweep of benchmarks."""
174 # if size < 10*1024**3:
175 if True:
176 result = run_benchmarks(32 * 32, size)
177
178 print(
179 '{:<7} {:<6} {:<9.4g} {:<12.4g} {:<11.4g} {:<13.4g} {:<11.3g}'.format(
180 size_str,
181 '32^2',
182 result['open_time'] * 1000,
183 result['write'],
184 result['seq_read'],
185 result['random_read'],
186 result['random_read_time'],
187 )
188 )
189 sys.stdout.flush()
190
191 result = run_benchmarks(128 * 128, size)
192
193 print(
194 '{:<7} {:<6} {:<9.4g} {:<12.4g} {:<11.4g} {:<13.4g} {:<11.3g}'.format(
195 size_str,
196 '128^2',
197 result['open_time'] * 1000,
198 result['write'],
199 result['seq_read'],
200 result['random_read'],
201 result['random_read_time'],
202 )
203 )
204 sys.stdout.flush()
205
206 result = run_benchmarks(1024 * 1024, size)
207
208 print(
209 '{:<7} {:<6} {:<9.4g} {:<12.4g} {:<11.4g} {:<13.4g} {:<11.3g}'.format(
210 size_str,
211 '1024^2',
212 result['open_time'] * 1000,
213 result['write'],
214 result['seq_read'],
215 result['random_read'],
216 result['random_read_time'],
217 )
218 )
219 sys.stdout.flush()
220
221
222 print(
223 """
224 ======= ====== ========= ============ =========== ============= ===========
225 Size N Open (ms) Write (MB/s) Read (MB/s) Random (MB/s) Random (ms)
226 ======= ====== ========= ============ =========== ============= ==========="""
227 )
228
229 run_sweep(128 * 1024**2, '128 MiB')
230 run_sweep(1 * 1024**3, '1 GiB')
231 # run_sweep(128*1024**3, "128 GiB");
232
233 print('======= ====== ========= ============ =========== ============= ===========')