xobjects 0.4.7__tar.gz → 0.5.1__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.
Files changed (51) hide show
  1. {xobjects-0.4.7/xobjects.egg-info → xobjects-0.5.1}/PKG-INFO +1 -1
  2. xobjects-0.5.1/xobjects/_version.py +1 -0
  3. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/context.py +3 -0
  4. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/context_cpu.py +14 -12
  5. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/context_cupy.py +5 -1
  6. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/context_pyopencl.py +7 -1
  7. xobjects-0.5.1/xobjects/general.py +52 -0
  8. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/hybrid_class.py +1 -1
  9. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/struct.py +4 -0
  10. {xobjects-0.4.7 → xobjects-0.5.1/xobjects.egg-info}/PKG-INFO +1 -1
  11. xobjects-0.4.7/xobjects/_version.py +0 -1
  12. xobjects-0.4.7/xobjects/general.py +0 -33
  13. {xobjects-0.4.7 → xobjects-0.5.1}/LICENSE +0 -0
  14. {xobjects-0.4.7 → xobjects-0.5.1}/pyproject.toml +0 -0
  15. {xobjects-0.4.7 → xobjects-0.5.1}/setup.cfg +0 -0
  16. {xobjects-0.4.7 → xobjects-0.5.1}/setup.py +0 -0
  17. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_align.py +0 -0
  18. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_array.py +0 -0
  19. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_buffer.py +0 -0
  20. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_capi.py +0 -0
  21. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_chunk.py +0 -0
  22. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_context_opencl.py +0 -0
  23. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_hybrid_class.py +0 -0
  24. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_kernel.py +0 -0
  25. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_linked_array.py +0 -0
  26. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_nplike_arrays.py +0 -0
  27. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_ref.py +0 -0
  28. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_scalars.py +0 -0
  29. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_shared_memory.py +0 -0
  30. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_strides.py +0 -0
  31. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_string.py +0 -0
  32. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_struct.py +0 -0
  33. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_to_json.py +0 -0
  34. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_typeutils.py +0 -0
  35. {xobjects-0.4.7 → xobjects-0.5.1}/tests/test_unionref.py +0 -0
  36. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/__init__.py +0 -0
  37. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/_patch_pyopencl_array.py +0 -0
  38. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/array.py +0 -0
  39. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/capi.py +0 -0
  40. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/linkedarray.py +0 -0
  41. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/ref.py +0 -0
  42. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/scalar.py +0 -0
  43. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/specialize_source.py +0 -0
  44. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/string.py +0 -0
  45. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/test_helpers.py +0 -0
  46. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/typeutils.py +0 -0
  47. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects/union.py +0 -0
  48. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects.egg-info/SOURCES.txt +0 -0
  49. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects.egg-info/dependency_links.txt +0 -0
  50. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects.egg-info/requires.txt +0 -0
  51. {xobjects-0.4.7 → xobjects-0.5.1}/xobjects.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xobjects
3
- Version: 0.4.7
3
+ Version: 0.5.1
4
4
  Summary: In-memory serialization and code generator for CPU and GPU
5
5
  Home-page: https://xsuite.readthedocs.io/
6
6
  Download-URL: https://pypi.python.org/pypi/xobjects
@@ -0,0 +1 @@
1
+ __version__ = "0.5.1"
@@ -254,6 +254,7 @@ class XContext(ABC):
254
254
  extra_classes: Sequence[Type] = (),
255
255
  extra_headers: Sequence[SourceType] = (),
256
256
  compile: bool = True, # noqa
257
+ extra_compile_args: Sequence[str] = (),
257
258
  ):
