2016-07-22 132 views
0
#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個核心進行了播客。當我跑它,我得到的錯誤:mpi4py傳遞字典對象

-------------------- 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'] 

它在我看來,這得到了傳遞到根以外的核心字典已經失去了一些功能的字典。爲什麼是這樣?我應該如何解決這個問題,並在通向根節點的路徑上進行傳遞?

在此先感謝!

回答

0

我對python或mpi4py沒有太多瞭解,但我在https://pythonhosted.org/mpi4py/usrman/tutorial.html處發現了一些代碼,這意味着您需要將comm.bcast的結果分配給其他等級的字典。

的代碼應該是

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

也許這能解決問題嗎?

+0

解決了這個問題!謝謝! –