简体   繁体   English

MPI矩阵矩阵乘法

[英]MPI matrix-matrix multiplication

Im currently trying to implement a matrix-matrix multiplication using C. I have the following code 我目前正在尝试使用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);
        }
    }

I need to be able to send the matrix_b used in the current process, and then receive it in the current process from the previous process. 我需要能够发送当前进程中使用的matrix_b,然后从上一个进程中在当前进程中接收它。 My program just hangs there and i have to terminate it. 我的程序只是挂在那里,我必须终止它。 Can someone shed me some light on how to approach this problem... 有人可以向我阐明如何解决此问题的方法吗?

Thank you so much for your time, your help is greatly appreciate it! 非常感谢您的宝贵时间,非常感谢您的帮助!

From MPI_Send docs: 从MPI_Send文档:

This routine may block until the message is received by the destination process. 该例程可能会阻塞,直到目标进程收到该消息为止。

and this is what is tripping you up. 这就是让您绊倒的原因。 Everyone is trying to send, but no-one is listening since everyone is trying to send, so everyone keeps waiting for someone to shut up and listen, but no-one ever does, and everyone is wondering what the hell everyone else is doing. 每个人都在尝试发送,但是没有人在听,因为每个人都在尝试发送,所以每个人都在等待某人闭嘴并倾听,但没人能做到,每个人都在想其他人在做什么。 :P :P

One method I can see for this is to stagger the communication. 我可以看到的一种方法是交错通信。 For example, assuming an even number of slices, first all the even processes send while all the odd processes listen; 例如,假设条带数量为偶数,首先所有偶数进程发送,而所有奇数进程监听; then the odd processes send, and the even processes listen. 然后,奇数个进程发送,偶数个进程监听。

EDIT: "How can I do that?" 编辑:“我该怎么做?” Pretty much as I have explained. 正如我所解释的。 Instead of your "send then recv", do something like this: 代替您的“先发送然后接收”,执行以下操作:

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();

Haven't tested, so there might be bugs, but in essence that's how I'd implement it. 还没有测试过,所以可能会有错误,但是从本质上讲,这就是我要实现的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM