I am new to MPI and have only learnt the basics. While performing the matrix vector multiplication based on the following algorithm, an error message appears that I can not understand and thus cannot fix.
The error message : Signal : Segmentation fault (11) Signal Code: Address not mapped (1)
/* Matrix-vector product Ab = c with parallel inner products*/
/* Row-oriented blockwise distribution of A */
/* Replicated distribution of vectors b and c */
local n = n/p;
for (i=0; i<local n; i++) local c[i] = 0;
for (i=0; i<local n; i++)
for (j=0; j<m; j++)
local c[i] = local c[i] + local A[i][j] * b[j];
multi broadcast(local c,local n,c);
/* Multi-broadcast operation of (c[0]; :::; c[localn]) to globalc*/
This is my code:
#include<stdio.h>
#include<stdlib.h>
#include "mpi.h"
#define n 4
#define m 4
void matrix_vector_product(double **matrix, double *vector, double *result, int rows, int cols)
{
for(int i=0; i<rows; i++)
result[i] = 0;
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
result[i] += matrix[i][j] * vector[j];
}
int main(int argc, char *argv[])
{
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
double **matrix;
matrix = (double **)calloc(n,sizeof(double *));
for (int i=0; i<n; i++)
{
matrix[i] = (double *)calloc(m, sizeof(double));
}
double *b;
b = (double *)calloc(m, sizeof(double));
double *c = NULL;
double *local_c = (double *)calloc(n, sizeof(double));
int local_n = n/size;
if (rank == 0)
{
for(int i=0; i<n; i++)
for(int j =0; j<m; j++)
matrix[i][j] = i+j;
for(int j =0; j<m; j++)
b[j] = j+1;
c = (double *)calloc(m, sizeof(double));
}
MPI_Scatter(&matrix, local_n*m, MPI_DOUBLE, &matrix, local_n*m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Bcast(b, m, MPI_DOUBLE, 0, MPI_COMM_WORLD);
matrix_vector_product(matrix, b, local_c, local_n, m);
MPI_Gather(local_c, local_n, MPI_DOUBLE, c, local_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if(rank==0){
printf(" Result = ");
for(int i=0; i<n; i++)
printf(" .2%f", c[i]);
}
MPI_Finalize();
free(matrix);
free(b);
free(c);
free(local_c);
return 0;
}
If you could also explain as to why this is happening that would be great since I want to clear my concepts.
I tried to multiply a *matrix *of size n x m with a vector of size m. Since a distributed memory distribution is assumed, I used MPI_Scatter operation to assign blocks of memory of rows of matrix to achieve row-wise block-wise distribution of A. b is replicated using MPI_Bcast operation. The result is calculated using a function matrix_vector_product() and the content of the local buffer(array here) c are gathered in c. I was expecting the output to be
Result = 8 10 12 14
simpletype*
, in your case double*
. So you can not pass matrix
which is a double**
.double *matrix
and use a conversion from 2D indexing to 1D. Then pass
MPI_Scatter(matrix,....`