7 #ifndef cf3_common_PE_gather_hpp
8 #define cf3_common_PE_gather_hpp
71 T* out_buf=out_values;
72 if (irank==root)
if (in_values==out_values) {
77 MPI_CHECK_RESULT(MPI_Gather, (const_cast<T*>(in_values), in_n*stride, type, out_buf, in_n*stride, type, root, comm));
80 if (irank==root)
if (in_values==out_values) {
81 memcpy(out_values,out_buf,nproc*in_n*stride*
sizeof(
T));
103 gathervm_impl(
const Communicator& comm,
const T* in_values,
const int in_n,
const int *in_map,
T* out_values,
const int *out_n,
const int *out_map,
const int root,
const int stride )
120 out_nstride=
new int[nproc];
121 out_disp=
new int[nproc];
123 for(
int i=0; i<nproc-1; i++) {
124 out_nstride[i]=stride*out_n[i];
125 out_disp[i+1]=out_disp[i]+out_nstride[i];
127 out_nstride[nproc-1]=out_n[nproc-1]*stride;
128 out_sum=out_disp[nproc-1]+stride*out_n[nproc-1];
132 const int in_sum=stride*in_n;
135 T *in_buf=(
T*)in_values;
138 if (stride==1) {
for(
int i=0; i<in_sum; i++) in_buf[i]=in_values[in_map[i]]; }
139 else {
for(
int i=0; i<in_sum/stride; i++) memcpy(&in_buf[stride*i],&in_values[stride*in_map[i]],stride*
sizeof(
T)); }
143 T *out_buf=out_values;
144 if (irank==root)
if ((out_map!=0)||(in_values==out_values)) {
149 MPI_CHECK_RESULT(MPI_Gatherv, (in_buf, in_sum, type, out_buf, out_nstride, out_disp, type, root, comm));
154 if (stride==1) {
for(
int i=0; i<out_sum; i++) out_values[out_map[i]]=out_buf[i]; }
155 else {
for(
int i=0; i<out_sum/stride; i++) memcpy(&out_values[stride*out_map[i]],&out_buf[stride*i],stride*
sizeof(
T)); }
157 }
else if (in_values==out_values) {
158 memcpy(out_values,out_buf,out_sum*
sizeof(
T));
164 if (in_map!=0)
delete[] in_buf;
166 delete[] out_nstride;
194 T* out_buf=out_values;
195 if (irank==root)
if (out_values==0) {
196 const int size=stride*nproc*in_n>1?stride*nproc*in_n:1;
220 gather(
const Communicator& comm,
const std::vector<T>& in_values, std::vector<T>& out_values,
const int root,
const int stride=1)
228 cf3_assert( in_values.size() % (stride) == 0 );
229 int in_n=(int)in_values.size();
231 out_values.resize(in_n*nproc);
232 out_values.reserve(in_n*nproc);
248 gather(
const Communicator& comm,
const T* in_values,
const int in_n,
const int *in_map,
T* out_values,
int *out_n,
const int *out_map,
const int root,
const int stride=1);
263 gather(
const Communicator& comm,
const T* in_values,
const int in_n,
T* out_values,
int *out_n,
const int root,
const int stride=1)
266 return gather(comm,in_values,in_n,0,out_values,out_n,0,root,stride);
274 gather(
const Communicator& comm,
const std::vector<T>& in_values,
const int in_n,
const std::vector<int>& in_map, std::vector<T>& out_values, std::vector<int>& out_n,
const std::vector<int>& out_map,
const int root,
const int stride=1);
290 gather(
const Communicator& comm,
const std::vector<T>& in_values,
const int in_n, std::vector<T>& out_values, std::vector<int>& out_n,
const int root,
const int stride=1)
293 std::vector<int> in_map(0);
294 std::vector<int> out_map(0);
299 if ((irank==root)&&(&in_values[0]==&out_values[0]))
301 std::vector<T> out_tmp(0);
302 gather(comm,in_values,in_n,in_map,out_tmp,out_n,out_map,root,stride);
303 out_values.assign(out_tmp.begin(),out_tmp.end());
307 gather(comm,in_values,in_n,in_map,out_values,out_n,out_map,root,stride);
330 gather(
const Communicator& comm,
const T* in_values,
const int in_n,
const int *in_map,
T* out_values,
int *out_n,
const int *out_map,
const int root,
const int stride)
339 for (
int i=0; i<nproc; i++) out_sum+=out_n[i];
340 if (out_sum==-nproc) {
341 if (out_map!=0)
throw cf3::common::ParallelError(
FromHere(),
"Trying to perform communication with receive map while receive counts are unknown, this is bad usage of parallel environment.");
344 if (irank==root)
for (
int i=0; i<nproc; i++) out_sum+=out_n[i];
348 T* out_buf=out_values;
353 for (
int i=0; i<out_sum; i++) out_sum_tmp=out_map[i]>out_sum_tmp?out_map[i]:out_sum_tmp;
354 out_sum=out_sum_tmp+1;
388 gather(
const Communicator& comm,
const std::vector<T>& in_values,
const int in_n,
const std::vector<int>& in_map, std::vector<T>& out_values, std::vector<int>& out_n,
const std::vector<int>& out_map,
const int root,
const int stride)
394 if ((
int)out_n.size()!=nproc)
cf3::common::BadValue(
FromHere(),
"Size of vector for number of items to be received does not match to number of processes.");
401 if (out_sum == -nproc){
402 if (out_map.size()!=0)
throw cf3::common::ParallelError(
FromHere(),
"Trying to perform communication with receive map while receive counts are unknown, this is bad usage of parallel environment.");
410 if (out_values.size() == 0 ){
411 if (out_map.size()!=0) {
414 if (out_sum!=0) out_sum++;
416 out_values.resize(stride*out_sum);
417 out_values.reserve(stride*out_sum);
423 detail::gathervm_impl(comm, (
T*)(&in_values[0]), in_n, (in_map.empty() ?
nullptr : &in_map[0]), (
T*)(&out_values[0]), &out_n[0], (out_map.empty() ?
nullptr : &out_map[0]), root, stride);
425 detail::gathervm_impl(comm, (
T*)(&in_values[0]), in_n, (in_map.empty() ?
nullptr : &in_map[0]), (
T*)0, (
int*)0, (
int*)0,
root, stride);
437 #endif // cf3_common_PE_gather_hpp
T * gather(const Communicator &comm, const T *in_values, const int in_n, T *out_values, const int root, const int stride=1)
void gatherc_impl(const Communicator &comm, const T *in_values, const int in_n, T *out_values, const int root, 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
void gathervm_impl(const Communicator &comm, const T *in_values, const int in_n, const int *in_map, T *out_values, const int *out_n, const int *out_map, const int root, const int stride)
Top-level namespace for coolfluid.
MPI_Comm Communicator
communicator
#define MPI_CHECK_RESULT(MPIFunc, Args)
Macro for checking return values of any mpi calls and throws exception on error.