在上一篇中我們介紹了 mpi4py 中的錯(cuò)誤處理囚痴,下面我們將介紹 mpi4py 中的 info 和 assertion。
MPI 允許用戶提供兩種類型的與應(yīng)用有關(guān)的信息:hints 和 assertions审葬。hints 是應(yīng)用中所做的有助于提高程序性能的選項(xiàng)深滚,MPI 實(shí)現(xiàn)可以忽略用戶提供的 hints 而不影響程序的正確性;assertions 是用戶(編程者)給 MPI 實(shí)現(xiàn)的有關(guān)其應(yīng)用程序行為的保證——如果程序給出的某個(gè) assertion 并不正確涣觉,MPI 實(shí)現(xiàn)可能會(huì)產(chǎn)生錯(cuò)誤的結(jié)果痴荐。在 MPI 中大多數(shù)的 hints 都是通過 MPI.Info 對(duì)象傳遞給 MPI 實(shí)現(xiàn)的,而 assertions 則是傳遞給一些 MPI 函數(shù)/方法的整數(shù)值官册。用戶可以不提供 hints 或 assertions生兆,應(yīng)用程序可以在不提供它們的情況下正確運(yùn)行,但是膝宁,提供合適的 hints 或正確的 assertions 可以提高應(yīng)用程序的性能鸦难。hints 和 assetions 都是可移植的。除了 MPI 標(biāo)準(zhǔn)提供的 hints 外员淫,不同的 MPI 實(shí)現(xiàn)可能會(huì)定義額外的不同的 hints合蔽,用戶應(yīng)該查看相關(guān)的文檔以了解可用的 hints 及其作用。
MPI-2 引進(jìn)了 Info 對(duì)象介返,MPI 中的很多函數(shù)都使用了 info 參數(shù)拴事,Info 參數(shù)以 <key沃斤,value> 字符串二元組的形式定義信息。MPI 本身也使用一些預(yù)留的關(guān)鍵字來(lái)保存環(huán)境與外部交互刃宵,以及其他一些屬性信息衡瓶。關(guān)鍵字和值分別由 MPI.MAX_INFO_KEY 和 MPI.MAX_INFO_VAL 定義上限。
hints 和 Info 對(duì)象
一般接口
MPI.Info.Create(type cls)
創(chuàng)建一個(gè) Info 對(duì)象牲证,將其 <key哮针,value> 二元組初始化為空。
MPI.Info.Dup(self)
復(fù)制當(dāng)前 Info 對(duì)象从隆,包括其中的 <key,value> 二元組缭裆。
MPI.Info.Delete(self, key)
從當(dāng)前 Info 對(duì)象中刪除 <key键闺,value> 二元組,如果 key
參數(shù)指定的關(guān)鍵字不存在澈驼,則會(huì)拋出 MPI.Exception 異常辛燥。
MPI.Info.Free(self)
刪除并釋放當(dāng)前 Info 對(duì)象,將其值設(shè)置為 MPI.INFO_NULL缝其。
MPI.Info.Set(self, key, value)
為當(dāng)前 Info 對(duì)象設(shè)置二元組 <key挎塌,value>,如果 key
所指定的關(guān)鍵字已經(jīng)存在内边,則覆蓋其原有的值榴都。如果 key
或 value
參數(shù)的字符串長(zhǎng)度超過上限,則會(huì)拋出 MPI.Exception 異常漠其。
MPI.Info.Get(self, key, int maxlen=-1)
從當(dāng)前 Info 對(duì)象中獲取關(guān)鍵字 key
的值嘴高,如果該關(guān)鍵字不存在,則返回 None和屎。maxlen
是獲取的值的最大長(zhǎng)度拴驮,如果該長(zhǎng)度小于值的實(shí)際長(zhǎng)度,則返回的是被截?cái)嗟闹担?code>maxlen 如果為小于 0 或大于 MPI.MAX_INFO_VAL柴信,則會(huì)使用 MPI.MAX_INFO_VAL套啤。
MPI.Info.Get_nkeys(self)
返回當(dāng)前 Info 對(duì)象中定義的關(guān)鍵字總數(shù)。
MPI.Info.Get_nthkey(self, int n)
返回當(dāng)前 Info 對(duì)象中第 n
個(gè)關(guān)鍵字随常,關(guān)鍵字編號(hào)從 0 開始潜沦。
類似與字典的接口
MPI.Info.__len__(self)
使用 len 函數(shù)獲取當(dāng)前 Info 對(duì)象中定義的關(guān)鍵字總數(shù)。
MPI.Info.__contains__(self, object key)
使用 in 來(lái)測(cè)試當(dāng)前 Info 對(duì)象中是否包含 key
關(guān)鍵字绪氛。
MPI.Info.__iter__(self)
迭代器接口止潮。
MPI.Info.__getitem__(self, object key)
使用 info[key] 獲取 key
對(duì)應(yīng)的值。
MPI.Info.__setitem__(self, object key, object value)
使用 info[key] = value 的方式為當(dāng)前 Info 對(duì)象設(shè)置 <key钞楼,value> 二元組喇闸。
MPI.Info.__delitem__(self, object key)
使用 del info[key] 的方式從當(dāng)前 Info 對(duì)象中刪除 <key,value> 二元組。
MPI.Info.get(self, object key, object default=None)
獲取當(dāng)前 Info 對(duì)象的 key
關(guān)鍵字對(duì)應(yīng)的值燃乍,如果 key
不存在唆樊,則返回 default
。
MPI.Info.keys(self)
返回當(dāng)前 Info 對(duì)象的所有關(guān)鍵字刻蟹,結(jié)果是一個(gè) list逗旁。
MPI.Info.values(self)
返回當(dāng)前 Info 對(duì)象的所有值,結(jié)果是一個(gè) list舆瘪。
MPI.Info.items(self)
返回當(dāng)前 Info 對(duì)象的所有 <key片效,value> 二元組,結(jié)果是一個(gè) list英古,其每一個(gè)元素為一個(gè)二元 tuple淀衣。
MPI.Info.update(self, other=(), **kwds)
以 other
和 kwds
中的 key 和 value 來(lái)更新當(dāng)前 Info 對(duì)象的關(guān)鍵字和值,類似于 Python 字典的 update 操作召调。
MPI.Info.copy(self)
復(fù)制當(dāng)前 Info 對(duì)象膨桥,包括其中的 <key,value> 二元組唠叛。同 MPI.Info.Dup只嚣。
MPI.Info.clear(self)
刪除當(dāng)前 Info 對(duì)象中的所有 <key,value> 二元組艺沼。
MPI-3 新方法
因?yàn)?Info 對(duì)象可以潛在地幫助 MPI 實(shí)現(xiàn)提供更高的性能册舞,MPI-3 中新增了若干與 Info 相關(guān)的方法,允許設(shè)置和獲取通信子障般,窗口以及文件句柄的 Info环础。另外,MPI-3 中增加一個(gè)新的通信子復(fù)制方法 MPI.Comm.Dup_with_info剩拢,該方法與 MPI.Comm.Dup 的作用類似线得,不過允許在復(fù)制通信子對(duì)象的時(shí)候?yàn)槠湓O(shè)置一個(gè)新的 Info 對(duì)象,而不是將原通信子的 Info 對(duì)象也一并復(fù)制過來(lái)徐伐。該方法對(duì) MPI 庫(kù)的開發(fā)非常有用贯钩,因?yàn)橥ㄐ抛訌?fù)制方法通常被庫(kù)的開發(fā)者用來(lái)在庫(kù)內(nèi)部創(chuàng)建一個(gè)私有的通信子,以防止庫(kù)內(nèi)的通信操作對(duì)外部的通信產(chǎn)生干擾办素,庫(kù)在復(fù)制通信子的過程中可能并不需要將用戶設(shè)置在原通信子上的某些 info 鍵值對(duì)也一并復(fù)制過去角雷,因此可以使用 MPI.Comm.Dup_with_info 在復(fù)制的過程中為其設(shè)置一個(gè)新的庫(kù)所需要的 Info 對(duì)象。下面是這些新方法的使用接口性穿。注意:在 mpi4py 中勺三,MPI.Comm.Dup 方法在傳遞一個(gè)有效的 info
參數(shù)時(shí)等價(jià)于 MPI.Comm.Dup_with_info。
通信子
MPI.Comm.Dup(self, Info info=None)
MPI.Comm.Dup_with_info(self, Info info)
MPI.Comm.Set_info(self, Info info)
MPI.Comm.Get_info(self)
窗口
MPI.Win.Set_info(self, Info info)
MPI.Win.Get_info(self)
文件
MPI.File.Set_info(self, Info info)
MPI.File.Get_info(self)
assertions
MPI 中需曾,assertions 主要使用在單邊通信的相關(guān)操作中吗坚,比如下面的一些方法接口:
MPI.Win.Fence(self, int assertion=0)
MPI.Win.Lock(self, int rank, int lock_type=LOCK_EXCLUSIVE, int assertion=0)
MPI.Win.Lock_all(self, int assertion=0)
MPI.Win.Post(self, Group group, int assertion=0)
MPI.Win.Start(self, Group group, int assertion=0)
這些方法及其 assertion 參數(shù)的作用和可能取值在單邊通信和MPI-3 中增強(qiáng)的單邊通信中作過相應(yīng)的介紹祈远。
例程
下面給出使用例程。
# info.py
"""
Demonstrates the usage of info and assertion.
Run this with 1 processes like:
$ mpiexec -n 1 python info.py
or
$ python info.py
"""
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
# create an info object
info = MPI.Info.Create()
# dupicate the info
info1 = info.Dup()
# free the duplicated info
info1.Free()
# becomes MPI.INFO_NULL after the free op
assert info1 == MPI.INFO_NULL
try:
# try to delete a non-existed key from info
info.Delete('a')
except MPI.Exception as e:
print e.error_string
# set key and value
info.Set('k1', 'v1')
info.Set('k2', 'v2')
info.Set('k3', 'v3')
print 'k1:', info.Get('k1')
print 'nkeys:', info.Get_nkeys()
print 'the second key:', info.Get_nthkey(1)
try:
# try to set a key with length > MPI.MAX_INFO_KEY
info.Set('k' * (MPI.MAX_INFO_KEY + 1), 'v')
except MPI.Exception as e:
print e.error_string
try:
# try to set a value with length > MPI.MAX_INFO_VAL
info.Set('k', 'v' * (MPI.MAX_INFO_VAL + 1))
except MPI.Exception as e:
print e.error_string
# dict interface
print 'len(info):', len(info)
print 'k1 in info:', 'k1' in info
# __iter__ method
for k in info:
print k
info['k4'] = 'v4'
print 'k4:', info['k4']
del info['k4']
print 'k4:', info.get('k4', 'v4_new')
print 'keys:', info.keys()
print 'values:', info.values()
print 'items:', info.items()
info.update({'k1': 'v1_new', 'k5': 'k5_new'})
print 'items after update:', info.items()
info.clear()
print 'items after clea:', info.items()
# info with comm
comm_info = MPI.Info.Create()
comm.Set_info(comm_info)
comm.Get_info()
info_dup = MPI.Info.Create()
comm.Dup_with_info(info_dup)
運(yùn)行結(jié)果如下:
$ python info.py
MPI_ERR_INFO_NOKEY: unknown key for given info object
k1: v1
nkeys: 3
the second key: k2
MPI_ERR_INFO_KEY: invalid key argument for info object
MPI_ERR_INFO_VALUE: invalid value argument for info object
len(info): 3
k1 in info: True
k1
k2
k3
k4: v4
k4: v4_new
keys: ['k1', 'k2', 'k3']
values: ['v1', 'v2', 'v3']
items: [('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')]
items after update: [('k1', 'v1_new'), ('k2', 'v2'), ('k3', 'v3'), ('k5', 'k5_new')]
items after clea: []
以上介紹了 mpi4py 中的 info 和 assertion商源,在下一篇中我們將介紹 mpi4py 中的 Status 對(duì)象车份。