xobjects 0.6.3__tar.gz → 0.6.5__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 (57) hide show
  1. {xobjects-0.6.3/xobjects.egg-info → xobjects-0.6.5}/PKG-INFO +1 -1
  2. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_kernel.py +2 -0
  3. xobjects-0.6.5/xobjects/_version.py +1 -0
  4. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/context_cpu.py +71 -10
  5. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/struct.py +27 -15
  6. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/test_helpers.py +31 -0
  7. {xobjects-0.6.3 → xobjects-0.6.5/xobjects.egg-info}/PKG-INFO +1 -1
  8. xobjects-0.6.3/xobjects/_version.py +0 -1
  9. {xobjects-0.6.3 → xobjects-0.6.5}/Architecture.md +0 -0
  10. {xobjects-0.6.3 → xobjects-0.6.5}/LICENSE +0 -0
  11. {xobjects-0.6.3 → xobjects-0.6.5}/MANIFEST.in +0 -0
  12. {xobjects-0.6.3 → xobjects-0.6.5}/README.md +0 -0
  13. {xobjects-0.6.3 → xobjects-0.6.5}/pyproject.toml +0 -0
  14. {xobjects-0.6.3 → xobjects-0.6.5}/setup.cfg +0 -0
  15. {xobjects-0.6.3 → xobjects-0.6.5}/setup.py +0 -0
  16. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_align.py +0 -0
  17. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_array.py +0 -0
  18. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_buffer.py +0 -0
  19. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_capi.py +0 -0
  20. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_chunk.py +0 -0
  21. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_common.py +0 -0
  22. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_context_opencl.py +0 -0
  23. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_hybrid_class.py +0 -0
  24. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_linked_array.py +0 -0
  25. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_nplike_arrays.py +0 -0
  26. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_ref.py +0 -0
  27. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_scalars.py +0 -0
  28. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_shared_memory.py +0 -0
  29. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_strides.py +0 -0
  30. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_string.py +0 -0
  31. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_struct.py +0 -0
  32. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_to_dict.py +0 -0
  33. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_typeutils.py +0 -0
  34. {xobjects-0.6.3 → xobjects-0.6.5}/tests/test_unionref.py +0 -0
  35. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/__init__.py +0 -0
  36. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/_patch_pyopencl_array.py +0 -0
  37. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/array.py +0 -0
  38. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/capi.py +0 -0
  39. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/context.py +0 -0
  40. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/context_cupy.py +0 -0
  41. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/context_pyopencl.py +0 -0
  42. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/general.py +0 -0
  43. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/headers/atomicadd.h +0 -0
  44. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/headers/common.h +0 -0
  45. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/hybrid_class.py +0 -0
  46. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/linkedarray.py +0 -0
  47. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/ref.py +0 -0
  48. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/scalar.py +0 -0
  49. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/specialize_source.py +0 -0
  50. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/string.py +0 -0
  51. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/typeutils.py +0 -0
  52. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects/union.py +0 -0
  53. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects.egg-info/SOURCES.txt +0 -0
  54. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects.egg-info/dependency_links.txt +0 -0
  55. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects.egg-info/entry_points.txt +0 -0
  56. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects.egg-info/requires.txt +0 -0
  57. {xobjects-0.6.3 → xobjects-0.6.5}/xobjects.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xobjects
3
- Version: 0.6.3
3
+ Version: 0.6.5
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
@@ -4,7 +4,9 @@
4
4
  # ########################################### #
5
5
 
6
6
  import cffi
7
+ import pytest
7
8
  import sysconfig
9
+ from pathlib import Path
8
10
 
9
11
  import numpy as np
10
12
 
@@ -0,0 +1 @@
1
+ __version__ = "0.6.5"
@@ -19,6 +19,60 @@ import scipy as sp
19
19
 
20
20
  _forbid_compile = False
21
21
  _suppress_warnings = False