258
259
  """
259
260
  Adds user-defined kernels to the context. The kernel source
@@ -333,6 +334,7 @@ class XContext(ABC):
333
334
  extra_classes=extra_classes,
334
335
  extra_headers=extra_headers,
335
336
  compile=compile,
337
+ extra_compile_args=extra_compile_args,
336
338
  )
337
339
  self.kernels.update(generated_kernels)
338
340
 
@@ -348,6 +350,7 @@ class XContext(ABC):
348
350
  extra_classes: Sequence[Type],
349
351
  extra_headers: Sequence[SourceType],
350
352
  compile: bool,
353
+ extra_compile_args: Sequence[str],
351
354
  ) -> Dict[Tuple[str, tuple], KernelType]:
352
355
  pass
353
356
 
@@ -169,8 +169,8 @@ class ContextCpu(XContext):
169
169
  specialize=True,
170
170
  apply_to_source=(),
171
171
  save_source_as=None,
172
- extra_compile_args: Sequence[str] = ("-O3", "-Wno-unused-function"),
173
- extra_link_args: Sequence[str] = ("-O3",),
172
+ extra_compile_args: Sequence[str] = (),
173
+ extra_link_args: Sequence[str] = (),
174
174
  extra_cdef="",
175
175
  extra_classes=(),
176
176
  extra_headers=(),
@@ -271,13 +271,16 @@ class ContextCpu(XContext):
271
271
  specialize=True,
272
272
  apply_to_source=(),
273
273
  save_source_as=None,
274
- extra_compile_args=("-O3", "-Wno-unused-function"),
275
- extra_link_args=("-O3",),
274
+ extra_compile_args=(),
275
+ extra_link_args=(),
276
276
  extra_cdef="",
277
277
  extra_classes=(),
278
278
  extra_headers=(),
279
279
  compile=True, # noqa
280
280
  ) -> Dict[Tuple[str, tuple], "KernelCpu"]:
281
+ extra_compile_args += ("-O3", "-Wno-unused-function")
282
+ extra_link_args += ("-O3",)
283
+
281
284
  # Determine names and paths
282
285
  clean_up_so = not module_name
283
286
  module_name = module_name or str(uuid.uuid4().hex)
@@ -409,20 +412,19 @@ class ContextCpu(XContext):
409
412
  ffi_interface.cdef("int omp_get_max_threads();")
410
413
 
411
414
  # Compile
412
- xtr_compile_args = ["-std=c99"]
413
- xtr_link_args = ["-std=c99"]
415
+ xtr_compile_args = ["-std=c99", "-DXO_CONTEXT_CPU"]
416
+ xtr_link_args = ["-std=c99", "-DXO_CONTEXT_CPU"]
414
417
  xtr_compile_args += extra_compile_args
415
418
  xtr_link_args += extra_link_args
416
419
 
417
420
  if self.openmp_enabled:
418
421
  xtr_compile_args.append("-fopenmp")
419
422
  xtr_link_args.append("-fopenmp")
420
-
421
- # https://mac.r-project.org/openmp/
422
- # on macos comment the above and uncomment the below flags to compile OpenMP with Xcode clang:
423
- # xtr_compile_args.append("-Xclang")
424
- # xtr_compile_args.append("-fopenmp")
425
- # xtr_link_args.append("-lomp")
423
+ xtr_compile_args.append("-DXO_CONTEXT_CPU_OPENMP")
424
+ xtr_link_args.append("-DXO_CONTEXT_CPU_OPENMP")
425
+ else:
426
+ xtr_compile_args.append("-DXO_CONTEXT_CPU_SERIAL")
427
+ xtr_link_args.append("-DXO_CONTEXT_CPU_SERIAL")
426
428
 
427
429
  if os.name == "nt": # windows
428
430
  # TODO: to be handled properly
@@ -416,6 +416,7 @@ class ContextCupy(XContext):
416
416
  specialize=True,
417
417
  apply_to_source=(),
418
418
  save_source_as=None,
419
+ extra_compile_args=(),
419
420
  extra_cdef=None,
420
421
  extra_classes=(),
421
422
  extra_headers=(),
@@ -454,7 +455,10 @@ class ContextCupy(XContext):
454
455
  with open(save_source_as, "w") as fid:
455
456
  fid.write(specialized_source)
456
457
 
457
- module = cupy.RawModule(code=specialized_source)
458
+ extra_compile_args = (*extra_compile_args, "-DXO_CONTEXT_CUDA")
459
+ module = cupy.RawModule(
460
+ code=specialized_source, options=extra_compile_args
461
+ )
458
462
 
459
463
  out_kernels = {}
460
464
  for pyname, kernel in kernel_descriptions.items():
@@ -180,6 +180,7 @@ class ContextPyopencl(XContext):
180
180
  specialize=True,
181
181
  apply_to_source=(),
182
182
  save_source_as=None,
183
+ extra_compile_args=(),
183
184
  extra_cdef=None,
184
185
  extra_classes=(),
185
186
  extra_headers=(),
@@ -218,8 +219,13 @@ class ContextPyopencl(XContext):
218
219
  with open(save_source_as, "w") as fid:
219
220
  fid.write(specialized_source)
220
221
 
222
+ extra_compile_args = (
223
+ *extra_compile_args,
224
+ "-cl-std=CL2.0",
225
+ "-DXO_CONTEXT_CL",
226
+ )
221
227
  prg = cl.Program(self.context, specialized_source).build(
222
- options="-cl-std=CL2.0",
228
+ options=extra_compile_args,
223
229
  )
224
230
 
225
231
  out_kernels = {}
@@ -0,0 +1,52 @@
1
+ # copyright ################################# #
2
+ # This file is part of the Xobjects Package. #
3
+ # Copyright (c) CERN, 2024. #
4
+ # ########################################### #
5
+ from numpy.testing import assert_allclose as np_assert_allclose
6
+ import numpy as np
7
+
8
+
9
+ class Print:
10
+ suppress = False
11
+
12
+ def __call__(self, *args, **kwargs):
13
+ if not self.suppress:
14
+ print(*args, **kwargs)
15
+
16
+
17
+ _print = Print()
18
+
19
+
20
+ def assert_allclose(a, b, rtol=0, atol=0, max_outliers=0):
21
+ if hasattr(a, "get"):
22
+ a = a.get()
23
+ if hasattr(b, "get"):
24
+ b = b.get()
25
+ try:
26
+ a = np.squeeze(a)
27
+ except:
28
+ pass
29
+ try:
30
+ b = np.squeeze(b)
31
+ except:
32
+ pass
33
+
34
+ try:
35
+ np_assert_allclose(a, b, rtol=rtol, atol=atol)
36
+ except AssertionError as e:
37
+ if max_outliers == 0:
38
+ raise e
39
+ if not allclose_with_outliers(a, b, rtol, atol, max_outliers):
40
+ raise AssertionError(
41
+ "Arrays are not close enough, even with outliers allowed."
42
+ ) from e
43
+
44
+
45
+ def allclose_with_outliers(a, b, rtol=1e-7, atol=0, max_outliers=0):
46
+ a = np.asanyarray(a)
47
+ b = np.asanyarray(b)
48
+ diff = np.abs(a - b)
49
+ allowed = atol + rtol * np.abs(b)
50
+ mask = diff > allowed
51
+ num_outliers = np.count_nonzero(mask)
52
+ return num_outliers <= max_outliers
@@ -96,7 +96,7 @@ class JEncoder(json.JSONEncoder):
96
96
  def _build_xofields_dict(bases, data):
97
97
  if "_xofields" in data.keys():
98
98
  xofields = data["_xofields"].copy()
99
- elif any(map(lambda b: hasattr(b, "_xofields"), bases)):
99
+ elif any(hasattr(b, "_xofields") for b in bases):
100
100
  n_filled = 0
101
101
  for bb in bases:
102
102
  if hasattr(bb, "_xofields") and len(bb._xofields.keys()) > 0:
@@ -473,6 +473,7 @@ class Struct(metaclass=MetaStruct):
473
473
  apply_to_source=(),
474
474
  save_source_as=None,
475
475
  extra_classes=(),
476
+ extra_compile_args=(),
476
477
  ):
477
478
  if only_if_needed:
478
479
  all_found = True
@@ -489,6 +490,7 @@ class Struct(metaclass=MetaStruct):
489
490
  extra_classes=[cls] + list(extra_classes),
490
491
  apply_to_source=apply_to_source,
491
492
  save_source_as=save_source_as,
493
+ extra_compile_args=extra_compile_args,
492
494
  )
493
495
 
494
496
  def compile_kernels(
@@ -497,6 +499,7 @@ class Struct(metaclass=MetaStruct):
497
499
  apply_to_source=(),
498
500
  save_source_as=None,
499
501
  extra_classes=(),
502
+ extra_compile_args=(),
500
503
  ):
501
504
  self.compile_class_kernels(
502
505
  context=self._context,
@@ -504,6 +507,7 @@ class Struct(metaclass=MetaStruct):
504
507
  apply_to_source=apply_to_source,
505
508
  save_source_as=save_source_as,
506
509
  extra_classes=extra_classes,
510
+ extra_compile_args=extra_compile_args,
507
511
  )
508
512
 
509
513
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xobjects
3
- Version: 0.4.7
3
+ Version: 0.5.1
4
4
  Summary: In-memory serialization and code generator for CPU and GPU
5
5
  Home-page: https://xsuite.readthedocs.io/
6
6
  Download-URL: https://pypi.python.org/pypi/xobjects
@@ -1 +0,0 @@
1
- __version__ = "0.4.7"
@@ -1,33 +0,0 @@
1
- # copyright ################################# #
2
- # This file is part of the Xobjects Package. #
3
- # Copyright (c) CERN, 2024. #
4
- # ########################################### #
5
- from numpy.testing import assert_allclose as np_assert_allclose
6
- import numpy as np
7
-
8
-
9
- class Print:
10
- suppress = False
11
-
12
- def __call__(self, *args, **kwargs):
13
- if not self.suppress:
14
- print(*args, **kwargs)
15
-
16
-
17
- _print = Print()
18
-
19
-
20
- def assert_allclose(a, b, rtol=1e-7, atol=1e-7):
21
- if hasattr(a, "get"):
22
- a = a.get()
23
- if hasattr(b, "get"):
24
- b = b.get()
25
- try:
26
- a = np.squeeze(a)
27
- except:
28
- pass
29
- try:
30
- b = np.squeeze(b)
31
- except:
32
- pass
33
- np_assert_allclose(a, b, rtol=rtol, atol=atol)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes