2017-08-14 19 views
0

loggingモジュールの共有ファイルを使用するファイルハンドラをmpi4pyにしようとしました。しかし、私はそれを書くことはできません。mpi4pyで共有MPIファイルに書き込めません

プログラム:

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") 

エラー:

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 

答えて

0

見つかり修正:

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") 
関連する問題