I have a large prange loop that efficiently does some process here. I'd like to track when one of them finds a better result. However, it seems writing to variables inside a prange loop will create a new private variable, so data can't be shared that way.
Here is a canonical example: this prange searches for the largest random value in a number of arrays. I'd like for them to "collaborate" so that only a new highest value is ever printed:
import numpy as np
from numba import jit, prange
@jit(nopython=True, parallel=True)
def test():
arr = np.random.rand(1000, 15000000)
largest_value = 0
for i in prange(arr.shape[0]):
for val in arr[i]:
if val > largest_value:
print('NEW LARGEST VALUE: ' + str(int(largest_value * 100000000)))
largest_value = val
test()
Running this will print bogus values such as:
...
NEW LARGEST VALUE: 85809181
NEW LARGEST VALUE: 93504938
NEW LARGEST VALUE: 75796671
NEW LARGEST VALUE: 83944676
NEW LARGEST VALUE: 99068766
This is because each thread, when writing to largest_value creates a local private copy, so they can't share their findings that way.
Are there any tricks at all to make this possible? Shared memory? Anything fancy I'm missing? Something else?