xobjects 0.7.2__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {xobjects-0.7.2/xobjects.egg-info → xobjects-0.8.0}/PKG-INFO +1 -1
- xobjects-0.8.0/tests/test_align.py +249 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_buffer.py +37 -3
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_hybrid_class.py +93 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_kernel.py +1 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_nplike_arrays.py +24 -0
- xobjects-0.8.0/tests/test_settings.py +27 -0
- xobjects-0.8.0/xobjects/_version.py +1 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/array.py +28 -19
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/context.py +4 -4
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/context_cupy.py +135 -11
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/context_pyopencl.py +15 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/headers/atomicadd.h +3 -3
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/hybrid_class.py +34 -2
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/raw_union.py +9 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/ref.py +3 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/scalar.py +2 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/settings.py +36 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/string.py +2 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/struct.py +27 -21
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/typeutils.py +5 -3
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/union.py +16 -5
- {xobjects-0.7.2 → xobjects-0.8.0/xobjects.egg-info}/PKG-INFO +1 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects.egg-info/SOURCES.txt +1 -0
- xobjects-0.7.2/tests/test_align.py +0 -18
- xobjects-0.7.2/xobjects/_version.py +0 -1
- {xobjects-0.7.2 → xobjects-0.8.0}/Architecture.md +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/LICENSE +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/MANIFEST.in +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/README.md +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/pyproject.toml +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/setup.cfg +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/setup.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_array.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_capi.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_chunk.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_common.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_context_opencl.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_general.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_linked_array.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_raw_union.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_ref.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_scalars.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_shared_memory.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_strides.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_string.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_struct.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_to_dict.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_typeutils.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/tests/test_unionref.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/__init__.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/_patch_pyopencl_array.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/capi.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/context_cpu.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/general.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/headers/common.h +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/linkedarray.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/specialize_source.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects/test_helpers.py +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects.egg-info/dependency_links.txt +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects.egg-info/entry_points.txt +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects.egg-info/requires.txt +0 -0
- {xobjects-0.7.2 → xobjects-0.8.0}/xobjects.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# copyright ################################# #
|
|
2
|
+
# This file is part of the Xobjects Package. #
|
|
3
|
+
# Copyright (c) CERN, 2021. #
|
|
4
|
+
# ########################################### #
|
|
5
|
+
|
|
6
|
+
import xobjects as xo
|
|
7
|
+
import ctypes
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
from xobjects.scalar import Complex64, Complex128
|
|
12
|
+
from xobjects.test_helpers import for_all_test_contexts
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@for_all_test_contexts
|
|
16
|
+
def test_create_aligned_objects(test_context):
|
|
17
|
+
for Arr in xo.Float64[3], xo.Float64[16], xo.Float64[17]:
|
|
18
|
+
buff = test_context.new_buffer(10)
|
|
19
|
+
assert buff.default_alignment == test_context.minimum_alignment
|
|
20
|
+
for i in range(4):
|
|
21
|
+
aa = Arr(_buffer=buff, _offset="aligned")
|
|
22
|
+
print(test_context, Arr._size, aa._offset)
|
|
23
|
+
assert aa._offset % test_context.minimum_alignment == 0
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.mark.parametrize(
|
|
27
|
+
"scalar, alignment",
|
|
28
|
+
[
|
|
29
|
+
(xo.UInt8, 1),
|
|
30
|
+
(xo.Int16, 2),
|
|
31
|
+
(xo.Int32, 4),
|
|
32
|
+
(xo.Float64, 8),
|
|
33
|
+
(Complex64, 4),
|
|
34
|
+
(Complex128, 8),
|
|
35
|
+
],
|
|
36
|
+
)
|
|
37
|
+
def test_type_alignment(scalar, alignment):
|
|
38
|
+
assert scalar._alignment == alignment
|
|
39
|
+
assert scalar[13]._alignment == scalar._alignment
|
|
40
|
+
assert scalar[13]._size == 13 * scalar._size
|
|
41
|
+
assert scalar[:]._alignment == max(8, scalar._alignment)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_struct_matches_native_layout():
|
|
45
|
+
class Native(ctypes.Structure):
|
|
46
|
+
_fields_ = [
|
|
47
|
+
("a", ctypes.c_uint8),
|
|
48
|
+
("b", ctypes.c_uint8),
|
|
49
|
+
("c", ctypes.c_int16),
|
|
50
|
+
("d", ctypes.c_float),
|
|
51
|
+
("e", ctypes.c_double),
|
|
52
|
+
("f", ctypes.c_uint8 * 13),
|
|
53
|
+
("g", ctypes.c_uint8),
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
class Compact(xo.Struct):
|
|
57
|
+
a = xo.UInt8
|
|
58
|
+
b = xo.UInt8
|
|
59
|
+
c = xo.Int16
|
|
60
|
+
d = xo.Float32
|
|
61
|
+
e = xo.Float64
|
|
62
|
+
f = xo.UInt8[13]
|
|
63
|
+
g = xo.UInt8
|
|
64
|
+
|
|
65
|
+
assert Compact._alignment == ctypes.alignment(Native)
|
|
66
|
+
assert Compact._size == ctypes.sizeof(Native)
|
|
67
|
+
for field in Compact._fields:
|
|
68
|
+
assert field.offset == getattr(Native, field.name).offset
|
|
69
|
+
|
|
70
|
+
values = dict(a=1, b=2, c=3, d=4, e=5, f=list(range(13)), g=6)
|
|
71
|
+
obj = Compact(**values)
|
|
72
|
+
native = Native.from_buffer_copy(
|
|
73
|
+
obj._buffer.to_bytearray(obj._offset, obj._size)
|
|
74
|
+
)
|
|
75
|
+
assert list(native.f) == values["f"]
|
|
76
|
+
assert native.g == 6
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@for_all_test_contexts
|
|
80
|
+
def test_nested_complex_alignment(test_context):
|
|
81
|
+
class Inner(xo.Struct):
|
|
82
|
+
flag = xo.UInt8
|
|
83
|
+
small = Complex64
|
|
84
|
+
large = Complex128
|
|
85
|
+
tail = xo.UInt8
|
|
86
|
+
|
|
87
|
+
class Outer(xo.Struct):
|
|
88
|
+
flag = xo.UInt8
|
|
89
|
+
items = Inner[2]
|
|
90
|
+
tail = xo.UInt8
|
|
91
|
+
|
|
92
|
+
assert [ff.offset for ff in Inner._fields] == [0, 4, 16, 32]
|
|
93
|
+
assert Inner._alignment == 8
|
|
94
|
+
assert Inner._size == 40
|
|
95
|
+
assert Outer.items.offset == 8
|
|
96
|
+
assert Outer._alignment == 8
|
|
97
|
+
assert Outer._size == 96
|
|
98
|
+
values = dict(flag=1, small=2 + 3j, large=4 + 5j, tail=6)
|
|
99
|
+
obj = Outer(items=[values, values], _context=test_context)
|
|
100
|
+
for item in obj.items:
|
|
101
|
+
assert item._offset % 8 == 0
|
|
102
|
+
assert item.small == values["small"]
|
|
103
|
+
assert item.large == values["large"]
|
|
104
|
+
assert item.tail == 6
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@pytest.mark.parametrize(
|
|
108
|
+
"shape, data_offset", [((None,), 16), ((2, None), 32), ((None, None), 40)]
|
|
109
|
+
)
|
|
110
|
+
@for_all_test_contexts
|
|
111
|
+
def test_dynamic_complex_array_alignment(shape, data_offset, test_context):
|
|
112
|
+
array_type = Complex128[shape]
|
|
113
|
+
values = np.arange(6).reshape((6,) if len(shape) == 1 else (2, 3))
|
|
114
|
+
values = values.astype("complex128") + 2j
|
|
115
|
+
array = array_type(values, _context=test_context)
|
|
116
|
+
assert array_type._data_offset == data_offset
|
|
117
|
+
np.testing.assert_array_equal(array.to_nparray(), values)
|
|
118
|
+
restored = array_type._from_buffer(array._buffer, array._offset)
|
|
119
|
+
np.testing.assert_array_equal(restored.to_nparray(), values)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@for_all_test_contexts
|
|
123
|
+
def test_dynamic_struct_alignment(test_context):
|
|
124
|
+
class Dynamic(xo.Struct):
|
|
125
|
+
tag = xo.UInt8
|
|
126
|
+
tail = xo.UInt8
|
|
127
|
+
text = xo.String
|
|
128
|
+
values = Complex128[:]
|
|
129
|
+
|
|
130
|
+
assert Dynamic._alignment == 8
|
|
131
|
+
assert Dynamic.tag.offset == 8
|
|
132
|
+
assert Dynamic.tail.offset == 9
|
|
133
|
+
assert Dynamic.values.offset == 16 # Int64 offset slot
|
|
134
|
+
assert Dynamic.text.offset == 24
|
|
135
|
+
values = dict(tag=1, tail=2, text="x", values=[3 + 4j])
|
|
136
|
+
obj = Dynamic(**values, _context=test_context)
|
|
137
|
+
assert obj.values._offset - obj._offset == 40
|
|
138
|
+
assert obj._size == 72
|
|
139
|
+
assert obj.text == "x"
|
|
140
|
+
assert obj.values[0] == 3 + 4j
|
|
141
|
+
for array_type in (Dynamic[2], Dynamic[3], Dynamic[:]):
|
|
142
|
+
count = array_type._shape[0] or 3
|
|
143
|
+
array = array_type([values] * count, _context=test_context)
|
|
144
|
+
restored = array_type._from_buffer(array._buffer, array._offset)
|
|
145
|
+
assert isinstance(restored._offsets, np.ndarray)
|
|
146
|
+
for item in restored:
|
|
147
|
+
assert item._offset % 8 == 0
|
|
148
|
+
assert item.values._offset % 8 == 0
|
|
149
|
+
assert item.text == "x"
|
|
150
|
+
assert item.values[0] == 3 + 4j
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@pytest.mark.parametrize("default_alignment", [1, 8, 128, 512])
|
|
154
|
+
@pytest.mark.parametrize("offset", [None, "aligned"])
|
|
155
|
+
def test_allocation_honors_type_and_buffer_alignment(
|
|
156
|
+
default_alignment, offset
|
|
157
|
+
):
|
|
158
|
+
buffer = xo.ContextCpu().new_buffer(1)
|
|
159
|
+
buffer.default_alignment = default_alignment
|
|
160
|
+
xo.UInt8[1](_buffer=buffer)
|
|
161
|
+
obj = Complex128[1]([1 + 2j], _buffer=buffer, _offset=offset)
|
|
162
|
+
assert obj._offset == max(8, default_alignment)
|
|
163
|
+
assert obj[0] == 1 + 2j
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@pytest.mark.parametrize("offset", ["packed", 1])
|
|
167
|
+
def test_explicit_offsets_bypass_allocation_alignment(offset):
|
|
168
|
+
buffer = xo.ContextCpu().new_buffer(128)
|
|
169
|
+
buffer.default_alignment = 128
|
|
170
|
+
xo.UInt8[1](_buffer=buffer)
|
|
171
|
+
obj = Complex128[1]([1 + 2j], _buffer=buffer, _offset=offset)
|
|
172
|
+
assert obj._offset == 1
|
|
173
|
+
assert obj[0] == 1 + 2j
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@for_all_test_contexts
|
|
177
|
+
def test_first_dynamic_field_alignment(test_context):
|
|
178
|
+
class FirstDynamic(xo.Struct):
|
|
179
|
+
tag = xo.UInt8
|
|
180
|
+
values = Complex128[:]
|
|
181
|
+
|
|
182
|
+
assert FirstDynamic.values.offset == 16
|
|
183
|
+
obj = FirstDynamic(tag=1, values=[2 + 3j], _context=test_context)
|
|
184
|
+
assert obj.values._offset % 8 == 0
|
|
185
|
+
assert obj.values[0] == 2 + 3j
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def test_fixed_string_array_stride():
|
|
189
|
+
string_type = xo.String.fixed(10)
|
|
190
|
+
for array_type in (string_type[3], string_type[:]):
|
|
191
|
+
array = array_type([0, 0, 0])
|
|
192
|
+
assert array._strides == (16,)
|
|
193
|
+
for index in range(3):
|
|
194
|
+
assert array._get_offset(index) % 8 == 0
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def test_reference_and_union_alignment():
|
|
198
|
+
class ByteStruct(xo.Struct):
|
|
199
|
+
value = xo.UInt8
|
|
200
|
+
|
|
201
|
+
class WideUnion(xo.RawUnion):
|
|
202
|
+
byte = xo.UInt8
|
|
203
|
+
complex = Complex128
|
|
204
|
+
|
|
205
|
+
assert xo.Ref[ByteStruct]._alignment == 8
|
|
206
|
+
assert xo.UnionRef._alignment == 8
|
|
207
|
+
assert xo.String._alignment == 8
|
|
208
|
+
assert WideUnion._alignment == 8
|
|
209
|
+
assert WideUnion._size == 16
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
@for_all_test_contexts
|
|
213
|
+
def test_compact_struct_generated_accessors(test_context):
|
|
214
|
+
class CompactKernel(xo.Struct):
|
|
215
|
+
a = xo.UInt8
|
|
216
|
+
b = xo.Int16
|
|
217
|
+
bytes = xo.UInt8[3]
|
|
218
|
+
tail = xo.UInt8
|
|
219
|
+
|
|
220
|
+
source = """
|
|
221
|
+
/*gpukern*/ void check_compact_accessors(
|
|
222
|
+
CompactKernel obj, /*gpuglmem*/ int64_t* result) {
|
|
223
|
+
result[0] = CompactKernel_get_b(obj);
|
|
224
|
+
result[1] = CompactKernel_get_bytes(obj, 2);
|
|
225
|
+
CompactKernel_set_tail(obj, 7);
|
|
226
|
+
}
|
|
227
|
+
"""
|
|
228
|
+
test_context.add_kernels(
|
|
229
|
+
sources=[source],
|
|
230
|
+
kernels={
|
|
231
|
+
"check_compact_accessors": xo.Kernel(
|
|
232
|
+
args=[
|
|
233
|
+
xo.Arg(CompactKernel, name="obj"),
|
|
234
|
+
xo.Arg(xo.Int64, pointer=True, name="result"),
|
|
235
|
+
],
|
|
236
|
+
n_threads=1,
|
|
237
|
+
),
|
|
238
|
+
},
|
|
239
|
+
)
|
|
240
|
+
obj = CompactKernel(
|
|
241
|
+
a=1, b=2, bytes=[3, 4, 5], tail=6, _context=test_context
|
|
242
|
+
)
|
|
243
|
+
assert CompactKernel._size == 8
|
|
244
|
+
result = test_context.zeros(2, dtype=np.int64)
|
|
245
|
+
test_context.kernels.check_compact_accessors(obj=obj, result=result)
|
|
246
|
+
np.testing.assert_array_equal(
|
|
247
|
+
test_context.nparray_from_context_array(result), [2, 5]
|
|
248
|
+
)
|
|
249
|
+
assert obj.tail == 7
|
|
@@ -62,6 +62,19 @@ def test_new_buffer(test_context):
|
|
|
62
62
|
_ = test_context.new_buffer(capacity=200)
|
|
63
63
|
|
|
64
64
|
|
|
65
|
+
@for_all_test_contexts
|
|
66
|
+
def test_new_buffer_is_zeroed(test_context):
|
|
67
|
+
buff = test_context.new_buffer(capacity=200)
|
|
68
|
+
assert buff.to_bytearray(0, 200) == bytearray(200)
|
|
69
|
+
|
|
70
|
+
# The capacity added when growing must be zeroed too, while the existing
|
|
71
|
+
# data is kept.
|
|
72
|
+
buff.update_from_buffer(0, b"\xff" * 200)
|
|
73
|
+
buff.grow(300)
|
|
74
|
+
assert buff.to_bytearray(0, 200) == b"\xff" * 200
|
|
75
|
+
assert buff.to_bytearray(200, 300) == bytearray(300)
|
|
76
|
+
|
|
77
|
+
|
|
65
78
|
@for_all_test_contexts
|
|
66
79
|
def test_read_write(test_context):
|
|
67
80
|
buff = test_context.new_buffer()
|
|
@@ -130,14 +143,35 @@ def test_free(test_context):
|
|
|
130
143
|
@for_all_test_contexts
|
|
131
144
|
def test_grow(test_context):
|
|
132
145
|
ch = Check(test_context, 200)
|
|
146
|
+
alignment = ch.buffer.default_alignment
|
|
147
|
+
|
|
148
|
+
def aligned(offset):
|
|
149
|
+
return -(-offset // alignment) * alignment
|
|
150
|
+
|
|
151
|
+
def grown(capacity, size):
|
|
152
|
+
# Without grow_step, a full buffer grows by its own capacity, or by
|
|
153
|
+
# enough to fit an aligned allocation of `size`, whichever is larger.
|
|
154
|
+
return capacity + max(capacity, size + alignment - 1)
|
|
155
|
+
|
|
133
156
|
st = ch.new_string(150)
|
|
157
|
+
assert st == 0
|
|
158
|
+
|
|
159
|
+
# The second string never fits after the first one in 200 bytes.
|
|
134
160
|
st = ch.new_string(60)
|
|
135
161
|
ch.check()
|
|
136
|
-
|
|
162
|
+
capacity = grown(200, 60)
|
|
163
|
+
assert st == aligned(150)
|
|
164
|
+
assert ch.buffer.capacity == capacity
|
|
137
165
|
assert ch.buffer.chunks[0].start == st + 60
|
|
138
|
-
assert ch.buffer.chunks[0].end ==
|
|
166
|
+
assert ch.buffer.chunks[0].end == capacity
|
|
167
|
+
|
|
168
|
+
# The third string is larger than the whole buffer.
|
|
169
|
+
free_start = st + 60
|
|
139
170
|
st = ch.new_string(500)
|
|
140
|
-
assert
|
|
171
|
+
assert aligned(free_start) + 500 > capacity
|
|
172
|
+
capacity = grown(capacity, 500)
|
|
173
|
+
assert st == aligned(free_start)
|
|
174
|
+
assert ch.buffer.capacity == capacity
|
|
141
175
|
ch.check()
|
|
142
176
|
|
|
143
177
|
|
|
@@ -427,3 +427,96 @@ def test_to_dict_python_vars():
|
|
|
427
427
|
assert all(td3.a == td2.a)
|
|
428
428
|
assert td3.b == td2.b
|
|
429
429
|
assert td3.c == td2.c
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def test_on_context_change():
|
|
433
|
+
class TestClass(xo.HybridClass):
|
|
434
|
+
_xofields = {
|
|
435
|
+
"value": xo.Float64,
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
def __init__(self, **kwargs):
|
|
439
|
+
self.context_changes = []
|
|
440
|
+
super().__init__(**kwargs)
|
|
441
|
+
|
|
442
|
+
def _on_context_change(self, old_context, new_context):
|
|
443
|
+
self.context_changes.append((old_context, new_context))
|
|
444
|
+
|
|
445
|
+
context0 = xo.ContextCpu()
|
|
446
|
+
context1 = xo.ContextCpu(omp_num_threads="auto")
|
|
447
|
+
|
|
448
|
+
# Initial construction
|
|
449
|
+
obj = TestClass(value=3.0, _context=context0)
|
|
450
|
+
|
|
451
|
+
assert obj._context is context0
|
|
452
|
+
assert obj.context_changes == [
|
|
453
|
+
(None, context0),
|
|
454
|
+
]
|
|
455
|
+
|
|
456
|
+
# Move to a different context
|
|
457
|
+
obj.move(_context=context1)
|
|
458
|
+
|
|
459
|
+
assert obj._context is context1
|
|
460
|
+
assert obj.context_changes == [
|
|
461
|
+
(None, context0),
|
|
462
|
+
(context0, context1),
|
|
463
|
+
]
|
|
464
|
+
|
|
465
|
+
# Moving within the same context should not trigger the hook
|
|
466
|
+
buffer1 = context1.new_buffer()
|
|
467
|
+
obj.move(_buffer=buffer1)
|
|
468
|
+
|
|
469
|
+
assert obj._context is context1
|
|
470
|
+
assert obj.context_changes == [
|
|
471
|
+
(None, context0),
|
|
472
|
+
(context0, context1),
|
|
473
|
+
]
|
|
474
|
+
|
|
475
|
+
# Copy to another context. The hook must run on the copy,
|
|
476
|
+
# without modifying the source object's history.
|
|
477
|
+
copied = obj.copy(_context=context0)
|
|
478
|
+
|
|
479
|
+
assert copied is not obj
|
|
480
|
+
assert copied._context is context0
|
|
481
|
+
|
|
482
|
+
assert obj.context_changes == [
|
|
483
|
+
(None, context0),
|
|
484
|
+
(context0, context1),
|
|
485
|
+
]
|
|
486
|
+
|
|
487
|
+
assert copied.context_changes == [
|
|
488
|
+
(None, context0),
|
|
489
|
+
(context0, context1),
|
|
490
|
+
(context1, context0),
|
|
491
|
+
]
|
|
492
|
+
|
|
493
|
+
# A copy in the same context should not add another event.
|
|
494
|
+
copied_same_context = obj.copy()
|
|
495
|
+
|
|
496
|
+
assert copied_same_context._context is context1
|
|
497
|
+
assert copied_same_context.context_changes == [
|
|
498
|
+
(None, context0),
|
|
499
|
+
(context0, context1),
|
|
500
|
+
]
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def test_on_context_change_with_direct_xoinitialize():
|
|
504
|
+
class TestClass(xo.HybridClass):
|
|
505
|
+
_xofields = {
|
|
506
|
+
"value": xo.Float64,
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
def __init__(self, **kwargs):
|
|
510
|
+
self.context_changes = []
|
|
511
|
+
self.xoinitialize(**kwargs)
|
|
512
|
+
|
|
513
|
+
def _on_context_change(self, old_context, new_context):
|
|
514
|
+
self.context_changes.append((old_context, new_context))
|
|
515
|
+
|
|
516
|
+
context = xo.ContextCpu()
|
|
517
|
+
|
|
518
|
+
obj = TestClass(value=3.0, _context=context)
|
|
519
|
+
|
|
520
|
+
assert obj.context_changes == [
|
|
521
|
+
(None, context),
|
|
522
|
+
]
|
|
@@ -15,7 +15,7 @@ import xobjects as xo
|
|
|
15
15
|
from xobjects.test_helpers import for_all_test_contexts, requires_context
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
@pytest.mark.parametrize(
|
|
18
|
+
@pytest.mark.parametrize("pickle_context", [False, True])
|
|
19
19
|
def test_kernel_cpu(pickle_context):
|
|
20
20
|
ctx = xo.ContextCpu()
|
|
21
21
|
src_code = r"""
|
|
@@ -16,6 +16,30 @@ def test_type(test_context):
|
|
|
16
16
|
assert isinstance(a, test_context.nplike_array_type)
|
|
17
17
|
|
|
18
18
|
|
|
19
|
+
@for_all_test_contexts
|
|
20
|
+
@pytest.mark.parametrize(
|
|
21
|
+
"layout",
|
|
22
|
+
["column", "reversed_column", "strided_matrix", "C", "F", "scalar"],
|
|
23
|
+
)
|
|
24
|
+
def test_nparray_to_context_array_layout(test_context, layout):
|
|
25
|
+
base = np.arange(24, dtype=np.float64).reshape(6, 4)
|
|
26
|
+
values = {
|
|
27
|
+
"column": base[:, 0],
|
|
28
|
+
"reversed_column": base[::-1, 0],
|
|
29
|
+
"strided_matrix": base[::2, ::2],
|
|
30
|
+
"C": base,
|
|
31
|
+
"F": np.asfortranarray(base),
|
|
32
|
+
"scalar": np.array(3.0),
|
|
33
|
+
}[layout]
|
|
34
|
+
|
|
35
|
+
transferred = test_context.nparray_to_context_array(values)
|
|
36
|
+
restored = test_context.nparray_from_context_array(transferred)
|
|
37
|
+
|
|
38
|
+
assert restored.shape == values.shape
|
|
39
|
+
assert restored.dtype == values.dtype
|
|
40
|
+
np.testing.assert_array_equal(restored, values)
|
|
41
|
+
|
|
42
|
+
|
|
19
43
|
@for_all_test_contexts
|
|
20
44
|
def test_ffts(test_context):
|
|
21
45
|
if "Pyopencl" in str(test_context):
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# copyright ################################# #
|
|
2
|
+
# This file is part of the Xobjects Package. #
|
|
3
|
+
# Copyright (c) CERN, 2026. #
|
|
4
|
+
# ########################################### #
|
|
5
|
+
|
|
6
|
+
import xobjects as xo
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_cuda_compute_capability():
|
|
11
|
+
assert xo.ContextCupy().cuda_compute_capability is None
|
|
12
|
+
|
|
13
|
+
with xo.settings.override(cuda_compute_capability=90):
|
|
14
|
+
assert xo.ContextCupy().cuda_compute_capability == 90
|
|
15
|
+
assert (
|
|
16
|
+
xo.ContextCupy(cuda_compute_capability=80).cuda_compute_capability
|
|
17
|
+
== 80
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
with pytest.raises(ValueError):
|
|
21
|
+
xo.ContextCupy(cuda_compute_capability=0)
|
|
22
|
+
|
|
23
|
+
with pytest.raises(ValueError):
|
|
24
|
+
xo.ContextCupy(cuda_compute_capability=-1)
|
|
25
|
+
|
|
26
|
+
with pytest.raises(ValueError):
|
|
27
|
+
xo.ContextCupy(cuda_compute_capability="90")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.8.0"
|
|
@@ -12,11 +12,11 @@ from .typeutils import (
|
|
|
12
12
|
allocate_on_buffer,
|
|
13
13
|
Info,
|
|
14
14
|
is_integer,
|
|
15
|
-
_to_slot_size,
|
|
16
15
|
default_conf,
|
|
17
16
|
)
|
|
18
17
|
from .ref import is_ref, is_unionref
|
|
19
18
|
from .scalar import Int64, is_scalar
|
|
19
|
+
from .context import _align
|
|
20
20
|
|
|
21
21
|
log = logging.getLogger(__name__)
|
|
22
22
|
|
|
@@ -186,7 +186,8 @@ class MetaArray(type):
|
|
|
186
186
|
data["_is_static_type"] = False
|
|
187
187
|
else:
|
|
188
188
|
data["_is_static_type"] = True
|
|
189
|
-
static_size = _itemtype._size
|
|
189
|
+
static_size = _align(_itemtype._size, _itemtype._alignment)
|
|
190
|
+
data["_itemsize"] = static_size
|
|
190
191
|
if "_shape" not in data:
|
|
191
192
|
raise ValueError(f"No shape defined for {cls}")
|
|
192
193
|
if "_order" not in data:
|
|
@@ -213,14 +214,18 @@ class MetaArray(type):
|
|
|
213
214
|
)
|
|
214
215
|
|
|
215
216
|
if data["_is_static_shape"] and data["_is_static_type"]:
|
|
216
|
-
|
|
217
|
+
alignment = _itemtype._alignment
|
|
218
|
+
_size = static_size
|
|
217
219
|
for d in _shape:
|
|
218
220
|
_size *= d
|
|
219
|
-
_size = _to_slot_size(_size)
|
|
220
221
|
else:
|
|
222
|
+
alignment = max(8, _itemtype._alignment)
|
|
221
223
|
_size = None
|
|
222
224
|
_data_offset += 8 # space for dynamic size
|
|
225
|
+
if data["_is_static_type"]:
|
|
226
|
+
_data_offset = _align(_data_offset, _itemtype._alignment)
|
|
223
227
|
|
|
228
|
+
data["_alignment"] = alignment
|
|
224
229
|
data["_size"] = _size
|
|
225
230
|
data["_data_offset"] = _data_offset
|
|
226
231
|
# need to applied to derived classes as well
|
|
@@ -256,6 +261,7 @@ class MetaArray(type):
|
|
|
256
261
|
|
|
257
262
|
|
|
258
263
|
class Array(metaclass=MetaArray):
|
|
264
|
+
_alignment: int
|
|
259
265
|
_shape: tuple
|
|
260
266
|
_order: tuple
|
|
261
267
|
_strides: tuple
|
|
@@ -326,8 +332,8 @@ class Array(metaclass=MetaArray):
|
|
|
326
332
|
offset = 0
|
|
327
333
|
for idx in iter_index(shape, order):
|
|
328
334
|
offsets[idx] = offset
|
|
329
|
-
offset += cls.
|
|
330
|
-
assert size ==
|
|
335
|
+
offset += cls._itemsize
|
|
336
|
+
assert size == offset
|
|
331
337
|
else: # handling other cases
|
|
332
338
|
offset = 8 # space for size data
|
|
333
339
|
# determine shape and order
|
|
@@ -379,19 +385,15 @@ class Array(metaclass=MetaArray):
|
|
|
379
385
|
offset += len(shape) * 8 # space for strides
|
|
380
386
|
order = mk_order(cls._order, shape)
|
|
381
387
|
if cls._is_static_type:
|
|
382
|
-
strides = get_strides(shape, order, cls.
|
|
388
|
+
strides = get_strides(shape, order, cls._itemsize)
|
|
383
389
|
else:
|
|
384
390
|
strides = get_strides(shape, order, 8)
|
|
385
391
|
items = np.prod(shape)
|
|
386
392
|
|
|
387
393
|
# needs items, order, shape, value
|
|
388
394
|
if cls._is_static_type:
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
# offsets[idx] = offset
|
|
392
|
-
# offset += cls._itemtype._size
|
|
393
|
-
offset += cls._itemtype._size * items
|
|
394
|
-
size = _to_slot_size(offset)
|
|
395
|
+
offset = cls._data_offset + cls._itemsize * items
|
|
396
|
+
size = _align(offset, cls._alignment)
|
|
395
397
|
|
|
396
398
|
else:
|
|
397
399
|
# args must be an array of correct dimensions
|
|
@@ -399,9 +401,10 @@ class Array(metaclass=MetaArray):
|
|
|
399
401
|
offset += items * 8
|
|
400
402
|
for idx in iter_index(shape, order):
|
|
401
403
|
extra[idx] = cls._itemtype._inspect_args(value[idx])
|
|
404
|
+
offset = _align(offset, cls._itemtype._alignment)
|
|
402
405
|
offsets[idx] = offset
|
|
403
406
|
offset += extra[idx].size
|
|
404
|
-
size =
|
|
407
|
+
size = _align(offset, cls._alignment)
|
|
405
408
|
info.offsets = offsets
|
|
406
409
|
info.extra = extra
|
|
407
410
|
|
|
@@ -439,7 +442,7 @@ class Array(metaclass=MetaArray):
|
|
|
439
442
|
coffset += 8
|
|
440
443
|
else:
|
|
441
444
|
if cls._is_static_type:
|
|
442
|
-
strides = (cls.
|
|
445
|
+
strides = (cls._itemsize,)
|
|
443
446
|
else:
|
|
444
447
|
strides = (8,)
|
|
445
448
|
self._strides = tuple(strides)
|
|
@@ -447,7 +450,11 @@ class Array(metaclass=MetaArray):
|
|
|
447
450
|
shape = cls._shape
|
|
448
451
|
if not cls._is_static_type:
|
|
449
452
|
items = np.prod(shape)
|
|
450
|
-
|
|
453
|
+
# Offsets are host metadata, also when the payload lives on a GPU.
|
|
454
|
+
# Device scalars must not become offsets used to slice the byte buffer.
|
|
455
|
+
self._offsets = buffer.context.nparray_from_context_array(
|
|
456
|
+
Int64._array_from_buffer(buffer, coffset, items)
|
|
457
|
+
)
|
|
451
458
|
return self
|
|
452
459
|
|
|
453
460
|
@classmethod
|
|
@@ -473,6 +480,8 @@ class Array(metaclass=MetaArray):
|
|
|
473
480
|
if not cls._is_static_type:
|
|
474
481
|
Int64._array_to_buffer(buffer, coffset, info.offsets)
|
|
475
482
|
coffset += 8 * len(info.offsets)
|
|
483
|
+
else:
|
|
484
|
+
coffset = offset + cls._data_offset
|
|
476
485
|
if hasattr(cls._itemtype, "_dtype") and hasattr(
|
|
477
486
|
value, "dtype"
|
|
478
487
|
): # is a scalar type:
|
|
@@ -495,7 +504,7 @@ class Array(metaclass=MetaArray):
|
|
|
495
504
|
ioffset = offset + cls._data_offset
|
|
496
505
|
for idx in range(info.items):
|
|
497
506
|
cls._itemtype._to_buffer(buffer, ioffset, value, None)
|
|
498
|
-
ioffset += cls.
|
|
507
|
+
ioffset += cls._itemsize
|
|
499
508
|
else:
|
|
500
509
|
for idx in iter_index(info.shape, cls._order):
|
|
501
510
|
cls._itemtype._to_buffer(
|
|
@@ -513,7 +522,7 @@ class Array(metaclass=MetaArray):
|
|
|
513
522
|
cls._itemtype._to_buffer(
|
|
514
523
|
buffer, ioffset, value[idx], info=None
|
|
515
524
|
)
|
|
516
|
-
ioffset += cls.
|
|
525
|
+
ioffset += cls._itemsize
|
|
517
526
|
else:
|
|
518
527
|
for idx in iter_index(info.shape, cls._order):
|
|
519
528
|
cls._itemtype._to_buffer(
|
|
@@ -529,7 +538,7 @@ class Array(metaclass=MetaArray):
|
|
|
529
538
|
info = cls._inspect_args(*args)
|
|
530
539
|
|
|
531
540
|
self._buffer, self._offset = allocate_on_buffer(
|
|
532
|
-
info.size, _context, _buffer, _offset
|
|
541
|
+
info.size, _context, _buffer, _offset, alignment=cls._alignment
|
|
533
542
|
)
|
|
534
543
|
|
|
535
544
|
cls._to_buffer(self._buffer, self._offset, info.value, info)
|
|
@@ -143,7 +143,7 @@ def _concatenate_sources(sources, apply_to_source=()):
|
|
|
143
143
|
|
|
144
144
|
|
|
145
145
|
def _align(offset, alignment):
|
|
146
|
-
"
|
|
146
|
+
"Round up to a multiple of the power-of-two alignment."
|
|
147
147
|
return (offset + alignment - 1) & (-alignment)
|
|
148
148
|
|
|
149
149
|
|
|
@@ -493,11 +493,11 @@ class XBuffer(ABC):
|
|
|
493
493
|
def _make_context(self):
|
|
494
494
|
"return a default context"
|
|
495
495
|
|
|
496
|
-
def allocate(self, size, align=True):
|
|
496
|
+
def allocate(self, size, align=True, alignment=1):
|
|
497
497
|
# find available free slot
|
|
498
498
|
# and update free slot if exists
|
|
499
499
|
if align:
|
|
500
|
-
alignment = self.default_alignment
|
|
500
|
+
alignment = max(self.default_alignment, alignment)
|
|
501
501
|
else:
|
|
502
502
|
alignment = 1
|
|
503
503
|
# sizepa = size + alignment - 1
|
|
@@ -520,7 +520,7 @@ class XBuffer(ABC):
|
|
|
520
520
|
self.grow(self.capacity)
|
|
521
521
|
|
|
522
522
|
# try again
|
|
523
|
-
return self.allocate(size, align=align)
|
|
523
|
+
return self.allocate(size, align=align, alignment=alignment)
|
|
524
524
|
|
|
525
525
|
def grow(self, capacity):
|
|
526
526
|
"""
|