0

I tried to make a file handler that uses shared file from mpi4py for the logging module. But I can't make it 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
hamster on wheels
  • 2,771
  • 17
  • 50

1 Answers1

0

Found a fix:

Python: convert string to 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")
hamster on wheels
  • 2,771
  • 17
  • 50