mpi編寫和運行步驟

並行計算,大大加速計算過程,多爽呀,快來學習!!!

參考鏈接:https://blog.csdn.net/huayunhualuo/article/details/100188349

第一步:編寫mpi程序

這裏是c++版本的,命名爲hello.cpp文件

#include <mpi.h>
#include <stdio.h>
#include <math.h>
int main(int argc,char* argv[])
{
    int myid, numprocs;
    int namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc,&argv);/* 初始化並行環境 */
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);/* 當前進程的ID號 */
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);/* 進程的總數 */
    MPI_Get_processor_name(processor_name,&namelen);/* 當前處理器的名稱 */
    fprintf(stderr,"Hello World! Process %d of %d on %s\n",myid, numprocs, processor_name);
    MPI_Finalize();/* 結束並行環境 */
    return 0;
}

第二步:編譯cpp程序

mpic++ hello.cpp -o hello

這裏會生成hello可執行文件

第三步:並行運行

輸入:mpirun -np 4 ./hello
輸出如下:
Hello World! Process 2 of 4 on juechen
Hello World! Process 3 of 4 on juechen
Hello World! Process 1 of 4 on juechen
Hello World! Process 0 of 4 on juechen

這個過程在超算下的運行過程如下:

 

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