4

I have my OpenMPI and gcc installed on my MacOS. C syntax for OpenMPI could run perfectly. However, the c++ syntax for OpenMPI are always not recognized. For example, line with void MPI::Init(int &argc, char**&argv); shows error of use of undeclared identifier 'MPI'.

Could anybody give some detailed procedure to fix this problem?

#include "mpi.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {


void MPI::Init(int &argc, char**&argv);

// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
//int Comm::Get_size() const
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);

// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
       processor_name, world_rank, world_size);


// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();

}
1
  • Does mpi.h declare a namespace MPI in which there is an Init() function? Or what is MPI? Commented Jul 31, 2016 at 4:21

3 Answers 3

4

I had the same issue when installing open-mpi using homebrew in Mac OS X (I am a newbie in Mac OS X coming from Debian/Ubuntu Linux). Fortunately, I solved it. As Zulan said, your open-mpi installation was built without C++ bindings. However, I think the best solution is to rebuild it directly from the source code. I downloaded the .tar files directly from its homepage, and if you look closer you will see that the C++ bindings are by default disabled (do the following inside the untar folder):

$ ./configure -help

--enable-mpi-cxx enable C++ MPI bindings (default: disabled)

So, basically I rebuilt open-mpi directly adding that flag, as follow:

$ ./configure --enable-mpi-cxx

Then, just make sure you know where you have installed it (if you used --prefix= or the default) to link it to your makefile properly.

for more information about this issue look here: https://github.com/open-mpi/ompi/issues/3173.

Sign up to request clarification or add additional context in comments.

1 Comment

I built open-mpi directly from the source using the --enable-mpi-cxx option. However the "mpi.h" file still does not declare a namespace MPI. This leads to "error: use of undeclared identifier 'MPI'".
2

It seems your OpenMPI installation was built without C++ bindings. While OpenMPI still contains the C++ bindings, they actually have been removed from the standard years ago. You will probably have to rebuild OpenMPI to enable the C++ bindings, that depends on how you installed it in the first place.

Since they are not part of the standard, the recommendation is to not the C++ bindings in the first place. Instead the most common recommendation is to use Boost.MPI.

Unfortunately it appears that Boost.MPI is no longer actively developed (last bug closed 2 years ago), and there are some really nasty open issues, so I cannot recommend Boost.MPI. There are some cool MPI type-traits approaches, but I do not know of a widely accepted / actively developed wrapper.

2 Comments

can I understand it as OpenMPI's full functions support C, while C++ is kind of partially supported? Are most OpenMPI application programmers prefer C instead of C++?
OpenMPI is an implementation of the MPI standard. Generally you develop having the MPI standard in mind - not a particular implementation. The C++ bindings were removed from the standard years ago. So you should not use them, even if some implementations may still provide them.
2

If you installed OpenMPI using homebrew, you need to first uninstall it:

brew uninstall openmpi

And then reinstall it, enabling C++ bindings:

brew install openmpi --with-cxx-bindings

See brew info openmpi for a full list of options. Note that since you're not using the default build of OpenMPI, brew will need to build it from source instead of using a precompiled bottle so it will take longer than usual to install.

1 Comment

I get this error "Error: invalid option: --with-cxx-bindings". brew info openmpi does not show the available options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.