From 13b176b7c46ec4c18aa38ea9e7378a730023e760 Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Wed, 14 Aug 2024 15:58:31 +0200 Subject: [PATCH 1/7] linked array copy return nparray on cpu --- xobjects/context_cpu.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xobjects/context_cpu.py b/xobjects/context_cpu.py index 2a2ad7f..d716ed8 100644 --- a/xobjects/context_cpu.py +++ b/xobjects/context_cpu.py @@ -106,6 +106,9 @@ def _build_view(cls, a): order="C", ) + def copy(self): + return np.array(self) + def _so_for_module_name(name, containing_dir=".") -> Path: # The so file name is something like: From 2d899c2309aeeab4b42ddf11c849bc8fbb528f38 Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 16 Aug 2024 14:19:44 +0200 Subject: [PATCH 2/7] keep buffer --- xobjects/context.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xobjects/context.py b/xobjects/context.py index a8b0c2c..1cb6d3c 100644 --- a/xobjects/context.py +++ b/xobjects/context.py @@ -210,14 +210,16 @@ class XContext(ABC): def __init__(self): self._kernels = KernelDict() - self._buffers = [] + self._buffers = weakref.WeakSet() + self._allocations = 0 def __str__(self): return type(self).__name__ def new_buffer(self, capacity=1048576): buf = self._make_buffer(capacity=capacity) - self.buffers.append(weakref.finalize(buf, log.debug, "free buf")) + self._buffers.add(buf) + self._allocations += 1 return buf @property From f66f8de93d7766767569f8d83527bfda066e9fcb Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 16 Aug 2024 14:27:43 +0200 Subject: [PATCH 3/7] revert to list --- xobjects/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xobjects/context.py b/xobjects/context.py index 1cb6d3c..776e34f 100644 --- a/xobjects/context.py +++ b/xobjects/context.py @@ -224,7 +224,7 @@ def new_buffer(self, capacity=1048576): @property def buffers(self): - return self._buffers + return list(self._buffers) @property def kernels(self): From 4d034daea62e6df3e7950e64a1d194f359332c7f Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 16 Aug 2024 14:55:20 +0200 Subject: [PATCH 4/7] merge --- xobjects/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xobjects/_version.py b/xobjects/_version.py index 3d26edf..f6b7e26 100644 --- a/xobjects/_version.py +++ b/xobjects/_version.py @@ -1 +1 @@ -__version__ = "0.4.1" +__version__ = "0.4.3" From 215c14ec4f3b294bc9dc9db78dfb18661570ffaa Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 16 Aug 2024 14:57:00 +0200 Subject: [PATCH 5/7] merge --- xobjects/context_cupy.py | 4 +++- xobjects/context_pyopencl.py | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/xobjects/context_cupy.py b/xobjects/context_cupy.py index ba3c427..556162c 100644 --- a/xobjects/context_cupy.py +++ b/xobjects/context_cupy.py @@ -473,6 +473,9 @@ def build_kernels( return out_kernels + def __str__(self): + return f"{type(self).__name__}:{cupy.cuda.get_device_id()}" + def nparray_to_context_array(self, arr): """ Copies a numpy array to the device memory. @@ -631,7 +634,6 @@ class KernelCupy(object): def __init__( self, function, description, block_size, context, shared_mem_size_bytes ): - self.function = function self.description = description self.block_size = block_size diff --git a/xobjects/context_pyopencl.py b/xobjects/context_pyopencl.py index f0ff800..c5277e3 100644 --- a/xobjects/context_pyopencl.py +++ b/xobjects/context_pyopencl.py @@ -218,7 +218,9 @@ def build_kernels( with open(save_source_as, "w") as fid: fid.write(specialized_source) - prg = cl.Program(self.context, specialized_source).build() + prg = cl.Program(self.context, specialized_source).build( + options="-cl-std=CL2.0", + ) out_kernels = {} for pyname, kernel in kernel_descriptions.items(): @@ -236,6 +238,11 @@ def build_kernels( return out_kernels + def __str__(self): + platform_id = cl.get_platforms().index(self.platform) + device_id = self.platform.get_devices().index(self.device) + return f"{type(self).__name__}:{platform_id}.{device_id}" + def nparray_to_context_array(self, arr): """ Copies a numpy array to the device memory. From ebb22a4a33573749e5a7f95dad61bf1d50e1fad5 Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 16 Aug 2024 14:57:38 +0200 Subject: [PATCH 6/7] merge --- xobjects/general.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xobjects/general.py b/xobjects/general.py index 5e1b5fc..9720c0e 100644 --- a/xobjects/general.py +++ b/xobjects/general.py @@ -3,6 +3,7 @@ # Copyright (c) CERN, 2024. # # ########################################### # from numpy.testing import assert_allclose as np_assert_allclose +import numpy as np class Print: @@ -21,4 +22,12 @@ def assert_allclose(a, b, rtol=1e-7, atol=1e-7): a = a.get() if hasattr(b, "get"): b = b.get() + try: + a = np.squeeze(a) + except: + pass + try: + b = np.squeeze(b) + except: + pass np_assert_allclose(a, b, rtol=rtol, atol=atol) From 341319aa65ae0af05db5b70164dd229d74ef9d16 Mon Sep 17 00:00:00 2001 From: Riccardo De Maria Date: Fri, 16 Aug 2024 15:46:05 +0200 Subject: [PATCH 7/7] fix pickle --- xobjects/context.py | 9 +++++++++ xobjects/context_cpu.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/xobjects/context.py b/xobjects/context.py index 776e34f..506fbce 100644 --- a/xobjects/context.py +++ b/xobjects/context.py @@ -222,6 +222,15 @@ def new_buffer(self, capacity=1048576): self._allocations += 1 return buf + def __getstate__(self): + state = self.__dict__ + del state["_buffers"] + return state + + def __setstate__(self, state): + self.__dict__.update(state) + self._buffers = weakref.WeakSet() + @property def buffers(self): return list(self._buffers) diff --git a/xobjects/context_cpu.py b/xobjects/context_cpu.py index d716ed8..a03b443 100644 --- a/xobjects/context_cpu.py +++ b/xobjects/context_cpu.py @@ -10,6 +10,7 @@ import uuid from pathlib import Path from typing import Callable, Dict, List, Sequence, Tuple +import weakref from .general import _print @@ -645,10 +646,12 @@ def openmp_enabled(self): def __getstate__(self): state = self.__dict__.copy() state["_kernels"] = {} + del state["_buffers"] return state def __setstate__(self, state): self.__dict__.update(state) + self._buffers = weakref.WeakSet() class BufferByteArray(XBuffer):