2

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?

4
  • Don't you have to use mpicc to compile MPI code? It has options to get relevant compiler defines/flags, so, you could add this to your make. Commented Apr 25, 2020 at 22:02
  • Craig Estey you're right, but I can't find how to do it properly. If I change C Complied to /usr/local/opt/open-mpi/bin/mpicc I got problems with Open MP. Do you know how to adequately add flags for mpicc? Commented Apr 25, 2020 at 22:17
  • @CraigEstey do you know solution? Commented Apr 25, 2020 at 23:04
  • Do man mpicc From that, you can use the -showme option. In CMakeLists, do a set of (e.g.) OPENMPI_C_FLAGS from the output of the mpicc command. I forget the exact syntax for set, but, in the shell it would be something like export C_FLAGS="`mpicc -showme`" Commented Apr 25, 2020 at 23:07

1 Answer 1

1

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})
Sign up to request clarification or add additional context in comments.

Comments

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.