22
+ allow_no_prebuilt_kernel = False
23
+
24
+
25
+ def _class_allows_no_prebuilt_kernel(cls):
26
+ return (
27
+ getattr(cls, 'allow_no_prebuilt_kernel', False)
28
+ or getattr(getattr(cls, '_DressingClass', None),
29
+ 'allow_no_prebuilt_kernel', False)
30
+ or getattr(getattr(cls, '_XoStruct', None),
31
+ 'allow_no_prebuilt_kernel', False)
32
+ )
33
+
34
+
35
+ def allow_no_prebuilt_kernel_enabled(context=None, classes=()):
36
+ if classes is None:
37
+ classes = ()
38
+ elif isinstance(classes, type):
39
+ classes = (classes,)
40
+
41
+ if os.environ.get('XSUITE_ALLOW_NO_PREBUILT_KERNELS') is not None:
42
+ return True
43
+ if allow_no_prebuilt_kernel:
44
+ return True
45
+ if any(_class_allows_no_prebuilt_kernel(cls) for cls in classes):
46
+ return True
47
+ return getattr(context, 'allow_no_prebuilt_kernel', False)
48
+
49
+
50
+ def _is_serial_cpu_context(context):
51
+ if context is None or not hasattr(context, 'openmp_enabled'):
52
+ return False
53
+ return context.openmp_enabled is False
54
+
55
+
56
+ def require_prebuilt_kernel(context=None, classes=()):
57
+ return (
58
+ not allow_no_prebuilt_kernel_enabled(context, classes=classes)
59
+ and _is_serial_cpu_context(context)
60
+ )
61
+
62
+
63
+ def no_prebuilt_kernel_jit_message():
64
+ return (
65
+ 'To allow just-in-time compilation instead, as in older Xsuite '
66
+ 'versions, set the environment variable '
67
+ '`XSUITE_ALLOW_NO_PREBUILT_KERNELS`, set '
68
+ '`xobjects.context_cpu.allow_no_prebuilt_kernel = True`, or set '
69
+ '`context.allow_no_prebuilt_kernel = True`. Classes that require '
70
+ 'just-in-time compilation can also define '
71
+ '`allow_no_prebuilt_kernel = True` as a class attribute. Using '
72
+ 'just-in-time compilation instead of prebuilt kernels may require '
73
+ 'lengthy compilation whenever a different kernel is needed.'
74
+ )
75
+
22
76
 
