Skip to content

Openmp loops with non constant size? #12

@elyurn

Description

@elyurn

A typical example of hybrid OpenMP+MPI code is a simple computation of Pi using Riemman integral.
I implemented it with PyOMP and numba-mpi, but it seems that we are limited by Numba (Only constant step size is supported for prange) :

import numba_mpi as nbmpi, numba as nb, numpy as np
from numba.openmp import openmp_context as openmp
from numba.openmp import omp_get_num_threads, omp_get_thread_num

@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, size): # loop over the 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

@nb.njit
def pi_numba_mpi(n_intervals=10000):
    pi = np.zeros(1)
    part = np.empty_like(pi)
    part[0] = get_pi_part(n_intervals, nbmpi.rank(), nbmpi.size()) # get the partial sum
    nbmpi.allreduce(part, pi) # sum the partial sums
    return pi[0]

pi_result = pi_numba_mpi()

Error:

Traceback (most recent call last):
  File "/home/usr/miniconda3/envs/pyomp/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/usr/miniconda3/envs/pyomp/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/usr/projectPhys743/Pi_allreduce/hello_world_mpi_openmp.py", line 25, in <module>
    pi_result = pi_numba_mpi()
  File "/home/usr/miniconda3/envs/pyomp/lib/python3.10/site-packages/numba/core/dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/usr/miniconda3/envs/pyomp/lib/python3.10/site-packages/numba/core/dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.CallConstraint object at 0x7f8b17e0e6e0>.
Failed in nopython mode pipeline (step: Handle with contexts)
Only constant step size is supported for prange
During: resolving callee type: type(CPUDispatcher(<function get_pi_part at 0x7f8b3bf0a9e0>))
During: typing of call at /home/usr/projectPhys743/Pi_allreduce/hello_world_mpi_openmp.py (21)

Enable logging at debug level for details.

File "hello_world_mpi_openmp.py", line 21:
def pi_numba_mpi(n_intervals=10000):
    <source elided>
    part = np.empty_like(pi)
    part[0] = get_pi_part(n_intervals, nbmpi.rank(), nbmpi.size()) # get the partial sum

Would you have a workaround for such usage of PyOMP?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions