MPI学习1--------点对点通信

点对点通信

将一个进程的消息发送到另外一个进程
int MPI_Send(void buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)*
void *buf:要发送的数据的首地址
int count:发送的数据量
MPI_Datatype datatype:发送的数据类型
int dest:发送到的进程编号
int tag:通信标志
MPI_Comm comm:通信域

接受从另外一个进程发送过来的消息
int MPI_Recv(void buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm,MPI_Status * status)*
void * buf:接收数据的首地址
int count:接收多少数据。要与MPI_Send对应
MPI_Datatype datatype:接收的数据类型
int source:从那个进程发送过来
MPI_Comm comm:通信域
MPI_Status * status:返回状态,这个变量保存很多内容,有发送数据进程标识,发送数据使用的tag标识,本接收操作返回的错误代码

int MPI_Init(int *argc, char ***argv)

int MPI_Comm_size(MPI_Comm comm, int *size)
MPI_Comm comm:通信域
int *size:返回当前进程个数

int MPI_Comm_rank(MPI_Comm comm, int *rank)
MPI_Comm comm:通信域
int *rank:返回当前进程编号。如:0,1,2,。。。

获取处理器名字
int MPI_Get_processor_name(char *name, int *resultlen)

一个简单的例子

进程0将其send变量的值发送给进程1中的变量recv。

#include<mpi.h>
#include<iostream>

using namespace std;
int main(int argc, char **argv){
    int myid, procnum;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);//获得当前进程编号
    MPI_Comm_size(MPI_COMM_WORLD, &procnum);//获得进程个数
    int send,recv;
    MPI_Status status;
    if(myid==0){
        send=1;
        MPI_Send(&send,1,MPI_INT,1,0,MPI_COMM_WORLD);//0号进程向1号进程发送一个int型变量
    }
    else{
    //1号或2,3,。。。号进程接受从0号进程发送来的内容,然后保存在recv中
        MPI_Recv(&recv,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
        cout<<"recv="<<recv<<endl;
    }
    MPI_Finalize();
    return 0;
}

结果(linux终端),单机上两个进程

a@node:/$ mpicxx test1.cpp -o test
a@node:/$ mpirun -np 2 ./test
a@node:/$ recv=1

结果(linux终端),集群2台计算机,每个计算机1个进程

a@node:/$ mpicxx test1.cpp -o test
a@node:/$ mpirun -f ./hostfile -np 2 ./test
a@node:/$ recv=1

hostfile文件格式:

ip地址:进程个数
ip地址:进程个数
...
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值