LinuxCommandLibrary

mpic++

Compile MPI C++ programs

TLDR

Compile an Open MPI program

$ mpic++ [path/to/source_file]
copy

Show all the wrapper-supplied flags
$ mpic++ --showme
copy

SYNOPSIS

mpic++ [standard_compiler_options] source_files [-o output_file]

PARAMETERS

-o file
    Specifies the name of the output executable or object file.

-c
    Compiles source files to object files without linking, producing .o files.

-Dmacro[=val]
    Defines a preprocessor macro, optionally with a specific value.

-Idir
    Adds dir to the list of directories to search for header files.

-Ldir
    Adds dir to the list of directories to search for libraries.

-llibrary
    Links the program with the specified library.

-g
    Includes debugging information in the output executable.

-O[level]
    Optimizes the compiled code, with an optional level (e.g., -O2, -O3).

-std=C++_standard
    Specifies the C++ language standard to use (e.g., c++11, c++17, c++20).

-showme
    Displays the full underlying compiler command that mpic++ would execute, without running it. Useful for debugging build issues.

-showme:compile
    Displays only the compile-time flags used by the wrapper.

-showme:link
    Displays only the link-time flags and libraries used by the wrapper.

DESCRIPTION

The mpic++ command is a crucial wrapper script designed for compiling and linking C++ applications that utilize the Message Passing Interface (MPI). It acts as a convenient front-end to the underlying C++ compiler (such as g++ or clang++), automating the complex process of including the necessary MPI header files and linking against the correct MPI libraries. Developers no longer need to manually specify cumbersome -I (include paths) or -L (library paths) and -l (library names) flags specific to their MPI installation. This automation significantly simplifies the build process for parallel programs, reduces configuration errors, and enhances the portability of MPI C++ code across different systems and MPI implementations (like Open MPI or MPICH). By using mpic++, programmers can focus on writing their parallel logic, trusting that the compilation environment is correctly set up.

CAVEATS

mpic++ requires an MPI implementation (e.g., Open MPI, MPICH) to be correctly installed and configured on the system.
Its specific behavior and the underlying C++ compiler invoked depend entirely on how MPI was configured during its installation.
It is a wrapper, not a compiler itself; it merely orchestrates the correct flags for the actual compiler.
Mixing different MPI implementations or versions within a single project can lead to compilation or runtime issues.

HOW IT WORKS INTERNALLY

mpic++ operates by querying the MPI installation's configuration. It retrieves the appropriate compiler (e.g., g++, clang++), include directories (-I flags), library directories (-L flags), and library names (-l flags) that are necessary for building an MPI application. It then constructs a full command line for the underlying C++ compiler, incorporating these MPI-specific flags along with any user-provided options and source files. This abstraction hides the complexity of MPI-specific build parameters from the developer.

PORTABILITY AND EASE OF USE

The primary benefit of using mpic++ is the significant boost in portability and ease of use it offers for MPI C++ development. By providing a consistent interface, it allows developers to compile their MPI code on different systems or with different MPI implementations without needing to modify their Makefiles or build scripts for specific MPI library locations. This greatly simplifies continuous integration, deployment, and collaborative development of parallel applications.

HISTORY

The Message Passing Interface (MPI) standard emerged in the early 1990s as a vendor-independent, portable standard for message-passing. To simplify the development of parallel applications, particularly regarding compilation and linking, MPI implementations (like MPICH and Open MPI) introduced wrapper scripts such as mpic++. Before these wrappers, developers had to manually manage complex include paths, library paths, and linker flags, which varied significantly between different MPI versions and system configurations. The introduction of mpic++ standardized this build process, making MPI programming more accessible, less error-prone, and promoting code portability across diverse high-performance computing environments.

SEE ALSO

mpicc(1), mpifort(1), g++(1), clang++(1), mpiexec(1), mpirun(1)

Copied to clipboard