"Exporting" variable outside OpenMP thread - checking convergence within prange

22 views
Skip to first unread message

truen...@gmail.com

unread,
Feb 19, 2018, 9:10:09 AM2/19/18
to cython-users
Hello all,

I have implemented an iteratively converging algorithm in cython. To test convergence, I have a 'tolerance' parameter that is checked at each iteration. It looks like this:

while not convergence:
    for n in prange(n_points, nogil=True):
        new_value = some_crazy_calculation(values_array, n)
        old_value = values_array[n]
        values_array[n] = new_value
        errors_array[n] = fabs((old_value -
new_value) / old_value)
   
    convergence = True
    for n in prange(n_points, nogil=True):
        if errors[n] > tolerance:
            convergence = False
            break

So right now, I'm first storing all the "errors" (relative change between iterations), and then iterating on them until I find a big enough "error" to decide the algorithm has not converged yet. Can I get rid of the second loop? Something like:

while not convergence:
    convergence = True
    for n in prange(n_points, nogil=True):
        new_value = some_crazy_calculation(values_array, n)
        old_value = values_array[n]
        values_array[n] = new_value
        if convergence and fabs((old_value -
new_value) / old_value) > tolerance:
            convergence = False

However, I could only get this to work using a single-threaded 'range' instead of 'prange'. What would be the way to make this work using OpenMP?

The full code is here if anyone is interested. NB: I'm happy with the performance at the moment, but I'm mostly asking out of curiosity, i.e., "long discussions/explanations are welcome".

Thanks,

-- Nicolas

Stefan Behnel

unread,
Feb 21, 2018, 12:11:40 AM2/21/18
to cython...@googlegroups.com
You can use a shared regression variable, i.e. count the unconverged cases with

if ... > tolerance:
unconverged += 1

Cython will recognise that pattern and do a global sum across all threads.

Stefan

truen...@gmail.com

unread,
Mar 9, 2018, 8:47:23 AM3/9/18
to cython-users
Thanks for your answer (that I only noticed today).

I don't think your proposition would work in my case. Maybe my pseudocode was not explicit enough, maybe I'm not understanding your proposition.

I'm not interested in counting how many "values" haven't converged, but I precisely want to stop checking as soon as one "error" is above "tolerance".

-- Nicolas
Reply all
Reply to author
Forward
0 new messages