1- /*
2- * Interfaces for SDC/MLSDC/PFASST algorithms.
3- */
4-
51#ifndef _PFASST_MPI_COMMUNICATOR_HPP_
62#define _PFASST_MPI_COMMUNICATOR_HPP_
73
8- #include < exception >
4+ #include < stdexcept >
95#include < vector>
6+ using namespace std ;
107
118#include < mpi.h>
129
1310#include " interfaces.hpp"
14- #include " logging.hpp"
1511
16- using namespace std ;
1712
1813namespace pfasst
1914{
2015 namespace mpi
2116 {
22-
2317 class MPIError
24- : public exception
18+ : public runtime_error
2519 {
2620 public:
27- const char * what () const throw()
28- {
29- return " mpi error" ;
30- }
21+ explicit MPIError (const string& msg=" " );
22+ virtual const char * what () const throw();
3123 };
3224
25+
26+ // forward declare for MPICommunicator
3327 class MPIStatus ;
3428
3529
@@ -47,115 +41,37 @@ namespace pfasst
4741 // ! @}
4842
4943 // ! @{
50- MPICommunicator ()
51- {}
52-
53- MPICommunicator (MPI_Comm comm)
54- {
55- set_comm (comm);
56- }
44+ MPICommunicator ();
45+ MPICommunicator (MPI_Comm comm);
5746 // ! @}
5847
5948 // ! @{
60- void set_comm (MPI_Comm comm)
61- {
62- this ->comm = comm;
63- MPI_Comm_size (this ->comm , &(this ->_size ));
64- MPI_Comm_rank (this ->comm , &(this ->_rank ));
65-
66- shared_ptr<MPIStatus> status = make_shared<MPIStatus>();
67- this ->status = status;
68- this ->status ->set_comm (this );
69- }
70-
71- int size () { return this ->_size ; }
72- int rank () { return this ->_rank ; }
49+ virtual void set_comm (MPI_Comm comm);
50+ virtual int size ();
51+ virtual int rank ();
7352 // ! @}
7453 };
7554
7655
7756 class MPIStatus
7857 : public IStatus
7958 {
59+ protected:
8060 vector<bool > converged;
8161 MPICommunicator* mpi;
8262
8363 public:
84-
85- virtual void set_comm (ICommunicator* comm)
86- {
87- this ->comm = comm;
88- this ->converged .resize (comm->size ());
89-
90- this ->mpi = dynamic_cast <MPICommunicator*>(comm); assert (this ->mpi );
91- }
92-
93- virtual void clear () override
94- {
95- std::fill (converged.begin (), converged.end (), false );
96- }
97-
98- virtual void set_converged (bool converged) override
99- {
100- LOG (DEBUG) << " mpi rank " << this ->comm ->rank () << " set converged to " << converged;
101- this ->converged .at (this ->comm ->rank ()) = converged;
102- }
103-
104- virtual bool get_converged (int rank) override
105- {
106- return this ->converged .at (rank);
107- }
108-
109- virtual void post ()
110- {
111- // noop: send/recv for status info is blocking
112- }
113-
114- virtual void send ()
115- {
116- // don't send forward if: single processor run, or we're the last processor
117- if (mpi->size () == 1 ) { return ; }
118- if (mpi->rank () == mpi->size () - 1 ) { return ; }
119-
120- int iconverged = converged.at (mpi->rank ()) ? 1 : 0 ;
121-
122- LOG (DEBUG) << " mpi rank " << this ->comm ->rank () << " status send " << iconverged;
123-
124- int err = MPI_Send (&iconverged, sizeof (int ), MPI_INT,
125- (mpi->rank () + 1 ) % mpi->size (), 1 , mpi->comm );
126-
127- if (err != MPI_SUCCESS) {
128- throw MPIError ();
129- }
130- }
131-
132- virtual void recv ()
133- {
134- // don't recv if: single processor run, or we're the first processor
135- if (mpi->size () == 1 ) { return ; }
136- if (mpi->rank () == 0 ) { return ; }
137-
138- if (get_converged (mpi->rank ()-1 )) {
139- LOG (DEBUG) << " mpi rank " << this ->comm ->rank () << " skipping status recv" ;
140- return ;
141- }
142-
143- MPI_Status stat;
144- int iconverged;
145- int err = MPI_Recv (&iconverged, sizeof (iconverged), MPI_INT,
146- (mpi->rank () - 1 ) % mpi->size (), 1 , mpi->comm , &stat);
147-
148- if (err != MPI_SUCCESS) {
149- throw MPIError ();
150- }
151-
152- converged.at (mpi->rank ()-1 ) = iconverged == 1 ? true : false ;
153-
154- LOG (DEBUG) << " mpi rank " << this ->comm ->rank () << " status recv " << iconverged;
155- }
64+ virtual void set_comm (ICommunicator* comm);
65+ virtual void clear () override ;
66+ virtual void set_converged (bool converged) override ;
67+ virtual bool get_converged (int rank) override ;
68+ virtual void post ();
69+ virtual void send ();
70+ virtual void recv ();
15671 };
157-
15872 } // ::pfasst::mpi
15973} // ::pfasst
16074
75+ #include " mpi_communicator_impl.hpp"
76+
16177#endif
0 commit comments