【问题标题】:MPI matrix-matrix multiplicationMPI矩阵-矩阵乘法
【发布时间】:2013-12-23 05:01:20
【问题描述】:

我目前正在尝试使用 C 实现矩阵-矩阵乘法。我有以下代码

for(index=0; index<p; index++) 
    {
        /* calculate the partial sum for matC given the row band of A and
         B */
        for (i=0; i<n/p; i++) 
            for (j=0; j<n; j++) 
                for (k=0; k<n; k++) 
                    storage_matC[i*n+j] += storage_matA[i*n+k]*storage_matB[k*n+j];

        if(index < p-1) 
        {
            /* mpi send storage_matB to the next process (id+1)%p */
            MPI_Send(storage_matB, n, MPI_FLOAT, (id+1)%p, 0, MPI_COMM_WORLD); 
            /* mpi receive storage_matB from the previous process */
            MPI_Recv(&storage_matB, n, MPI_FLOAT, id, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        }
    }

我需要能够发送当前进程中使用的matrix_b,然后在当前进程中从前一个进程中接收。我的程序只是挂在那里,我必须终止它。 有人可以告诉我如何解决这个问题...

非常感谢您的宝贵时间,非常感谢您的帮助!

【问题讨论】:

    标签: c matrix mpi


    【解决方案1】:

    来自 MPI_Send 文档:

    此例程可能会阻塞,直到目标进程接收到消息。

    这就是绊倒你的原因。每个人都在尝试发送,但没有人在听,因为每个人都在尝试发送,所以每个人都在等待有人闭嘴听,但从来没有人这样做,每个人都在想其他人到底在做什么。 :P

    我可以看到的一种方法是错开沟通。例如,假设切片数为偶数,首先所有偶数进程发送,所有奇数进程监听;然后奇数进程发送,偶数进程监听。

    编辑:“我该怎么做?”和我解释的差不多。而不是你的“发送然后接收”,做这样的事情:

    odd_ring = p % 2
    
    // first trip: evens send, odds receive
    if (id % 2) recv();
    else if (!odd_ring || id != p - 1) send();
    
    // second trip: odds send, evens receive
    if (id % 2) send();
    else if (!odd_ring || id) recv();
    
    // only when we have odd number of processes -
    // collecting stragglers: last sends to first
    if (odd_ring)
      if (id == p - 1) send();
      else if (!id) recv();
    

    尚未测试,因此可能存在错误,但本质上我就是这样实现的。

    【讨论】:

    • 这完全有道理!但我怎样才能在我的代码中实现它?我该怎么做?
    • @user2913269:我用伪 C 扩展了答案。
    猜你喜欢
    • 2018-09-08
    • 2017-10-30
    • 2012-11-04
    • 2014-04-21
    • 2015-04-03
    • 2017-10-12
    • 2016-01-23
    • 2013-03-05
    • 2017-07-18
    相关资源
    最近更新 更多