mpi4py 中的共享文件指針 I/O 操作

上一篇中我們介紹了 mpi4py 中的非阻塞 I/O 操作屏轰,下面我們將介紹 mpi4py 中的共享文件指針 I/O 操作拯坟。

共享文件指針

每個(gè)通過 MPI.File.Open 打開的文件洞就,除了每個(gè)進(jìn)程所擁有的獨(dú)立文件指針之外奖年,還存在一個(gè)全局唯一的為每個(gè)進(jìn)程所共享的共享文件指針顾犹。共享文件指針也是以相對于進(jìn)程當(dāng)前文件視圖的相對位置計(jì)算腺劣,并且共享文件指針和每個(gè)進(jìn)程的獨(dú)立文件指針是相互獨(dú)立绿贞,互不影響的。使用共享文件指針 I/O 操作方法要求所有的進(jìn)程必須使用相同的文件視圖橘原,每個(gè)進(jìn)程對共享文件指針讀/寫方法的一次調(diào)用都會(huì)將共享文件指針從當(dāng)前位置移動(dòng)到加上所讀/寫數(shù)據(jù)量的新位置籍铁,即各個(gè)進(jìn)程對共享文件指針讀/寫方法的多次調(diào)用的實(shí)際效果相當(dāng)于將這些進(jìn)程的每次調(diào)用按一定的順序串行化后執(zhí)行的累加,但對非集合方法調(diào)用靠柑,這種串行化的順序是不確定的寨辩,如果需要保證某種確定的順序,則應(yīng)用程序需使用其他機(jī)制歼冰,如同步操作等靡狞,來保證串行順序的確定性。共享文件指針的集合操作(包括分步集合操作)訪問文件順序默認(rèn)為按照進(jìn)程 rank 從小到大順序排列隔嫡,排在后面的進(jìn)程使用排在前面的進(jìn)程更新過的共享文件指針訪問數(shù)據(jù)甸怕。為防止相同進(jìn)程再次發(fā)起共享文件指針操作打亂當(dāng)前的執(zhí)行,MPI 要求只有參加集合操作的所有進(jìn)程都返回后才可發(fā)起下一個(gè)共享文件指針操作腮恩。這個(gè)規(guī)定只是從語義上保證共享文件指針操作的順序性梢杭。實(shí)際上,具體實(shí)現(xiàn)時(shí)可以根據(jù)進(jìn)程編號(hào)和調(diào)用參數(shù)計(jì)算出每個(gè)進(jìn)程要訪問的文件位置秸滴,從而可在底層調(diào)用顯式偏移地址或獨(dú)立文件指針 I/O 方法使之依然高效地并行執(zhí)行武契。

共享文件指針定位操作

打開文件的進(jìn)程組可以顯式地通過 MPI.File.Seek_shared 來移動(dòng)共享文件指針,其方法接口如下:

MPI.File.Seek_shared(self, Offset offset, int whence=SEEK_SET)

移動(dòng)共享文件指針到指定偏移位置 offset。該方法是一個(gè)集合操作咒唆,根據(jù) whence 參數(shù)更新共享文件指針届垫。whence 的可能取值如下:

  • MPI.SEEK_SET:將文件指針設(shè)置為指向 offset 參數(shù)設(shè)置的位置;
  • MPI.SEEK_CUR:將文件指針設(shè)置為指向當(dāng)前指針加上 offset 參數(shù)值之后的位置全释;
  • MPI.SEEK_END:將文件指針設(shè)置為指向文件末尾再加上 offset 參數(shù)值之后的位置装处。
MPI.File.Get_position_shared(self)

返回共享文件指針相對當(dāng)前文件視圖的位置(以 etype 為單位)。

使用共享文件指針的讀/寫操作方法接口如下:

阻塞共享文件指針 I/O 操作

非集合操作

MPI.File.Read_shared(self, buf, Status status=None)

MPI.File.Write_shared(self, buf, Status status=None)

集合操作

