Note
Go to the end to download the full example code.
reduction performance benchmarks#
This example benchmarks the reduction 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
28 def benchmark_f32(N, nb_repeat=10):
29 times = []
30 for i in range(nb_repeat):
31 buf = shamrock.backends.DeviceBuffer_f32()
32 buf.resize(N)
33 buf.fill(0)
34 times.append(shamrock.algs.benchmark_reduction_sum(buf, N))
35 return min(times), max(times), sum(times) / nb_repeat
36
37
38 def benchmark_f64(N, nb_repeat=10):
39 times = []
40 for i in range(nb_repeat):
41 buf = shamrock.backends.DeviceBuffer_f64()
42 buf.resize(N)
43 buf.fill(0)
44 times.append(shamrock.algs.benchmark_reduction_sum(buf, N))
45 return min(times), max(times), sum(times) / nb_repeat
Run the performance test for all parameters
50 def run_performance_sweep():
51 # Define parameter ranges
52 # logspace as array
53 particle_counts = np.logspace(2, 7, 20).astype(int).tolist()
54
55 # Initialize results matrix
56 results_f32 = []
57 results_f64 = []
58
59 print(f"Particle counts: {particle_counts}")
60
61 total_runs = len(particle_counts)
62 current_run = 0
63
64 for i, N in enumerate(particle_counts):
65 current_run += 1
66
67 print(
68 f"[{current_run:2d}/{total_runs}] Running N={N:5d}...",
69 end=" ",
70 )
71
72 start_time = time.time()
73 min_time, max_time, mean_time = benchmark_f32(N)
74 results_f32.append(min_time)
75 min_time, max_time, mean_time = benchmark_f64(N)
76 results_f64.append(min_time)
77 elapsed = time.time() - start_time
78
79 print(f"mean={mean_time:.3f}s (took {elapsed:.1f}s)")
80
81 return particle_counts, results_f32, results_f64
List current implementation
86 current_impl = shamrock.algs.get_current_impl_reduction()
87
88 print(current_impl)
impl_param(impl_name="group_reduction128", params="")
List all implementations available
92 all_default_impls = shamrock.algs.get_default_impl_list_reduction()
93
94 print(all_default_impls)
[impl_param(impl_name="fallback", params=""), impl_param(impl_name="group_reduction16", params=""), impl_param(impl_name="group_reduction128", params=""), impl_param(impl_name="group_reduction256", params="")]
Run the performance benchmarks for all implementations
99 for impl in all_default_impls:
100 shamrock.algs.set_impl_reduction(impl.impl_name, impl.params)
101
102 print(f"Running reduction performance benchmarks for {impl}...")
103
104 # Run the performance sweep
105 particle_counts, results_f32, results_f64 = run_performance_sweep()
106
107 (line,) = plt.plot(particle_counts, results_f64, "--.", label=impl.impl_name + " (f64)")
108 plt.plot(
109 particle_counts, results_f32, ":", color=line.get_color(), label=impl.impl_name + " (f32)"
110 )
111
112
113 Nobj = np.array(particle_counts)
114 Time100M = Nobj / 1e8
115 plt.plot(particle_counts, Time100M, color="grey", linestyle="-", alpha=0.7, label="100M obj/sec")
116
117
118 plt.xlabel("Number of elements")
119 plt.ylabel("Time (s)")
120 plt.title("reduction performance benchmarks")
121
122 plt.xscale("log")
123 plt.yscale("log")
124
125 plt.grid(True)
126
127 plt.legend()
128 plt.show()

Info: setting reduction implementation to impl : fallback [tree][rank=0]
Running reduction performance benchmarks for impl_param(impl_name="fallback", 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.001s (took 0.0s)
[15/20] Running N=483293... mean=0.001s (took 0.0s)
[16/20] Running N=885866... mean=0.001s (took 0.0s)
[17/20] Running N=1623776... mean=0.003s (took 0.1s)
[18/20] Running N=2976351... mean=0.006s (took 0.1s)
[19/20] Running N=5455594... mean=0.009s (took 0.2s)
[20/20] Running N=10000000... mean=0.025s (took 0.5s)
Info: setting reduction implementation to impl : group_reduction16 [tree][rank=0]
Running reduction performance benchmarks for impl_param(impl_name="group_reduction16", 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.001s (took 0.0s)
[16/20] Running N=885866... mean=0.001s (took 0.0s)
[17/20] Running N=1623776... mean=0.002s (took 0.1s)
[18/20] Running N=2976351... mean=0.005s (took 0.1s)
[19/20] Running N=5455594... mean=0.009s (took 0.2s)
[20/20] Running N=10000000... mean=0.025s (took 0.5s)
Info: setting reduction implementation to impl : group_reduction128 [tree][rank=0]
Running reduction performance benchmarks for impl_param(impl_name="group_reduction128", 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.001s (took 0.0s)
[16/20] Running N=885866... mean=0.001s (took 0.0s)
[17/20] Running N=1623776... mean=0.002s (took 0.1s)
[18/20] Running N=2976351... mean=0.004s (took 0.1s)
[19/20] Running N=5455594... mean=0.008s (took 0.2s)
[20/20] Running N=10000000... mean=0.023s (took 0.5s)
Info: setting reduction implementation to impl : group_reduction256 [tree][rank=0]
Running reduction performance benchmarks for impl_param(impl_name="group_reduction256", 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.001s (took 0.0s)
[16/20] Running N=885866... mean=0.001s (took 0.0s)
[17/20] Running N=1623776... mean=0.002s (took 0.1s)
[18/20] Running N=2976351... mean=0.005s (took 0.1s)
[19/20] Running N=5455594... mean=0.008s (took 0.2s)
[20/20] Running N=10000000... mean=0.026s (took 0.5s)
Total running time of the script: (0 minutes 4.904 seconds)
Estimated memory usage: 366 MB