Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
global_var.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
17#include <cmath>
18
19template<class T, GlobalVariableType redop>
20T int_reduce_get_start_var();
21
22template<>
23f32 int_reduce_get_start_var<f32, GlobalVariableType::min>() {
24 return INFINITY;
25}
26
27template<>
28f64 int_reduce_get_start_var<f64, GlobalVariableType::min>() {
29 return INFINITY;
30}
31
32template<>
33f32 int_reduce_get_start_var<f32, GlobalVariableType::max>() {
34 return 0;
35}
36
37template<>
38f64 int_reduce_get_start_var<f64, GlobalVariableType::max>() {
39 return 0;
40}
41
42template<>
43f32 int_reduce_get_start_var<f32, GlobalVariableType::sum>() {
44 return 0;
45}
46
47template<>
48f64 int_reduce_get_start_var<f64, GlobalVariableType::sum>() {
49 return 0;
50}
51
52template<class T, GlobalVariableType redop>
53T int_reduce_val_loc(T a, T b);
54
55template<>
56f32 int_reduce_val_loc<f32, GlobalVariableType::min>(f32 a, f32 b) {
57 return sycl::min(a, b);
58}
59
60template<>
61f32 int_reduce_val_loc<f32, GlobalVariableType::max>(f32 a, f32 b) {
62 return sycl::max(a, b);
63}
64
65template<>
66f32 int_reduce_val_loc<f32, GlobalVariableType::sum>(f32 a, f32 b) {
67 return a + b;
68}
69
70template<class T, GlobalVariableType redop>
71T int_reduce_val_mpi(T val_acc_loc);
72
73template<>
74f32 int_reduce_val_mpi<f32, GlobalVariableType::min>(f32 val_acc_loc) {
75 f32 ret;
76 shamcomm::mpi::Allreduce(&val_acc_loc, &ret, 1, mpi_type_f32, MPI_MIN, MPI_COMM_WORLD);
77 return ret;
78}
79
80template<>
81f32 int_reduce_val_mpi<f32, GlobalVariableType::max>(f32 val_acc_loc) {
82 f32 ret;
83 shamcomm::mpi::Allreduce(&val_acc_loc, &ret, 1, mpi_type_f32, MPI_MAX, MPI_COMM_WORLD);
84 return ret;
85}
86
87template<>
88f32 int_reduce_val_mpi<f32, GlobalVariableType::sum>(f32 val_acc_loc) {
89 f32 ret;
90 shamcomm::mpi::Allreduce(&val_acc_loc, &ret, 1, mpi_type_f32, MPI_SUM, MPI_COMM_WORLD);
91 return ret;
92}
93
94template<class T, GlobalVariableType redop>
95T int_reduce(std::unordered_map<u64, T> &val_map) {
96 T val_acc_loc = int_reduce_get_start_var<T, redop>();
97 for (auto &[k, val] : val_map) {
98 val_acc_loc = int_reduce_val_loc<T, redop>(val_acc_loc, val);
99 }
100
101 return int_reduce_val_mpi<T, redop>(val_acc_loc);
102}
103
104template<>
106
107 final_val = int_reduce<f32, GlobalVariableType::min>(val_map);
108
109 is_reduced = true;
110}
111
112template<>
114
115 final_val = int_reduce<f32, GlobalVariableType::max>(val_map);
116
117 is_reduced = true;
118}
119
120template<>
122
123 final_val = int_reduce<f32, GlobalVariableType::sum>(val_map);
124
125 is_reduced = true;
126}
double f64
Alias for double.
float f32
Alias for float.
void Allreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
MPI wrapper for MPI_Allreduce.
Definition wrapper.cpp:119