Note
Go to the end to download the full example code.
is_all_true performance benchmarks#
This example benchmarks the is_all_true performance for the different algorithms available in Shamrock
9 import random
10 import time
11
12 import matplotlib.colors as colors
13 import matplotlib.pyplot as plt
14 import numpy as np
15
16 import shamrock
17
18 # If we use the shamrock executable to run this script instead of the python interpreter,
19 # we should not initialize the system as the shamrock executable needs to handle specific MPI logic
20 if not shamrock.sys.is_initialized():
21 shamrock.change_loglevel(1)
22 shamrock.sys.init("0:0")
Main benchmark functions
30 def benchmark_is_all_true_random(N, nb_repeat=10):
31 times = []
32 for i in range(nb_repeat):
33 random.seed(111)
34 buf = shamrock.algs.mock_buffer_u8(random.randint(0, 1000000), N, 0, 1)
35 times.append(shamrock.algs.benchmark_is_all_true(buf, N))
36 return min(times), max(times), sum(times) / nb_repeat
37
38
39 def benchmark_is_all_true_ones(N, nb_repeat=10):
40 times = []
41 for i in range(nb_repeat):
42 buf = shamrock.backends.DeviceBuffer_u8()
43 buf.resize(N)
44 buf.fill(1)
45 times.append(shamrock.algs.benchmark_is_all_true(buf, N))
46 return min(times), max(times), sum(times) / nb_repeat
47
48
49 def benchmark_is_all_true_zeros(N, nb_repeat=10):
50 times = []
51 for i in range(nb_repeat):
52 buf = shamrock.backends.DeviceBuffer_u8()
53 buf.resize(N)
54 buf.fill(0)
55 times.append(shamrock.algs.benchmark_is_all_true(buf, N))
56 return min(times), max(times), sum(times) / nb_repeat
Run the performance test for all parameters
61 def run_performance_sweep():
62
63 # Define parameter ranges
64 # logspace as array
65 particle_counts = np.logspace(2, 7, 20).astype(int).tolist()
66
67 # Initialize results matrix
68 results_random = []
69 results_ones = []
70 results_zeros = []
71
72 print(f"Particle counts: {particle_counts}")
73
74 total_runs = len(particle_counts)
75 current_run = 0
76
77 for i, N in enumerate(particle_counts):
78 current_run += 1
79
80 print(
81 f"[{current_run:2d}/{total_runs}] Running N={N:5d}...",
82 end=" ",
83 )
84
85 start_time = time.time()
86 min_time, max_time, mean_time = benchmark_is_all_true_random(N)
87 results_random.append(mean_time)
88 min_time, max_time, mean_time = benchmark_is_all_true_ones(N)
89 results_ones.append(mean_time)
90 min_time, max_time, mean_time = benchmark_is_all_true_zeros(N)
91 results_zeros.append(mean_time)
92 elapsed = time.time() - start_time
93
94 print(f"mean={mean_time:.3f}s (took {elapsed:.1f}s)")
95
96 return particle_counts, results_random, results_ones, results_zeros
List current implementation
101 current_impl = shamrock.algs.get_current_impl_is_all_true()
102
103 print(current_impl)
impl_param(impl_name="host", params="")
List all implementations available
107 all_default_impls = shamrock.algs.get_default_impl_list_is_all_true()
108
109 print(all_default_impls)
[impl_param(impl_name="host", params=""), impl_param(impl_name="sum_reduction", params="")]
Run the performance benchmarks for all implementations
114 for impl in all_default_impls:
115 shamrock.algs.set_impl_is_all_true(impl.impl_name, impl.params)
116
117 print(f"Running is_all_true performance benchmarks for {impl}...")
118
119 # Run the performance sweep
120 particle_counts, results_random, results_ones, results_zeros = run_performance_sweep()
121
122 plt.plot(particle_counts, results_random, "--", label=impl.impl_name + " (random set)")
123 plt.plot(particle_counts, results_ones, "--+", label=impl.impl_name + " (all ones)")
124 plt.plot(particle_counts, results_zeros, "--o", label=impl.impl_name + " (all zeros)")
125
126
127 Nobj = np.array(particle_counts)
128 Time100M = Nobj / 1e8
129 plt.plot(particle_counts, Time100M, color="grey", linestyle="-", alpha=0.7, label="100M obj/sec")
130
131
132 plt.xlabel("Number of elements")
133 plt.ylabel("Time (s)")
134 plt.title("is_all_true performance benchmarks")
135
136 plt.xscale("log")
137 plt.yscale("log")
138
139 plt.grid(True)
140
141 plt.legend()
142 plt.show()

