当前位置: 首页>>代码示例>>Python>>正文


Python multiprocessing.RawArray方法代码示例

本文整理汇总了Python中multiprocessing.RawArray方法的典型用法代码示例。如果您正苦于以下问题:Python multiprocessing.RawArray方法的具体用法?Python multiprocessing.RawArray怎么用?Python multiprocessing.RawArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在multiprocessing的用法示例。


在下文中一共展示了multiprocessing.RawArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: extract_params_as_shared_arrays

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def extract_params_as_shared_arrays(link):
    assert isinstance(link, chainer.Link)
    shared_arrays = {}
    for param_name, param in link.namedparams():
        typecode = param.array.dtype.char
        shared_arrays[param_name] = mp.RawArray(typecode, param.array.ravel())

    for persistent_name, persistent in chainerrl.misc.namedpersistent(link):
        if isinstance(persistent, np.ndarray):
            typecode = persistent.dtype.char
            shared_arrays[persistent_name] = mp.RawArray(
                typecode, persistent.ravel())
        else:
            assert np.isscalar(persistent)
            # Wrap by a 1-dim array because multiprocessing.RawArray does not
            # accept a 0-dim array.
            persistent_as_array = np.asarray([persistent])
            typecode = persistent_as_array.dtype.char
            shared_arrays[persistent_name] = mp.RawArray(
                typecode, persistent_as_array)
    return shared_arrays 
开发者ID:chainer,项目名称:chainerrl,代码行数:23,代码来源:async_.py

示例2: borrow_all_memories

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def borrow_all_memories(shared_params, memshared_instances):
    """
    Run theano_borrow_memory on a list of params and shared memory
    sharedctypes.

    Parameters
    ----------
    shared_params : list
        of :class:`theano.tensor.sharedvar.TensorSharedVariable`
        the Theano shared variable where
        shared memory should be used instead.
    memshared_instances : dict of tuples
        of :class:`multiprocessing.RawArray` and their shapes
        the memory shared across processes (e.g.from `memshare_sparams`)

    Notes
    -----
    Same as `borrow_memory` but for lists of shared memories and
    theano variables. See `borrow_memory`
    """
    for sparam in shared_params:
        borrow_memory(sparam, *memshared_instances[sparam.name]) 
开发者ID:hvasbath,项目名称:beat,代码行数:24,代码来源:parallel.py

示例3: test_array

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def test_array(self, raw=False):
        seq = [680, 626, 934, 821, 150, 233, 548, 982, 714, 831]
        if raw:
            arr = self.RawArray('i', seq)
        else:
            arr = self.Array('i', seq)

        self.assertEqual(len(arr), len(seq))
        self.assertEqual(arr[3], seq[3])
        self.assertEqual(list(arr[2:7]), list(seq[2:7]))

        arr[4:8] = seq[4:8] = array.array('i', [1, 2, 3, 4])

        self.assertEqual(list(arr[:]), seq)

        self.f(seq)

        p = self.Process(target=self.f, args=(arr,))
        p.daemon = True
        p.start()
        p.join()

        self.assertEqual(list(arr[:]), seq) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:25,代码来源:_test_multiprocessing.py

示例4: __new__

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def __new__(cls, shape, dtype=None, buffer=None, offset=None, strides=None,
                order=None):
        # init buffer
        if buffer is None:
            assert offset is None
            assert strides is None
            size = int(np.prod(shape))
            nbytes = size * np.dtype(dtype).itemsize
            # this is the part that can be passed between processes
            shmem = mp.RawArray(ctypes.c_char, nbytes)
            offset = 0
        elif isinstance(buffer, ctypes.Array):
            # restoring from a pickle
            shmem = buffer
        else:
            raise ValueError(
                f"{cls.__name__} does not support specifying custom "
                f" buffers, but was given {buffer!r}")

        # init array
        obj = np.ndarray.__new__(cls, shape, dtype=dtype, buffer=shmem,
                                 offset=offset, strides=strides, order=order)
        obj._shmem = shmem

        return obj 
开发者ID:astooke,项目名称:rlpyt,代码行数:27,代码来源:buffer.py

示例5: mpraw_as_np

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def mpraw_as_np(shape, dtype):
    """Construct a numpy array of the specified shape and dtype for which the
    underlying storage is a multiprocessing RawArray in shared memory.

    Parameters
    ----------
    shape : tuple
      Shape of numpy array
    dtype : data-type
      Data type of array

    Returns
    -------
    arr : ndarray
      Numpy array
    """

    sz = int(np.product(shape))
    csz = sz * np.dtype(dtype).itemsize
    raw = mp.RawArray('c', csz)
    return np.frombuffer(raw, dtype=dtype, count=sz).reshape(shape) 
开发者ID:alphacsc,项目名称:alphacsc,代码行数:23,代码来源:parcnsdl.py

