files.new_contents: 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, Ch
  
  [... the rest is omitted ...]
files.new_path: cupy/cuda/memory.pxd
files.old_contents: 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
  
  [... the rest is omitted ...]
files.old_path: cupy/cuda/memory.pxd
files.new_contents: # 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 =
  
  [... the rest is omitted ...]
files.new_path: cupy/cuda/memory.pyx
files.old_contents: # 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)
  
  
  [... the rest is omitted ...]
files.old_path: cupy/cuda/memory.pyx
files.new_contents: 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(
              c
  
  [... the rest is omitted ...]
files.new_path: tests/cupy_tests/cuda_tests/test_memory.py
files.old_contents: 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_c
  
  [... the rest is omitted ...]
files.old_path: tests/cupy_tests/cuda_tests/test_memory.py