clion 运行 MPI

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切换到工程所在的目录。

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值