python - Cannot write to shared MPI file with mpi4py -
i tried make file handler uses shared file mpi4py
logging
module. can't make write.
program:
from mpi4py import mpi import io class mpilogfile(object): def __init__(self, comm, filename, mode): self.file_handle = mpi.file.open(comm, filename, mode) self.file_handle.set_atomicity(true) self.buffer = io.stringio() def write(self, msg): print("msg type:", type(msg)) self.buffer.write(msg) self.file_handle.write_shared(self.buffer) def close(self): self.file_handle.sync() self.file_handle.close() comm = mpi.comm_world logfile = mpilogfile( comm, "test.log", mpi.mode_wronly | mpi.mode_create | mpi.mode_append ) logfile.write("hello")
error:
traceback (most recent call last): file "test_mpi.py", line 21, in <module> logfile.write("hello") file "test_mpi.py", line 13, in write self.file_handle.write_shared(self.buffer) file "mpi/file.pyx", line 438, in mpi4py.mpi.file.write_shared (src/mpi4py.mpi.c:138222) file "mpi/msgbuffer.pxi", line 1044, in mpi4py.mpi.message_io_write (src/mpi4py.mpi.c:39788) file "mpi/msgbuffer.pxi", line 1030, in mpi4py.mpi._p_msg_io.for_write (src/mpi4py.mpi.c:39639) file "mpi/msgbuffer.pxi", line 143, in mpi4py.mpi.message_simple (src/mpi4py.mpi.c:30601) typeerror: message: expecting buffer or list/tuple
found fix:
python: convert string byte array
https://github.com/mpi4py/mpi4py/blob/master/test/test_io.py
from mpi4py import mpi import array class mpilogfile(object): def __init__(self, comm, filename, mode): self.file_handle = mpi.file.open(comm, filename, mode) self.file_handle.set_atomicity(true) self.buffer = bytearray def write(self, msg): b = bytearray() b.extend(map(ord, msg)) self.file_handle.write_shared(b) def close(self): self.file_handle.sync() self.file_handle.close() comm = mpi.comm_world logfile = mpilogfile( comm, "test.log", mpi.mode_wronly | mpi.mode_create | mpi.mode_append ) logfile.write("hello")
Comments
Post a Comment