示例6: mpraw_as_np

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def mpraw_as_np(shape, dtype):
    """Construct a numpy array of the specified shape and dtype for
    which the underlying storage is a multiprocessing RawArray in shared
    memory.

    Parameters
    ----------
    shape : tuple
      Shape of numpy array
    dtype : data-type
      Data type of array

    Returns
    -------
    arr : ndarray
      Numpy array
    """

    sz = int(np.product(shape))
    csz = sz * np.dtype(dtype).itemsize
    raw = mp.RawArray('c', csz)
    return np.frombuffer(raw, dtype=dtype, count=sz).reshape(shape) 
开发者ID:bwohlberg,项目名称:sporco,代码行数:24,代码来源:parcbpdn.py

示例7: share_array

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def share_array(arr_np):
    """
    Return a ctypes array allocated from shared memory with data from a
    NumPy array of type `float`.

    Parameters
    ----------
            arr_np : :obj:`numpy.ndarray`
                    NumPy array.

    Returns
    -------
            array of :obj:`ctype`
    """

    arr = mp.RawArray('d', arr_np.ravel())
    return arr, arr_np.shape 
开发者ID:stefanch,项目名称:sGDML,代码行数:19,代码来源:predict.py

示例8: _share_array

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def _share_array(arr_np, typecode_or_type):
    """
    Return a ctypes array allocated from shared memory with data from a
    NumPy array.

    Parameters
    ----------
        arr_np : :obj:`numpy.ndarray`
            NumPy array.
        typecode_or_type : char or :obj:`ctype`
            Either a ctypes type or a one character typecode of the
            kind used by the Python array module.

    Returns
    -------
        array of :obj:`ctype`
    """

    arr = mp.RawArray(typecode_or_type, arr_np.ravel())
    return arr, arr_np.shape 
开发者ID:stefanch,项目名称:sGDML,代码行数:22,代码来源:train.py

示例9: _create_shared_arr

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def _create_shared_arr(self):
        TYPE = {
            np.float32: ctypes.c_float,
            np.float64: ctypes.c_double,
            np.uint8: ctypes.c_uint8,
            np.int8: ctypes.c_int8,
            np.int32: ctypes.c_int32,
        }
        ctype = TYPE[self.output_dtype]
        arr = mp.RawArray(ctype, int(np.prod(self.output_shape)))
        return arr 
开发者ID:tensorpack,项目名称:dataflow,代码行数:13,代码来源:parallel_map.py

示例10: extract_states_as_shared_arrays

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def extract_states_as_shared_arrays(optimizer):
    assert isinstance(optimizer, chainer.Optimizer)
    assert hasattr(optimizer, 'target'), 'Optimizer.setup must be called first'
    shared_arrays = {}
    for param_name, param in optimizer.target.namedparams():
        shared_arrays[param_name] = {}
        ensure_initialized_update_rule(param)
        state = param.update_rule.state
        for state_name, state_val in state.items():
            shared_arrays[param_name][
                state_name] = mp.RawArray('f', state_val.ravel())
    return shared_arrays 
开发者ID:chainer,项目名称:chainerrl,代码行数:14,代码来源:async_.py

示例11: extract_params_as_shared_arrays

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def extract_params_as_shared_arrays(link):
    assert isinstance(link, chainer.Link)
    shared_arrays = {}
    for param_name, param in link.namedparams():
        shared_arrays[param_name] = mp.RawArray('f', param.data.ravel())
    return shared_arrays 
开发者ID:muupan,项目名称:async-rl,代码行数:8,代码来源:async.py

示例12: extract_states_as_shared_arrays

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def extract_states_as_shared_arrays(optimizer):
    assert isinstance(optimizer, chainer.Optimizer)
    assert hasattr(optimizer, 'target'), 'Optimizer.setup must be called first'
    shared_arrays = {}
    for state_name, state in optimizer._states.items():
        shared_arrays[state_name] = {}
        for param_name, param in state.items():
            shared_arrays[state_name][
                param_name] = mp.RawArray('f', param.ravel())
    return shared_arrays 
开发者ID:muupan,项目名称:async-rl,代码行数:12,代码来源:async.py

示例13: __init__

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def __init__(self, *, slot_bytes, slot_count):
        """Initializer.

        Args:
            slot_bytes: How big each buffer in the array should be.
            slot_count: How many buffers should be in the array.
        """
        self.slot_bytes = slot_bytes
        self.slot_count = slot_count
        self.length_bytes = 4
        slot_type = ctypes.c_byte * (slot_bytes + self.length_bytes)
        self.array = multiprocessing.RawArray(slot_type, slot_count) 
开发者ID:bslatkin,项目名称:ringbuffer,代码行数:14,代码来源:ringbuffer.py

示例14: malloc_contiguous

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def malloc_contiguous(self, size, initial_val=None):
        if initial_val is None:
            return RawArray(ctypes.c_float, size)
        else:
            return RawArray(ctypes.c_float, initial_val) 
开发者ID:traai,项目名称:async-deep-rl,代码行数:7,代码来源:shared_utils.py

示例15: __init__

# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import RawArray [as 别名]
def __init__(self, num_actors):
        self.updated = RawArray(ctypes.c_int, num_actors) 
开发者ID:traai,项目名称:async-deep-rl,代码行数:4,代码来源:shared_utils.py


注:本文中的multiprocessing.RawArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。