简单的mpi发送和接收的程序

来源:互联网 发布:mac os x无法升级 编辑:程序博客网 时间:2024/05/16 09:32
简单的mpi发送和接收的程序

随手写了个简单的mpi发送和接收的程序。
这个和都志辉老师的那本<mpi并行程序设计> p.30 是差不多的。

有一点,要想看到效果,要保证 MpiRun.exe  -np x $(TargetPath)
的这个 x 大于 1。呵呵。



#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
int myid,numprocs;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
    char buff[100];

MPI_Init(&argc,&argv);

MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Get_processor_name(processor_name,&namelen);

fprintf(stderr,
"/nHello World!Process %d of %d on %s/n",
myid,numprocs,processor_name);

    memset(buff,0,100);
    if (myid == 0)
    {
        strcpy(buff,"0 to 1");
        MPI_Send(buff,strlen(buff),MPI_CHAR,1,99,MPI_COMM_WORLD);
        fprintf(stderr,"/nsend:%s",buff);
    }
    else
    {
        MPI_Status sta;
        if (myid == 1)
        {
            MPI_Recv(buff,20,MPI_CHAR,0,99,MPI_COMM_WORLD,&sta);
            fprintf(stderr,"/nrecv:%s",buff);
        }
    }

MPI_Finalize();
    
    if (myid == 0)
    {
        printf("/nPress a key and exit./n");

        getch();
    }
return 0;
}