Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/tsim/utils/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import types
from collections.abc import Callable, Iterable
from concurrent.futures import ThreadPoolExecutor, wait
from typing import TypeVar
from typing import Any, TypeVar

import numba
import numba.core.config

# NOTE: waking numba's threads costs about 0.1 ms, more than many kernel calls
# take serially. Numba's default threading layer aborts the process when two
Expand Down Expand Up @@ -63,7 +64,9 @@ def jit(
# it reads them, so the name carries the flags: an entry cached without nogil
# would keep holding the GIL.
function = _renamed(function, "_nogil_contract" if contract else "_nogil")
options = _prange_only() if parallel else False
# NOTE: numba's stubs type ``parallel`` as bool, but its ParallelOptions also
# takes a dict of options at runtime.
options: Any = _prange_only() if parallel else False
fastmath = {"contract"} if contract else False
try:
return numba.njit(cache=True, parallel=options, nogil=True, fastmath=fastmath)(
Expand Down Expand Up @@ -109,7 +112,7 @@ def _mark_task_thread() -> None:
def _pool() -> ThreadPoolExecutor:
# NOTE: one pool for every call, as large as numba's thread count can get.
return ThreadPoolExecutor(
numba.config.NUMBA_NUM_THREADS, # pyright: ignore[reportAttributeAccessIssue]
numba.core.config.NUMBA_NUM_THREADS, # pyright: ignore[reportAttributeAccessIssue]
initializer=_mark_task_thread,
)

Expand Down
3 changes: 2 additions & 1 deletion test/integration/test_trajectory_sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numba
import numpy as np
import pytest
from numba.core import config as numba_config

import tsim.statevector.shots as shots_module
import tsim.utils.kernels as kernels
Expand Down Expand Up @@ -191,7 +192,7 @@ def test_trajectory_sharing_is_on_by_default():


# NOTE: numba's most threads, which it sets as a config attribute at import.
_LIMIT = numba.config.NUMBA_NUM_THREADS # pyright: ignore[reportAttributeAccessIssue]
_LIMIT = numba_config.NUMBA_NUM_THREADS # pyright: ignore[reportAttributeAccessIssue]


@pytest.fixture
Expand Down
3 changes: 2 additions & 1 deletion test/unit/utils/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time

import numba
import numba.np.ufunc
import numpy as np
import pytest

Expand All @@ -21,7 +22,7 @@ def _square_roots(out):
@kernel
def _thread_ids(out):
for i in numba.prange(out.shape[0]):
out[i] = numba.get_thread_id()
out[i] = numba.np.ufunc.get_thread_id()


def test_serial_and_parallel_runs_agree():
Expand Down
Loading
Loading