Python 多处理和共享 numpy 数组
Python multiprocessing and shared numpy array
我有一个问题,与此类似:
import numpy as np
C = np.zeros((100,10))
for i in range(10):
C_sub = get_sub_matrix_C(i, other_args) # shape 10x10
C[i*10:(i+1)*10,:10] = C_sub
因此,显然没有必要运行将其作为串行计算,因为每个子矩阵都可以独立计算。
我想使用多处理模块并为 for 循环创建最多 4 个进程。
我阅读了一些有关多处理的教程,但无法弄清楚如何使用它来解决我的问题。
感谢您的帮助
下面的食谱也许可以完成这项工作。欢迎提问。
import numpy as np
import multiprocessing
def processParallel():
def own_process(i, other_args, out_queue):
C_sub = get_sub_matrix_C(i, other_args)
out_queue.put(C_sub)
sub_matrices_list = []
out_queue = multiprocessing.Queue()
other_args = 0
for i in range(10):
p = multiprocessing.Process(
target=own_process,
args=(i, other_args, out_queue))
procs.append(p)
p.start()
for i in range(10):
sub_matrices_list.extend(out_queue.get())
for p in procs:
p.join()
return sub_matrices_list
C = np.zeros((100,10))
result = processParallel()
for i in range(10):
C[i*10:(i+1)*10,:10] = result[i]
并行化该代码的一种简单方法是使用 Pool
个进程:
pool = multiprocessing.Pool()
results = pool.starmap(get_sub_matrix_C, ((i, other_args) for i in range(10)))
for i, res in enumerate(results):
C[i*10:(i+1)*10,:10] = res
我使用了 starmap
,因为 get_sub_matrix_C
函数有多个参数(starmap(f, [(x1, ..., xN)])
调用 f(x1, ..., xN)
)。
但是请注意,serialization/deserialization 可能会花费大量时间 和 space,因此您可能必须使用更底层的解决方案来避免这种开销.
看起来你是 运行 python 的过时版本。您可以将 starmap
替换为普通的 map
但是您必须提供一个采用单个参数的函数:
def f(args):
return get_sub_matrix_C(*args)
pool = multiprocessing.Pool()
results = pool.map(f, ((i, other_args) for i in range(10)))
for i, res in enumerate(results):
C[i*10:(i+1)*10,:10] = res
我有一个问题,与此类似:
import numpy as np
C = np.zeros((100,10))
for i in range(10):
C_sub = get_sub_matrix_C(i, other_args) # shape 10x10
C[i*10:(i+1)*10,:10] = C_sub
因此,显然没有必要运行将其作为串行计算,因为每个子矩阵都可以独立计算。 我想使用多处理模块并为 for 循环创建最多 4 个进程。 我阅读了一些有关多处理的教程,但无法弄清楚如何使用它来解决我的问题。
感谢您的帮助
下面的食谱也许可以完成这项工作。欢迎提问。
import numpy as np
import multiprocessing
def processParallel():
def own_process(i, other_args, out_queue):
C_sub = get_sub_matrix_C(i, other_args)
out_queue.put(C_sub)
sub_matrices_list = []
out_queue = multiprocessing.Queue()
other_args = 0
for i in range(10):
p = multiprocessing.Process(
target=own_process,
args=(i, other_args, out_queue))
procs.append(p)
p.start()
for i in range(10):
sub_matrices_list.extend(out_queue.get())
for p in procs:
p.join()
return sub_matrices_list
C = np.zeros((100,10))
result = processParallel()
for i in range(10):
C[i*10:(i+1)*10,:10] = result[i]
并行化该代码的一种简单方法是使用 Pool
个进程:
pool = multiprocessing.Pool()
results = pool.starmap(get_sub_matrix_C, ((i, other_args) for i in range(10)))
for i, res in enumerate(results):
C[i*10:(i+1)*10,:10] = res
我使用了 starmap
,因为 get_sub_matrix_C
函数有多个参数(starmap(f, [(x1, ..., xN)])
调用 f(x1, ..., xN)
)。
但是请注意,serialization/deserialization 可能会花费大量时间 和 space,因此您可能必须使用更底层的解决方案来避免这种开销.
看起来你是 运行 python 的过时版本。您可以将 starmap
替换为普通的 map
但是您必须提供一个采用单个参数的函数:
def f(args):
return get_sub_matrix_C(*args)
pool = multiprocessing.Pool()
results = pool.map(f, ((i, other_args) for i in range(10)))
for i, res in enumerate(results):
C[i*10:(i+1)*10,:10] = res