files: from libcpp cimport vector
  from libcpp cimport unordered_map
  
  from cupy.cuda cimport device
  
  
  cdef class Chunk:
  
      cdef:
          readonly device.Device device
          readonly object mem
          readonly size_t ptr
          readonly Py_ssize_t offset
          readonly Py_ssize_t size
          public object stream_ptr
          public Chunk prev
          public Chunk next
  
  cdef class MemoryPointer:
  
      cdef:
          readonly device.Device device
          readonly object mem
          readonly size_t ptr
  
      cpdef copy_from_device(self, MemoryPointer src, Py_ssize_t size)
      cpdef copy_from_device_async(self, MemoryPointer src, size_t size,
                                   stream=?)
      cpdef copy_from_host(self, mem, size_t size)
      cpdef copy_from_host_async(self, mem, size_t size, stream=?)
      cpdef copy_from(self, mem, size_t size)
      cpdef copy_from_async(self, mem, size_t size, stream=?)
      cpdef copy_to_host(self, mem, size_t size)
      cpdef copy_to_host_async(self, mem, size_t size, stream=?)
      cpdef memset(self, int value, size_t size)
      cpdef memset_async(self, int value, size_t size, stream=?)
  
  
  cpdef MemoryPointer alloc(Py_ssize_t size)
  
  
  cpdef set_allocator(allocator=*)
  
  
  cdef class SingleDeviceMemoryPool:
  
      cdef:
          object _allocator
          dict _in_use
          dict _free
          object __weakref__
          object _weakref
          object _free_lock
          object _in_use_lock
          readonly Py_ssize_t _allocation_unit_size
          readonly int _device_id
          unordered_map.unordered_map[size_t, vector.vector[int]] _index
  
      cpdef MemoryPointer _alloc(self, Py_ssize_t size)
      cpdef MemoryPointer malloc(self, Py_ssize_t size)
      cpdef MemoryPointer _malloc(self, Py_ssize_t size)
      cpdef free(self, size_t ptr, Py_ssize_t size)
      cpdef free_all_blocks(self)
      cpdef free_all_free(self)
      cpdef n_free_blocks(self)
      cpdef used_bytes(self)
      cpdef free_bytes(self)
      cpdef total_bytes(self)
      cpdef Py_ssize_t _round_size(self, Py_ssize_t size)
      cpdef int _bin_index_from_size(self, Py_ssize_t size)
      cpdef list _arena(self, size_t stream_ptr)
      cdef vector.vector[int]* _arena_index(self, size_t stream_ptr)
      cpdef _append_to_free_list(self, Py_ssize_t size, chunk, size_t stream_ptr)
      cpdef bint _remove_from_free_list(self, Py_ssize_t size,
                                        chunk, size_t stream_ptr) except *
      cpdef tuple _split(self, Chunk chunk, Py_ssize_t size)
      cpdef Chunk _merge(self, Chunk head, Chunk remaining)
  
  cdef class MemoryPool:
  
      cdef:
          object _pools
  
      cpdef MemoryPointer malloc(self, Py_ssize_t size)
      cpdef free_all_blocks(self)
      cpdef free_all_free(self)
      cpdef n_free_blocks(self)
      cpdef used_bytes(self)
      cpdef free_bytes(self)
      cpdef total_bytes(self)
  
  
  [... the rest is omitted ...]
files: cupy/cuda/memory.pxd
files: from libcpp cimport vector
  
  from cupy.cuda cimport device
  
  
  cdef class Chunk:
  
      cdef:
          readonly device.Device device
          readonly object mem
          readonly size_t ptr
          readonly Py_ssize_t offset
          readonly Py_ssize_t size
          public Chunk prev
          public Chunk next
  
  cdef class MemoryPointer:
  
      cdef:
          readonly device.Device device
          readonly object mem
          readonly size_t ptr
  
      cpdef copy_from_device(self, MemoryPointer src, Py_ssize_t size)
      cpdef copy_from_device_async(self, MemoryPointer src, size_t size, stream)
      cpdef copy_from_host(self, mem, size_t size)
      cpdef copy_from_host_async(self, mem, size_t size, stream)
      cpdef copy_from(self, mem, size_t size)
      cpdef copy_from_async(self, mem, size_t size, stream)
      cpdef copy_to_host(self, mem, size_t size)
      cpdef copy_to_host_async(self, mem, size_t size, stream)
      cpdef memset(self, int value, size_t size)
      cpdef memset_async(self, int value, size_t size, stream)
  
  
  cpdef MemoryPointer alloc(Py_ssize_t size)
  
  
  cpdef set_allocator(allocator=*)
  
  
  cdef class SingleDeviceMemoryPool:
  
      cdef:
          object _allocator
          dict _in_use
          list _free
          object __weakref__
          object _weakref
          object _free_lock
          object _in_use_lock
          readonly Py_ssize_t _allocation_unit_size
          readonly int _device_id
          vector.vector[int] _index
  
      cpdef MemoryPointer _alloc(self, Py_ssize_t size)
      cpdef MemoryPointer malloc(self, Py_ssize_t size)
      cpdef MemoryPointer _malloc(self, Py_ssize_t size)
      cpdef free(self, size_t ptr, Py_ssize_t size)
      cpdef free_all_blocks(self)
      cpdef free_all_free(self)
      cpdef n_free_blocks(self)
      cpdef used_bytes(self)
      cpdef free_bytes(self)
      cpdef total_bytes(self)
      cpdef Py_ssize_t _round_size(self, Py_ssize_t size)
      cpdef int _bin_index_from_size(self, Py_ssize_t size)
      cpdef _append_to_free_list(self, Py_ssize_t size, chunk)
      cpdef bint _remove_from_free_list(self, Py_ssize_t size, chunk) except *
      cpdef tuple _split(self, Chunk chunk, Py_ssize_t size)
      cpdef Chunk _merge(self, Chunk head, Chunk remaining)
  
  cdef class MemoryPool:
  
      cdef:
          object _pools
  
      cpdef MemoryPointer malloc(self, Py_ssize_t size)
      cpdef free_all_blocks(self)
      cpdef free_all_free(self)
      cpdef n_free_blocks(self)
      cpdef used_bytes(self)
      cpdef free_bytes(self)
      cpdef total_bytes(self)
  
  
  [... the rest is omitted ...]
