I'm trying to gather some rows of different matrices from nodes in an MPI configuration. So far I've got the program to receive one row to another process with the code I have below, i.e. the code will change the matrix recv
to the numbers 1..7
but ideally what I'd like it to do is change the first two rows, numbers 1..7
on the first row and 8..14
on the second, but this doesn't happen when I change the send/receive count on line 55/57. The blocks should be laid out contiguously in memory so I'm not sure where I'm going wrong currently, any help would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
double **allocMatrix(int dim) {
int i;
double **matrix;
matrix = (double **)malloc(dim*sizeof(double *));
for(i=0; i < dim; i++) {
matrix[i] = (double *)malloc(dim*sizeof(double));
}
return matrix;
}
void printMatrix(double **values, int size) {
int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("%10lf ", values[i][j]);
}
printf("\n");
}
}
int main(int argc, char* argv[]) {
MPI_Init(&argc, &argv);
int size, rank, i, j;
int dimensions = 7;
MPI_Comm_size(MPI_COMM_WORLD, &size);//number of processes
MPI_Comm_rank(MPI_COMM_WORLD, &rank);//rank for each process
double **send = allocMatrix(dimensions);
double **recv = allocMatrix(dimensions);
int count = 0;
for (i=0; i<dimensions; i++) {
for (j=0; j<dimensions; j++) {
if (rank == 0) {
recv[i][j] = 0;
} else {
send[i][j] = ++count;
}
}
}
MPI_Datatype arrType;
MPI_Type_vector(1, dimensions, 0, MPI_DOUBLE, &arrType);
MPI_Type_commit(&arrType);
int recvCounts[size];
int displs[size];
recvCounts[0] = 0;
displs[0] = 0;
recvCounts[1] = 1;
displs[1] = 0;
MPI_Gatherv(&(send[0][0]), 1, arrType,
&(recv[0][0]), recvCounts, displs, arrType,
0, MPI_COMM_WORLD);
if (rank == 0) {
printMatrix(recv, dimensions);
}
MPI_Finalize();
return 0;
}
Output:
make gatherv
mpicc -Wall -o gatherv gatherv.c && ./gather
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Desired output:
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
8.000000 9.000000 10.00000 11.00000 12.00000 13.00000 14.00000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000