7 #ifndef cf3_common_PE_all_reduce_hpp
8 #define cf3_common_PE_all_reduce_hpp
60 template<
typename T,
typename Op>
62 all_reduce_impl(
const Communicator& comm, Op,
const T* in_values,
const int in_n,
const int *in_map,
T* out_values,
const int *out_map,
const int stride)
70 T *in_buf=(
T*)in_values;
73 if (stride==1) {
for(
int i=0; i<in_n; i++) in_buf[i]=in_values[in_map[i]]; }
74 else {
for(
int i=0; i<in_n; i++) memcpy(&in_buf[stride*i],&in_values[stride*in_map[i]],stride*
sizeof(
T)); }
78 T *out_buf=out_values;
79 if ((out_map!=0)||(in_values==out_values)) {
84 MPI_CHECK_RESULT(MPI_Allreduce, ( in_buf, out_buf, in_n*stride, type, op_, comm ));
88 if (stride==1) {
for(
int i=0; i<in_n; i++) out_values[out_map[i]]=out_buf[i]; }
89 else {
for(
int i=0; i<in_n; i++) memcpy(&out_values[stride*out_map[i]],&out_buf[stride*i],stride*
sizeof(
T)); }
91 }
else if (in_values==out_values) {
92 memcpy(out_values,out_buf,in_n*stride*
sizeof(
T));
97 if (in_map!=0)
delete[] in_buf;
115 template<
typename T,
typename Op>
120 T* out_buf=out_values;
122 const int size=stride*in_n>1?stride*in_n:1;
140 template<
typename T,
typename Op>
142 all_reduce(
const Communicator& comm,
const Op& op,
const std::vector<T>& in_values, std::vector<T>& out_values,
const int stride=1)
146 out_values.resize(in_values.size());
147 out_values.reserve(in_values.size());
169 template<
typename T,
typename Op>
171 all_reduce(
const Communicator& comm,
const Op& op,
const T* in_values,
const int in_n,
const int *in_map,
T* out_values,
const int *out_map,
const int stride=1)
174 T* out_buf=out_values;
179 for (
int i=0; i<out_sum; i++) out_sum_tmp=out_map[i]>out_sum_tmp?out_map[i]:out_sum_tmp;
180 out_sum=out_sum_tmp+1;
207 template<
typename T,
typename Op>
209 all_reduce(
const Communicator& comm,
const Op& op,
const std::vector<T>& in_values,
const std::vector<int>& in_map, std::vector<T>& out_values,
const std::vector<int>& out_map,
const int stride=1)
215 if (out_values.size() == 0 ){
216 int out_sum=in_map.size();
217 if (out_map.size()!=0) {
220 out_values.resize(stride*out_sum);
221 out_values.reserve(stride*out_sum);
225 detail::all_reduce_impl(comm, op, (
T*)(&in_values[0]), in_map.size(), (in_map.empty() ?
nullptr : &in_map[0]), (
T*)(&out_values[0]), (out_map.empty() ?
nullptr : &out_map[0]), stride);
236 #endif // cf3_common_PE_all_reduce_hpp
void all_reduce_impl(const Communicator &comm, Op, const T *in_values, const int in_n, const int *in_map, T *out_values, const int *out_map, const int stride)
MPI_Datatype Datatype
datatype
Datatype get_mpi_datatype(const T &ref_of_type)
ACCESS AND REGISTRATION MECHANISM.
#define boost_foreach
lowercase version of BOOST_FOREACH
Top-level namespace for coolfluid.
MPI_Comm Communicator
communicator
T * all_reduce(const Communicator &comm, const Op &op, const T *in_values, const int in_n, T *out_values, const int stride=1)
#define MPI_CHECK_RESULT(MPIFunc, Args)
Macro for checking return values of any mpi calls and throws exception on error.
MPI_Op Operation
operation (mostly for reduce and all_reduce)