OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
AsyncSendBuffers.h
Go to the documentation of this file.
1 #include <vector>
2 #include <algorithm>
3 #include <string.h>
4 #include "mpi.h"
5 #include "boost/smart_ptr.hpp"
6 #include "boost/bind.hpp"
7 
9 
10 public:
11 
12  AsyncSendBuffer(std::ostringstream &os) {
13  this->size_req = new MPI_Request();
14  this->buffer_req = new MPI_Request();
15  this->buf_size = os.str().length();
16  buffer = new char[buf_size];
17  memcpy(buffer, os.str().c_str(), buf_size);
18  }
19 
21  delete size_req;
22  delete buffer_req;
23  delete[] buffer;
24  }
25 
26  bool hasCompleted() {
27  int bufferflag = 0;
28  MPI_Test(this->buffer_req, &bufferflag, MPI_STATUS_IGNORE);
29  if(bufferflag) {
30  int sizeflag = 0;
31  MPI_Test(this->buffer_req, &sizeflag, MPI_STATUS_IGNORE);
32  if(sizeflag) {
33  return true;
34  }
35  }
36  return false;
37  }
38 
39  void send(int recv_rank, int size_tag, int data_tag, MPI_Comm comm) {
40  MPI_Isend(&buf_size, 1, MPI_LONG, recv_rank, size_tag, comm, size_req);
41  MPI_Isend(buffer, buf_size, MPI_CHAR, recv_rank, data_tag, comm, buffer_req);
42  }
43 
44 
45 private:
46 
47  // can't use smart pointers because MPI will hold last valid reference to
48  // pointer
49  MPI_Request *size_req;
50  MPI_Request *buffer_req;
51  char *buffer;
52 
53  size_t buf_size;
54 
55 };
56 
57 
59 
60 public:
62 
63  void insert(boost::shared_ptr<AsyncSendBuffer> buf) {
64  collection_.push_back(buf);
65  }
66 
67  void cleanup() {
68  collection_.erase(std::remove_if(
69  collection_.begin(), collection_.end(), boost::bind(&AsyncSendBuffer::hasCompleted, _1)),
70  collection_.end());
71  }
72 
73  size_t size() {
74  return collection_.size();
75  }
76 
77 private:
78  std::vector< boost::shared_ptr<AsyncSendBuffer> > collection_;
79 };
80 
AsyncSendBuffer(std::ostringstream &os)
std::vector< boost::shared_ptr< AsyncSendBuffer > > collection_
MPI_Request * buffer_req
void send(int recv_rank, int size_tag, int data_tag, MPI_Comm comm)
MPI_Request * size_req
void insert(boost::shared_ptr< AsyncSendBuffer > buf)