Open elyurn opened 6 days ago
The error message Only constant step size is supported for prange
is misleading. In reality we have put this restriction ourselves in PyOMP code generation (https://github.com/Python-for-HPC/numbaWithOpenmp/blob/296fb858b0a6800323e9b46b027c32636ecc80c7/numba/openmp.py#L3752), which is unrelated to prange
. I'm tracking this in python-for-hpc/numbaWithOpenmp#39
PyOMP can and will support non-constant steps in parallel for
. I'm tracking this in this issue python-for-hpc/numbaWithOpenMP#40. We plan to support that in the next bugfix release.
As a workaround you can refactor the parallel loop to step size 1 and do some extra work in the loop body:
with openmp("parallel for reduction(+:partial_sum)"):
for i in range(rank+1, n_intervals): # loop over the intervals
i = i*size
if i<n_intervals:
x = h*(i-0.5) # x value at the center of the interval
partial_sum += 4.0/(1.0 + x**2) # add the height of the rectangle to the partial sum
return h*partial_sum # return the partial sum
Let me know if that works (it should).
I will keep this issue open until we fix code generation in PyOMP.
Nice, thank you for the support. It indeed works, although the resulting integral is false using your code (bad indices). FYI, the solution to my specific problem:
@nb.njit
def get_pi_part(n_intervals=10000, rank=0, size=1):
h = 1.0/n_intervals # width of each interval
partial_sum = 0.0 # initialize the partial sum
with openmp("parallel for reduction(+:partial_sum)"):
# for i in range(rank+1, n_intervals): # loop over the intervals
for i in range((n_intervals-rank+size-2)//size): # loop over the intervals
i = i*size + rank+1
if i<n_intervals:
x = h*(i-0.5) # x value at the center of the interval
partial_sum += 4.0/(1.0 + x**2) # add the height of the rectangle to the partial sum
return h*partial_sum # return the partial sum
Glad you worked it out, fixing my buggy version
A typical example of hybrid OpenMP+MPI code is a simple computation of Pi using Riemman integral. I implemented it with
PyOMP
andnumba-mpi
, but it seems that we are limited by Numba (Only constant step size is supported for prange) :Error:
Would you have a workaround for such usage of PyOMP?