How should I use parallel=True

Hello,

I cannot get parallel=True to work.

A simple function such as :
@njit(cache=True, parallel=True)
def compute_domain_sum(domains: NDArray, data: NDArray) → NDArray:
domain_sum = np.full(2, data[-1], dtype=np.int32)
for i in prange(len(data) - 1):
c = data[i]
if c > 0:
domain_sum[MIN] -= domains[i, MAX] * c
domain_sum[MAX] -= domains[i, MIN] * c
elif c < 0:
domain_sum[MIN] -= domains[i, MIN] * c
domain_sum[MAX] -= domains[i, MAX] * c
return domain_sum

randomly generates a seg fault.
Can anybody help ?

Thank you,
Yan

You didn’t post a complete program. That could be helpful.

At first glance, you may have a race condition in the write to domain_sum.

Actually, it is systematic :

@njit(cache=True, parallel=True)
def compute_domain_sum(domains: NDArray, data: NDArray) → NDArray:
domain_sum = np.full(2, data[-1], dtype=np.int32)
for i in prange(len(data) - 1):
c = data[i]
if c > 0:
domain_sum[MIN] -= domains[i, MAX] * c
domain_sum[MAX] -= domains[i, MIN] * c
elif c < 0:
domain_sum[MIN] -= domains[i, MIN] * c
domain_sum[MAX] -= domains[i, MAX] * c
return domain_sum

if name == “main”:
n = 1000
domains = np.array([[-100, 100]] * n, dtype=np.int32)
data = np.array(list(range(n + 1)), dtype=np.int32)
compute_domain_sum(domains, data)

That is still not a complete program that reproduces the issue. An example of a complete program similar to yours would be:

from numba import njit, prange
import numpy as np

MIN = 0
MAX = 1


@njit(cache=True, parallel=True)
def compute_domain_sum(domains, data):
    domain_sum = np.full(2, data[-1], dtype=np.int32)
    for i in prange(len(data) - 1):
        c = data[i]
        if c > 0:
            domain_sum[MIN] -= domains[i, MAX] * c
            domain_sum[MAX] -= domains[i, MIN] * c
        elif c < 0:
            domain_sum[MIN] -= domains[i, MIN] * c
            domain_sum[MAX] -= domains[i, MAX] * c
        return domain_sum


if __name__ == "__main__":
    n = 1000
    domains = np.array([[-100, 100]] * n, dtype=np.int32)
    data = np.array(list(range(n + 1)), dtype=np.int32)
    compute_domain_sum(domains, data)

However, this shows no issue for me (apart from parallel transformation not being applicable), and I had to use some guesswork to fill in the missing pieces (and correct the transformations made to the code because it wasn’t enclosed in backticks in the post).

Some guidance on producing these kinds of minimal examples is in Craft Minimal Bug Reports — Matthew Rocklin

1 Like