mpi4py 中 I/O 相关的 hints

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

MPI 允许用户为 MPI 实现传递一些 hints。对 I/O 操作,这是通过一些 I/O 操作方法的 info 参数来进行传递的,这些方法包括 MPI.File.Open,MPI.File.Set_view,MPI.File.Set_info,MPI.File.Delete 等。默认情况下,这些方法的 info 参数都为 MPI.INFO_NULL,表示用户不传递任何 hints 给 MPI 实现。当需要传递一些 hints 时,可通过 MPI.Info 对象为文件指定一些如文件访问模式、文件系统相关的附加信息(即与 I/O 相关的 hints),并利用这些信息优化 I/O 访问性能,减少系统资源占用等。MPI 实现可以选择忽略某些或所有用户传递的 hints 而不会影响程序的正确性。需要注意的是,通过 MPI.Info 对象设置的 hints 的键和值都是字符串。

I/O 相关 hints 方法

MPI.File.Set_info(self, Info info)

通过 MPI.Info 对象向 MPI 实现传递 I/O 相关的 hints。

MPI.File.Get_info(self)

获取当前文件正在使用的 hints,返回值是一个 MPI.Info 对象。

MPI 预留的 I/O 相关 hints

下表列出了 MPI 预留的 I/O 相关的 hints,这些 hints 主要影响并行文件的访问模式,指导文件在 I/O 设备上的布局。

名称类型一致性说明
access_style逗号分隔的字符串指出文件访问方式,可能的取值有:read_once,write-once,read_mostly,write_mostly,sequential,reverse_sequential,random
collective_buffering逻辑(true/false)指示应用程序是否可以使用集合缓冲机制,集合缓冲机制是由集合访问操作提供的优化措施。此机制下,可指定进程组内某个子集的进程独立访问文件。这些进程可利用缓冲区机制把小的磁盘访问组合起来,形成大的访问动作。集合缓冲访问机制还需其他 hints 配合,分别是:cb_block_size,cb_buffer_size,cb_nodes 等
cb_block_size整数指定集合缓冲机制使用的块大小,所有参与集合缓冲机制访问的目标节点(target nodes)都要以该值指定的块大小访问磁盘
cb_buffer_size整数用于集合缓冲机制的总缓冲区大小,通常为 cb_block_size 的整数倍
cb_nodes整数指定参与集合缓冲机制访问的目标节点数
chunked逗号分隔的整数指定访问包含多维数组文件的访问模式,通常为仅访问其中一个子数组。逗号隔开的列表给出数组维数,对 C 语言或 Fortran 语言,列表中的维数次序相反。C 语言中高维在前,低维在后;Fortran 中高维在后,低维在前
chunked_item逗号分隔的整数以字节为单位指定数组各维维数。指定子数组维数,高维在前,低维在后
filename字符串为打开文件指定名称。MPI.File.Open,MPI.File.Set_view,MPI.File.Set_info,MPI.File.Delete 等忽略此关键字
file_perm字符串创建文件时指定许可权限。仅当用于 MPI.File.Open 且其打开模式 amode 包含 MPI.MODE_CREATE 模式时,该关键字才起作用。具体合法值取决于 MPI 版本的定义
io_node_list逗号分隔的字符串指定用于存储文件的 I/O 设备列表。通常在创建文件时使用
nb_proc整数指定可访问文件的并行进程个数。主要用于创建文件时
num_io_nodes整数指定系统中 I/O 设备个数。主要用于创建文件时
striping_factor整数指定文件条块化时一个文件跨设备存储的 I/O 设备个数。仅在创建文件时使用
striping_unit整数文件条块化分割的单位,即在把文件从一个 I/O 设备切换到下一个 I/O 设备时的最大数据块容量。仅在创建文件时使用

不同的 MPI 实现可能还有额外的 hints,用户应该查阅其相关文档以了解其可用的 hints 及这些 hints 的作用。

例程

下面给出使用例程。

# io_hints.py

"""
Demonstrates how to pass I/O related hints to the implementation.

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

import numpy as np
from mpi4py import MPI


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

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

filename = 'temp.txt'

# create a MPI.Info object
info = MPI.Info.Create()
# set some MPI predefined hints
# number of I/O devices across which the file should be striped
info['striping_factor'] = '16'
# the striping unit in bytes
info['striping_unit'] = '1048576'
# buffer size for collective I/O
info['cb_buffer_size'] = '8388608'
# number of processes that should perform disk accesses during collective I/O
info['cb_nodes'] = '%d' % comm.size
# set some additional hints supported by ROMIO
# buffer size for data sieving in independent reads
info['ind_rd_buffer_size'] = '2097152'
# buffer size for data sieving in independent writes
info['ind_wr_buffer_size'] = '1048576'

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

# free the info object
info.Free()

# get the currently used hints
info_used = fh.Get_info()
if rank == 0:
    for k in info_used:
        print '%s: %s' % (k, info_used[k])

# close the file
fh.Close()

运行结果如下:

$ mpiexec -n 4 python io_hints.py
cb_buffer_size: 8388608
romio_cb_read: automatic
romio_cb_write: automatic
cb_nodes: 1
romio_no_indep_rw: false
romio_cb_pfr: disable
romio_cb_fr_types: aar
romio_cb_fr_alignment: 1
romio_cb_ds_threshold: 0
romio_cb_alltoall: automatic
ind_rd_buffer_size: 2097152
ind_wr_buffer_size: 1048576
romio_ds_read: automatic
romio_ds_write: automatic
striping_unit: 1048576
cb_config_list: *:1

以上介绍了 mpi4py 中 I/O 相关的 hints,在下一篇中我们将介绍 mpi4py 中 I/O 操作的一致性语义。

    原文作者:自可乐
    原文地址: https://www.jianshu.com/p/d6949050d3fa
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