File: openmp_helpers_inner.pyx

package info (click to toggle)
python-threadpoolctl 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: python: 1,230; sh: 134; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
cimport openmp
from cython.parallel import prange


def check_openmp_num_threads(int n):
    """Run a short parallel section with OpenMP

    Return the number of threads that where effectively used by the
    OpenMP runtime.
    """
    cdef int num_threads = -1

    with nogil:
        num_threads = inner_openmp_loop(n)
    return num_threads


cdef int inner_openmp_loop(int n) nogil:
    """Run a short parallel section with OpenMP

    Return the number of threads that where effectively used by the
    OpenMP runtime.

    This function is expected to run without the GIL and can be called
    by an outer OpenMP / prange loop written in Cython in another file.
    """
    cdef long n_sum = 0
    cdef int i, num_threads

    for i in prange(n):
        num_threads = openmp.omp_get_num_threads()
        n_sum += i

    if n_sum != (n - 1) * n / 2:
        # error
        return -1

    return num_threads