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 # Define parameter ranges
63 # logspace as array
64 particle_counts = np.logspace(2, 7, 20).astype(int).tolist()
65
66 # Initialize results matrix
67 results_random = []
68 results_ones = []
69 results_zeros = []
70
71 print(f"Particle counts: {particle_counts}")
72
73 total_runs = len(particle_counts)
74 current_run = 0
75
76 for i, N in enumerate(particle_counts):
77 current_run += 1
78
79 print(
80 f"[{current_run:2d}/{total_runs}] Running N={N:5d}...",
81 end=" ",
82 )
83
84 start_time = time.time()
85 min_time, max_time, mean_time = benchmark_is_all_true_random(N)
86 results_random.append(mean_time)
87 min_time, max_time, mean_time = benchmark_is_all_true_ones(N)
88 results_ones.append(mean_time)
89 min_time, max_time, mean_time = benchmark_is_all_true_zeros(N)
90 results_zeros.append(mean_time)
91 elapsed = time.time() - start_time
92
93 print(f"mean={mean_time:.3f}s (took {elapsed:.1f}s)")
94
95 return particle_counts, results_random, results_ones, results_zeros
List current implementation
100 current_impl = shamrock.algs.get_current_impl_is_all_true()
101
102 print(current_impl)
impl_param(impl_name="host", params="")
List all implementations available
106 all_default_impls = shamrock.algs.get_default_impl_list_is_all_true()
107
108 print(all_default_impls)
[impl_param(impl_name="host", params=""), impl_param(impl_name="sum_reduction", params="")]
Run the performance benchmarks for all implementations
113 for impl in all_default_impls:
114 shamrock.algs.set_impl_is_all_true(impl.impl_name, impl.params)
115
116 print(f"Running is_all_true performance benchmarks for {impl}...")
117
118 # Run the performance sweep
119 particle_counts, results_random, results_ones, results_zeros = run_performance_sweep()
120
121 plt.plot(particle_counts, results_random, "--", label=impl.impl_name + " (random set)")
122 plt.plot(particle_counts, results_ones, "--+", label=impl.impl_name + " (all ones)")
123 plt.plot(particle_counts, results_zeros, "--o", label=impl.impl_name + " (all zeros)")
124
125
126 Nobj = np.array(particle_counts)
127 Time100M = Nobj / 1e8
128 plt.plot(particle_counts, Time100M, color="grey", linestyle="-", alpha=0.7, label="100M obj/sec")
129
130
131 plt.xlabel("Number of elements")
132 plt.ylabel("Time (s)")
133 plt.title("is_all_true performance benchmarks")
134
135 plt.xscale("log")
136 plt.yscale("log")
137
138 plt.grid(True)
139
140 plt.legend()
141 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.2s)
[17/20] Running N=1623776... mean=0.000s (took 0.3s)
[18/20] Running N=2976351... mean=0.000s (took 0.4s)
[19/20] Running N=5455594... mean=0.000s (took 0.7s)
[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.001s (took 0.0s)
[13/20] Running N=143844... mean=0.000s (took 0.0s)
[14/20] Running N=263665... mean=0.000s (took 0.1s)
[15/20] Running N=483293... mean=0.001s (took 0.1s)
[16/20] Running N=885866... mean=0.001s (took 0.2s)
[17/20] Running N=1623776... mean=0.002s (took 0.3s)
[18/20] Running N=2976351... mean=0.003s (took 0.6s)
[19/20] Running N=5455594... mean=0.006s (took 1.2s)
[20/20] Running N=10000000... mean=0.016s (took 2.2s)
Total running time of the script: (0 minutes 8.478 seconds)
Estimated memory usage: 229 MB