使用共享文件指針的阻塞集合操作被命名為 xxxx_ordered 是因?yàn)槠湓L問文件的順序默認(rèn)是按照進(jìn)程 rank 從小到大的順序排列的浸船。

MPI.File.Read_ordered(self, buf, Status status=None)

MPI.File.Write_ordered(self, buf, Status status=None)

注意:對阻塞共享文件指針 I/O妄迁,沒有對應(yīng)的分步集合操作。

非阻塞共享文件指針 I/O 操作

非集合操作

非集合的非阻塞共享文件指針操作調(diào)用完后會(huì)立即返回一個(gè) MPI.Request 對象李命,可以使用其 Test登淘,Wait 等方法來檢測或等待其完成。

MPI.File.Iread_shared(self, buf)

MPI.File.Iwrite_shared(self, buf)

分步集合操作

分步集合的非阻塞共享文件指針操作是一種帶有一定限制性的非阻塞 I/O 操作项戴,其限制條件及其使用注意事項(xiàng)基本同上一篇中介紹過的使用獨(dú)立文件指針和顯式偏移地址的分步集合操作形帮。

MPI.File.Read_ordered_begin(self, buf)

MPI.File.Read_ordered_end(self, buf, Status status=None)

MPI.File.Write_ordered_begin(self, buf)

MPI.File.Write_ordered_end(self, buf, Status status=None)

注意:對非阻塞共享文件指針 I/O,沒有對應(yīng)的真正意義上的集合操作周叮,只有分步集合操作。

例程

下面給出使用例程界斜。

# shared_io.py

"""
Demonstrates the usage of I/O with shared file pointers.

Run this with 4 processes like:
$ mpiexec -n 4 python shared_io.py
"""

import numpy as np
from mpi4py import MPI


comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

buf = np.full((5,), rank, dtype='i')

filename = 'temp.txt'

# the etype
etype = MPI.INT
filetype = MPI.INT

# -------------------------------------------------------------------------------
# use blocking non-collective shared file pointer I/O

# open the file for read and write, create it if it does not exist,
# and delete it on close
fh = MPI.File.Open(comm, filename, amode= MPI.MODE_CREATE | MPI.MODE_RDWR | MPI.MODE_DELETE_ON_CLOSE)

# set the file view
fh.Set_view(0, etype, filetype)

# each process writes buf to file by using blocking non-collective
# shared file pointer write
# there is usually no oreder of the write in this case
print 'rank %d writes %s to file' % (rank, buf)
fh.Write_shared(buf)

# synchronize here to make sure all processes have done the write
comm.barrier()

# reset the shared file pointer
fh.Seek_shared(0)

# check what's in the file
if rank == 0:
    buf1 = np.zeros(5 * size, dtype='i')
    fh.Read_shared(buf1)
    # fh.Read(buf1)
    # fh.Read_at(0, buf1)
    print 'data in the file with Write_shared: %s' % buf1

# close the file
fh.Close()


# # -------------------------------------------------------------------------------
# use blocking collective shared file pointer I/O

# open the file for read and write, create it if it does not exist,
# and delete it on close
fh = MPI.File.Open(comm, filename, amode= MPI.MODE_CREATE | MPI.MODE_RDWR | MPI.MODE_DELETE_ON_CLOSE)

# set the file view
fh.Set_view(0, etype, filetype)

# each process writes buf to file by using blocking collective
# shared file pointer write
# data will be writen in the order of the rank in this case
print 'rank %d writes %s to file' % (rank, buf)
fh.Write_ordered(buf)

# no need barrier synchronizition when use collective write
# comm.barrier()

# reset the shared file pointer
fh.Seek_shared(0)

# check what's in the file
if rank == 0:
    buf1 = np.zeros(5 * size, dtype='i')
    fh.Read_shared(buf1)
    # fh.Read(buf1)
    # fh.Read_at(0, buf1)
    print 'data in the file with Write_ordered: %s' % buf1

# close the file
fh.Close()

# -------------------------------------------------------------------------------
# use nonblocking split collective shared file pointer I/O