files: cupy/cuda/memory.pxd
files: # distutils: language = c++
  import collections
  import ctypes
  import gc
  import warnings
  import weakref
  
  from fastrlock cimport rlock
  from libcpp cimport algorithm
  
  from cupy.cuda import memory_hook
  from cupy.cuda import runtime
  from cupy.cuda import stream as stream_module
  
  from cupy.cuda cimport device
  from cupy.cuda cimport runtime
  
  
  class OutOfMemoryError(MemoryError):
  
      def __init__(self, size, total):
          msg = 'out of memory to allocate %d bytes ' \
                '(total %d bytes)' % (size, total)
          super(OutOfMemoryError, self).__init__(msg)
  
  
  class Memory(object):
  
      """Memory allocation on a CUDA device.
  
      This class provides an RAII interface of the CUDA memory allocation.
  
      Args:
          ~Memory.device (cupy.cuda.Device): Device whose memory the pointer
              refers to.
          ~Memory.size (int): Size of the memory allocation in bytes.
  
      """
  
      def __init__(self, Py_ssize_t size):
          self.size = size
          self.device = None
          self.ptr = 0
          if size > 0:
              self.device = device.Device()
              self.ptr = runtime.malloc(size)
  
      def __del__(self):
          if self.ptr:
              runtime.free(self.ptr)
  
      def __int__(self):
          """Returns the pointer value to the head of the allocation."""
          return self.ptr
  
  
  class ManagedMemory(Memory):
  
      """Managed memory (Unified memory) allocation on a CUDA device.
  
      This class provides an RAII interface of the CUDA managed memory
      allocation.
  
      Args:
          device (cupy.cuda.Device): Device whose memory the pointer refers to.
          size (int): Size of the memory allocation in bytes.
  
      """
  
      def __init__(self, Py_ssize_t size):
          self.size = size
          self.device = None
          self.ptr = 0
          if size > 0:
              self.device = device.Device()
              self.ptr = runtime.mallocManaged(size)
  
      def prefetch(self, stream):
          """(experimental) Prefetch memory.
  
          Args:
              stream (cupy.cuda.Stream): CUDA stream.
          """
          runtime.memPrefetchAsync(self.ptr, self.size, self.device.id,
                                   stream.ptr)
  
      def advise(self, int advise, device.Device device):
          """(experimental) Advise about the usage of this memory.
  
          Args:
              advics (int): Advise to be applied for this memory.
              device (cupy.cuda.Device): Device to apply the advice for.
  
          """
          runtime.memAdvise(self.ptr, self.size, advise, device.id)
  
  
  cdef set _peer_access_checked = set()
  
  
  cpdef _set_peer_access(int device, int peer):
      device_pair = device, peer
  
      if device_pair in _peer_access_checked:
          return
      cdef int can_access = runtime.deviceCanAccessPeer(device, peer)
      _peer_access_checked.add(device_pair)
      if not can_access:
          return
  
      cdef int current = runtime.getDevice()
      runtime.setDevice(device)
      try:
          runtime.deviceEnablePeerAccess(peer)
      finally:
          runtime.setDevice(current)
  
  cdef class Chunk:
  
      """A chunk points to a device memory.
  
      A chunk might be a splitted memory block from a larger allocation.
      The prev/next pointers contruct a doubly-linked list of memory addresses
      sorted by base address that must be contiguous.
  
      Args:
          mem (Memory): The device memory buffer.
          offset (int): An offset bytes from the head of the buffer.
          size (int): Chunk size in bytes.
          stream_ptr (size_t): Raw stream handle of cupy.cuda.Stream
  
      Attributes:
          device (cupy.cuda.Device): Device whose memory the pointer refers to.
          mem (Memory): The device memory buffer.
          ptr (size_t): Memory address.
          offset (int): An offset bytes from the head of the buffer.
          size (int): Chunk size in bytes.
          prev (Chunk): prev memory pointer if split from a larger allocation
          next (Chunk): next memory pointer if split from a larger allocation
          stream_ptr (size_t): Raw stream handle of cupy.cuda.Stream
      """
  
      def __init__(self, mem, Py_ssize_t offset, Py_ssize_t size, stream_ptr):
          assert mem.ptr > 0 or offset == 0
          self.mem = mem
          self.device = mem.device
          self.ptr = mem.ptr + offset
          self.offset = offset
          self.size = size
          self.stream_ptr = stream_ptr
          self.prev = None
          self.next = None
  
  cdef class MemoryPointer:
  
      """Pointer to a point on a device memory.
  
      An instance of this class holds a reference to the original memory buffer
      and a pointer to a place within this buffer.
  
      Args:
          mem (Memory): The device memory buffer.
          offset (int): An offset from the head of the buffer to the place this
              pointer refers.
  
      Attributes:
          ~MemoryPointer.device (cupy.cuda.Device): Device whose memory the
              pointer refers to.
          mem (Memory): The device memory buffer.
          ptr (size_t): Pointer to the place within the buffer.
      """
  
      def __init__(self, mem, Py_ssize_t offset):
          assert mem.ptr > 0 or offset == 0
          self.mem = mem
          self.device = mem.device
          self.ptr = mem.ptr + offset
  
      def __int__(self):
          """Returns the pointer value."""
          return self.ptr
  
      def __add__(x, y):
          """Adds an offset to the pointer."""
          cdef MemoryPointer self
          cdef Py_ssize_t offset
          if isinstance(x, MemoryPointer):
              self = x
              offset = <Py_ssize_t?>y
          else:
              self = <MemoryPointer?>y
              offset = <Py_ssize_t?>x
          assert self.ptr > 0 or offset == 0
          return MemoryPointer(self.mem,
                               self.ptr - self.mem.ptr + offset)
  
      def __iadd__(self, Py_ssize_t offset):
          """Adds an offset to the pointer in place."""
          assert self.ptr > 0 or offset == 0
          self.ptr += offset
          return self
  
      def __sub__(self, offset):
          """Subtracts an offset from the pointer."""
          return self + -offset
  
      def __isub__(self, Py_ssize_t offset):
          """Subtracts an offset from the pointer in place."""
          return self.__iadd__(-offset)
  
      cpdef copy_from_device(self, MemoryPointer src, Py_ssize_t size):
          """Copies a memory sequence from a (possibly different) device.
  
          Args:
              src (cupy.cuda.MemoryPointer): Source memory pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              _set_peer_access(src.device.id, self.device.id)
              runtime.memcpy(self.ptr, src.ptr, size,
                             runtime.memcpyDefault)
  
      cpdef copy_from_device_async(self, MemoryPointer src, size_t size,
                                   stream=None):
          """Copies a memory from a (possibly different) device asynchronously.
  
          Args:
              src (cupy.cuda.MemoryPointer): Source memory pointer.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
                  The default uses CUDA stream of the current context.
  
          """
          if stream is None:
              stream = stream_module.get_current_stream()
          if size > 0:
              _set_peer_access(src.device.id, self.device.id)
              runtime.memcpyAsync(self.ptr, src.ptr, size,
                                  runtime.memcpyDefault, stream.ptr)
  
      cpdef copy_from_host(self, mem, size_t size):
          """Copies a memory sequence from the host memory.
  
          Args:
              mem (ctypes.c_void_p): Source memory pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              runtime.memcpy(self.ptr, mem.value, size,
                             runtime.memcpyHostToDevice)
  
      cpdef copy_from_host_async(self, mem, size_t size, stream=None):
          """Copies a memory sequence from the host memory asynchronously.
  
          Args:
              mem (ctypes.c_void_p): Source memory pointer. It must be a pinned
                  memory.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
                  The default uses CUDA stream of the current context.
  
          """
          if stream is None:
              stream = stream_module.get_current_stream()
          if size > 0:
              runtime.memcpyAsync(self.ptr, mem.value, size,
                                  runtime.memcpyHostToDevice, stream.ptr)
  
      cpdef copy_from(self, mem, size_t size):
          """Copies a memory sequence from a (possibly different) device or host.
  
          This function is a useful interface that selects appropriate one from
          :meth:`~cupy.cuda.MemoryPointer.copy_from_device` and
          :meth:`~cupy.cuda.MemoryPointer.copy_from_host`.
  
          Args:
              mem (ctypes.c_void_p or cupy.cuda.MemoryPointer): Source memory
                  pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if isinstance(mem, MemoryPointer):
              self.copy_from_device(mem, size)
          else:
              self.copy_from_host(mem, size)
  
      cpdef copy_from_async(self, mem, size_t size, stream=None):
          """Copies a memory sequence from an arbitrary place asynchronously.
  
          This function is a useful interface that selects appropriate one from
          :meth:`~cupy.cuda.MemoryPointer.copy_from_device_async` and
          :meth:`~cupy.cuda.MemoryPointer.copy_from_host_async`.
  
          Args:
              mem (ctypes.c_void_p or cupy.cuda.MemoryPointer): Source memory
                  pointer.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
                  The default uses CUDA stream of the current context.
  
          """
          if stream is None:
              stream = stream_module.get_current_stream()
          if isinstance(mem, MemoryPointer):
              self.copy_from_device_async(mem, size, stream)
          else:
              self.copy_from_host_async(mem, size, stream)
  
      cpdef copy_to_host(self, mem, size_t size):
          """Copies a memory sequence to the host memory.
  
          Args:
              mem (ctypes.c_void_p): Target memory pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              runtime.memcpy(mem.value, self.ptr, size,
                             runtime.memcpyDeviceToHost)
  
      cpdef copy_to_host_async(self, mem, size_t size, stream=None):
          """Copies a memory sequence to the host memory asynchronously.
  
          Args:
              mem (ctypes.c_void_p): Target memory pointer. It must be a pinned
                  memory.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
                  The default uses CUDA stream of the current context.
  
          """
          if stream is None:
              stream = stream_module.get_current_stream()
          if size > 0:
              runtime.memcpyAsync(mem.value, self.ptr, size,
                                  runtime.memcpyDeviceToHost, stream.ptr)
  
      cpdef memset(self, int value, size_t size):
          """Fills a memory sequence by constant byte value.
  
          Args:
              value (int): Value to fill.
              size (int): Size of the sequence in bytes.
  
         
  
  [... the rest is omitted ...]
files: cupy/cuda/memory.pyx
files: # distutils: language = c++
  import collections
  import ctypes
  import gc
  import warnings
  import weakref
  
  from fastrlock cimport rlock
  from libcpp cimport algorithm
  
  from cupy.cuda import memory_hook
  from cupy.cuda import runtime
  
  from cupy.cuda cimport device
  from cupy.cuda cimport runtime
  
  
  class OutOfMemoryError(MemoryError):
  
      def __init__(self, size, total):
          msg = 'out of memory to allocate %d bytes ' \
                '(total %d bytes)' % (size, total)
          super(OutOfMemoryError, self).__init__(msg)
  
  
  class Memory(object):
  
      """Memory allocation on a CUDA device.
  
      This class provides an RAII interface of the CUDA memory allocation.
  
      Args:
          ~Memory.device (cupy.cuda.Device): Device whose memory the pointer
              refers to.
          ~Memory.size (int): Size of the memory allocation in bytes.
  
      """
  
      def __init__(self, Py_ssize_t size):
          self.size = size
          self.device = None
          self.ptr = 0
          if size > 0:
              self.device = device.Device()
              self.ptr = runtime.malloc(size)
  
      def __del__(self):
          if self.ptr:
              runtime.free(self.ptr)
  
      def __int__(self):
          """Returns the pointer value to the head of the allocation."""
          return self.ptr
  
  
  class ManagedMemory(Memory):
  
      """Managed memory (Unified memory) allocation on a CUDA device.
  
      This class provides an RAII interface of the CUDA managed memory
      allocation.
  
      Args:
          device (cupy.cuda.Device): Device whose memory the pointer refers to.
          size (int): Size of the memory allocation in bytes.
  
      """
  
      def __init__(self, Py_ssize_t size):
          self.size = size
          self.device = None
          self.ptr = 0
          if size > 0:
              self.device = device.Device()
              self.ptr = runtime.mallocManaged(size)
  
      def prefetch(self, stream):
          """(experimental) Prefetch memory.
  
          Args:
              stream (cupy.cuda.Stream): CUDA stream.
          """
          runtime.memPrefetchAsync(self.ptr, self.size, self.device.id,
                                   stream.ptr)
  
      def advise(self, int advise, device.Device device):
          """(experimental) Advise about the usage of this memory.
  
          Args:
              advics (int): Advise to be applied for this memory.
              device (cupy.cuda.Device): Device to apply the advice for.
  
          """
          runtime.memAdvise(self.ptr, self.size, advise, device.id)
  
  
  cdef set _peer_access_checked = set()
  
  
  cpdef _set_peer_access(int device, int peer):
      device_pair = device, peer
  
      if device_pair in _peer_access_checked:
          return
      cdef int can_access = runtime.deviceCanAccessPeer(device, peer)
      _peer_access_checked.add(device_pair)
      if not can_access:
          return
  
      cdef int current = runtime.getDevice()
      runtime.setDevice(device)
      try:
          runtime.deviceEnablePeerAccess(peer)
      finally:
          runtime.setDevice(current)
  
  cdef class Chunk:
  
      """A chunk points to a device memory.
  
      A chunk might be a splitted memory block from a larger allocation.
      The prev/next pointers contruct a doubly-linked list of memory addresses
      sorted by base address that must be contiguous.
  
      Args:
          mem (Memory): The device memory buffer.
          offset (int): An offset bytes from the head of the buffer.
          size (int): Chunk size in bytes.
  
      Attributes:
          device (cupy.cuda.Device): Device whose memory the pointer refers to.
          mem (Memory): The device memory buffer.
          ptr (int): Memory address.
          offset (int): An offset bytes from the head of the buffer.
          size (int): Chunk size in bytes.
          prev (Chunk): prev memory pointer if split from a larger allocation
          next (Chunk): next memory pointer if split from a larger allocation
      """
  
      def __init__(self, mem, Py_ssize_t offset, Py_ssize_t size):
          assert mem.ptr > 0 or offset == 0
          self.mem = mem
          self.device = mem.device
          self.ptr = mem.ptr + offset
          self.offset = offset
          self.size = size
          self.prev = None
          self.next = None
  
  cdef class MemoryPointer:
  
      """Pointer to a point on a device memory.
  
      An instance of this class holds a reference to the original memory buffer
      and a pointer to a place within this buffer.
  
      Args:
          mem (Memory): The device memory buffer.
          offset (int): An offset from the head of the buffer to the place this
              pointer refers.
  
      Attributes:
          ~MemoryPointer.device (cupy.cuda.Device): Device whose memory the
              pointer refers to.
          mem (Memory): The device memory buffer.
          ptr (int): Pointer to the place within the buffer.
      """
  
      def __init__(self, mem, Py_ssize_t offset):
          assert mem.ptr > 0 or offset == 0
          self.mem = mem
          self.device = mem.device
          self.ptr = mem.ptr + offset
  
      def __int__(self):
          """Returns the pointer value."""
          return self.ptr
  
      def __add__(x, y):
          """Adds an offset to the pointer."""
          cdef MemoryPointer self
          cdef Py_ssize_t offset
          if isinstance(x, MemoryPointer):
              self = x
              offset = <Py_ssize_t?>y
          else:
              self = <MemoryPointer?>y
              offset = <Py_ssize_t?>x
          assert self.ptr > 0 or offset == 0
          return MemoryPointer(self.mem,
                               self.ptr - self.mem.ptr + offset)
  
      def __iadd__(self, Py_ssize_t offset):
          """Adds an offset to the pointer in place."""
          assert self.ptr > 0 or offset == 0
          self.ptr += offset
          return self
  
      def __sub__(self, offset):
          """Subtracts an offset from the pointer."""
          return self + -offset
  
      def __isub__(self, Py_ssize_t offset):
          """Subtracts an offset from the pointer in place."""
          return self.__iadd__(-offset)
  
      cpdef copy_from_device(self, MemoryPointer src, Py_ssize_t size):
          """Copies a memory sequence from a (possibly different) device.
  
          Args:
              src (cupy.cuda.MemoryPointer): Source memory pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              _set_peer_access(src.device.id, self.device.id)
              runtime.memcpy(self.ptr, src.ptr, size,
                             runtime.memcpyDefault)
  
      cpdef copy_from_device_async(self, MemoryPointer src, size_t size, stream):
          """Copies a memory from a (possibly different) device asynchronously.
  
          Args:
              src (cupy.cuda.MemoryPointer): Source memory pointer.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
  
          """
          if size > 0:
              _set_peer_access(src.device.id, self.device.id)
              runtime.memcpyAsync(self.ptr, src.ptr, size,
                                  runtime.memcpyDefault, stream.ptr)
  
      cpdef copy_from_host(self, mem, size_t size):
          """Copies a memory sequence from the host memory.
  
          Args:
              mem (ctypes.c_void_p): Source memory pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              runtime.memcpy(self.ptr, mem.value, size,
                             runtime.memcpyHostToDevice)
  
      cpdef copy_from_host_async(self, mem, size_t size, stream):
          """Copies a memory sequence from the host memory asynchronously.
  
          Args:
              mem (ctypes.c_void_p): Source memory pointer. It must be a pinned
                  memory.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
  
          """
          if size > 0:
              runtime.memcpyAsync(self.ptr, mem.value, size,
                                  runtime.memcpyHostToDevice, stream.ptr)
  
      cpdef copy_from(self, mem, size_t size):
          """Copies a memory sequence from a (possibly different) device or host.
  
          This function is a useful interface that selects appropriate one from
          :meth:`~cupy.cuda.MemoryPointer.copy_from_device` and
          :meth:`~cupy.cuda.MemoryPointer.copy_from_host`.
  
          Args:
              mem (ctypes.c_void_p or cupy.cuda.MemoryPointer): Source memory
                  pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if isinstance(mem, MemoryPointer):
              self.copy_from_device(mem, size)
          else:
              self.copy_from_host(mem, size)
  
      cpdef copy_from_async(self, mem, size_t size, stream):
          """Copies a memory sequence from an arbitrary place asynchronously.
  
          This function is a useful interface that selects appropriate one from
          :meth:`~cupy.cuda.MemoryPointer.copy_from_device_async` and
          :meth:`~cupy.cuda.MemoryPointer.copy_from_host_async`.
  
          Args:
              mem (ctypes.c_void_p or cupy.cuda.MemoryPointer): Source memory
                  pointer.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
  
          """
          if isinstance(mem, MemoryPointer):
              self.copy_from_device_async(mem, size, stream)
          else:
              self.copy_from_host_async(mem, size, stream)
  
      cpdef copy_to_host(self, mem, size_t size):
          """Copies a memory sequence to the host memory.
  
          Args:
              mem (ctypes.c_void_p): Target memory pointer.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              runtime.memcpy(mem.value, self.ptr, size,
                             runtime.memcpyDeviceToHost)
  
      cpdef copy_to_host_async(self, mem, size_t size, stream):
          """Copies a memory sequence to the host memory asynchronously.
  
          Args:
              mem (ctypes.c_void_p): Target memory pointer. It must be a pinned
                  memory.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
  
          """
          if size > 0:
              runtime.memcpyAsync(mem.value, self.ptr, size,
                                  runtime.memcpyDeviceToHost, stream.ptr)
  
      cpdef memset(self, int value, size_t size):
          """Fills a memory sequence by constant byte value.
  
          Args:
              value (int): Value to fill.
              size (int): Size of the sequence in bytes.
  
          """
          if size > 0:
              runtime.memset(self.ptr, value, size)
  
      cpdef memset_async(self, int value, size_t size, stream):
          """Fills a memory sequence by constant byte value asynchronously.
  
          Args:
              value (int): Value to fill.
              size (int): Size of the sequence in bytes.
              stream (cupy.cuda.Stream): CUDA stream.
  
          """
          if size > 0:
              runtime.memsetAsync(self.ptr, value, size, stream.ptr)
  
  
  cpdef MemoryPointer _malloc(Py_ssize_t size):
      mem = Memory(size)
      return MemoryPointer(mem, 0)
  
  
  cpdef MemoryPointer malloc_managed(Py_ssize_t size):
      """Allocate managed memory (unified memory).
  
     
  
  [... the rest is omitted ...]
