MPI學習筆記——MPI基本框架

MPI程序的基本框架:

MPI_Init(&argc,&argv);
MPI_Comm_rank(comm,&rank);
... do something
MPI_Finalize();


有六個MPI基本函數:

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

int MPI_Finalize(void)

int MPI_Comm_rank(MPI_Comm comm, int *rank) -- IN comm, OUT rank

提供當前進程在通信域中的進程標識號。

int MPI_Comm_size(MPI_Comm comm, int *size) -- IN comm, OUT size

提供通信域中的進程數量。

int MPI_Send(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) - IN all

消息發送。

int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status * status)

IN count, datatype, source, tag, comm. OUT buf, status

消息接收。其中返回狀態status需要先行分配空間,至少包括MPI_SOURCE, MPI_TAG, MPI_ERROR三個域。對它執行MPI_Get_count調用可以得到接收消息的長度。

關於消息接收下節再仔細討論。

最後附上一個MPI的簡單程序,來展示MPI的編寫:

 

#include "mpi.h"
#include <stdio.h>
#include <math.h>

void main(argc,argv)

int argc;
char *argv[];
{
int myid,numprocs;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];

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, "Hello World! Process %d of %d on %s/n",
myid,numprocs,processor_name);

MPI_Finalize();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章