IMTEK-Simulation / NuMPI

Utilities for MPI-parallel numerical calculations with Python
MIT License
2 stars 1 forks source link

or operations not working on MPIStub OpeningModes #16

Closed sannant closed 5 years ago

sannant commented 5 years ago

Here a comparison in the behaviour of

from mpi4py import MPI
from NuMPI import MPIStub
MPI.MODE_RDONLY
Out[4]: 2
MPIStub.MODE_RDONLY
Out[5]: <OpeningModes.MODE_RDONLY: 'r'>
MPI.MODE_WRONLY
Out[6]: 4
MPIStub.MODE_WRONLY
Out[7]: <OpeningModes.MODE_WRONLY: 'a'>
MPI.MODE_CREATE | MPI.MODE_WRONLY 
Out[9]: 5
MPIStub.MODE_CREATE | MPIStub.MODE_WRONLY 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3265, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-9e933743c2b7>", line 1, in <module>
    MPIStub.MODE_CREATE | MPIStub.MODE_WRONLY
TypeError: unsupported operand type(s) for |: 'OpeningModes' and 'OpeningModes'

Combining the modes with the | - Operator is part of MPI3 Standart.

I guess we should make an operator overloading that maps the bitwise or to the concatenation of strings. See

class File(object):
    def __init__(self, comm, filename, amode):
        assert isinstance(comm, Communicator)
        self.file = open(filename, amode.value + 'b')
pastewka commented 5 years ago

Did you encounter a problem somewhere? We should only implement things that we actually need somewhere.

sannant commented 5 years ago

Yes see CI tests:

tests/test_LBFGS_interface.py ...........                                [ 15%]
tests/test_LBFGS_minimization_problems.py ....                           [ 21%]
tests/test_MPI_LBFGS.py .......s                                         [ 32%]
tests/test_ParallelNumpy.py ................                             [ 54%]
tests/test_io.py xFFFFFFFxFx                                             [ 70%]
tests/test_minimization_problems.py ..............                       [ 90%]
tests/test_simple_LBFGS.py .......                                       [100%]
=================================== FAILURES ===================================
_______________________ test_FileSave_2D[make_2d_slab_x] _______________________
decompfun = <function make_2d_slab_x at 0x7f8c915227b8>
comm = <NuMPI.MPIStub.Communicator object at 0x7f8c9d5da278>
globaldata = array([[0.4359949 , 0.02592623, 0.54966248, ..., 0.27405925, 0.01025004,
        0.62935972],
       [0.29517231, 0.18...23,
        0.79000339],
       [0.41333251, 0.63899521, 0.95422417, ..., 0.52988765, 0.09137043,
        0.52526125]])
    @pytest.mark.parametrize("decompfun",[make_2d_slab_x, make_2d_slab_y])
    def test_FileSave_2D(decompfun, comm, globaldata):
        distdata = decompfun(comm, globaldata)

        save_npy("test_Filesave_2D.npy",
                 distdata.data,
                 distdata.subdomain_location,
>                distdata.domain_resolution, comm)
tests/test_io.py:159: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
fn = 'test_Filesave_2D.npy'
data = array([[0.4359949 , 0.02592623, 0.54966248, ..., 0.27405925, 0.01025004,
        0.62935972],
       [0.29517231, 0.18...23,
        0.79000339],
       [0.41333251, 0.63899521, 0.95422417, ..., 0.52988765, 0.09137043,
        0.52526125]])
subdomain_location = [0, 0], resolution = (128, 128)
comm = <NuMPI.MPIStub.Communicator object at 0x7f8c9d5da278>
    def save_npy(fn, data, subdomain_location=None, resolution=None, comm=MPI.COMM_WORLD):
        """

        Parameters
        ----------
        data : numpy array : data owned by the processor
        location : index of the first element of data within the global data
        resolution : resolution of the global data
        comm : MPI communicator

        Returns
        -------

        """
        if len(data.shape) != 2: raise ValueError
        subdomain_resolution = data.shape
        if subdomain_location is None:
            subdomain_location = (0,0)
        if resolution is None:
            resolution = subdomain_resolution

        from numpy.lib.format import dtype_to_descr, magic
        magic_str = magic(1, 0)

        arr_dict_str = str({'descr': dtype_to_descr(data.dtype),
                            'fortran_order': False,
                            'shape': resolution})

        while (len(arr_dict_str) + len(magic_str) + 2) % 16 != 15:
            arr_dict_str += ' '
        arr_dict_str += '\n'
        header_len = len(arr_dict_str) + len(magic_str) + 2

>       file = MPI.File.Open(comm, fn, MPI.MODE_CREATE | MPI.MODE_WRONLY)
E       TypeError: unsupported operand type(s) for |: 'OpeningModes' and 'OpeningModes'