1.9. Automatic parallelization with
Setting the parallel option for
an experimental Numba feature that attempts to automatically parallelize and
perform other optimizations on (part) of a function. At the moment, this
feature only works on CPUs.
Some operations inside a user defined function, e.g., adding a scalar value to
an array, are known to have parallel semantics. A user program may contain
many such operations and while each operation could be parallelized
individually, such an approach often has lackluster performance due to poor
cache behavior. Instead, with auto-parallelization, Numba attempts to
identify such operations in a user program, and fuse adjacent ones together,
to form one or more kernels that are automatically run in parallel.
The process is fully automated without modifications to the user program,
which is in contrast to Numba’s
guvectorize() mechanism, where manual effort is required
to create parallel kernels.
1.9.1. Supported Operations¶
In this section, we give a list of all the array operations that have parallel semantics and for which we attempt to parallelize.
All numba array operations that are supported by Case study: Array Expressions, which include common arithmetic functions between Numpy arrays, and between arrays and scalars, as well as Numpy ufuncs. They are often called element-wise or point-wise array operations:
Numpy reduction functions
prod. Note that they have to be written as
Numpy array creation functions
dotfunction between a matrix and a vector, or two vectors. In all other cases, Numba’s default implementation is used.
Multi-dimensional arrays are also supported for the above operations when operands have matching dimension and size. The full semantics of Numpy broadcast between arrays with mixed dimensionality or size is not supported, nor is the reduction across a selected dimension.
1.9.2. Explicit Parallel Loops¶
Another experimental feature of this module is support for explicit parallel
loops. One can use Numba’s
prange instead of
range to specify that a
loop can be parallelized. The user is required to make sure that the loop does
not have cross iteration dependencies except the supported reductions.
Currently, reductions on scalar values are supported and are inferred from
in-place operations. The example below demonstrates a parallel loop with a
A is a one-dimensional Numpy array):
from numba import njit, prange @njit(parallel=True) def prange_test(A): s = 0 for i in prange(A.shape): s += A[i] return s
In this section, we give an example of how this feature helps parallelize Logistic Regression:
@numba.jit(nopython=True, parallel=True) def logistic_regression(Y, X, w, iterations): for i in range(iterations): w -= np.dot(((1.0 / (1.0 + np.exp(-Y * np.dot(X, w))) - 1.0) * Y), X) return w
We will not discuss details of the algorithm, but instead focus on how this program behaves with auto-parallelization:
Yis a vector of size
N x Dmatrix, and
wis a vector of size
- The function body is an iterative loop that updates variable
w. The loop body consists of a sequence of vector and matrix operations.
- The inner
dotoperation produces a vector of size
N, followed by a sequence of arithmetic operations either between a scalar and vector of size
N, or two vectors both of size
- The outer
dotproduces a vector of size
D, followed by an inplace array subtraction on variable
- With auto-parallelization, all operations that produce array of size
Nare fused together to become a single parallel kernel. This includes the inner
dotoperation and all point-wise array operations following it.
- The outer
dotoperation produces a result array of different dimension, and is not fused with the above kernel.
Here, the only thing required to take advantage of parallel hardware is to set
the parallel option for
jit(), with no
modifications to the
logistic_regression function itself. If we were to
give an equivalence parallel implementation using
it would require a pervasive change that rewrites the code to extract kernel
computation that can be parallelized, which was both tedious and challenging.