From 51bf728cd41f5252b68369b874ee04ac7cafd442 Mon Sep 17 00:00:00 2001 From: Hao Gao Date: Sun, 10 May 2020 13:18:50 -0500 Subject: [PATCH 1/3] Keep level_start_box_nrs in host memory for to_device --- boxtree/tools.py | 12 +++++++++--- boxtree/tree.py | 7 +++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/boxtree/tools.py b/boxtree/tools.py index 5dc5831..23193d7 100644 --- a/boxtree/tools.py +++ b/boxtree/tools.py @@ -270,7 +270,7 @@ class DeviceDataRecord(Record): instances on the host. """ - def _transform_arrays(self, f): + def _transform_arrays(self, f, exclude_fields=frozenset()): result = {} def transform_val(val): @@ -290,6 +290,9 @@ class DeviceDataRecord(Record): return f(val) for field_name in self.__class__.fields: + if field_name in exclude_fields: + continue + try: attr = getattr(self, field_name) except AttributeError: @@ -335,9 +338,12 @@ class DeviceDataRecord(Record): return self._transform_arrays(try_with_queue) - def to_device(self, queue): + def to_device(self, queue, exclude_fields=frozenset()): """ Return a copy of `self` in all :class:`numpy.ndarray` arrays are transferred to device memory as :class:`pyopencl.array.Array` objects. + + :arg exclude_fields: a :class:`frozenset` containing fields excluding from + transferring to the device memory. """ def _to_device(attr): @@ -346,7 +352,7 @@ class DeviceDataRecord(Record): else: return attr - return self._transform_arrays(_to_device) + return self._transform_arrays(_to_device, exclude_fields) # }}} diff --git a/boxtree/tree.py b/boxtree/tree.py index c09ee65..f4614b4 100644 --- a/boxtree/tree.py +++ b/boxtree/tree.py @@ -389,6 +389,13 @@ class Tree(DeviceDataRecord): # }}} + def to_device(self, queue, exclude_fields=frozenset()): + # level_start_box_nrs should remain in host memory + exclude_fields = set(exclude_fields) + exclude_fields.add("level_start_box_nrs") + + return super().to_device(queue, frozenset(exclude_fields)) + # }}} -- GitLab From 2e60bb642ded0fd1a4916c3bf9f1b8b0589ddc7c Mon Sep 17 00:00:00 2001 From: Hao Gao Date: Sun, 10 May 2020 14:21:08 -0500 Subject: [PATCH 2/3] Python2 compatibility --- boxtree/tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boxtree/tree.py b/boxtree/tree.py index f4614b4..a67a7c2 100644 --- a/boxtree/tree.py +++ b/boxtree/tree.py @@ -394,7 +394,7 @@ class Tree(DeviceDataRecord): exclude_fields = set(exclude_fields) exclude_fields.add("level_start_box_nrs") - return super().to_device(queue, frozenset(exclude_fields)) + return super(Tree, self).to_device(queue, frozenset(exclude_fields)) # }}} -- GitLab From 8e5e5c959feea485db3464092dcdeedfe872f944 Mon Sep 17 00:00:00 2001 From: "[6~" Date: Mon, 11 May 2020 17:06:40 -0500 Subject: [PATCH 3/3] Placate flake8 3.8 --- boxtree/tools.py | 6 +++--- boxtree/tree_build.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/boxtree/tools.py b/boxtree/tools.py index 23193d7..9c74d96 100644 --- a/boxtree/tools.py +++ b/boxtree/tools.py @@ -46,9 +46,9 @@ VectorArg = partial(_VectorArg, with_offset=True) AXIS_NAMES = ("x", "y", "z", "w") -def padded_bin(i, l): - """Format *i* as binary number, pad it to length *l*.""" - return bin(i)[2:].rjust(l, "0") +def padded_bin(i, nbits): + """Format *i* as binary number, pad it to length *nbits*.""" + return bin(i)[2:].rjust(nbits, "0") # NOTE: Order of positional args should match GappyCopyAndMapKernel.__call__() diff --git a/boxtree/tree_build.py b/boxtree/tree_build.py index ff944c6..8f652aa 100644 --- a/boxtree/tree_build.py +++ b/boxtree/tree_build.py @@ -718,8 +718,8 @@ class TreeBuilder(object): # Currently undocumented. lr_lookbehind_levels = kwargs.get("lr_lookbehind", 1) minimal_new_level_length += sum( - 2**(l*dimensions) * new_level_leaf_counts[level - l] - for l in range(1, 1 + min(level, lr_lookbehind_levels))) + 2**(lev*dimensions) * new_level_leaf_counts[level - lev] + for lev in range(1, 1 + min(level, lr_lookbehind_levels))) nboxes_minimal = \ sum(minimal_upper_level_lengths) + minimal_new_level_length @@ -740,8 +740,8 @@ class TreeBuilder(object): # Recompute the level padding. for ulevel in range(level): upper_level_padding[ulevel] = sum( - 2**(l*dimensions) * new_level_leaf_counts[ulevel - l] - for l in range( + 2**(lev*dimensions) * new_level_leaf_counts[ulevel - lev] + for lev in range( 1, 1 + min(ulevel, lr_lookbehind_levels))) new_upper_level_unused_box_counts = np.max( -- GitLab