CMakeLists.txt
include_directories("C:/Program Files (x86)/Microsoft SDKs/MPI/Include") link_directories("C:/Program Files (x86)/Microsoft SDKs/MPI/Lib/x64") # 查找 OpenMP(重要!) find_package(OpenMP REQUIRED) add_executable(hello mpi_hello.c) target_link_libraries(hello msmpi msmpifec msmpifmc) if(OpenMP_FOUND) # 现代 CMake 方式(推荐) target_link_libraries(hello PRIVATE OpenMP::OpenMP_C) # 或者传统方式 # target_compile_options(hello PRIVATE ${OpenMP_C_FLAGS}) # target_link_libraries(hello ${OpenMP_C_LIBRARIES}) endif()hello_mpi.c
#include "mpi.h" #include <omp.h> #include <stdio.h> int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); #pragma omp parallel { int tid = omp_get_thread_num(); printf("MPI rank %d, OpenMP thread %d\n", rank, tid); } MPI_Finalize(); return 0; }