1. 首先进行MPI的安装,将MPI安装到/usr/local/mpich中,可参考其他博客的安装。

2. 然后更改clion的cmakelist.txt文件:

cmake_minimum_required(VERSION 3.13)
project(eigen)

find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})

SET(CMAKE_CXX_COMPILER mpicxx)
SET(CMAKE_C_COMPILER  mpicc)

add_executable(mpi_spmv mpi_spmv.cpp)

将要运行的文件,加入到add_executable中,我设置编译后的名称为mpi_spmv(名称可自行设置)

3. 修改clion的运行配置:

将Executable切换到本地的mpirun所在路径,路径为/usr/bin/mpirun

Working directory切换到工程所在的目录。

clion 运行 MPI

4. 尝试运行mpi_spmv.cpp文件:

#include "mpi_hello.h"
#include "cstdio"
#include "cstring"
#include "mpi.h"

const int MAX_STRING = 100;

int main() {
    char greeting[MAX_STRING];
    int comm_sz;
    int my_rank;

    MPI_Init(NULL, NULL);
    MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    if (my_rank != 0) {
        sprintf(greeting, "Greeting from process %d of %d!", my_rank, comm_sz);
        MPI_Send(greeting, strlen(greeting) + 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
    } else {
        printf("Greetings from process %d of %d!\n", my_rank, comm_sz);
        for (int q = 1; q < comm_sz; ++q) {
            MPI_Recv(greeting, MAX_STRING, MPI_CHAR, q, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            printf("%s\n", greeting);
        }
    }
    MPI_Finalize();
    return 0;
}

运行结果为:

/usr/bin/mpirun -np 4 ./mpi_spmv
Greetings from process 0 of 4!
Greeting from process 1 of 4!
Greeting from process 2 of 4!
Greeting from process 3 of 4!

Process finished with exit code 0

 

相关文章:

  • 2021-07-21
  • 2021-04-25
  • 2021-11-12
  • 2021-11-03
  • 2021-07-07
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-22
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-05-17
相关资源
相似解决方案