在上一篇中我們介紹了 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。