我有以下for循环:
$ git remote rename heroku heroku-staging
for j in range(len(a_nested_list_of_ints)):
arr_1_, arr_2_, arr_3_ = foo(a_nested_list_of_ints[j])
arr_1[j,:] = arr_1_.data.numpy()
arr_2[j,:] = arr_2_.data.numpy()
arr_3[j,:] = arr_3_.data.numpy()
是int的嵌套列表。但是,这需要很多时间才能完成。如何通过多处理对其进行优化?到目前为止,我尝试使用a_nested_list_of_ints
multiprocessing
但是,我得到了:
p = Pool(5)
for j in range(len(a_nested_list_of_ints)):
arr_1_, arr_2_, arr_3_ = p.map(foo,a_nested_list_of_ints[j])
arr_1[j,:] = arr_1_.data.numpy()
arr_2[j,:] = arr_2_.data.numpy()
arr_3[j,:] = arr_3_.data.numpy()
此处:
ValueError: not enough values to unpack (expected 3, got 2)
关于如何使上述操作更快的任何想法?我什至还尝试过使用starmap,但是它不能正常工作。
答案 0 :(得分:4)
这是一个有效的pool
演示:
In [11]: def foo(i):
...: return np.arange(i), np.arange(10-i)
...:
In [12]: with multiprocessing.Pool(processes=2) as pool:
...: x = pool.map(foo, range(10))
...:
In [13]: x
Out[13]:
[(array([], dtype=int64), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])),
(array([0]), array([0, 1, 2, 3, 4, 5, 6, 7, 8])),
(array([0, 1]), array([0, 1, 2, 3, 4, 5, 6, 7])),
(array([0, 1, 2]), array([0, 1, 2, 3, 4, 5, 6])),
(array([0, 1, 2, 3]), array([0, 1, 2, 3, 4, 5])),
(array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])),
(array([0, 1, 2, 3, 4, 5]), array([0, 1, 2, 3])),
(array([0, 1, 2, 3, 4, 5, 6]), array([0, 1, 2])),
(array([0, 1, 2, 3, 4, 5, 6, 7]), array([0, 1])),
(array([0, 1, 2, 3, 4, 5, 6, 7, 8]), array([0]))]
pool.map
正在执行迭代,而不是外部for
外部循环。
并进一步接近您的示例:
In [14]: def foo(alist):
...: return np.arange(*alist), np.zeros(alist,int)
...:
...:
In [15]: alists=[(0,3),(1,4),(1,6,2)]
In [16]: with multiprocessing.Pool(processes=2) as pool:
...: x = pool.map(foo, alists)
...:
In [17]: x
Out[17]:
[(array([0, 1, 2]), array([], shape=(0, 3), dtype=int64)),
(array([1, 2, 3]), array([[0, 0, 0, 0]])),
(array([1, 3, 5]), array([[[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]]]))]
请注意,pool.map
返回一个列表,其中所有情况均来自alists
。解开x
的包装没有意义。
x,y = pool.map(...) # too many values to pack error
我可以使用x
惯用法解开zip*
的包装:
In [21]: list(zip(*x))
Out[21]:
[(array([0, 1, 2]), array([1, 2, 3]), array([1, 3, 5])),
(array([], shape=(0, 3), dtype=int64), array([[0, 0, 0, 0]]), array([[[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]]]))]
这是2个元组的列表;实际上是转置的列表版本。可以将其打开:
In [23]: y,z = zip(*x)
In [24]: y
Out[24]: (array([0, 1, 2]), array([1, 2, 3]), array([1, 3, 5]))
In [25]: z
Out[25]:
(array([], shape=(0, 3), dtype=int64), array([[0, 0, 0, 0]]), array([[[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]]]))
答案 1 :(得分:0)
这是我经常使用的多处理实现。它将把您的列表(在这种情况下为a_nested_list_of_ints
)划分为多个核心。然后,它在每个拆分列表上运行foo
函数,每个核一个列表。
def split_list(all_params, instances):
return list(np.array_split(all_params, instances))
# split the list up into equal chucks for each core
n_proc = multiprocessing.cpu_count()
split_items = split_list(to_calc, n_proc)
# create the multiprocessing pool
pool = Pool(processes=n_proc)
all_calcs = []
for i in range(n_proc):
# the arguments to the foo definition have to be a tuple - (split[i],)
async_calc = pool.apply_async(foo, (split_items[i],))
all_calcs.append(async_calc)
pool.close()
pool.join()
# get results
all_results = []
for result in all_calcs:
all_results += result.get()
print(all_results)