Skip to content

Commit 845b359

Browse files
authored
Merge pull request #139 from rdemaria/memory_optim
linked array copy return nparray on cpu
2 parents 78adb14 + 341319a commit 845b359

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

xobjects/context.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,30 @@ class XContext(ABC):
210210

211211
def __init__(self):
212212
self._kernels = KernelDict()
213-
self._buffers = []
213+
self._buffers = weakref.WeakSet()
214+
self._allocations = 0
214215

215216
def __str__(self):
216217
return type(self).__name__
217218

218219
def new_buffer(self, capacity=1048576):
219220
buf = self._make_buffer(capacity=capacity)
220-
self.buffers.append(weakref.finalize(buf, log.debug, "free buf"))
221+
self._buffers.add(buf)
222+
self._allocations += 1
221223
return buf
222224

225+
def __getstate__(self):
226+
state = self.__dict__
227+
del state["_buffers"]
228+
return state
229+
230+
def __setstate__(self, state):
231+
self.__dict__.update(state)
232+
self._buffers = weakref.WeakSet()
233+
223234
@property
224235
def buffers(self):
225-
return self._buffers
236+
return list(self._buffers)
226237

227238
@property
228239
def kernels(self):

xobjects/context_cpu.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import uuid
1111
from pathlib import Path
1212
from typing import Callable, Dict, List, Sequence, Tuple
13+
import weakref
1314

1415
from .general import _print
1516

@@ -106,6 +107,9 @@ def _build_view(cls, a):
106107
order="C",
107108
)
108109

110+
def copy(self):
111+
return np.array(self)
112+
109113

110114
def _so_for_module_name(name, containing_dir=".") -> Path:
111115
# The so file name is something like:
@@ -642,10 +646,12 @@ def openmp_enabled(self):
642646
def __getstate__(self):
643647
state = self.__dict__.copy()
644648
state["_kernels"] = {}
649+
del state["_buffers"]
645650
return state
646651

647652
def __setstate__(self, state):
648653
self.__dict__.update(state)
654+
self._buffers = weakref.WeakSet()
649655

650656

651657
class BufferByteArray(XBuffer):

xobjects/context_cupy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,6 @@ class KernelCupy(object):
634634
def __init__(
635635
self, function, description, block_size, context, shared_mem_size_bytes
636636
):
637-
638637
self.function = function
639638
self.description = description
640639
self.block_size = block_size

0 commit comments

Comments
 (0)