Prog1: helloworld.c 最简单的MPI程序

来源:互联网 发布:telnet端口号445端口 编辑:程序博客网 时间:2024/05/20 14:20

1. 程序源代码

#include<stdio.h>#include"mpi.h"int main(int argc, char *argv[]){        int totalTaskNum, rankID;        int rt = MPI_Init(&argc, &argv);        if(rt != MPI_SUCCESS){                printf("Error starting MPI.\n");                MPI_Abort(MPI_COMM_WORLD, rt);        }        MPI_Comm_size(MPI_COMM_WORLD, &totalTaskNum);        MPI_Comm_rank(MPI_COMM_WORLD, &rankID);        printf("Hellow, world! %dth of totalTaskNum = %d\n", rankID, totalTaskNum);        MPI_Finalize();        return 0;}

2. 编译,执行

[amao@amao991 mpi-study]$ mpicc -o helloworld helloworld.c
[amao@amao991 mpi-study]$ mpiexec -n 6 ./helloworld
Hellow, world! 1th of totalTaskNum = 6
Hellow, world! 0th of totalTaskNum = 6
Hellow, world! 3th of totalTaskNum = 6
Hellow, world! 2th of totalTaskNum = 6
Hellow, world! 4th of totalTaskNum = 6
Hellow, world! 5th of totalTaskNum = 6

3. 总结

(1) 使用了缺省了machinefile,已经设置在环境变量中了

(2) 学习了最基本的四个函数

MPI_Init(&argc, &argv) //在c语言环境下,可以通过该函数把命令行参数传给各个进程task

MPI_Comm_size(MPI_COMM_WORLD, &totalTaskNum);  //该函数确定进程组内进程的个数,mpiexec -n 6 ./helloworld中-n 6告诉它的

MPI_Comm_rank(MPI_COMM_WORLD, &rankID);     //确定当前进程的ID,位于0~totalTaskNum-1之间

MPI_Finalize()   //结束MPI执行,放在代码的最后

(3) MPI_Abort函数

MPI_Abort(MPI_COMM_WORLD, rt);  //终结与communicator相关的所有进程

-------------------------------------

4. 用c++写的最简单的MPI程序

#include"mpi.h"#include<iostream>using namespace std;int main(){int rankID;int sizeNum;MPI_Init(0,0);MPI_Comm_size(MPI_COMM_WORLD, &sizeNum);MPI_Comm_rank(MPI_COMM_WORLD, &rankID);//printf("Hi! %d of total %d\n",rankID,sizeNum);cout<<"Hi ! "<<rankID<<" of total ="<<sizeNum<<endl;MPI_Finalize();return 0;}








原创粉丝点击