files: cupy/cuda/memory.pyx
files: import ctypes
  import unittest
  
  import cupy.cuda
  from cupy.cuda import memory
  from cupy.cuda import stream as stream_module
  from cupy import testing
  
  
  class MockMemory(memory.Memory):
      cur_ptr = 1
  
      def __init__(self, size):
          self.ptr = MockMemory.cur_ptr
          MockMemory.cur_ptr += size
          self.size = size
          self.device = None
  
      def __del__(self):
          self.ptr = 0
          pass
  
  
  def mock_alloc(size):
      mem = MockMemory(size)
      return memory.MemoryPointer(mem, 0)
  
  # -----------------------------------------------------------------------------
  # Memory pointer
  
  
  @testing.gpu
  class TestMemoryPointer(unittest.TestCase):
  
      def test_int(self):
          pval = MockMemory.cur_ptr
          memptr = mock_alloc(1)
          self.assertEqual(pval, int(memptr))
  
      def test_add(self):
          pval = MockMemory.cur_ptr
          memptr = mock_alloc(8)
  
          memptr2 = memptr + 4
          self.assertIsInstance(memptr2, memory.MemoryPointer)
          self.assertEqual(pval + 4, int(memptr2))
  
          memptr3 = 4 + memptr
          self.assertIsInstance(memptr3, memory.MemoryPointer)
          self.assertEqual(pval + 4, int(memptr3))
  
          memptr += 4
          self.assertIsInstance(memptr, memory.MemoryPointer)
          self.assertEqual(pval + 4, int(memptr))
  
      def test_sub(self):
          pval = MockMemory.cur_ptr
          memptr = mock_alloc(8) + 4
  
          memptr2 = memptr - 4
          self.assertIsInstance(memptr2, memory.MemoryPointer)
          self.assertEqual(pval, int(memptr2))
  
          memptr -= 4
          self.assertIsInstance(memptr, memory.MemoryPointer)
          self.assertEqual(pval, int(memptr))
  
      def test_copy_to_and_from_host(self):
          a_gpu = memory.alloc(4)
          a_cpu = ctypes.c_int(100)
          a_gpu.copy_from(ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p), 4)
          b_cpu = ctypes.c_int()
          a_gpu.copy_to_host(
              ctypes.cast(ctypes.byref(b_cpu), ctypes.c_void_p), 4)
          self.assertEqual(b_cpu.value, a_cpu.value)
  
      def test_copy_from_device(self):
          a_gpu = memory.alloc(4)
          a_cpu = ctypes.c_int(100)
          a_gpu.copy_from(ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p), 4)
  
          b_gpu = memory.alloc(4)
          b_gpu.copy_from(a_gpu, 4)
          b_cpu = ctypes.c_int()
          b_gpu.copy_to_host(
              ctypes.cast(ctypes.byref(b_cpu), ctypes.c_void_p), 4)
          self.assertEqual(b_cpu.value, a_cpu.value)
  
      def test_memset(self):
          a_gpu = memory.alloc(4)
          a_gpu.memset(1, 4)
          a_cpu = ctypes.c_ubyte()
          for i in range(4):
              a_gpu.copy_to_host(
                  ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p), 1)
              self.assertEqual(a_cpu.value, 1)
              a_gpu += 1
  
  
  # -----------------------------------------------------------------------------
  # Memory pool
  
  @testing.gpu
  class TestSingleDeviceMemoryPool(unittest.TestCase):
  
      def setUp(self):
          self.pool = memory.SingleDeviceMemoryPool(allocator=mock_alloc)
          self.unit = self.pool._allocation_unit_size
          self.stream = stream_module.Stream()
          self.stream_ptr = self.stream.ptr
  
      def test_round_size(self):
          self.assertEqual(self.pool._round_size(self.unit - 1), self.unit)
          self.assertEqual(self.pool._round_size(self.unit), self.unit)
          self.assertEqual(self.pool._round_size(self.unit + 1), self.unit * 2)
  
      def test_bin_index_from_size(self):
          self.assertEqual(self.pool._bin_index_from_size(self.unit - 1), 0)
          self.assertEqual(self.pool._bin_index_from_size(self.unit), 0)
          self.assertEqual(self.pool._bin_index_from_size(self.unit + 1), 1)
  
      def test_split(self):
          mem = MockMemory(self.unit * 4)
          chunk = memory.Chunk(mem, 0, mem.size, self.stream_ptr)
          head, tail = self.pool._split(chunk, self.unit * 2)
          self.assertEqual(head.ptr, chunk.ptr)
          self.assertEqual(head.offset, 0)
          self.assertEqual(head.size, self.unit * 2)
          self.assertEqual(head.prev, None)
          self.assertEqual(head.next.ptr, tail.ptr)
          self.assertEqual(head.stream_ptr, self.stream_ptr)
          self.assertEqual(tail.ptr, chunk.ptr + self.unit * 2)
          self.assertEqual(tail.offset, self.unit * 2)
          self.assertEqual(tail.size, self.unit * 2)
          self.assertEqual(tail.prev.ptr, head.ptr)
          self.assertEqual(tail.next, None)
          self.assertEqual(tail.stream_ptr, self.stream_ptr)
  
          head_of_head, tail_of_head = self.pool._split(head, self.unit)
          self.assertEqual(head_of_head.ptr, chunk.ptr)
          self.assertEqual(head_of_head.offset, 0)
          self.assertEqual(head_of_head.size, self.unit)
          self.assertEqual(head_of_head.prev, None)
          self.assertEqual(head_of_head.next.ptr, tail_of_head.ptr)
          self.assertEqual(head_of_head.stream_ptr, self.stream_ptr)
          self.assertEqual(tail_of_head.ptr, chunk.ptr + self.unit)
          self.assertEqual(tail_of_head.offset, self.unit)
          self.assertEqual(tail_of_head.size, self.unit)
          self.assertEqual(tail_of_head.prev.ptr, head_of_head.ptr)
          self.assertEqual(tail_of_head.next.ptr, tail.ptr)
          self.assertEqual(tail_of_head.stream_ptr, self.stream_ptr)
  
          head_of_tail, tail_of_tail = self.pool._split(tail, self.unit)
          self.assertEqual(head_of_tail.ptr, chunk.ptr + self.unit * 2)
          self.assertEqual(head_of_tail.offset, self.unit * 2)
          self.assertEqual(head_of_tail.size, self.unit)
          self.assertEqual(head_of_tail.prev.ptr, tail_of_head.ptr)
          self.assertEqual(head_of_tail.next.ptr, tail_of_tail.ptr)
          self.assertEqual(head_of_tail.stream_ptr, self.stream_ptr)
          self.assertEqual(tail_of_tail.ptr, chunk.ptr + self.unit * 3)
          self.assertEqual(tail_of_tail.offset, self.unit * 3)
          self.assertEqual(tail_of_tail.size, self.unit)
          self.assertEqual(tail_of_tail.prev.ptr, head_of_tail.ptr)
          self.assertEqual(tail_of_tail.next, None)
          self.assertEqual(tail_of_tail.stream_ptr, self.stream_ptr)
  
      def test_merge(self):
          mem = MockMemory(self.unit * 4)
          chunk = memory.Chunk(mem, 0, mem.size, self.stream_ptr)
  
          head, tail = self.pool._split(chunk, self.unit * 2)
          head_ptr, tail_ptr = head.ptr, tail.ptr
          head_of_head, tail_of_head = self.pool._split(head, self.unit)
          head_of_tail, tail_of_tail = self.pool._split(tail, self.unit)
  
          merged_head = self.pool._merge(head_of_head, tail_of_head)
          self.assertEqual(merged_head.ptr, head.ptr)
          self.assertEqual(merged_head.offset, head.offset)
          self.assertEqual(merged_head.size, head.size)
          self.assertEqual(merged_head.prev, None)
          self.assertEqual(merged_head.next.ptr, tail_ptr)
          self.assertEqual(merged_head.stream_ptr, self.stream_ptr)
  
          merged_tail = self.pool._merge(head_of_tail, tail_of_tail)
          self.assertEqual(merged_tail.ptr, tail.ptr)
          self.assertEqual(merged_tail.offset, tail.offset)
          self.assertEqual(merged_tail.size, tail.size)
          self.assertEqual(merged_tail.prev.ptr, head_ptr)
          self.assertEqual(merged_tail.next, None)
          self.assertEqual(merged_tail.stream_ptr, self.stream_ptr)
  
          merged = self.pool._merge(merged_head, merged_tail)
          self.assertEqual(merged.ptr, chunk.ptr)
          self.assertEqual(merged.offset, chunk.offset)
          self.assertEqual(merged.size, chunk.size)
          self.assertEqual(merged.prev, None)
          self.assertEqual(merged.next, None)
          self.assertEqual(merged.stream_ptr, self.stream_ptr)
  
      def test_alloc(self):
          p1 = self.pool.malloc(self.unit * 4)
          p2 = self.pool.malloc(self.unit * 4)
          p3 = self.pool.malloc(self.unit * 8)
          self.assertNotEqual(p1.ptr, p2.ptr)
          self.assertNotEqual(p1.ptr, p3.ptr)
          self.assertNotEqual(p2.ptr, p3.ptr)
  
      def test_alloc_split(self):
          p = self.pool.malloc(self.unit * 4)
          ptr = p.ptr
          del p
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          self.assertEqual(ptr, head.ptr)
          self.assertEqual(ptr + self.unit * 2, tail.ptr)
  
      def test_free(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          p2 = self.pool.malloc(self.unit * 4)
          self.assertEqual(ptr1, p2.ptr)
  
      def test_free_stream(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          with self.stream:
              p2 = self.pool.malloc(self.unit * 4)
          self.assertNotEqual(ptr1, p2.ptr)
  
      def test_free_merge(self):
          p = self.pool.malloc(self.unit * 4)
          ptr = p.ptr
          del p
  
          # merge head into tail
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          self.assertEqual(ptr, head.ptr)
          del tail
          del head
          p = self.pool.malloc(self.unit * 4)
          self.assertEqual(ptr, p.ptr)
          del p
  
          # merge tail into head
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          self.assertEqual(ptr, head.ptr)
          del head
          del tail
          p = self.pool.malloc(self.unit * 4)
          self.assertEqual(ptr, p.ptr)
          del p
  
      def test_free_different_size(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          p2 = self.pool.malloc(self.unit * 8)
          self.assertNotEqual(ptr1, p2.ptr)
  
      def test_free_all_blocks(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          self.pool.free_all_
  
  [... the rest is omitted ...]
files: tests/cupy_tests/cuda_tests/test_memory.py
files: import ctypes
  import unittest
  
  import cupy.cuda
  from cupy.cuda import memory
  from cupy import testing
  
  
  class MockMemory(memory.Memory):
      cur_ptr = 1
  
      def __init__(self, size):
          self.ptr = MockMemory.cur_ptr
          MockMemory.cur_ptr += size
          self.size = size
          self.device = None
  
      def __del__(self):
          self.ptr = 0
          pass
  
  
  def mock_alloc(size):
      mem = MockMemory(size)
      return memory.MemoryPointer(mem, 0)
  
  # -----------------------------------------------------------------------------
  # Memory pointer
  
  
  @testing.gpu
  class TestMemoryPointer(unittest.TestCase):
  
      def test_int(self):
          pval = MockMemory.cur_ptr
          memptr = mock_alloc(1)
          self.assertEqual(pval, int(memptr))
  
      def test_add(self):
          pval = MockMemory.cur_ptr
          memptr = mock_alloc(8)
  
          memptr2 = memptr + 4
          self.assertIsInstance(memptr2, memory.MemoryPointer)
          self.assertEqual(pval + 4, int(memptr2))
  
          memptr3 = 4 + memptr
          self.assertIsInstance(memptr3, memory.MemoryPointer)
          self.assertEqual(pval + 4, int(memptr3))
  
          memptr += 4
          self.assertIsInstance(memptr, memory.MemoryPointer)
          self.assertEqual(pval + 4, int(memptr))
  
      def test_sub(self):
          pval = MockMemory.cur_ptr
          memptr = mock_alloc(8) + 4
  
          memptr2 = memptr - 4
          self.assertIsInstance(memptr2, memory.MemoryPointer)
          self.assertEqual(pval, int(memptr2))
  
          memptr -= 4
          self.assertIsInstance(memptr, memory.MemoryPointer)
          self.assertEqual(pval, int(memptr))
  
      def test_copy_to_and_from_host(self):
          a_gpu = memory.alloc(4)
          a_cpu = ctypes.c_int(100)
          a_gpu.copy_from(ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p), 4)
          b_cpu = ctypes.c_int()
          a_gpu.copy_to_host(
              ctypes.cast(ctypes.byref(b_cpu), ctypes.c_void_p), 4)
          self.assertEqual(b_cpu.value, a_cpu.value)
  
      def test_copy_from_device(self):
          a_gpu = memory.alloc(4)
          a_cpu = ctypes.c_int(100)
          a_gpu.copy_from(ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p), 4)
  
          b_gpu = memory.alloc(4)
          b_gpu.copy_from(a_gpu, 4)
          b_cpu = ctypes.c_int()
          b_gpu.copy_to_host(
              ctypes.cast(ctypes.byref(b_cpu), ctypes.c_void_p), 4)
          self.assertEqual(b_cpu.value, a_cpu.value)
  
      def test_memset(self):
          a_gpu = memory.alloc(4)
          a_gpu.memset(1, 4)
          a_cpu = ctypes.c_ubyte()
          for i in range(4):
              a_gpu.copy_to_host(
                  ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p), 1)
              self.assertEqual(a_cpu.value, 1)
              a_gpu += 1
  
  
  # -----------------------------------------------------------------------------
  # Memory pool
  
  @testing.gpu
  class TestSingleDeviceMemoryPool(unittest.TestCase):
  
      def setUp(self):
          self.pool = memory.SingleDeviceMemoryPool(allocator=mock_alloc)
          self.unit = self.pool._allocation_unit_size
  
      def test_round_size(self):
          self.assertEqual(self.pool._round_size(self.unit - 1), self.unit)
          self.assertEqual(self.pool._round_size(self.unit), self.unit)
          self.assertEqual(self.pool._round_size(self.unit + 1), self.unit * 2)
  
      def test_bin_index_from_size(self):
          self.assertEqual(self.pool._bin_index_from_size(self.unit - 1), 0)
          self.assertEqual(self.pool._bin_index_from_size(self.unit), 0)
          self.assertEqual(self.pool._bin_index_from_size(self.unit + 1), 1)
  
      def test_split(self):
          mem = MockMemory(self.unit * 4)
          chunk = memory.Chunk(mem, 0, mem.size)
          head, tail = self.pool._split(chunk, self.unit * 2)
          self.assertEqual(head.ptr,      chunk.ptr)
          self.assertEqual(head.offset,   0)
          self.assertEqual(head.size,     self.unit * 2)
          self.assertEqual(head.prev,     None)
          self.assertEqual(head.next.ptr, tail.ptr)
          self.assertEqual(tail.ptr,      chunk.ptr + self.unit * 2)
          self.assertEqual(tail.offset,   self.unit * 2)
          self.assertEqual(tail.size,     self.unit * 2)
          self.assertEqual(tail.prev.ptr, head.ptr)
          self.assertEqual(tail.next,     None)
  
          head_of_head, tail_of_head = self.pool._split(head, self.unit)
          self.assertEqual(head_of_head.ptr,      chunk.ptr)
          self.assertEqual(head_of_head.offset,   0)
          self.assertEqual(head_of_head.size,     self.unit)
          self.assertEqual(head_of_head.prev,     None)
          self.assertEqual(head_of_head.next.ptr, tail_of_head.ptr)
          self.assertEqual(tail_of_head.ptr,      chunk.ptr + self.unit)
          self.assertEqual(tail_of_head.offset,   self.unit)
          self.assertEqual(tail_of_head.size,     self.unit)
          self.assertEqual(tail_of_head.prev.ptr, head_of_head.ptr)
          self.assertEqual(tail_of_head.next.ptr, tail.ptr)
  
          head_of_tail, tail_of_tail = self.pool._split(tail, self.unit)
          self.assertEqual(head_of_tail.ptr,      chunk.ptr + self.unit * 2)
          self.assertEqual(head_of_tail.offset,   self.unit * 2)
          self.assertEqual(head_of_tail.size,     self.unit)
          self.assertEqual(head_of_tail.prev.ptr, tail_of_head.ptr)
          self.assertEqual(head_of_tail.next.ptr, tail_of_tail.ptr)
          self.assertEqual(tail_of_tail.ptr,      chunk.ptr + self.unit * 3)
          self.assertEqual(tail_of_tail.offset,   self.unit * 3)
          self.assertEqual(tail_of_tail.size,     self.unit)
          self.assertEqual(tail_of_tail.prev.ptr, head_of_tail.ptr)
          self.assertEqual(tail_of_tail.next,     None)
  
      def test_merge(self):
          mem = MockMemory(self.unit * 4)
          chunk = memory.Chunk(mem, 0, mem.size)
  
          head, tail = self.pool._split(chunk, self.unit * 2)
          head_ptr, tail_ptr = head.ptr, tail.ptr
          head_of_head, tail_of_head = self.pool._split(head, self.unit)
          head_of_tail, tail_of_tail = self.pool._split(tail, self.unit)
  
          merged_head = self.pool._merge(head_of_head, tail_of_head)
          self.assertEqual(merged_head.ptr,      head.ptr)
          self.assertEqual(merged_head.offset,   head.offset)
          self.assertEqual(merged_head.size,     head.size)
          self.assertEqual(merged_head.prev,     None)
          self.assertEqual(merged_head.next.ptr, tail_ptr)
  
          merged_tail = self.pool._merge(head_of_tail, tail_of_tail)
          self.assertEqual(merged_tail.ptr,      tail.ptr)
          self.assertEqual(merged_tail.offset,   tail.offset)
          self.assertEqual(merged_tail.size,     tail.size)
          self.assertEqual(merged_tail.prev.ptr, head_ptr)
          self.assertEqual(merged_tail.next,     None)
  
          merged = self.pool._merge(merged_head, merged_tail)
          self.assertEqual(merged.ptr,    chunk.ptr)
          self.assertEqual(merged.offset, chunk.offset)
          self.assertEqual(merged.size,   chunk.size)
          self.assertEqual(merged.prev,   None)
          self.assertEqual(merged.next,   None)
  
      def test_alloc(self):
          p1 = self.pool.malloc(self.unit * 4)
          p2 = self.pool.malloc(self.unit * 4)
          p3 = self.pool.malloc(self.unit * 8)
          self.assertNotEqual(p1.ptr, p2.ptr)
          self.assertNotEqual(p1.ptr, p3.ptr)
          self.assertNotEqual(p2.ptr, p3.ptr)
  
      def test_alloc_split(self):
          p = self.pool.malloc(self.unit * 4)
          ptr = p.ptr
          del p
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          self.assertEqual(ptr, head.ptr)
          self.assertEqual(ptr + self.unit * 2, tail.ptr)
  
      def test_free(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          p2 = self.pool.malloc(self.unit * 4)
          self.assertEqual(ptr1, p2.ptr)
  
      def test_free_merge(self):
          p = self.pool.malloc(self.unit * 4)
          ptr = p.ptr
          del p
  
          # merge head into tail
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          self.assertEqual(ptr, head.ptr)
          del tail
          del head
          p = self.pool.malloc(self.unit * 4)
          self.assertEqual(ptr, p.ptr)
          del p
  
          # merge tail into head
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          self.assertEqual(ptr, head.ptr)
          del head
          del tail
          p = self.pool.malloc(self.unit * 4)
          self.assertEqual(ptr, p.ptr)
          del p
  
      def test_free_different_size(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          p2 = self.pool.malloc(self.unit * 8)
          self.assertNotEqual(ptr1, p2.ptr)
  
      def test_free_all_blocks(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          self.pool.free_all_blocks()
          p2 = self.pool.malloc(self.unit * 4)
          self.assertNotEqual(ptr1, p2.ptr)
          del p2
  
          # do not free splitted blocks
          head = self.pool.malloc(self.unit * 2)
          tail = self.pool.malloc(self.unit * 2)
          tailptr = tail.ptr
          del tail
          self.pool.free_all_blocks()
          p = self.pool.malloc(self.unit * 2)
          self.assertEqual(tailptr, p.ptr)
          del head
  
      def test_free_all_free(self):
          p1 = self.pool.malloc(self.unit * 4)
          ptr1 = p1.ptr
          del p1
          with testing.assert_warns(DeprecationWarning):
              self.pool.free_all_free()
          p2 = self.pool.malloc
  
  [... the rest is omitted ...]
files: tests/cupy_tests/cuda_tests/test_memory.py