为什么这个异步矩阵乘法代码比同步慢?

发布于 2025-01-15 09:23:33 字数 651 浏览 4 评论 0原文

我试图通过使用并发.futures.threadPoolExecutor() 线程来实现比同步乘法函数更快的矩阵乘法函数。代码如下:

def asyncmult(m1, m2):
threads = []
prod = np.zeros((m1.shape[0], m2.shape[1]))

def multvecs(inp):
    vec1, vec2, index = inp
    i, j = index
    sum = 0
    for k in range(vec1.shape[0]):
        sum += vec1[k] * vec2[k]
    prod[i][j] = sum

with concurrent.futures.ThreadPoolExecutor() as executor:
    for i in range(m1.shape[0]):
        for j in range(m2.shape[1]):
            f = executor.submit(multvecs, (m1[i], m2[:, j], (i, j)))
            threads.append(f)
return prod

在绘制输入矩阵大小增加所花费的时间时,该函数的性能比同步函数的性能更差。 为什么会发生这种情况?我该如何解决这个问题?

I'm trying to implement a matrix multiplication function that is faster than my synchronous multiplication function by using concurrent.futures.threadPoolExecutor() threads. Here is the code:

def asyncmult(m1, m2):
threads = []
prod = np.zeros((m1.shape[0], m2.shape[1]))

def multvecs(inp):
    vec1, vec2, index = inp
    i, j = index
    sum = 0
    for k in range(vec1.shape[0]):
        sum += vec1[k] * vec2[k]
    prod[i][j] = sum

with concurrent.futures.ThreadPoolExecutor() as executor:
    for i in range(m1.shape[0]):
        for j in range(m2.shape[1]):
            f = executor.submit(multvecs, (m1[i], m2[:, j], (i, j)))
            threads.append(f)
return prod

on plotting against time taken with increase in input matrix size this function performs the worse than synchronous.
why is this happening? How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文