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
52 # Define parameter ranges
53 # logspace as array
54 particle_counts = np.logspace(2, 7, 20).astype(int).tolist()
55
56 # Initialize results matrix
57 results_f32 = []
58 results_f64 = []
59
60 print(f"Particle counts: {particle_counts}")
61
62 total_runs = len(particle_counts)
63 current_run = 0
64
65 for i, N in enumerate(particle_counts):
66 current_run += 1
67
68 print(
69 f"[{current_run:2d}/{total_runs}] Running N={N:5d}...",
70 end=" ",
71 )
72
73 start_time = time.time()
74 min_time, max_time, mean_time = benchmark_f32(N)
75 results_f32.append(min_time)
76 min_time, max_time, mean_time = benchmark_f64(N)
77 results_f64.append(min_time)
78 elapsed = time.time() - start_time
79
80 print(f"mean={mean_time:.3f}s (took {elapsed:.1f}s)")
81
82 return particle_counts, results_f32, results_f64
List current implementation
87 current_impl = shamrock.algs.get_current_impl_reduction()
88
89 print(current_impl)
impl_param(impl_name="group_reduction128", params="")
List all implementations available
93 all_default_impls = shamrock.algs.get_default_impl_list_reduction()
94
95 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
100 for impl in all_default_impls:
101 shamrock.algs.set_impl_reduction(impl.impl_name, impl.params)
102
103 print(f"Running reduction performance benchmarks for {impl}...")
104
105 # Run the performance sweep
106 particle_counts, results_f32, results_f64 = run_performance_sweep()
107
108 (line,) = plt.plot(particle_counts, results_f64, "--.", label=impl.impl_name + " (f64)")
109 plt.plot(
110 particle_counts, results_f32, ":", color=line.get_color(), label=impl.impl_name + " (f32)"
111 )
112
113
114 Nobj = np.array(particle_counts)
115 Time100M = Nobj / 1e8
116 plt.plot(particle_counts, Time100M, color="grey", linestyle="-", alpha=0.7, label="100M obj/sec")
117
118
119 plt.xlabel("Number of elements")
120 plt.ylabel("Time (s)")
121 plt.title("reduction performance benchmarks")
122
123 plt.xscale("log")
124 plt.yscale("log")
125
126 plt.grid(True)
127
128 plt.legend()
129 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.003s (took 0.0s)
[17/20] Running N=1623776... mean=0.005s (took 0.1s)
[18/20] Running N=2976351... mean=0.009s (took 0.2s)
[19/20] Running N=5455594... mean=0.009s (took 0.3s)
[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.004s (took 0.1s)
[19/20] Running N=5455594... mean=0.008s (took 0.2s)
[20/20] Running N=10000000... mean=0.021s (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.000s (took 0.0s)
[16/20] Running N=885866... mean=0.001s (took 0.0s)
[17/20] Running N=1623776... mean=0.002s (took 0.0s)
[18/20] Running N=2976351... mean=0.003s (took 0.1s)
[19/20] Running N=5455594... mean=0.006s (took 0.2s)
[20/20] Running N=10000000... mean=0.018s (took 0.4s)
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.0s)
[18/20] Running N=2976351... mean=0.003s (took 0.1s)
[19/20] Running N=5455594... mean=0.006s (took 0.2s)
[20/20] Running N=10000000... mean=0.018s (took 0.4s)
Total running time of the script: (0 minutes 4.791 seconds)
Estimated memory usage: 267 MB