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

如何使进程能够在主程序的数组中编写?

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

如何使进程能够在主程序的数组中编写?

矩阵乘法是指分别计算所得矩阵的每个元素。对于Pool来说,这似乎是一份工作。由于这是家庭作业(还要遵循SO代码),因此我仅说明Pool本身的用法,而不是整个解决方案。

因此,您必须编写一个例程来计算所得矩阵的第(i,j)个元素:

def getProductElement(m1, m2, i, j):    # some calculations    return element

然后初始化池:

from multiprocessing import Pool, cpu_countpool = Pool(processes=cpu_count())

然后,您需要提交工作。您也可以将它们组织成矩阵,但是为什么麻烦,让我们列出一个清单。

result = []# here you need to iterate through the the columns of the first and the rows of# the second matrix. How you do it, depends on the implementation (how you store# the matrices). Also, make sure you check the dimensions are the same.# The simplest case is if you have a list of columns:N = len(m1)M = len(m2[0])for i in range(N):    for j in range(M):        results.append(pool.apply_async(getProductElement, (m1, m2, i, j)))

然后用结果填充结果矩阵:

m = []count = 0for i in range(N):    column = []    for j in range(M):        column.append(results[count].get())    m.append(column)

同样,代码的确切形状取决于您如何表示矩阵。



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

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

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