我正在运行以下代码来使用mpi_bcast测试广播文件。基本上,我在根目录创建了一个文本文件“ test.txt”,并广播到其他节点。
import json
import numpy as np
import multiprocessing
import time
import sys
import os
import math
from scipy.special import comb
import itertools
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
with open('test.txt', 'w') as f:
for x in range(50):
f.write('{}\n'.format(x))
data = open('test.txt', 'r')
print(rank)
else:
data = None
data = comm.bcast(data, root=0)
if rank != 0:
f_out = open('test.txt', 'w')
for i, line in enumerate(data):
f_out.write(line)
f_out.close()
print('rank',rank)
然后,当我在3个进程上运行mpiexec -n 3 python3 test.py时,出现以下错误消息。
回溯(最近通话最近一次):
File "test.py", line 26, in <module>
data = comm.bcast(data, root=0)
File "mpi4py/MPI/Comm.pyx", line 1257, in mpi4py.MPI.Comm.bcast
File "mpi4py/MPI/msgpickle.pxi", line 629, in mpi4py.MPI.PyMPI_bcast
File "mpi4py/MPI/msgpickle.pxi", line 104, in mpi4py.MPI.Pickle.dump
File "mpi4py/MPI/msgpickle.pxi", line 91, in mpi4py.MPI.Pickle.cdumps
TypeError: cannot serialize '_io.TextIOWrapper' object
我认为该错误发生在以下行:data = comm.bcast(data, root=0)
。
是因为无法将文件对象传递给bcast函数吗?如何解决此错误?