Info: setting is_all_true implementation to impl : host [tree][rank=0]
Running is_all_true performance benchmarks for impl_param(impl_name="host", params="")...
Particle counts: [100, 183, 335, 615, 1128, 2069, 3792, 6951, 12742, 23357, 42813, 78475, 143844, 263665, 483293, 885866, 1623776, 2976351, 5455594, 10000000]
[ 1/20] Running N= 100... mean=0.000s (took 0.0s)
[ 2/20] Running N= 183... mean=0.000s (took 0.0s)
[ 3/20] Running N= 335... mean=0.000s (took 0.0s)
[ 4/20] Running N= 615... mean=0.000s (took 0.0s)
[ 5/20] Running N= 1128... mean=0.000s (took 0.0s)
[ 6/20] Running N= 2069... mean=0.000s (took 0.0s)
[ 7/20] Running N= 3792... mean=0.000s (took 0.0s)
[ 8/20] Running N= 6951... mean=0.000s (took 0.0s)
[ 9/20] Running N=12742... mean=0.000s (took 0.0s)
[10/20] Running N=23357... mean=0.000s (took 0.0s)
[11/20] Running N=42813... mean=0.000s (took 0.0s)
[12/20] Running N=78475... mean=0.000s (took 0.0s)
[13/20] Running N=143844... mean=0.000s (took 0.0s)
[14/20] Running N=263665... mean=0.000s (took 0.0s)
[15/20] Running N=483293... mean=0.000s (took 0.1s)
[16/20] Running N=885866... mean=0.000s (took 0.1s)
[17/20] Running N=1623776... mean=0.000s (took 0.2s)
[18/20] Running N=2976351... mean=0.000s (took 0.4s)
[19/20] Running N=5455594... mean=0.001s (took 0.6s)
[20/20] Running N=10000000... mean=0.001s (took 1.1s)
Info: setting is_all_true implementation to impl : sum_reduction [tree][rank=0]
Running is_all_true performance benchmarks for impl_param(impl_name="sum_reduction", params="")...
Particle counts: [100, 183, 335, 615, 1128, 2069, 3792, 6951, 12742, 23357, 42813, 78475, 143844, 263665, 483293, 885866, 1623776, 2976351, 5455594, 10000000]
[ 1/20] Running N= 100... mean=0.000s (took 0.0s)
[ 2/20] Running N= 183... mean=0.000s (took 0.0s)
[ 3/20] Running N= 335... mean=0.000s (took 0.0s)
[ 4/20] Running N= 615... mean=0.000s (took 0.0s)
[ 5/20] Running N= 1128... mean=0.000s (took 0.0s)
[ 6/20] Running N= 2069... mean=0.000s (took 0.0s)
[ 7/20] Running N= 3792... mean=0.000s (took 0.0s)
[ 8/20] Running N= 6951... mean=0.000s (took 0.0s)
[ 9/20] Running N=12742... mean=0.000s (took 0.0s)
[10/20] Running N=23357... mean=0.000s (took 0.0s)
[11/20] Running N=42813... mean=0.000s (took 0.0s)
[12/20] Running N=78475... mean=0.000s (took 0.0s)
[13/20] Running N=143844... mean=0.000s (took 0.0s)
[14/20] Running N=263665... mean=0.000s (took 0.0s)
[15/20] Running N=483293... mean=0.000s (took 0.1s)
[16/20] Running N=885866... mean=0.001s (took 0.1s)
[17/20] Running N=1623776... mean=0.001s (took 0.3s)
[18/20] Running N=2976351... mean=0.002s (took 0.5s)
[19/20] Running N=5455594... mean=0.004s (took 0.9s)
[20/20] Running N=10000000... mean=0.010s (took 1.8s)
Total running time of the script: (0 minutes 7.502 seconds)
Estimated memory usage: 171 MB