首页 > 解决方案 > Parallelization with Numba

问题描述

I am trying to make some code run in parallel using Numba, but the results differ depending on whether the code is executed in parallel or not. I am not sure whether my problem simply cannot be parallelized the way I want to do it or whether I am making a mistake. This is the code:

import numba as nb
import numpy as np


@nb.njit()
def add_to_img(image, i1, i2):
    image[np.int(i1), np.int(i2)] += 1


def iterate_over_indices(image, indices1, indices2):
    for i in nb.prange(len(indices1)): 
        add_to_img(image, indices1[i], indices2[i])


iterate_seq = nb.njit(iterate_over_indices)
iterate_par = nb.njit(iterate_over_indices, parallel=True)


for _ in range(5):
    image_seq = np.zeros((3, 3))
    image_par = np.zeros_like(image_seq)
    ind1 = np.random.uniform(0, image_seq.shape[0], size=1000)
    ind2 = np.random.uniform(0, image_seq.shape[1], size=1000)
    iterate_seq(image_seq, ind1, ind2)
    iterate_par(image_par, ind1, ind2)
    print(np.array_equal(image_seq, image_par))

Most of the times, image_seq is different from image_par. What is the reason for this? I am running on a machine with 4 physical cores and 8 threads.

标签: pythonparallel-processingnumba

解决方案


好的,我自己想通了,当 2 个线程想要同时写入同一个像素image[np.int(i1), np.int(i2)]时,就会出现问题。这将导致仅注册一次写入。如果没有坐标出现两次,则不会出现此问题。


推荐阅读