Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use multithreading in numba

I have a function that performs a point in polygon test. It takes two 2D numpy array as input (a series of points, and a polygon). The function returns a boolean as output (True if the point lies inside the polygon, False otherwise). The code is borrowed from this SO answer. An example below:

from numba import jit
from numba.pycc import CC
cc = CC('nbspatial')
import numpy as np

@cc.export('array_tracing2', 'b1[:](f8[:,:], f8[:,:])')
@jit(nopython=True, nogil=True)
def array_tracing2(xy, poly):
    D = np.empty(len(xy), dtype=numba.boolean)
    n = len(poly)
    for i in range(1, len(D) - 1):
        inside = False
        p2x = 0.0
        p2y = 0.0
        xints = 0.0
        p1x,p1y = poly[0]
        x = xy[i][0]
        y = xy[i][1]
        for i in range(n+1):
            p2x,p2y = poly[i % n]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        if p1x == p2x or x <= xints:
                            inside = not inside
            p1x,p1y = p2x,p2y
        D[i] = inside
    return D


if __name__ == "__main__":
    cc.compile()

The code above can be compiled by running python numba_src.py and tested with:

import numpy as np
# regular polygon for testing
lenpoly = 10000
polygon = np.array([[np.sin(x)+0.5,np.cos(x)+0.5] for x in np.linspace(0,2*np.pi,lenpoly)[:-1]])

# random points set of points to test 
N = 100000
# making a list instead of a generator to help debug
pp = np.array([np.random.random(N), np.random.random(N)]).reshape(N,2)


import nbspatial
nbspatial.array_tracing2(pp, polygon) 

My attempt is to parallelize the code above so to make use of all the available CPUs.

I tryied following the example from the numba official documentation using @njit

import numba

@njit(parallel=True)
def array_tracing3(xy, poly):
    D = np.empty(len(xy), dtype=numba.boolean)
    n = len(poly)
    for i in range(1, len(D) - 1):
        inside = False
        p2x = 0.0
        p2y = 0.0
        xints = 0.0
        p1x,p1y = poly[0]
        x = xy[i][0]
        y = xy[i][1]
        for i in range(n+1):
            p2x,p2y = poly[i % n]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        if p1x == p2x or x <= xints:
                            inside = not inside
            p1x,p1y = p2x,p2y
        D[i] = inside
    return D

the code above completed for N=1000000 in 55'' vs 1' 33'' of the precompiled serial version. The system monitor shows only one CPU running at 100%.

How can I try to make use of the whole CPUs available, and return the result in a single array of booleansd?

like image 474
epifanio Avatar asked May 08 '26 03:05

epifanio


1 Answers

Numba's parallel=True enables automatic parallelism only for certain functions, not for all loops. You should replace one of your range() expressions with numba.prange to enable multicore computation.

See: https://numba.pydata.org/numba-doc/dev/user/parallel.html

like image 167
John Zwinck Avatar answered May 09 '26 19:05

John Zwinck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!