1、首先都需要包含头文件
#include<mpi.h>
其次要对其进行初始化
int MPI_Init(int *argc, char **argv)
进行收束全文的方法采用
int MPI_Finalize(void)
3、可通过MPI_Comm_size
的方式进行得到对应的进程数目,MPI_COMM_WORLD是对应组的默认参数
int num;
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
printf("Hello World! The number of processes is %d\n",numprocs);
4、通过MPI_Comm_rank
可获得进程中对应的id序列
int myid;
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
5、获得对应处理机的名字MPI_Get_processor_name (name, &len)
,len为对应返回名字的 长度
int len;
char name[MPI_MAX_PROCESSOR_NAME];
MPI_Get_processor_name (name, &len);
printf("Hello, world. I am %s.\n", name);
6、MPI函数统计对应时间,MPI_Wtime()
可以统计对应开始的时间,而MPI_Wtick()
代表时钟精度
start = MPI_Wtime();
printf("The precision is: %f\n", MPI_Wtick());
finish = MPI_Wtime();
7、通过MPI_Barrier(MPI_Comm comm)
函数可以将其进行同步,保证同时开始或者某个函数调用结束之前不能返回值,起到的作用是阻止调用直到communicator中所有进程已经完成调用,任意一个进程的调用只能在所有communicator中成员已经开始调用之后
MPI_Comm comm : 通信子;
MPI_Barrier(MPI_COMM_WORLD);
8、消息通信具体函数内容
发送函数:
int MPI_Send(void* msg_buf_p, int msg_size, MPI_Datatype msg_type, int dest, int tag, MPI_Comm communicator)
void* msg_buf_p : 发送缓冲区的起始地址;
int buf_size : 缓冲区大小;
MPI_Datatype msg_type : 发送信息的数据类型;
int dest :目标进程的id值;
int tag : 消息标签;
MPI_Comm communicator : 通信子
接受函数:
int MPI_Recv(void* msg_buf_p, int buf_size, MPI_Datatype msg_type, int source, int tag, MPI_Comm communicator, MPI_Status *status_p)
void* msg_buf_p : 缓冲区的起始地址;
int buf_size : 缓冲区大小;
MPI_Datatype msg_type : 发送信息的数据类型;
int dest :目标进程的id值;
int tag : 消息标签;
MPI_Comm communicator : 通信子;
MPI_Status *status_p : status_p对象,包含实际接收到的消息的有关信息
MPI_Status status;
if(myid != 0) {
strcpy(message, "hello world!");
//your code here
MPI_Send(message, strlen(message)+1, MPI_CHAR, 0, 99, MPI_COMM_WORLD);
//end of your code
}
else {
//myid == 0
for(source=1; source<numprocs; source++) {
//your code here
MPI_Recv(message, 100,<