23
77
  from .context import (
24
78
  Kernel,
@@ -150,8 +204,7 @@ class ContextCpu(XContext):
150
204
  """
151
205
  super().__init__()
152
206
  self.omp_num_threads = omp_num_threads
153
- if omp_num_threads == 0:
154
- self.allow_prebuilt_kernels = True
207
+ self.allow_prebuilt_kernels = True
155
208
 
156
209
  def __str__(self):
157
210
  if not self.openmp_enabled:
@@ -415,8 +468,18 @@ class ContextCpu(XContext):
415
468
  log.debug(f"cffi def {pyname} {signature}")
416
469
 
417
470
  if self.openmp_enabled:
418
- ffi_interface.cdef("void omp_set_num_threads(int);")
419
- ffi_interface.cdef("int omp_get_max_threads();")
471
+ # The wrapper is needed to ensure that the omp functions are linked
472
+ ffi_interface.cdef("void xo_omp_set_num_threads(int);")
473
+ ffi_interface.cdef("int xo_omp_get_max_threads();")
474
+ specialized_source += """
475
+ void xo_omp_set_num_threads(int num_threads) {
476
+ omp_set_num_threads(num_threads);
477
+ }
478
+
479
+ int xo_omp_get_max_threads(void) {
480
+ return omp_get_max_threads();
481
+ }
482
+ """
420
483
 
421
484
  # Compile
422
485
  xtr_compile_args = ["-std=c99", "-DXO_CONTEXT_CPU"]
@@ -528,8 +591,8 @@ class ContextCpu(XContext):
528
591
  spec.loader.exec_module(module)
529
592
 
530
593
  if self.openmp_enabled:
531
- self.omp_set_num_threads = module.lib.omp_set_num_threads
532
- self.omp_get_max_threads = module.lib.omp_get_max_threads
594
+ self.omp_set_num_threads = module.lib.xo_omp_set_num_threads
595
+ self.omp_get_max_threads = module.lib.xo_omp_get_max_threads
533
596
 
534
597
  return module
535
598
 
@@ -768,10 +831,8 @@ class BufferNumpy(XBuffer):
768
831
  value = nplike_to_numpy(value)
769
832
  if dest_dtype != value.dtype:
770
833
  value = value.astype(dtype=dest_dtype) # make a copy
771
- src = value.view("int8")
772
- self.buffer[offset : offset + src.nbytes] = value.flatten().view(
773
- "int8"
774
- )
834
+ src = value.flatten().view("int8")
835
+ self.buffer[offset : offset + src.nbytes] = src
775
836
 
776
837
  def to_bytearray(self, offset, nbytes):
777
838
  """copy in byte array: used in update_from_xbuffer"""
@@ -518,25 +518,37 @@ class Struct(metaclass=MetaStruct):
518
518
  extra_classes=(),
519
519
  extra_compile_args=(),
520
520
  ):
521
-
522
521
  if context.allow_prebuilt_kernels:
523
522
  _print_state = Print.suppress
524
523
  Print.suppress = True
525
524
  try:
526
- from xsuite import (
527
- get_suitable_kernel,
528
- XSK_PREBUILT_KERNELS_LOCATION,
529
- )
530
-
531
- kernel_info = get_suitable_kernel(
532
- config={},
533
- tracker_element_classes=[],
534
- classes=list(extra_classes) + [cls],
535
- )
536
- except ImportError:
537
- kernel_info = None
538
-
539
- Print.suppress = _print_state
525
+ try:
526
+ from xsuite import (
527
+ get_suitable_kernel,
528
+ XSK_PREBUILT_KERNELS_LOCATION,
529
+ )
530
+
531
+ kernel_info = get_suitable_kernel(
532
+ config={},
533
+ tracker_element_classes=[],
534
+ classes=list(extra_classes) + [cls],
535
+ context=context,
536
+ )
537
+ except ImportError as err:
538
+ from . import context_cpu
539
+
540
+ if not context_cpu.require_prebuilt_kernel(
541
+ context, classes=list(extra_classes) + [cls]):
542
+ kernel_info = None
543
+ else:
544
+ raise ImportError(
545
+ 'Xsuite is required to load prebuilt kernels but could '
546
+ 'not be imported. Please install it with '
547
+ f'`pip install xsuite`. '
548
+ f'{context_cpu.no_prebuilt_kernel_jit_message()}'
549
+ ) from err
550
+ finally:
551
+ Print.suppress = _print_state
540
552
  if kernel_info:
541
553
  kernels = context.kernels_from_file(
542
554
  module_name=kernel_info["module_name"],
@@ -108,6 +108,37 @@ def fix_random_seed(seed: int):
108
108
  return decorator
109
109
 
110
110
 
111
+ def allow_no_prebuilt_kernels(test_function=None, *, skip_when_forbid_compile=True):
112
+ """Allow JIT compilation for tests that intentionally compile kernels.
113
+
114
+ By default, the wrapped test is skipped when compilation is forbidden by
115
+ ``XOBJECTS_FORBID_COMPILE``. Use ``skip_when_forbid_compile=False`` when
116
+ the test has more specific ``skip_if_forbid_compile()`` guards inside the
117
+ test.
118
+ """
119
+
120
+ def decorator(test_function):
121
+ @wraps(test_function)
122
+ def wrapper(*args, **kwargs):
123
+ if skip_when_forbid_compile:
124
+ skip_if_forbid_compile()
125
+ old_value = os.environ.get("XSUITE_ALLOW_NO_PREBUILT_KERNELS")
126
+ os.environ["XSUITE_ALLOW_NO_PREBUILT_KERNELS"] = "1"
127
+ try:
128
+ return test_function(*args, **kwargs)
129
+ finally:
130
+ if old_value is None:
131
+ del os.environ["XSUITE_ALLOW_NO_PREBUILT_KERNELS"]
132
+ else:
133
+ os.environ["XSUITE_ALLOW_NO_PREBUILT_KERNELS"] = old_value
134
+
135
+ return wrapper
136
+
137
+ if test_function is None:
138
+ return decorator
139
+ return decorator(test_function)
140
+
141
+
111
142
  def skip_if_forbid_compile():
112
143
  if os.environ.get("XOBJECTS_FORBID_COMPILE"):
113
144
  pytest.skip(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xobjects
3
- Version: 0.6.3
3
+ Version: 0.6.5
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.6.3"
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
File without changes
File without changes
File without changes
File without changes
File without changes