Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
comm_mpi_smart.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2019 Y. H. Tang, S. Kudo, X. Bian, Z. Li, G. E. Karniadakis *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ******************************************************************************/
39 
48 #ifndef COMM_MPI_SMART_H
49 #define COMM_MPI_SMART_H
50 
51 #include <cstdint>
52 #include <cstdlib>
53 #include "../general/util.h"
54 #include "comm.h"
55 #include "comm_mpi.h"
56 #include "comm_factory.h"
57 #include "message/message.h"
58 #include "../storage/stream.h"
59 
60 namespace mui {
61 
62 class comm_mpi_smart : public comm_mpi {
63 private:
64  std::list<std::pair<MPI_Request,std::shared_ptr<std::vector<char> > > > send_buf;
65 
66 public:
67  comm_mpi_smart( const char URI[], const bool quiet, MPI_Comm world = MPI_COMM_WORLD ) : comm_mpi(URI, quiet, world) {}
68  virtual ~comm_mpi_smart() {
69  // Call blocking MPI_Test on any remaining MPI_Isend messages in buffer and if complete, pop before destruction or warn
70  test_completion_blocking();
71  }
72 
73 private:
74  void send_impl_( message msg, const std::vector<bool> &is_sending ) {
75  auto bytes = std::vector<char>(msg.detach());
76 
77  if(bytes.size() > INT_MAX) {
78  std::cerr << "MUI Error [comm_mpi_smart.h]: Trying to send more data than is possible with MPI_Isend." << std::endl
79  << "This is likely because there is too much data per MPI rank." << std::endl
80  << "The program will now abort. Try increasing the number of MPI ranks." << std::endl;
81  std::abort();
82  }
83 
84  for( int i = 0; i < remote_size_; i++ ) {
85  if( is_sending[i] ) {
86  send_buf.emplace_back(MPI_Request(), std::make_shared<std::vector<char> >(bytes));
87  MPI_Isend(send_buf.back().second->data(), send_buf.back().second->size(), MPI_BYTE, i, 0,
88  domain_remote_, &(send_buf.back().first));
89  }
90  }
91 
92  // Call non-blocking MPI_Test on outstanding MPI_Isend messages in buffer and if complete, pop
93  test_completion();
94  }
95 
96  message recv_impl_() {
97  // Catch any unsent MPI_Isend calls, non-blocking
98  test_completion();
99 
100  MPI_Status status;
101  MPI_Probe(MPI_ANY_SOURCE, 0, domain_remote_, &status);
102  int count;
103  MPI_Get_count(&status, MPI_BYTE, &count);
104  std::vector<char> rcv_buf(count);
105  MPI_Recv( rcv_buf.data(), count, MPI_BYTE, status.MPI_SOURCE, status.MPI_TAG, domain_remote_, MPI_STATUS_IGNORE );
106 
107  // Catch any unsent MPI_Isend calls, non-blocking
108  test_completion();
109 
110  return message::make(std::move(rcv_buf));
111  }
112 
115  void test_completion() {
116  for( auto itr=send_buf.begin(), end=send_buf.end(); itr != end; ) {
117  int test = false;
118  MPI_Test(&(itr->first), &test, MPI_STATUS_IGNORE);
119  if( test ) itr = send_buf.erase(itr);
120  else ++itr;
121  }
122  }
123 
126  void test_completion_blocking() {
127  while (send_buf.size() > 0) {
128  for( auto itr=send_buf.begin(), end=send_buf.end(); itr != end; ) {
129  MPI_Wait(&(itr->first), MPI_STATUS_IGNORE);
130  itr = send_buf.erase(itr);
131  }
132  }
133  }
134 };
135 
136 inline communicator *create_comm_mpi_smart( const char URI[], const bool quiet ) {
137  return new comm_mpi_smart(URI, quiet);
138 }
139 
140 const static bool registered = comm_factory::instance().link( "mpi", create_comm_mpi_smart );
141 
142 }
143 #endif
Definition: comm_mpi_smart.h:62
comm_mpi_smart(const char URI[], const bool quiet, MPI_Comm world=MPI_COMM_WORLD)
Definition: comm_mpi_smart.h:67
virtual ~comm_mpi_smart()
Definition: comm_mpi_smart.h:68
Definition: comm_mpi.h:61
MPI_Comm domain_remote_
Definition: comm_mpi.h:173
int remote_size_
Definition: comm_mpi.h:176
Definition: comm.h:55
static dispatcher< std::string, std::function< communicator *(const char[], const bool)> > & instance()
Definition: lib_singleton.h:58
File containing class definition of communication interface. This is the base class for all other com...
Structures and methods to create a new communicator based on chosen protocols.
Class definition of base MPI communicator.
Structure to contain and manipulate data from internal data to MPI message.
Definition: comm.h:54
communicator * create_comm_mpi_smart(const char URI[], const bool quiet)
Definition: comm_mpi_smart.h:136
Definition: message.h:61
std::vector< char > detach()
Definition: message.h:96
static message make(const id_type &id, types &&... data)
Definition: message.h:72