Edy Bourne
Edy Bourne

Reputation: 6207

How to prevent Numba from parallelizing a particular loop when parallel=True is used?

I have a test code like so:

import numpy as np
from numba import njit, prange
import random


def func():
    a = np.empty(0)
    for i in range(10):
        b = np.arange(np.random.randint(10))
        a = np.concatenate((a, b))
        print(len(b), ' / ', len(a))


func()

It works as expected:

3  /  3
6  /  9
5  /  14
8  /  22
9  /  31
6  /  37
9  /  46
5  /  51
3  /  54
9  /  63

If I compile with Numba by adding @njit(parallel=True) decorator, it stops working because Numba parallelizes this loop which causes issues:

2  /  0
1  /  0
2  /  0
7  /  0
0  /  0
3  /  0
2  /  0
5  /  0
7  /  0
2  /  0

In my real code there is another loop in this function which uses prange, so I want parallel=True on the whole function. But I don't want it for this loop.

How to prevent this?

Upvotes: 1

Views: 286

Answers (1)

Edy Bourne
Edy Bourne

Reputation: 6207

I ended up splitting this portion of the code out of the @njit'ed function which had parallel=True, and on this function that was factored out I used @njit but left parallel=False

This did it for me.

Upvotes: 3

Related Questions