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

上一篇中我们介绍了 mpi4py 中的非阻塞 I/O 操作,下面我们将介绍 mpi4py 中的共享文件指针 I/O 操作。

共享文件指针

每个通过 MPI.File.Open 打开的文件,除了每个进程所拥有的独立文件指针之外,还存在一个全局唯一的为每个进程所共享的共享文件指针。共享文件指针也是以相对于进程当前文件视图的相对位置计算,并且共享文件指针和每个进程的独立文件指针是相互独立,互不影响的。使用共享文件指针 I/O 操作方法要求所有的进程必须使用相同的文件视图,每个进程对共享文件指针读/写方法的一次调用都会将共享文件指针从当前位置移动到加上所读/写数据量的新位置,即各个进程对共享文件指针读/写方法的多次调用的实际效果相当于将这些进程的每次调用按一定的顺序串行化后执行的累加,但对非集合方法调用,这种串行化的顺序是不确定的,如果需要保证某种确定的顺序,则应用程序需使用其他机制,如同步操作等,来保证串行顺序的确定性。共享文件指针的集合操作(包括分步集合操作)访问文件顺序默认为按照进程 rank 从小到大顺序排列,排在后面的进程使用排在前面的进程更新过的共享文件指针访问数据。为防止相同进程再次发起共享文件指针操作打乱当前的执行,MPI 要求只有参加集合操作的所有进程都返回后才可发起下一个共享文件指针操作。这个规定只是从语义上保证共享文件指针操作的顺序性。实际上,具体实现时可以根据进程编号和调用参数计算出每个进程要访问的文件位置,从而可在底层调用显式偏移地址或独立文件指针 I/O 方法使之依然高效地并行执行。

共享文件指针定位操作

打开文件的进程组可以显式地通过 MPI.File.Seek_shared 来移动共享文件指针,其方法接口如下:

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

移动共享文件指针到指定偏移位置 offset。该方法是一个集合操作,根据 whence 参数更新共享文件指针。whence 的可能取值如下:

  • MPI.SEEK_SET:将文件指针设置为指向 offset 参数设置的位置;
  • MPI.SEEK_CUR:将文件指针设置为指向当前指针加上 offset 参数值之后的位置;
  • MPI.SEEK_END:将文件指针设置为指向文件末尾再加上 offset 参数值之后的位置。
MPI.File.Get_position_shared(self)

返回共享文件指针相对当前文件视图的位置(以 etype 为单位)。

使用共享文件指针的读/写操作方法接口如下:

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

非集合操作

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

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

集合操作

使用共享文件指针的阻塞集合操作被命名为 xxxx_ordered 是因为其访问文件的顺序默认是按照进程 rank 从小到大的顺序排列的。

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

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

注意:对阻塞共享文件指针 I/O,没有对应的分步集合操作。

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

非集合操作

非集合的非阻塞共享文件指针操作调用完后会立即返回一个 MPI.Request 对象,可以使用其 Test,Wait 等方法来检测或等待其完成。

MPI.File.Iread_shared(self, buf)

MPI.File.Iwrite_shared(self, buf)

分步集合操作

分步集合的非阻塞共享文件指针操作是一种带有一定限制性的非阻塞 I/O 操作,其限制条件及其使用注意事项基本同上一篇中介绍过的使用独立文件指针和显式偏移地址的分步集合操作。

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,没有对应的真正意义上的集合操作,只有分步集合操作。

例程

下面给出使用例程。

# 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()

运行结果如下:

$ 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 相关的 hints。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容