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")
Use shamrock documentation style for matplotlib
28 shamrock.matplotlib.set_shamrock_mpl_style()
Main benchmark functions
35 def benchmark_is_all_true_random(N, nb_repeat=10):
36 times = []
37 for i in range(nb_repeat):
38 random.seed(111)
39 buf = shamrock.algs.mock_buffer_u8(random.randint(0, 1000000), N, 0, 1)
40 times.append(shamrock.algs.benchmark_is_all_true(buf, N))
41 return min(times), max(times), sum(times) / nb_repeat
42
43
44 def benchmark_is_all_true_ones(N, nb_repeat=10):
45 times = []
46 for i in range(nb_repeat):
47 buf = shamrock.backends.DeviceBuffer_u8()
48 buf.resize(N)
49 buf.fill(1)
50 times.append(shamrock.algs.benchmark_is_all_true(buf, N))
51 return min(times), max(times), sum(times) / nb_repeat
52
53
54 def benchmark_is_all_true_zeros(N, nb_repeat=10):
55 times = []
56 for i in range(nb_repeat):
57 buf = shamrock.backends.DeviceBuffer_u8()
58 buf.resize(N)
59 buf.fill(0)
60 times.append(shamrock.algs.benchmark_is_all_true(buf, N))
61 return min(times), max(times), sum(times) / nb_repeat
Run the performance test for all parameters
66 def run_performance_sweep():
67 # Define parameter ranges
68 # logspace as array
69 particle_counts = np.logspace(2, 7, 20).astype(int).tolist()
70
71 # Initialize results matrix
72 results_random = []
73 results_ones = []
74 results_zeros = []
75
76 print(f"Particle counts: {particle_counts}")
77
78 total_runs = len(particle_counts)
79 current_run = 0
80
81 for i, N in enumerate(particle_counts):
82 current_run += 1
83
84 print(
85 f"[{current_run:2d}/{total_runs}] Running N={N:5d}...",
86 end=" ",
87 )
88
89 start_time = time.time()
90 min_time, max_time, mean_time = benchmark_is_all_true_random(N)
91 results_random.append(mean_time)
92 min_time, max_time, mean_time = benchmark_is_all_true_ones(N)
93 results_ones.append(mean_time)
94 min_time, max_time, mean_time = benchmark_is_all_true_zeros(N)
95 results_zeros.append(mean_time)
96 elapsed = time.time() - start_time
97
98 print(f"mean={mean_time:.3f}s (took {elapsed:.1f}s)")
99
100 return particle_counts, results_random, results_ones, results_zeros
List current implementation
105 current_impl = shamrock.algs.get_current_impl_is_all_true()
106
107 print(current_impl)
impl_param(impl_name="host", params="")
List all implementations available
111 all_default_impls = shamrock.algs.get_default_impl_list_is_all_true()
112
113 print(all_default_impls)
[impl_param(impl_name="host", params=""), impl_param(impl_name="sum_reduction", params="")]
Run the performance benchmarks for all implementations
118 for impl in all_default_impls:
119 shamrock.algs.set_impl_is_all_true(impl.impl_name, impl.params)
120
121 print(f"Running is_all_true performance benchmarks for {impl}...")
122
123 # Run the performance sweep
124 particle_counts, results_random, results_ones, results_zeros = run_performance_sweep()
125
126 plt.plot(particle_counts, results_random, "--", label=impl.impl_name + " (random set)")
127 plt.plot(particle_counts, results_ones, "--+", label=impl.impl_name + " (all ones)")
128 plt.plot(particle_counts, results_zeros, "--o", label=impl.impl_name + " (all zeros)")
129
130
131 Nobj = np.array(particle_counts)
132 Time100M = Nobj / 1e8
133 plt.plot(particle_counts, Time100M, color="grey", linestyle="-", alpha=0.7, label="100M obj/sec")
134
135
136 plt.xlabel("Number of elements")
137 plt.ylabel("Time (s)")
138 plt.title("is_all_true performance benchmarks")
139
140 plt.xscale("log")
141 plt.yscale("log")
142
143 plt.grid(True)
144
145 plt.legend(fontsize=10)
146 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.3s)
[19/20] Running N=5455594... mean=0.000s (took 0.5s)
[20/20] Running N=10000000... mean=0.001s (took 0.9s)
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.2s)
[18/20] Running N=2976351... mean=0.003s (took 0.4s)
[19/20] Running N=5455594... mean=0.006s (took 0.7s)
[20/20] Running N=10000000... mean=0.013s (took 1.4s)
Total running time of the script: (0 minutes 6.197 seconds)
Estimated memory usage: 223 MB