To run my software with MPI, I need to create a simple method that will return MPI::COMM_WORLD.
So in my class, we have:
#include <mpi.h>
class Parallel{
public:
MPI::Comm getCommunicator(){
return MPI::COMM_WORLD;
}
protected:
int iproc;
};
int main(int argc, char *argv[]){
Parallel* parallel;
MPI::Init(argc, argv);
int my_rank;
my_rank = parallel->getCommunicator().Get_rank();
MPI::Finalize();
return 0;
}
How should I implement the getCommunicator() method in order it returns MPI::COMM_WORLD? When I try to compile the above, I get the following error:
invalid abstract return type for member function 'MPI::Comm Parallel ::getCommunicator()
Zulan is right, the C++ bindings are deleted from MPI 3, so new code really shouldn't be written using them.
The reason they've been removed is that they weren't particularly well thought out or maintained, and they're definitely not very idiomatic, so there are lots of strange cases you run into if you use them. Boost::MPI is much better, but unfortunately only covers MPI 1.
If for the purposes of maintaining existing code you have to have this, the issue is as is described here - at least in OpenMPI, MPI::Comm is defined as a pure virtual class, so you can't return an object of that type since there's no way of creating one; you can only return a subtype. (I assume it was done this way so you could have intra- and inter-communicators being subtypes).
A classic way of dealing with this situation is to return a reference to the object, rather than the object itself, and let the compiler deal with the upcasting:
Gives