mpi4py 传递 dict 对象

mpi4py passing dict object

#mpiexec -n 3 python pass_dict.py
from mpi4py import MPI
import psycopg2
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

tax_dict={}
if rank == 0:
    tax_files=['2008','2009','2011','2012','2013','2014','2015']
    file_dir='/zhome/nah316/taxonomy/'
    for tax_file in tax_files:
        filename=file_dir + tax_file+'.csv'
        with open(filename,'r') as f:
            temp={}
            for line in f:
                temp_list=[]
                splitted_line = line.split()
                tag=splitted_line[1]
                temp_list.append(tag)
                temp[splitted_line[1]] = temp_list
            tax_dict[tax_file]=temp
else:
    tax_dict=None

comm.bcast(tax_dict, root = 0)

print  '-' * 20, rank , '-'* 30
print tax_dict['2015']['InvestmentSoldNotYetPurchasedRestrictedCost']    

这是我尝试构建一个字典的字典,并将其在通信器上广播到其他 2 个核心的代码。当我 运行 它时,我得到错误:

-------------------- 2 ------------------------------
Traceback (most recent call last):
  File "pass_dict.py", line 33, in <module>
    print tax_dict['2015']['InvestmentSoldNotYetPurchasedRestrictedCost']
TypeError: 'NoneType' object has no attribute '__getitem__'
-------------------- 1 ------------------------------
Traceback (most recent call last):
  File "pass_dict.py", line 33, in <module>
    print tax_dict['2015']['InvestmentSoldNotYetPurchasedRestrictedCost']
TypeError: 'NoneType' object has no attribute '__getitem__'
-------------------- 0 ------------------------------
['InvestmentSoldNotYetPurchasedRestrictedCost']

在我看来,传递给核心而不是根的字典已经失去了一些作为字典的功能。为什么是这样?我应该如何解决这个问题并在根节点上进行传递?

提前致谢!

我不太了解 python 或 mpi4py 的详细信息,但我在 https://mpi4py.scipy.org/docs/usrman/tutorial.html 找到了一些代码,这意味着您需要将 comm.bcast 的结果分配给其他等级的词典。

代码应该是

tax_dict = comm.bcast(tax_dict, root = 0)

也许这能解决问题?