【并行计算2】MPI实训

本文详细介绍了MPI在并行计算中的应用,包括初始化、进程管理、消息通信、数据打包与解包、规约操作、广播、收集、散发、组管理和通信子管理等多个方面,是并行计算学习的重要参考资料。
摘要由CSDN通过智能技术生成

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,<
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值