# open the file for read and write, create it if it does not exist,
# and delete it on close
fh = MPI.File.Open(comm, filename, amode= MPI.MODE_CREATE | MPI.MODE_RDWR | MPI.MODE_DELETE_ON_CLOSE)

# set the file view
fh.Set_view(0, etype, filetype)

# each process writes buf to file by using nonblocking split collective
# shared file pointer write
# data will be writen in the order of the rank in this case
print 'rank %d writes %s to file' % (rank, buf)
fh.Write_ordered_begin(buf)

# can do some computatin/communication here
for i in range(10):
    pass

fh.Write_ordered_end(buf)

# no need barrier synchronizition when use collective write
# comm.barrier()

# reset the shared file pointer
fh.Seek_shared(0)

# check what's in the file
if rank == 0:
    buf1 = np.zeros(5 * size, dtype='i')
    fh.Read_shared(buf1)
    # fh.Read(buf1)
    # fh.Read_at(0, buf1)
    print 'data in the file with Write_ordered_begin and Write_ordered_end: %s' % buf1

# reset the shared file pointer
fh.Seek_shared(0)

# check with Read_ordered_begin and Read_ordered_end
buf2 = np.zeros_like(buf)
fh.Read_ordered_begin(buf2)
fh.Read_ordered_end(buf2)
assert np.allclose(buf, buf2)

# close the file
fh.Close()

運(yùn)行結(jié)果如下:

$ mpiexec -n 4 python shared_io.py
rank 1 writes [1 1 1 1 1] to file
rank 2 writes [2 2 2 2 2] to file
rank 3 writes [3 3 3 3 3] to file
rank 0 writes [0 0 0 0 0] to file
data in the file with Write_shared: [3 3 3 3 3 2 2 2 2 2 0 0 0 0 0 1 1 1 1 1]
rank 0 writes [0 0 0 0 0] to file
rank 2 writes [2 2 2 2 2] to file
rank 3 writes [3 3 3 3 3] to file
rank 1 writes [1 1 1 1 1] to file
data in the file with Write_ordered: [0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]
rank 2 writes [2 2 2 2 2] to file
rank 0 writes [0 0 0 0 0] to file
rank 1 writes [1 1 1 1 1] to file
rank 3 writes [3 3 3 3 3] to file
data in the file with Write_ordered_begin and Write_ordered_end: [0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]

以上介紹了 mpi4py 中的共享文件指針 I/O 操作仿耽,在下一篇中我們將介紹 mpi4py 中 I/O 相關(guān)的 hints。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末各薇,一起剝皮案震驚了整個(gè)濱河市项贺,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌峭判,老刑警劉巖开缎,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異林螃,居然都是意外死亡奕删,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門疗认,熙熙樓的掌柜王于貴愁眉苦臉地迎上來完残,“玉大人,你說我怎么就攤上這事横漏〗魃瑁” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵缎浇,是天一觀的道長扎拣。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么二蓝? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任尊蚁,我火速辦了婚禮,結(jié)果婚禮上侣夷,老公的妹妹穿的比我還像新娘横朋。我一直安慰自己,他們只是感情好百拓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布琴锭。 她就那樣靜靜地躺著,像睡著了一般衙传。 火紅的嫁衣襯著肌膚如雪决帖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天蓖捶,我揣著相機(jī)與錄音地回,去河邊找鬼。 笑死俊鱼,一個(gè)胖子當(dāng)著我的面吹牛刻像,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播并闲,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼细睡,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了帝火?” 一聲冷哼從身側(cè)響起溜徙,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎犀填,沒想到半個(gè)月后蠢壹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡九巡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年图贸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片比庄。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡求妹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出佳窑,到底是詐尸還是另有隱情制恍,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布神凑,位于F島的核電站净神,受9級(jí)特大地震影響何吝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鹃唯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一爱榕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧坡慌,春花似錦黔酥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至熄求,卻和暖如春渣玲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背弟晚。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國打工忘衍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人卿城。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓枚钓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親藻雪。 傳聞我的和親對象是個(gè)殘疾皇子秘噪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容