MPI - 缓冲区和非阻塞通信
转载自: Introduction to MPI - Part II (Youtube)
Buffering
Suppose we have
if(rank==)
MPI_Send(sendbuf,...,,...)
if(rank==)
MPI_Recv(recvbuf,...,,...)
These are blocking communications, which means they will not return until the arguments to the functions can be safely modified by subsequent statements in the program.
Assume that process 1 is not ready to receive. Then there are 3 possibilities for process 0:
(1) stops and waits until process 1 is ready to receive
(2) copies the message at sendbuf into a system buffer (can be on process 0, process 1 or somewhere else) and returns from MPI_Send
(3) fails
As long as buffer space is available, (2) is a reasonable alternative.
An MPI implementation is permitted to copy the message to be sent into internal storage, but it is not required to do so.
What if not enough space is available?
>> In applications communicating large amounts of data, there may not be enough momory (left) in buffers.
>> Until receive starts, no place to store the send message.
>> Practically, (1) results in a serial execution.
A programmer should not assume that the system provides adequate buffering.
Now consider a program executing:
Process 0 | Process 1 |
MPI_Send to process 1 | MPI_Send to process 0 |
MPI_Recv from process 1 | MPI_Recv from process 0 |
Such a program may work in many cases, but it is certain to fail for message of some size that is large enough.
There are some possible solutions:
>> Ordered send and receive - make sure each receive is matched with send in execution order across processes.
>> The aboved matched pairing can be difficult in complex applications. An alternative is to use MPI_Sendrecv. It performs both send and receive such that if no buffering is available, no deadlock will occur.
>> Buffered sends. MPI allows the programmer to provide a buffer into which data can be placed until it is delivered (or at lease left in buffer) via MPI_Bsend.
>> Nonblocking communication. Initiated, then program proceeds while the communication is ongoing, until a check that communication is completed later in the program. IMPORTANT: must make certain data not modified until communication has completed.
Safe programs
>> A program is safe if it will produce correct results even if the system provides no buffering.
>> Need safe programs for portability.
>> Most programmers expect the system to provide some buffering, hence many unsafe MPI programs are around.
>> Write safe programs using matching send with receive, MPI_Sendrecv, allocating own buffers, nonblocking operations.
Nonblocking communications
>> nonblocking communications are useful for overlapping communication with computation, and ensuring safe programs.
>> a nonblocking operation request the MPI library to perform an operation (when it can).
>> nonblocking operations do not wait for any communication events to complete.
>> nonblocking send and receive: return almost immediately
>> can safely modify a send (receive) buffer only after send (receive) is completed.
>> "wait" routines will let program know when a nonblocking operation is done.
Example - Communication between processes in ring topology
>> With blocking communications it is not possible to write a simple code to accomplish this data exchange.
>> For example, if we have MPI_Send first in all processes, program will get stuck as there will be no matching MPI_Recv to send data.
>> Nonblocking communication avoids this problem.
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h" int main(int argc, char *argv[]) {
int numtasks, rank, next, prev, buf[], tag1=, tag2=; tag1=tag2=;
MPI_Request reqs[];
MPI_Status stats[]; MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); prev= rank-;
next= rank+;
if(rank == ) prev= numtasks - ;
if(rank == numtasks-) next= ;
MPI_Irecv(&buf[], , MPI_INT, prev, tag1, MPI_COMM_WORLD, &reqs[]);
MPI_Irecv(&buf[], , MPI_INT, next, tag2, MPI_COMM_WORLD, &reqs[]);
MPI_Isend(&rank, , MPI_INT, prev, tag2, MPI_COMM_WORLD, &reqs[]);
MPI_Isend(&rank, , MPI_INT, next, tag1, MPI_COMM_WORLD, &reqs[]);
MPI_Waitall(, reqs, stats); printf("Task %d communicated with tasks %d & %d\n",rank,prev,next);
MPI_Finalize();
return ;
}
Summary for Nonblocking Communications
>> nonblocking send can be posted whether a matching receive has been posted or not.
>> send is completed when data has been copied out of send buffer.
>> nonblocking send can be matched with blocking receive and vice versa.
>> communications are initiated by sender
>> a communication will generally have lower overhead if a receive buffer is already posted when a sender initiates a communication.
MPI - 缓冲区和非阻塞通信的更多相关文章
- 【MPI学习4】MPI并行程序设计模式:非阻塞通信MPI程序设计
这一章讲了MPI非阻塞通信的原理和一些函数接口,最后再用非阻塞通信方式实现Jacobi迭代,记录学习中的一些知识. (1)阻塞通信与非阻塞通信 阻塞通信调用时,整个程序只能执行通信相关的内容,而无法执 ...
- 用Java实现非阻塞通信
用ServerSocket和Socket来编写服务器程序和客户程序,是Java网络编程的最基本的方式.这些服务器程序或客户程序在运行过程中常常会阻塞.例如当一个线程执行ServerSocket的acc ...
- UE4 Socket多线程非阻塞通信
转自:https://blog.csdn.net/lunweiwangxi3/article/details/50468593 ue4自带的Fsocket用起来依旧不是那么的顺手,感觉超出了我的理解范 ...
- Java NIO Socket 非阻塞通信
相对于非阻塞通信的复杂性,通常客户端并不需要使用非阻塞通信以提高性能,故这里只有服务端使用非阻塞通信方式实现 客户端: package com.test.client; import java.io. ...
- 利用Python中SocketServer 实现客户端与服务器间非阻塞通信
利用SocketServer模块来实现网络客户端与服务器并发连接非阻塞通信 版权声明 本文转自:http://blog.csdn.net/cnmilan/article/details/9664823 ...
- 【python】网络编程-SocketServer 实现客户端与服务器间非阻塞通信
利用SocketServer模块来实现网络客户端与服务器并发连接非阻塞通信.首先,先了解下SocketServer模块中可供使用的类:BaseServer:包含服务器的核心功能与混合(mix-in)类 ...
- 基于MFC的socket编程(异步非阻塞通信)
对于许多初学者来说,网络通信程序的开发,普遍的一个现象就是觉得难以入手.许多概念,诸如:同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)等,初学者往往迷惑不清, ...
- TCP非阻塞通信
一.SelectableChannel SelectableChannel支持阻塞和非阻塞模式的channel 非阻塞模式下的SelectableChannel,读写不会阻塞 SelectableCh ...
- java网络通信之非阻塞通信
java中提供的非阻塞类主要包含在java.nio,包括: 1.ServerSocketChannel:ServerSocket替代类,支持阻塞与非阻塞: 2.SocketChannel:Socket ...
随机推荐
- HDU6128 二次剩余/二次域求二次剩余解/LL快速乘法取模
LINK 题意:求满足模p下$\frac{1}{a_i+a_j}\equiv\frac{1}{a_i}+\frac{1}{a_j}$的对数,其中$n,p(1\leq n\leq10^5,2\leq p ...
- awk是全局周期
需要折行时需要用转译符,转译回车,回车是提交命令 \ 如果你的命令中有单引号也可以 awk 支持C语言 awk '{name[$1]=name[$1]+$2} END{f ...
- 51nod 1217 Minimum Modular
N个不同的数a[1],a[2]...a[n],你可以从中去掉K个数,并且找到一个正整数M,使得剩下的N - K个数,Mod M的结果各不相同,求M的最小值. Input 第1行:2个数N, K,中间用 ...
- 【洛谷 P4134】 [BJOI2012]连连看(费用流)
题目链接 首先是可以\(O(n^2)\)枚举出所有符合要求的点对的,然后考虑建图. 还是拆点把每个点拆成入点和出点,源点连入点,出点连汇点,流量都是1,费用都是0. 然后对于没对符合要求的\((x,y ...
- Python面向对象学习 1 (什么是面向对象,面向对象的应用场景,待更新)
程序设计的三种基本结构: 面向对象,面向过程,函数式编程 1,什么是面向对象编程 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就 ...
- 1-spring xml 和 注解 解析过程
spring mvc 入口 DispatcherServlet,类关系图如下所示 DispatcherServlet 就是一个 Servlet,那Servlet 的初始化方法 init()在哪里,通过 ...
- vsftpd 安装配置详细教程
linux下ftp软件不少,大致特点:<br /> wu-ftp:比较老牌,但针对它的攻击比较多,设置比较麻烦,但功能比较强大.<br /> vsftpd:功能强大,配置也比较 ...
- Mysql 主主复制失败恢复【转】
Mysql 主主复制失败 Mysql 主主复制失败 故障描述 架构信息 节点信息 故障分析 同步AIPPRD2的从环境 同步AIPPRD1的从环境 故障描述 原因描述 因为机柜PDU老化, 导致整个机 ...
- ssh使两台机器建立连接
ssh利用口令建立连接过程: 客户端--> 发送连接请求 --> 远程主机 --> 返回远程主机的公钥 --> 公钥加密客户端私钥+客户端公钥返回远程主机 --> 远程主 ...
- u-boot引导内核过程
目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...