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,。。。

一个简单的例子

进程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);
    }
    else{
        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

猜你喜欢

转载自blog.csdn.net/k5722166/article/details/80926838