Chaste  Release::2017.1
ObjectCommunicator.hpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef _OBJECTCOMMUNICATOR_HPP_
37 #define _OBJECTCOMMUNICATOR_HPP_
38 
39 // Serialisation headers - must come first
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/archive/binary_iarchive.hpp>
42 
43 #include <boost/scoped_array.hpp>
44 #include <boost/shared_ptr.hpp>
45 #include <boost/serialization/shared_ptr.hpp>
46 #include "PetscTools.hpp" // For MPI methods
47 #include "Exception.hpp"
48 
49 const unsigned MAX_BUFFER_SIZE = 1000000;
50 
56 template<typename CLASS>
58 {
59 private:
60 
62  char* mRecvBuffer;
63 
67  std::vector<char* > mSendBuffer;
68 
72  std::vector<std::string> mSendString;
73 
76 
79 
81  MPI_Request mMpiRequest;
82 
84  bool mIsWriting;
85 
86 public:
87 
92 
100  void SendObject(boost::shared_ptr<CLASS> const pObject, unsigned destinationProcess, unsigned tag);
101 
109  void ISendObject(boost::shared_ptr<CLASS> const pObject, unsigned destinationProcess, unsigned tag);
110 
120  boost::shared_ptr<CLASS> RecvObject(unsigned sourceProcess, unsigned tag, MPI_Status& status);
121 
128  void IRecvObject(unsigned sourceProcess, unsigned tag);
129 
135  boost::shared_ptr<CLASS> GetRecvObject();
136 
149  boost::shared_ptr<CLASS> SendRecvObject(boost::shared_ptr<CLASS> const pSendObject, unsigned destinationProcess, unsigned sendTag, unsigned sourceProcess, unsigned sourceTag, MPI_Status& status);
150 };
151 
152 #include <sstream>
153 #include <string>
154 #include <cstring>
155 
156 // Implementation needs to be here, as CLASS could be anything
157 template<typename CLASS>
159  : mIsWriting(false)
160 {
163 }
164 
165 template<typename CLASS>
166 void ObjectCommunicator<CLASS>::SendObject(boost::shared_ptr<CLASS> const pObject, unsigned destinationProcess, unsigned tag)
167 {
168  // Create an output archive
169  std::ostringstream ss(std::ios::binary);
170  boost::archive::binary_oarchive output_arch(ss);
171 
172  output_arch << pObject;
173 
174  const std::string send_msg = ss.str();
175 
176  // Get + send string length
177  unsigned string_length = send_msg.size();
178  MPI_Send(&string_length, 1, MPI_UNSIGNED, destinationProcess, tag, PetscTools::GetWorld());
179 
180  // Send archive data
181  // The buffer is treated as const, but not specified as such by MPI_Send's signature
182  char* send_buf = const_cast<char*>(send_msg.data());
183  MPI_Send(send_buf, string_length, MPI_BYTE, destinationProcess, tag, PetscTools::GetWorld());
184 }
185 
186 template<typename CLASS>
187 void ObjectCommunicator<CLASS>::ISendObject(boost::shared_ptr<CLASS> const pObject, unsigned destinationProcess, unsigned tag)
188 {
189  MPI_Request request;
190 
191  // Create an output archive
192  std::ostringstream ss(std::ios::binary);
193  boost::archive::binary_oarchive output_arch(ss);
194 
195  output_arch << pObject;
196 
197  mSendString[destinationProcess] = ss.str();
198  mSendBufferLength = mSendString[destinationProcess].size();
199 
200  // Make sure we are not going to overrun the asynchronous buffer size.
201  assert(mSendBufferLength < MAX_BUFFER_SIZE);
202 
203  // Send archive data
204  // The buffer is treated as const, but not specified as such by MPI_Send's signature
205  mSendBuffer[destinationProcess] = const_cast<char*>(mSendString[destinationProcess].data());
206  MPI_Isend(mSendBuffer[destinationProcess], mSendBufferLength, MPI_BYTE, destinationProcess, tag, PetscTools::GetWorld(), &request);
207  MPI_Request_free(&request); //This is evil because it allows for another non-blocking send to overwrite the buffer
208 }
209 
210 template<typename CLASS>
211 boost::shared_ptr<CLASS> ObjectCommunicator<CLASS>::RecvObject(unsigned sourceProcess, unsigned tag, MPI_Status& status)
212 {
213  unsigned string_length = 0;
214  MPI_Recv(&string_length, 1, MPI_UNSIGNED, sourceProcess, tag, PetscTools::GetWorld(), &status);
215 
216  char* recv_array = new char[string_length];
217  MPI_Recv(recv_array, string_length, MPI_BYTE, sourceProcess , tag, PetscTools::GetWorld(), &status);
218 
219  // Extract a proper object from the buffer
220  std::string recv_string(recv_array, string_length);
221  delete[] recv_array;
222  std::istringstream ss(recv_string, std::ios::binary);
223 
224  boost::shared_ptr<CLASS> p_recv_object(new CLASS);
225  boost::archive::binary_iarchive input_arch(ss);
226 
227  input_arch >> p_recv_object;
228 
229  return p_recv_object;
230 }
231 
232 template<typename CLASS>
233 void ObjectCommunicator<CLASS>::IRecvObject(unsigned sourceProcess, unsigned tag)
234 {
235  assert(!mIsWriting); // Make sure the buffers are not already being used by a previous call to IRecvObject.
236 
237  mIsWriting = true;
238 
239  mRecvBuffer = new char[MAX_BUFFER_SIZE];
240  MPI_Irecv(mRecvBuffer, MAX_BUFFER_SIZE, MPI_BYTE, sourceProcess, tag, PetscTools::GetWorld(), &mMpiRequest);
241 }
242 
243 template<typename CLASS>
245 {
246  if (!mIsWriting)
247  {
248  EXCEPTION("No object to receive in ObjectCommunicator::GetRecvObject");
249  }
250 
251  MPI_Status return_status;
252 
253  MPI_Wait(&mMpiRequest, &return_status);
254 
255  int recv_size;
256  MPI_Get_count(&return_status, MPI_BYTE, &recv_size);
257 
258  // Extract a proper object from the buffer
259  std::string recv_string(mRecvBuffer, recv_size);
260  std::istringstream ss(recv_string, std::ios::binary);
261 
262  boost::shared_ptr<CLASS> p_recv_object(new CLASS);
263  boost::archive::binary_iarchive input_arch(ss);
264 
265  input_arch >> p_recv_object;
266 
267  // Tidy up
268  delete[] mRecvBuffer;
269 
270  mIsWriting = false;
271 
272  return p_recv_object;
273 }
274 
275 template<typename CLASS>
276 boost::shared_ptr<CLASS> ObjectCommunicator<CLASS>::SendRecvObject(boost::shared_ptr<CLASS> const pSendObject, unsigned destinationProcess, unsigned sendTag, unsigned sourceProcess, unsigned sourceTag, MPI_Status& status)
277 {
278  // Create an output archive
279  std::ostringstream oss(std::ios::binary);
280  boost::archive::binary_oarchive output_arch(oss);
281 
282  output_arch << pSendObject;
283 
284  std::string send_msg = oss.str();
285 
286  // Get + send string length
287  unsigned send_string_length = send_msg.size();
288  unsigned recv_string_length;
289 
290  MPI_Sendrecv(&send_string_length, 1, MPI_UNSIGNED, destinationProcess, sendTag, &recv_string_length, 1, MPI_UNSIGNED, sourceProcess, sourceTag, PetscTools::GetWorld(), &status);
291 
292  boost::scoped_array<char> recv_array(new char[recv_string_length]);
293 
294  // Send archive data
295  char* send_buf = const_cast<char*>(send_msg.data());
296  MPI_Sendrecv(send_buf, send_string_length, MPI_BYTE, destinationProcess, sendTag, recv_array.get(), recv_string_length, MPI_BYTE, sourceProcess, sourceTag, PetscTools::GetWorld(), &status);
297 
298  // Extract received object
299  std::string recv_string(recv_array.get(), recv_string_length);
300  std::istringstream iss(recv_string, std::ios::binary);
301 
302  boost::shared_ptr<CLASS> p_recv_object(new CLASS);
303  boost::archive::binary_iarchive input_arch(iss);
304 
305  input_arch >> p_recv_object;
306 
307  return p_recv_object;
308 }
309 
310 #endif // _OBJECTCOMMUNICATOR_HPP_
boost::shared_ptr< CLASS > SendRecvObject(boost::shared_ptr< CLASS > const pSendObject, unsigned destinationProcess, unsigned sendTag, unsigned sourceProcess, unsigned sourceTag, MPI_Status &status)
#define EXCEPTION(message)
Definition: Exception.hpp:143
static MPI_Comm GetWorld()
Definition: PetscTools.cpp:174
void IRecvObject(unsigned sourceProcess, unsigned tag)
std::vector< std::string > mSendString
void SendObject(boost::shared_ptr< CLASS > const pObject, unsigned destinationProcess, unsigned tag)
boost::shared_ptr< CLASS > GetRecvObject()
std::vector< char * > mSendBuffer
void ISendObject(boost::shared_ptr< CLASS > const pObject, unsigned destinationProcess, unsigned tag)
boost::shared_ptr< CLASS > RecvObject(unsigned sourceProcess, unsigned tag, MPI_Status &status)
static unsigned GetNumProcs()
Definition: PetscTools.cpp:108