并行填充numpy数组?

2024-04-16 18:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的代码

import numpy as np
A = np.zeros((10000, 10))

for i in range(10000):

    # Some time-consuming calculations which result in a 10 element 1D array 'a'

    A[i, :] = a

如何并行化for循环,以便并行填充数组A?我的理解是,多个进程通常不应该写入同一个变量,所以我不清楚这样做的正确方式是什么


Tags: 代码inimportnumpywhichfortimeas
1条回答
网友
1楼 · 发布于 2024-04-16 18:19:27

下面的代码为数组的每一行创建一个线程,但不确定它的效率有多高

import numpy as np
import threading

def thread_function(index, array):
  # aforementioned time-consuming calculation, resulting in 'a'
  a = np.ones(10)    # placeholder for calculation

  array[index, :] = a

if __name__ == "__main__":
  A = np.zeros((10000, 10))
  threads = []

  for i in range(10000):
    threads.append(threading.Thread(target=thread_function, args=(i, A)))
    threads[i].start()

  for i in range(10000):
    threads[i].join()

  print(A)

https://repl.it/@RobertClarke64/Python-Multithreading
事实上,它并不是特别快,但是当计算需要很长时间时,它可能会明显快于串联运行每个计算

相关问题 更多 >