MPI + Open MP混合初始化

问题描述 投票:0回答:1

我使用Mac OS和CLion IDE,我的任务是使用两个并行库(Open MP和MPI)。

问题是

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)

我想,我不知道如何为它写合适的CMakeLists。

我的CMakeLists看起来是这样的。

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)

作为一个C编译器,在首选项中是

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

而且我还写了CMake选项。

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

我可以使用Open MP的功能,但MPI是不可能的。

有什么建议吗?

c cmake openmp clion openmpi
1个回答
1
投票

我终于知道如何混合使用Open MP和MPI了。这个CMake文件对我很有用

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})
© www.soinside.com 2019 - 2024. All rights reserved.