siftoshka
siftoshka

Reputation: 63

MPI + Open MP Hybrid Initialization

I am using Mac OS with CLion IDE and my task is to use both parallel libraries (Open MP and MPI).

The problem is

Undefined symbols for architecture x86_64:
  "_MPI_Init", referenced from:
      _main in main.c.o
ld: symbol(s) not found for architecture x86_64
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)

I assume, that I cannot figure out how to write proper CMakeLists for it.

My CMakeLists looks like:

cmake_minimum_required(VERSION 3.15.3)
project(SeqSol C)

set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/opt/llvm/lib")
set(OPENMP_INCLUDES "/usr/local/opt/llvm/include")

OPTION(USE_OpenMP "Use OpenMP to enable <omp.h>" ON)
OPTION(USE_MPI "Use MPI to enable <mpi.h>" ON)

# Find OpenMP
if (APPLE AND USE_OpenMP)
    if (CMAKE_C_COMPILER_ID MATCHES "Clang")
        set(OpenMP_C "${CMAKE_C_COMPILER}")
        set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
        set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp9")
        set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
        set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
        set(OpenMP_libiomp9_LIBRARY ${OpenMP_C_LIB_NAMES})
    endif ()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
        set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
        set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp9")
        set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        set(OpenMP_libiomp9_LIBRARY ${OpenMP_CXX_LIB_NAMES})
    endif ()
endif ()

if (USE_OpenMP)
    find_package(OpenMP REQUIRED)
endif (USE_OpenMP)

if (USE_MPI)
    find_package(MPI REQUIRED)
endif (USE_MPI)

if (MPI_FOUND)
    include_directories(${MPI_INCLUDES_PATH})
    link_directories(${MPI_LIBRARIES_PATH})
endif(MPI_FOUND)

if (OPENMP_FOUND)
    include_directories("${OPENMP_INCLUDES}")
    link_directories("${OPENMP_LIBRARIES}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif (OPENMP_FOUND)

add_executable(SeqSol main.c problem.c fileparse.c)

As a C Compiler in Preferences is

/usr/local/opt/llvm/bin/clang 

and I also wrote CMake Options:

-DCMAKE_C_COMPILER=/usr/local/opt/llvm/bin/clang

I can use Open MP features, but it is impossible for MPI.

Any suggestions?

Upvotes: 2

Views: 305

Answers (1)

siftoshka
siftoshka

Reputation: 63

I finally figure out how to mix open mp and mpi. This CMake File works for me

cmake_minimum_required(VERSION 3.15.3)
project(SeqSol C)

set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/opt/llvm/lib")
set(OPENMP_INCLUDES "/usr/local/opt/llvm/include")

OPTION(USE_OpenMP "Use OpenMP to enable <omp.h>" ON)
OPTION(USE_MPI "Use MPI to enable <mpi.h>" ON)

# Find OpenMP
if (APPLE AND USE_OpenMP)
    if (CMAKE_C_COMPILER_ID MATCHES "Clang")
        set(OpenMP_C "${CMAKE_C_COMPILER}")
        set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
        set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp9")
        set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
        set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
        set(OpenMP_libiomp9_LIBRARY ${OpenMP_C_LIB_NAMES})
    endif ()
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
        set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
        set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp9")
        set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        set(OpenMP_libiomp9_LIBRARY ${OpenMP_CXX_LIB_NAMES})
    endif ()
endif ()

if (USE_OpenMP)
    find_package(OpenMP REQUIRED)
endif (USE_OpenMP)

if (USE_MPI)
    find_package(MPI REQUIRED)
endif (USE_MPI)

if (MPI_FOUND)
    include_directories(${MPI_INCLUDES_PATH})
endif(MPI_FOUND)


if (OPENMP_FOUND)
    include_directories("${OPENMP_INCLUDES}")
    link_directories("${OPENMP_LIBRARIES}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif (OPENMP_FOUND)

add_executable(SeqSol main.c problem.c fileparse.c)
target_link_libraries(SeqSol ${MPI_LIBRARIES})

Upvotes: 1

Related Questions