I doing an application using C and MPI that makes a vector by Matrix multiplication, but I keep having errors like Error posting readv , and An existing connection was forcibly closed by the remote host (10054)
Here is the code:
#include "stdio.h"
#include "mpi.h"
#define W 5
#define H 5
void make_matrix(int[]);
void make_vector(int []);
void main(int argc, char* argv[])
{
int myrank,size,k;
int matrix[H*W];
int vec[W];
int res[W];
static int col_count = 0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
MPI_Comm_rank(MPI_COMM_WORLD,&size);
MPI_Bcast(vec,W,MPI_INT,0,MPI_COMM_WORLD);
if(myrank != 0)
{
for(k=0; k<W; k++)
{
vec[k]+= matrix[k*W+col_count];
}
col_count++;
printf("%d ",vec[col_count]);
}
MPI_Finalize();
}
void make_matrix(int a[])
{
int i;
for(i=0; i<H*W; i+=1)
{
a[i] = i;
}
};
void make_vector(int v[])
{
int i;
for(i=0; i<H; i++)
v[i] = i*2;
};
MPI_Bcast()is a collective function, which means that every process in the communicator must call it. In other words, don’t callMPI_Recv(). So get rid of yourif(myrank == 0)conditional and have all processes call:Note that I have
vecabove, not&vec; sincevecis already an array, it is the pointer that MPI needs. Also, your result will appear invecon the non-root processes; there is no need for the separatearrarray.I recommend that you read some examples of MPI and try to make your code look more like them.