python - 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.
解决方案
好的,我自己想通了,当 2 个线程想要同时写入同一个像素image[np.int(i1), np.int(i2)]
时,就会出现问题。这将导致仅注册一次写入。如果没有坐标出现两次,则不会出现此问题。
推荐阅读
- python - pipenv install --system 但仅适用于用户
- xml - xmlstarlet:如何使用现有元素的值添加元素
- node.js - 出现错误:发送后无法设置标题
- ios - 适用于 Mac 和 iOS 的应用内购买
- c# - Google OR-Tools HRESULT:Windows Server 2012 R2 上的 0x8007007E
- javascript - TypeScript:未定义枚举对象
- java - 旋转画布和绘制位图性能
- dart - 使身体可滚动颤振
- r - 如何在保留向量的同时取消列出嵌套列表
- php - 如何将已经连接的表与laravel中的其他表连接起来?