g4tools  5.4.0
hmpi
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_hmpi
5 #define tools_mpi_hmpi
6 
7 // code to send, receive histos through MPI.
8 
9 #include <tools/histo/hmpi>
10 #include <tools/histo/hd2mpi>
11 
12 #include <tools/histo/h1d>
13 #include <tools/histo/h2d>
14 #include <tools/histo/h3d>
15 #include <tools/histo/p1d>
16 #include <tools/histo/p2d>
17 
18 #include "wrmpi"
19 
20 namespace tools {
21 namespace mpi {
22 
23 class hmpi : public virtual tools::histo::hmpi {
24  typedef tools::histo::hmpi parent;
25 protected:
26  typedef unsigned int num_t;
27  static const std::string& s_class() {
28  static const std::string s_v("tools::mpi::hmpi");
29  return s_v;
30  }
31 public:
32  virtual bool pack(const tools::histo::h1d& a_h) {
33  if(!m_wrmpi.spack(a_h.s_cls())) return false;
34  if(!histo_data_duiuid_pack(m_wrmpi,a_h.dac())) return false;
35  return true;
36  }
37  virtual bool pack(const tools::histo::h2d& a_h) {
38  if(!m_wrmpi.spack(a_h.s_cls())) return false;
39  if(!histo_data_duiuid_pack(m_wrmpi,a_h.dac())) return false;
40  return true;
41  }
42  virtual bool pack(const tools::histo::h3d& a_h) {
43  if(!m_wrmpi.spack(a_h.s_cls())) return false;
44  if(!histo_data_duiuid_pack(m_wrmpi,a_h.dac())) return false;
45  return true;
46  }
47  virtual bool pack(const tools::histo::p1d& a_h) {
48  if(!m_wrmpi.spack(a_h.s_cls())) return false;
49  if(!profile_data_duiuidd_pack(m_wrmpi,a_h.get_histo_data())) return false;
50  return true;
51  }
52  virtual bool pack(const tools::histo::p2d& a_h) {
53  if(!m_wrmpi.spack(a_h.s_cls())) return false;
54  if(!profile_data_duiuidd_pack(m_wrmpi,a_h.get_histo_data())) return false;
55  return true;
56  }
57 public:
58  virtual bool beg_send(unsigned int a_nhist) {
60  return m_wrmpi.pack(a_nhist);
61  }
62  virtual bool send(int a_dest) {
64  m_out << "tools::mpi::hmpi::send : rank " << m_rank << " : MPI_Send failed." << std::endl;
65  return false;
66  }
68  return true;
69  }
70 public:
71  virtual bool wait_histos(int a_src,std::vector< std::pair<std::string,void*> >& a_hists) {
72  a_hists.clear();
73 
74  typedef std::pair<std::string,void*> class_pointer;
75 
76  MPI_Status status;
77  if(::MPI_Probe(a_src,m_tag,m_comm,&status)!=MPI_SUCCESS) {
78  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank << " : MPI_Probe : failed." << std::endl;
79  return false;
80  }
81 
82  int buffer_size = 0;
83  if(::MPI_Get_count(&status,MPI_CHAR,&buffer_size)!=MPI_SUCCESS) {
84  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank << " : MPI_Get_count : failed." << std::endl;
85  return false;
86  }
87 
88  if(!buffer_size) {
89  m_out << "exlb::mpi::wait_histos : MPI_Get_count returns zero data." << std::endl;
90  return false;
91  }
92 
93  if(m_verbose) m_out << "rank " << m_rank << " : get_count " << buffer_size << std::endl;
94 
95  char* buffer = new char[buffer_size];
96  if(!buffer) {
97  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank << " : can't alloc buffer of size " << buffer_size << std::endl;
98  return false;
99  }
100 
101  if(::MPI_Recv(buffer,buffer_size,MPI_CHAR,a_src,m_tag,m_comm,&status)!=MPI_SUCCESS) {
102  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank << " : MPI_Recv : failed." << std::endl;
103  delete [] buffer;
104  return false;
105  }
106 
107  if(m_verbose) m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank << " : unpack data ..." << std::endl;
108 
109  wrmpi _mpi(m_out,m_comm,buffer_size,buffer); //give ownership of buffer to _mpi.
110 
111  num_t nhist;
112  if(!_mpi.unpack(nhist)) return false;
113 
114  if(m_verbose)
115  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank << " : number of histos to unpack " << nhist << std::endl;
116 
117  {for(num_t ihist=0;ihist<nhist;ihist++) {
118 
119  std::string scls;
120  if(!_mpi.sunpack(scls)) return false;
121 
122  if(scls==tools::histo::h1d::s_class()) {
124  if(!histo_data_duiuid_unpack(_mpi,hdata)) return false;
125  tools::histo::h1d* h = new tools::histo::h1d("",10,0,1);
126  h->copy_from_data(hdata);
127  if(m_verbose) {
128  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank
129  << " : got a " << scls
130  << ", title " << h->title()
131  << ", mean_x " << h->mean() << ", rms " << h->rms()
132  << std::endl;
133  }
134  a_hists.push_back(class_pointer(h->s_cls(),h)); //give ownership of h.
135 
136  } else if(scls==tools::histo::h2d::s_class()) {
138  if(!histo_data_duiuid_unpack(_mpi,hdata)) return false;
139  tools::histo::h2d* h = new tools::histo::h2d("",10,0,1,10,0,1);
140  h->copy_from_data(hdata);
141  if(m_verbose) {
142  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank
143  << " : got a " << scls
144  << ", title " << h->title()
145  << ", mean_x " << h->mean_x() << ", rms_x " << h->rms_x()
146  << ", mean_y " << h->mean_y() << ", rms_y " << h->rms_y()
147  << std::endl;
148  }
149  a_hists.push_back(class_pointer(h->s_cls(),h)); //give ownership of h.
150 
151  } else if(scls==tools::histo::h3d::s_class()) {
153  if(!histo_data_duiuid_unpack(_mpi,hdata)) return false;
154  tools::histo::h3d* h = new tools::histo::h3d("",10,0,1,10,0,1,10,0,1);
155  h->copy_from_data(hdata);
156  if(m_verbose) {
157  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank
158  << " : got a " << scls
159  << ", title " << h->title()
160  << ", mean_x " << h->mean_x() << ", rms_x " << h->rms_x()
161  << ", mean_y " << h->mean_y() << ", rms_y " << h->rms_y()
162  << ", mean_z " << h->mean_z() << ", rms_z " << h->rms_z()
163  << std::endl;
164  }
165  a_hists.push_back(class_pointer(h->s_cls(),h)); //give ownership of h.
166 
167  } else if(scls==tools::histo::p1d::s_class()) {
169  if(!profile_data_duiuidd_unpack(_mpi,pdata)) return false;
170  tools::histo::p1d* h = new tools::histo::p1d("",10,0,1);
171  h->copy_from_data(pdata);
172  if(m_verbose) {
173  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank
174  << " : got a " << scls
175  << ", title " << h->title()
176  << ", mean_x " << h->mean() << ", rms " << h->rms()
177  << std::endl;
178  }
179  a_hists.push_back(class_pointer(h->s_cls(),h)); //give ownership of h.
180 
181  } else if(scls==tools::histo::p2d::s_class()) {
183  if(!profile_data_duiuidd_unpack(_mpi,pdata)) return false;
184  tools::histo::p2d* h = new tools::histo::p2d("",10,0,1,10,0,1);
185  h->copy_from_data(pdata);
186  if(m_verbose) {
187  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank
188  << " : got a " << scls
189  << ", title " << h->title()
190  << ", mean_x " << h->mean_x() << ", rms_x " << h->rms_x()
191  << ", mean_y " << h->mean_y() << ", rms_y " << h->rms_y()
192  << std::endl;
193  }
194  a_hists.push_back(class_pointer(h->s_cls(),h)); //give ownership of h.
195 
196  } else {
197  m_out << "tools::mpi::hmpi::wait_histos : rank " << m_rank
198  << " : got not treated class " << scls
199  << std::endl;
200  }
201 
202  }} //ihist
203 
204  return true;
205  }
206 
207 public:
208  virtual int rank() const { return m_rank;}
209  virtual bool comm_rank(int& a_rank) const {
210  if(::MPI_Comm_rank(m_comm,&a_rank)!=MPI_SUCCESS) {a_rank=-1;return false;}
211  return true;
212  }
213  virtual bool comm_size(int& a_size) const {
214  if(::MPI_Comm_size(m_comm,&a_size)!=MPI_SUCCESS) {a_size=0;return false;}
215  return true;
216  }
217 public:
218  hmpi(std::ostream& a_out,int a_rank,int a_tag,const MPI_Comm& a_comm,bool a_verbose = false)
219  :m_out(a_out)
220  ,m_rank(a_rank)
221  ,m_tag(a_tag)
222  ,m_comm(a_comm)
223  ,m_verbose(a_verbose)
224  ,m_wrmpi(a_out,a_comm)
225  {
226 #ifdef TOOLS_MEM
227  tools::mem::increment(s_class().c_str());
228 #endif
229  }
230  virtual ~hmpi(){
231 #ifdef TOOLS_MEM
232  tools::mem::decrement(s_class().c_str());
233 #endif
234  }
235 protected:
236  hmpi(const hmpi& a_from)
237  :parent(a_from)
238  ,m_out(a_from.m_out)
239  ,m_rank(a_from.m_rank)
240  ,m_tag(a_from.m_tag)
241  ,m_comm(a_from.m_comm)
242  ,m_verbose(a_from.m_verbose)
243  ,m_wrmpi(a_from.m_out,a_from.m_comm)
244  {
245 #ifdef TOOLS_MEM
246  tools::mem::increment(s_class().c_str());
247 #endif
248  }
249  hmpi& operator=(const hmpi& a_from){
250  m_rank = a_from.m_rank;
251  m_tag = a_from.m_tag;
252  m_verbose = a_from.m_verbose;
253  return *this;
254  }
255 protected:
256  std::ostream& m_out;
257  int m_rank;
258  int m_tag;
259  const MPI_Comm& m_comm;
260  bool m_verbose;
262 };
263 
264 }}
265 
266 #endif
wrmpi
MPI_CHAR
#define MPI_CHAR
Definition: dummy_mpi.h:20
tools::mpi::hmpi::pack
virtual bool pack(const tools::histo::h1d &a_h)
Definition: hmpi:32
h2d
tools::histo::p1d
Definition: p1d:12
tools::histo::profile_data_duiuidd_pack
bool profile_data_duiuidd_pack(impi &a_mpi, const profile_data< double, unsigned int, unsigned int, double, double > &a_pd)
profile_data to C struct ////////////////////////////////////////////////
Definition: hd2mpi:170
MPI_Send
int MPI_Send(const void *, int, MPI_Datatype, int, int, MPI_Comm)
Definition: dummy_mpi.h:52
tools::histo::b2::rms_x
TC rms_x() const
Definition: b2:37
tools::histo::h3d::s_class
static const std::string & s_class()
Definition: h3d:15
tools::histo::b3::rms_x
TC rms_x() const
Definition: b3:52
tools::mpi::hmpi::s_class
static const std::string & s_class()
Definition: hmpi:27
tools::mpi::hmpi::num_t
unsigned int num_t
Definition: hmpi:26
tools::mpi::wrmpi::pack_reset
virtual void pack_reset()
Definition: wrmpi:383
MPI_Comm_size
int MPI_Comm_size(MPI_Comm, int *)
Definition: dummy_mpi.h:64
tools::histo::h1d
Definition: h1d:14
MPI_Recv
int MPI_Recv(void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Status *)
Definition: dummy_mpi.h:53
tools::histo::p1d::s_class
static const std::string & s_class()
Definition: p1d:15
tools::histo::h2d::s_class
static const std::string & s_class()
Definition: h2d:15
tools::mpi::hmpi::m_verbose
bool m_verbose
Definition: hmpi:260
p1d
tools::histo::base_histo::dac
const hd_t & dac() const
Definition: base_histo:56
tools::histo::histo_data_duiuid_unpack
bool histo_data_duiuid_unpack(impi &a_mpi, histo_data< double, unsigned int, unsigned int, double > &a_hd)
Definition: hd2mpi:104
tools::histo::p2d
Definition: p2d:12
hd2mpi
tools::histo::b3::mean_x
TC mean_x() const
Definition: b3:37
tools::histo::b2::mean_x
TC mean_x() const
Definition: b2:27
tools::mpi::hmpi::m_tag
int m_tag
Definition: hmpi:258
tools::mpi::hmpi::wait_histos
virtual bool wait_histos(int a_src, std::vector< std::pair< std::string, void * > > &a_hists)
Definition: hmpi:71
_MPI_Status
to pass h2mpi, hs2mpi /////////////////////////////////////
Definition: dummy_mpi.h:44
tools::histo::histo_data_duiuid_pack
bool histo_data_duiuid_pack(impi &a_mpi, const histo_data< double, unsigned int, unsigned int, double > &a_hd)
Definition: hd2mpi:36
tools::histo::h2d
Definition: h2d:12
tools::histo::b3::mean_z
TC mean_z() const
Definition: b3:47
tools::mpi::wrmpi::buffer
const char * buffer() const
Definition: wrmpi:483
tools::histo::b3::mean_y
TC mean_y() const
Definition: b3:42
tools::histo::b2::mean_y
TC mean_y() const
Definition: b2:32
tools::histo::b3::rms_y
TC rms_y() const
Definition: b3:58
tools::mpi::hmpi::m_rank
int m_rank
Definition: hmpi:257
tools::histo::p1d::s_cls
const std::string & s_cls() const
Definition: p1d:19
MPI_Comm_rank
int MPI_Comm_rank(MPI_Comm, int *)
Definition: dummy_mpi.h:65
MPI_SUCCESS
#define MPI_SUCCESS
Definition: dummy_mpi.h:30
tools::mpi::hmpi::operator=
hmpi & operator=(const hmpi &a_from)
Definition: hmpi:249
tools::histo::profile_data
Definition: profile_data:13
tools::mpi::hmpi::comm_size
virtual bool comm_size(int &a_size) const
Definition: hmpi:213
tools::histo::p2d::s_class
static const std::string & s_class()
Definition: p2d:15
h1d
tools::mpi::hmpi::hmpi
hmpi(const hmpi &a_from)
Definition: hmpi:236
tools::histo::p2::copy_from_data
void copy_from_data(const pd_t &a_from)
Definition: p2:101
tools::histo::h3::copy_from_data
void copy_from_data(const hd_t &a_from)
Definition: h3:34
tools::histo::p1::copy_from_data
void copy_from_data(const pd_t &a_from)
Definition: p1:109
tools::histo::profile_data_duiuidd_unpack
bool profile_data_duiuidd_unpack(impi &a_mpi, profile_data< double, unsigned int, unsigned int, double, double > &a_pd)
mpi to profile_data /////////////////////////////////////////////////////
Definition: hd2mpi:190
tools::mpi::hmpi::m_out
std::ostream & m_out
Definition: hmpi:256
tools::histo::b1::rms
TC rms() const
Definition: b1:37
tools::histo::h1d::s_cls
const std::string & s_cls() const
Definition: h1d:21
tools::histo::h2d::s_cls
const std::string & s_cls() const
Definition: h2d:19
hmpi
MPI_Comm
void * MPI_Comm
to pass hd2mpi ////////////////////////////////////////////
Definition: dummy_mpi.h:13
tools::histo::hmpi
Definition: hmpi:22
h3d
tools::mpi::hmpi::beg_send
virtual bool beg_send(unsigned int a_nhist)
Definition: hmpi:58
tools::histo::histo_data< double, unsigned int, unsigned int, double >
tools::mpi::wrmpi::sunpack
virtual bool sunpack(std::string &a_s)
Definition: wrmpi:343
tools::histo::p2d::s_cls
const std::string & s_cls() const
Definition: p2d:19
tools::mpi::hmpi::pack
virtual bool pack(const tools::histo::p1d &a_h)
Definition: hmpi:47
tools::mpi::hmpi::m_wrmpi
wrmpi m_wrmpi
Definition: hmpi:261
tools::mpi::hmpi::comm_rank
virtual bool comm_rank(int &a_rank) const
Definition: hmpi:209
tools::histo::h1d::s_class
static const std::string & s_class()
Definition: h1d:17
tools::mpi::hmpi::send
virtual bool send(int a_dest)
Definition: hmpi:62
tools::mpi::hmpi
Definition: hmpi:23
tools::mpi::hmpi::rank
virtual int rank() const
Definition: hmpi:208
tools::histo::h1::copy_from_data
void copy_from_data(const hd_t &a_from)
Definition: h1:40
tools::histo::base_histo::title
const std::string & title() const
Definition: base_histo:87
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::mpi::hmpi::pack
virtual bool pack(const tools::histo::p2d &a_h)
Definition: hmpi:52
tools::mpi::hmpi::hmpi
hmpi(std::ostream &a_out, int a_rank, int a_tag, const MPI_Comm &a_comm, bool a_verbose=false)
Definition: hmpi:218
tools::mpi::wrmpi::pack
virtual bool pack(char a_val)
Definition: wrmpi:51
MPI_Get_count
int MPI_Get_count(const MPI_Status *, MPI_Datatype, int *)
Definition: dummy_mpi.h:51
p2d
tools::histo::b3::rms_z
TC rms_z() const
Definition: b3:64
tools::histo::p1::get_histo_data
pd_t get_histo_data() const
Definition: p1:117
MPI_Probe
int MPI_Probe(int, int, MPI_Comm, MPI_Status *)
Definition: dummy_mpi.h:50
tools::mpi::wrmpi::ipos
int ipos() const
Definition: wrmpi:479
tools::histo::h3d
Definition: h3d:12
tools::histo::h2::copy_from_data
void copy_from_data(const hd_t &a_from)
Definition: h2:34
tools::mpi::wrmpi::spack
virtual bool spack(const std::string &a_s)
Definition: wrmpi:142
tools::mpi::wrmpi::unpack
virtual bool unpack(char &a_val)
Definition: wrmpi:235
tools::histo::p2::get_histo_data
pd_t get_histo_data() const
Definition: p2:109
tools::histo::h3d::s_cls
const std::string & s_cls() const
Definition: h3d:19
tools::mpi::hmpi::pack
virtual bool pack(const tools::histo::h3d &a_h)
Definition: hmpi:42
tools::histo::b1::mean
TC mean() const
Definition: b1:30
tools::mpi::hmpi::pack
virtual bool pack(const tools::histo::h2d &a_h)
Definition: hmpi:37
tools::mpi::hmpi::~hmpi
virtual ~hmpi()
Definition: hmpi:230
tools::mpi::wrmpi
Definition: wrmpi:41
tools::histo::b2::rms_y
TC rms_y() const
Definition: b2:43
tools::mpi::hmpi::m_comm
const MPI_Comm & m_comm
Definition: hmpi:259