Decorators and Context Managers#

pygram11.omp_disabled(*, key=None)[source]#

Context manager to disable OpenMP.

Parameters:

key (str, optional) – Specific threshold key to turn off.

Examples

Using a specific key:

>>> import pygram11
>>> import numpy as np
>>> with pygram11.omp_disabled(key="thresholds.var1d"):
...     data = np.random.standard_normal(size=(200,))
...     result = pygram11.histogram(data, bins=[-2, -1, 1.5, 3.2])
>>> result[0].shape
(3,)

Disable all thresholds:

>>> import pygram11
>>> import numpy as np
>>> with pygram11.omp_disabled():
...     data = np.random.standard_normal(size=(200,))
...     result = pygram11.histogram(data, bins=12, range=(-3, 3))
>>> result[0].shape
(12,)
pygram11.omp_forced(*, key=None)[source]#

Context manager to force enable OpenMP.

Parameters:

key (str, optional) – Specific threshold key to turn on.

Examples

Using a specific key:

>>> import pygram11
>>> import numpy as np
>>> with pygram11.omp_forced(key="thresholds.var1d"):
...     data = np.random.standard_normal(size=(200,))
...     result = pygram11.histogram(data, bins=[-2, -1, 1.5, 3.2])
>>> result[0].shape
(3,)

Enable all thresholds:

>>> import pygram11
>>> import numpy as np
>>> with pygram11.omp_forced():
...     data = np.random.standard_normal(size=(200,))
...     result = pygram11.histogram(data, bins=10, range=(-3, 3))
>>> result[0].shape
(10,)
pygram11.without_omp(*args, **kwargs)[source]#

Wrap a function to disable OpenMP while it’s called.

If a specific key is defined, only that threshold will be modified to turn OpenMP off.

The settings of the pygram11 OpenMP threshold configurations will be restored to their previous values at the end of the function that is being wrapped.

Parameters:

key (str, optional) – Specific threshold key to turn off.

Examples

Writing a function with this decorator:

>>> import numpy as np
>>> from pygram11 import histogram, without_omp
>>> @without_omp
... def single_threaded_histogram():
...     data = np.random.standard_normal(size=(1000,))
...     return pygram11.histogram(data, bins=10, range=(-5, 5), flow=True)

Defining a specific key:

>>> import pygram11.config
>>> previous = pygram11.config.get("thresholds.var1d")
>>> @without_omp(key="thresholds.var1d")
... def single_threaded_histogram2():
...     print(f"in function threshold: {pygram11.config.get('thresholds.var1d')}")
...     data = np.random.standard_normal(size=(1000,))
...     return pygram11.histogram(data, bins=[-2, -1, 1.5, 3.2])
>>> result = single_threaded_histogram2()
in function threshold: 9223372036854775807
>>> previous
5000
>>> previous == pygram11.config.get("thresholds.var1d")
True
>>> result[0].shape
(3,)
pygram11.with_omp(*args, **kwargs)[source]#

Wrap a function to always enable OpenMP while it’s called.

If a specific key is defined, only that threshold will be modified to turn OpenMP on.

The settings of the pygram11 OpenMP threshold configurations will be restored to their previous values at the end of the function that is being wrapped.

Parameters:

key (str, optional) – Specific threshold key to turn on.

Examples

Writing a function with this decorator:

>>> import numpy as np
>>> from pygram11 import histogram, with_omp
>>> @with_omp
... def multi_threaded_histogram():
...     data = np.random.standard_normal(size=(1000,))
...     return pygram11.histogram(data, bins=10, range=(-5, 5), flow=True)

Defining a specific key:

>>> import pygram11.config
>>> previous = pygram11.config.get("thresholds.var1d")
>>> @with_omp(key="thresholds.var1d")
... def multi_threaded_histogram2():
...     print(f"in function threshold: {pygram11.config.get('thresholds.var1d')}")
...     data = np.random.standard_normal(size=(1000,))
...     return pygram11.histogram(data, bins=[-2, -1, 1.5, 3.2])
>>> result = multi_threaded_histogram2()
in function threshold: 0
>>> previous
5000
>>> previous == pygram11.config.get("thresholds.var1d")
True
>>> result[0].shape
(3,)