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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章