栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在python numpy中并行化求和计算?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在python numpy中并行化求和计算?

我想出了如何使用多处理,apply_async和回调将数组的总和并行化,所以我将其发布在这里供其他人使用。我使用的示例页面并行的Python的总和回调类,虽然我没有真正使用该程序包实施。不过,它给了我使用回调的想法。这是我最终使用的简化代码,它可以完成我想要的操作。

import multiprocessingimport numpy as npimport threadclass Sum: #again, this class is from ParallelPython's example pre (I modified for an array and added comments)    def __init__(self):        self.value = np.zeros((1,512*512)) #this is the initialization of the sum        self.lock = thread.allocate_lock()        self.count = 0    def add(self,value):        self.count += 1        self.lock.acquire() #lock so sum is correct if two processes return at same time        self.value += value #the actual summation        self.lock.release()def computation(index):    array1 = np.ones((1,512*512))*index #this is where the array-returning computation goes    return array1def summers(num_iters):    pool = multiprocessing.Pool(processes=8)    sumArr = Sum() #create an instance of callback class and zero the sum    for index in range(num_iters):        singlepoolresult = pool.apply_async(computation,(index,),callback=sumArr.add)    pool.close()    pool.join() #waits for all the processes to finish    return sumArr.value

我还可以使用并行映射来完成此工作,这在另一个答案中建议。我已经尝试过了,但是没有正确实现。两种方法都有效,我认为这个答案很好地说明了使用哪种方法(映射或apply.async)的问题。对于地图版本,您无需定义Sum类,summers函数将变为

def summers(num_iters):    pool = multiprocessing.Pool(processes=8)    outputArr = np.zeros((num_iters,1,512*512)) #you wouldn't have to initialize these    sumArr = np.zeros((1,512*512))   #but I do to make sure I have the memory    outputArr = np.array(pool.map(computation, range(num_iters)))    sumArr = outputArr.sum(0)    pool.close() #not sure if this is still needed since map waits for all iterations    return sumArr


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号