MPI聚合通訊之MPI Barrier函式

2021-06-22 05:12:53 字數 2309 閱讀 6590

mpi_barrier函式 用於乙個通訊子中所有程序的同步,呼叫函式時程序將處於等待狀態,直到通訊子中所有程序 都呼叫了該函式後才繼續執行。

如:

#include "stdio.h"

#include "string.h"

#include "mpi.h"

int main(int agc,char *agv)

編譯執行結果為:

process 3: 0

process 3: 1

process 3: 2

process 3: 3

process 3: 4

waiting.....

process 3: 5

process 3: 6

process 3: 7

process 3: 8

process 3: 9

process 0: 0

process 0: 1

process 0: 2

process 0: 3

process 0: 4

waiting.....

process 0: 5

process 0: 6

process 0: 7

process 0: 8

process 0: 9

process 1: 0

process 1: 1

process 1: 2

process 1: 3

process 1: 4

waiting.....

process 1: 5

process 1: 6

process 1: 7

process 1: 8

process 1: 9

process 2: 0

process 2: 1

process 2: 2

process 2: 3

process 2: 4

waiting.....

process 2: 5

process 2: 6

process 2: 7

process 2: 8

process 2: 9

可以看到程序直接將0-9列印出來,中間並沒有等待。現在將mpi_barrier的注釋去掉,即:

#include "stdio.h"

#include "string.h"

#include "mpi.h"

int main(int agc,char *agv)

列印輸出如下:

process 3: 0

process 3: 1

process 3: 2

process 3: 3

process 3: 4

waiting.....

process 0: 0

process 0: 1

process 0: 2

process 0: 3

process 0: 4

waiting.....

process 1: 0

process 1: 1

process 1: 2

process 1: 3

process 1: 4

waiting.....

process 2: 0

process 2: 1

process 2: 2

process 2: 3

process 2: 4

waiting.....

process 1: 5

process 1: 6

process 1: 7

process 1: 8

process 1: 9

process 3: 5

process 3: 6

process 3: 7

process 3: 8

process 3: 9

process 0: 5

process 0: 6

process 0: 7

process 0: 8

process 0: 9

process 2: 5

process 2: 6

process 2: 7

process 2: 8

process 2: 9

可以看出來程序在輸出0-4之後等待其它程序完成列印0-4操作之後,再列印5-9.

MPI聚合通訊之MPI Gather函式

mpi gather sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root comm sendbuf 傳送訊息緩衝區的起始位址 可變 sendcount 傳送訊息緩衝區中的資料個數 整型 sendtype 傳送訊息緩衝區中的資料型別 控...

MPI就緒通訊

mpi就緒通訊類似udp通訊 當我們的阻塞執行緒進行阻塞接受的時候mpi recv的時候 那麼會告訴對方我的執行緒已經在阻塞等待好接受資訊了 傳送資訊方可以毫無顧及的傳送了 mpi rsend 那麼這個時候 傳送忽略三次握手 還有緩衝操作 都可以忽略掉 直接進行傳送 這樣提高了通訊的效率並且優化更加...

平行計算MPI 二 MPI阻塞通訊

阻塞通訊 訊息傳送方的send需要接收方的recv配合才能完成。mpi對阻塞通訊一些底層函式進行了一些封裝,方便我們呼叫。1.標準通訊模式 mpi send buf,buf size,mpi char,process1,tag,mpi comm world mpi recv buf,buf size...