Note that there are some explanatory texts on larger screens.

plurals
  1. POSending blocks of 2D array rows using MPI in C
    text
    copied!<p>I am trying to do a matrix multiplication using MPI in C. (c &lt;= a*b)</p> <p>I am running the following code on 4 nodes. All the matrices are 8*8 in size. </p> <pre><code>(num of rows in a matrix % num of nodes == 0) </code></pre> <p>matrix b[][] is broadcast so all the nodes get the same copy. For matrix a[][], instead of broadcasting, I want to send only the set of rows that is needed by each node. </p> <p>But when I run the following code and print the matrix a[][] after MPI_Recv() worker nodes print 0s instead of the values assigned in the master node.</p> <p>Can you point out what am I doing wrong here?</p> <pre><code>#include &lt;stdio.h&gt; #include "mpi.h" #include "matrix.c" // matrix definitions and matrix operation functions are here int main(int argc, char *argv[]) { MPI_Status status; int num, rank, size, tag, high,low,i; int offset, tmphigh,rows; MPI_Init(&amp;argc, &amp;argv); MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank); MPI_Comm_size(MPI_COMM_WORLD, &amp;size); rows=MAX/size; // MAX is the length(=height) of the matrices tag = 201; low=rank*rows; high=low+rows; if (rank == 0) { fillMatrix(b,MAX); fillMatrix(a,MAX); } MPI_Bcast(&amp;b[0][0],MAX*MAX,MPI_INT,0,MPI_COMM_WORLD); if(rank==0){ for(i=1;i&lt;size;i++){ offset=i*rows; MPI_Send(&amp;a[offset][0],rows*MAX,MPI_INT,i,tag,MPI_COMM_WORLD); } }else{ MPI_Recv(&amp;a[low][0],rows*MAX,MPI_INT,0,tag,MPI_COMM_WORLD,&amp;status); } printMatrix(a,MAX); MPI_Finalize(); return 0; } </code></pre> <p>here is how matrices are created</p> <pre><code>int a[MAX][MAX], b[MAX][MAX], c[MAX][MAX]; int len; //(edited after Jeremy W. Sherman's comment ) //this was the reason that caused this problem. changing this to int len=MAX; solved the problem void fillMatrix(int (*matrix)[len], int len){ int i=0,j=0; for(i=0;i&lt;len;i++){ for(j=0;j&lt;len;j++){ matrix[i][j]=j; } } //printMatrix(matrix,len); } </code></pre> <p>Thank You.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload