Example #1
0
def as_shared(source):
    """Convert array to a shared memory array
  
  Arguments
  ---------
  source : array
    The source array to use as template.
  copy : bool
    If True, the data in source is copied.
  order : C', 'F', or None
    The order to use for an array if copied or not a shared array. If None, the order of the source is used.

  Returns
  -------
  array : array
    A shared memory array wrapped as ndarray based on the source array.
  """
    if isinstance(source, (Source, VirtualSource)):
        return source
    elif sma.is_shared(source):
        return Source(array=source)
    elif isinstance(source, (list, tuple, np.ndarray)):
        return Source(array=sma.as_shared(source))
    else:
        raise ValueError('Source %r cannot be transforemd to a shared array!' %
                         source)
Example #2
0
def _test():
    #from importlib import reload
    import numpy as np
    import ClearMap.ParallelProcessing.SharedMemoryArray as sma
    #reload(sma)

    n = 10
    array = sma.zeros(n)
    non_shared = np.zeros(n)

    def propagate(arg):
        i, a = arg
        for j in range(1000):
            array[i] = i

    def propagate_non_shared(arg):
        i, a = arg
        for j in range(1000):
            non_shared[i] = i

    sma.is_shared(non_shared)
    sma.is_shared(array)

    pool = sma.mp.Pool(processes=4)
    pp = pool.map(propagate, zip(range(n), [None] * n))
    print(array)

    pool = sma.mp.Pool(processes=4)
    pp = pool.map(propagate_non_shared, zip(range(n), [None] * n))
    #analysis:ignore
    print(non_shared)
Example #3
0
def _test():
    #from importlib import reload
    #import numpy as np  #analysis:ignore
    import ClearMap.IO.SMA as sma

    n = 10
    array = sma.zeros(n)

    s = sma.Source(array=array)
    print(s)

    v = s.as_virtual()
    print(v)
    s2 = v.as_source()
Example #4
0
 def insert(array):
     self = SharedMemmoryManager.instance()
     # next handle
     self.handle()
     # convert to shared array and insert into handle
     self.arrays[self.current] = sma.as_shared(array)
     # update cnt
     self.count += 1
     return self.current
Example #5
0
 def zeros(shape, dtype=None, order=None):
     self = SharedMemmoryManager.instance()
     self.lock.acquire()
     # next handle
     self.handle()
     # create array in shared memory segment and wrap with numpy
     self.arrays[self.current] = sma.zeros(shape, dtype, order)
     # update cnt
     self.count += 1
     self.lock.release()
     return self.current
Example #6
0
def _shared(shape=None, dtype=None, order=None, array=None, handle=None):
    if handle is not None:
        array = smm.get(handle)

    if array is None:
        return sma.array(shape=shape, dtype=dtype, order=order)

    elif is_shared(array):
        if shape is None and dtype is None and order is None:
            return array

        shape = shape if shape is not None else array.shape
        dtype = dtype if dtype is not None else array.dtype
        order = order if order is not None else npy.order(array)

        if shape != array.shape:
            raise ValueError('Shapes do not match!')

        if np.dtype(dtype) == array.dtype and order == npy.order(array):
            return array
        else:
            new = sma.array(shape=shape, dtype=dtype, order=order)
            new[:] = array
            return new

    elif isinstance(array, (np.ndarray, list, tuple)):
        array = np.asarray(array)

        shape = shape if shape is not None else array.shape
        dtype = dtype if dtype is not None else array.dtype
        order = order if order is not None else npy.order(array)

        if shape != array.shape:
            raise ValueError('Shapes do not match!')

        new = sma.array(shape=shape, dtype=dtype, order=order)
        new[:] = array
        return new

    else:
        raise ValueError('Cannot create shared array from array %r!' % array)
Example #7
0
def is_shared(source):
    """Returns True if array is a shared memory array
  
  Arguments
  ---------
  source : array
    The source array to use as template.
   
  Returns
  -------
  is_shared : bool
    True if the array is a shared memory array.
  """
    if isinstance(source, (Source, VirtualSource)):
        return True
    else:
        return sma.is_shared(source)