g4tools  5.4.0
wait_buffer
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_mpi_wait_buffer
5 #define tools_mpi_wait_buffer
6 
7 #ifdef TOOLS_USE_NATIVE_MPI
8 #include <mpi.h>
9 #else
10 #include "dummy_mpi.h"
11 #endif
12 
13 #include <ostream>
14 
15 namespace tools {
16 namespace mpi {
17 
18 inline bool wait_buffer(std::ostream& a_out,int a_rank,int a_src,int a_tag,const MPI_Comm& a_comm,
19  int& a_buffer_size,char*& a_buffer,int& a_probe_src,bool a_verbose = false) {
20  a_buffer = 0;
21  a_buffer_size = 0;
22  a_probe_src = -1;
23 
24  MPI_Status status;
25  if(::MPI_Probe(a_src,a_tag,a_comm,&status)!=MPI_SUCCESS) {
26  a_out << "tools::mpi::wait_buffer : rank " << a_rank << " : MPI_Probe : failed." << std::endl;
27  return false;
28  }
29 
30  if(::MPI_Get_count(&status,MPI_CHAR,&a_buffer_size)!=MPI_SUCCESS) {
31  a_out << "tools::mpi::wait_buffer : rank " << a_rank << " : MPI_Get_count : failed." << std::endl;
32  a_buffer_size = 0;
33  return false;
34  }
35 
36  a_probe_src = status.MPI_SOURCE;
37 
38  if(!a_buffer_size) {
39  a_out << "exlb::mpi::wait_buffer : MPI_Get_count returns zero data." << std::endl;
40  a_probe_src = -1;
41  return false;
42  }
43 
44  if(a_verbose) a_out << "tools::mpi::wait_buffer : rank " << a_rank << " : get_count " << a_buffer_size << std::endl;
45 
46  a_buffer = new char[a_buffer_size];
47  if(!a_buffer) {
48  a_out << "tools::mpi::wait_buffer : rank " << a_rank << " : can't alloc buffer of size " << a_buffer_size << std::endl;
49  a_buffer_size = 0;
50  a_probe_src = -1;
51  return false;
52  }
53 
54  if(::MPI_Recv(a_buffer,a_buffer_size,MPI_CHAR,status.MPI_SOURCE,status.MPI_TAG,a_comm,&status)!=MPI_SUCCESS) {
55  a_out << "tools::mpi::wait_buffer : rank " << a_rank << " : MPI_Recv : failed." << std::endl;
56  a_buffer_size = 0;
57  delete [] a_buffer;
58  a_buffer = 0;
59  a_probe_src = -1;
60  return false;
61  }
62 
63  if(a_verbose) a_out << "tools::mpi::wait_buffer : rank " << a_rank << " : unpack data ..." << std::endl;
64 
65  return true;
66 }
67 
68 }}
69 
70 #endif
MPI_CHAR
#define MPI_CHAR
Definition: dummy_mpi.h:20
_MPI_Status::MPI_TAG
int MPI_TAG
Definition: dummy_mpi.h:46
MPI_Recv
int MPI_Recv(void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Status *)
Definition: dummy_mpi.h:53
_MPI_Status::MPI_SOURCE
int MPI_SOURCE
Definition: dummy_mpi.h:45
_MPI_Status
to pass h2mpi, hs2mpi /////////////////////////////////////
Definition: dummy_mpi.h:44
dummy_mpi.h
MPI_SUCCESS
#define MPI_SUCCESS
Definition: dummy_mpi.h:30
MPI_Comm
void * MPI_Comm
to pass hd2mpi ////////////////////////////////////////////
Definition: dummy_mpi.h:13
tools::mpi::wait_buffer
bool wait_buffer(std::ostream &a_out, int a_rank, int a_src, int a_tag, const MPI_Comm &a_comm, int &a_buffer_size, char *&a_buffer, int &a_probe_src, bool a_verbose=false)
Definition: wait_buffer:18
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
MPI_Get_count
int MPI_Get_count(const MPI_Status *, MPI_Datatype, int *)
Definition: dummy_mpi.h:51
MPI_Probe
int MPI_Probe(int, int, MPI_Comm, MPI_Status *)
Definition: dummy_mpi.h:50