簡單的mpi發送和接收的程序
#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;
}
要保證 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;
}
#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;
}