2

I'm trying to build an executable from C++ source code which uses MPI, on a GNU/Linux Devuan Chimaera system. Now, I'm an MPI/OpenMP newbie, I'm just trying to adapt this code, which isn't mine, to be built with CMake - when before it had a Makefile. My build succeeds, but I'm seeing segfaults, so I want to make sure my problem isn't with the build phase, which bugs me.

My CMakeLists.txt has:

find_package(OpenMP REQUIRED)
find_package(MPI REQUIRED)

and my system has OpenMPI 4.1.1 installed, which is found. I do this for my target:

target_link_libraries(my_executable PRIVATE MPI::MPI_CXX OpenMP::OpenMP_CXX)

but nothing else which indicates its expecting to be compiled by mpicxx.

... and indeed, when I configure (with CMake 3.22.1) and then build, the usual c++ executable gets invoked to compile (and then link) the my_target executable.

Questions:

  • Can source code which originally was getting compiled with mpicxx be compiled with "just" a C++ compiler, with the appropriate includes?
  • Assuming there's any merit to using mpicxx for compilation - how do I get CMake to use it for my target?

Edit: It's been suggested to me to try using mpirun to run my program. With it, I get no segmentation faults, consistently; it's only when I run directly that I see them.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • You don't specify what `my_target` is, but if it's a library, those should probably be PUBLIC dependencies. "Default" privacy is tricky https://stackoverflow.com/questions/51396608/what-is-default-target-link-libraries-privacy-setting#comment108374911_51404894 – StoryTeller - Unslander Monica Jan 15 '22 at 21:45
  • `mpicxx` is a shell script around the regular C++ compiler, with include and library paths set. So yes, if cmake can find those paths, you can use the base compiler. – Victor Eijkhout Jan 15 '22 at 21:52
  • 1
    @StoryTeller-UnslanderMonica: Clarified that it's an executable - `my_executable`. – einpoklum Jan 15 '22 at 22:34
  • @VictorEijkhout: I use OpenMPI 4.1.1, and there, `mpicxx` is _not_ a shell script. It's still kind of a wrapper, though. I think. – einpoklum Jan 15 '22 at 22:45
  • I've never used OpenMPI. Please explain? What *is* it? – Victor Eijkhout Jan 15 '22 at 23:20
  • I do this routinely on CrayPE systems that provide compiler wrappers. I've never needed to do more than link to `MPI::MPI_CXX` to make it work, there. However, in my case, this is explicitly supported by the vendor... https://docs.nersc.gov/development/build-tools/cmake/#mpi – Alex Reinking Jan 16 '22 at 01:38
  • 1
    `mpicxx` is a wrapper for the C++ compiler (it simply adds the include/library path and the MPI libraries at link time). This is indeed a binary by default with Open MPI (but you can make it a shell script by passing the `--enable-script-wrapper-compilers` option at `configure` time. – Gilles Gouaillardet Jan 16 '22 at 02:07
  • @VictorEijkhout: [This is OpenMPI](https://en.wikipedia.org/wiki/Open_MPI). – einpoklum Jan 16 '22 at 07:49
  • @AlexReinking: It's an executable, so it doesn't matter all that much, but sure. – einpoklum Jan 16 '22 at 14:45

1 Answers1

3
  • Can source code which originally was getting compiled with mpicxx be compiled with "just" a C++ compiler, with the appropriate includes?

Yes, and you're doing it correctly.

The MPI systems I've used (Cori, Perlmutter, Stampede) have all provided implementations that work correctly with CMake's MPI support. However, it's possible that a sufficiently poorly administered system will break this.

  • Assuming there's any merit to using mpicxx for compilation - how do I get CMake to use it for my target?

This is a toolchain setting... set CMAKE_CXX_COMPILER to /path/to/mpicxx either at the command line, in a preset, or in a toolchain file.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • But I can't have a different compiler for non-MPI and MPI C++ programs? – einpoklum Jan 16 '22 at 07:39
  • No, you cannot. CMake allows one compiler per language and doesn't consider MPI C++ to be distinct from non-MPI C++ (and it isn't... it's not like UPC++, say). – Alex Reinking Jan 16 '22 at 13:33
  • Are any of these MPI implementations FOSS? – einpoklum Jan 16 '22 at 16:28
  • So, I un-accepted this answer, because when I used the exact same command-lines as CMake was generating, but with the mpicxx wrapper, the linked program did not crash with a segmentation fault. – einpoklum Jan 16 '22 at 19:25