7 #ifndef cf3_common_PE_reduce_hpp
8 #define cf3_common_PE_reduce_hpp
59 template<
typename T,
typename Op>
61 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 root,
const int stride)
73 T *in_buf=(
T*)in_values;
76 if (stride==1) {
for(
int i=0; i<in_n; i++) in_buf[i]=in_values[in_map[i]]; }
77 else {
for(
int i=0; i<in_n; i++) memcpy(&in_buf[stride*i],&in_values[stride*in_map[i]],stride*
sizeof(
T)); }
81 T *out_buf=out_values;
83 if ((out_map!=0)||(in_values==out_values)) {
89 MPI_CHECK_RESULT(MPI_Reduce, ( in_buf, out_buf, in_n*stride, type, op_, root, comm ));
94 if (stride==1) {
for(
int i=0; i<in_n; i++) out_values[out_map[i]]=out_buf[i]; }
95 else {
for(
int i=0; i<in_n; i++) memcpy(&out_values[stride*out_map[i]],&out_buf[stride*i],stride*
sizeof(
T)); }
97 }
else if (in_values==out_values) {
98 memcpy(out_values,out_buf,in_n*stride*
sizeof(
T));
104 if (in_map!=0)
delete[] in_buf;
122 template<
typename T,
typename Op>
124 reduce(
const Communicator& comm,
const Op& op,
const T* in_values,
const int in_n,
T* out_values,
const int root,
const int stride=1)
131 T* out_buf=out_values;
134 const int size=stride*in_n>1?stride*in_n:1;
157 template<
typename T,
typename Op>
159 reduce(
const Communicator& comm,
const Op& op,
const std::vector<T>& in_values, std::vector<T>& out_values,
const int root,
const int stride=1)
168 out_values.resize(in_values.size());
169 out_values.reserve(in_values.size());
174 detail::reduce_impl(comm, op, (
T*)(&in_values[0]), in_values.size()/stride, (
int*)0, (
T*)(&out_values[0]), (
int*)0,
root, stride);
176 detail::reduce_impl(comm, op, (
T*)(&in_values[0]), in_values.size()/stride, (
int*)0, (
T*)0, (
int*)0, root, stride);
196 template<
typename T,
typename Op>
198 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 root,
const int stride=1)
205 T* out_buf=out_values;
211 for (
int i=0; i<out_sum; i++) out_sum_tmp=out_map[i]>out_sum_tmp?out_map[i]:out_sum_tmp;
212 out_sum=out_sum_tmp+1;
244 template<
typename T,
typename Op>
246 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 root,
const int stride=1)
257 if (out_values.size() == 0 ){
258 int out_sum=in_map.size();
259 if (out_map.size()!=0) {
262 out_values.resize(stride*out_sum);
263 out_values.reserve(stride*out_sum);
269 detail::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]), root, stride);
271 detail::reduce_impl(comm, op, (
T*)(&in_values[0]), in_map.size(), (in_map.empty() ?
nullptr : &in_map[0]), (
T*)0, (
int*)0, root, stride);
283 #endif // cf3_common_PE_reduce_hpp
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
T * reduce(const Communicator &comm, const Op &op, const T *in_values, const int in_n, T *out_values, const int root, const int stride=1)
Top-level namespace for coolfluid.
void 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 root, const int stride)
MPI_Comm Communicator
communicator
#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)