For example, if I want to multiply 4 of 2 times 2 matrix together, for simplicity, let's say all of them are identical and have entry 1.
Then I am wondering how should I use global reduction in mpi to parallize this? Let's assume the size is 4.
Could you please give me an idea of doing this? Thanks!
# include <stdio.h>
# include <mpi.h>
# define N 4
//Create the 2 times 2 matrix type
typedef double Matrix[2][2];
void printMatrix(Matrix m);
void unitMatrix(Matrix m);
void randomMatrix(Matrix m);
void multMatrix(Matrix r, Matrix a, Matrix b);
void copyMatrix(Matrix out, Matrix in);
double random_number(void);
void my_range(int n, int *i1, int *i2);
int main(int argc, char *argv[])
{
//Create a single matrix a
Matrix a;
Matrix buf;
//Create a set of 100 matrix
Matrix b[N];
int i;
int rank, i1, i2;
double row1[2];
double row2[2];
double col1[2];
double col2[2];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
my_range(N, &i1, &i2);
for (i = 0; i < i1; i++) {
randomMatrix(a);
}
for (i = i1; i <= i2; i++) {
randomMatrix(b[i]);
}
for (i = i2 + 1; i < N; i++) {
randomMatrix(a);
}
unitMatrix(a);
for (i = i1; i <= i2; i++) {
multMatrix(a, a, b[i]);
MPI_Reduce(&a,&buf, 4, MPI_DOUBLE, MPI_PROD, 0,
MPI_COMM_WORLD);
}
if (rank == 0) printMatrix(buf);
MPI_Finalize();
return 0;
}
//print a single matrix
void printMatrix(Matrix m)
{
printf("%26.18e %26.18e %26.18e %26.18e\n",
m[0][0], m[0][1], m[1][0], m[1][1]);
}
void unitMatrix(Matrix m)
{
m[0][0] = 1.0;
m[0][1] = 0.0;
m[1][0] = 0.0;
m[1][1] = 1.0;
}
void randomMatrix(Matrix m)
{
m[0][0] = 1.0;
m[0][1] = 1.0;
m[1][0] = 1.0;
m[1][1] = 1.0;
}
double random_number(void)
{
const int mr = 714025;
const int ia = 1366;
const int ic = 150889;
const double qdnorm = 1.0 / mr;
static int irandom = 0;
irandom = (ia * irandom + ic) % mr;
return(irandom * qdnorm);
}
void multMatrix(Matrix r, Matrix a, Matrix b)
{
// multMatrix(r, a, b) calculates r = a * b
// multMatrix(a, a, b) calculates a = a * b
// multMatrix(a, b, a) calculates a = b * a
Matrix tmp;
tmp[0][0] = a[0][0] * b[0][0] + a[1][0] * b[0][1];
tmp[0][1] = a[0][1] * b[0][0] + a[1][1] * b[0][1];
tmp[1][0] = a[0][0] * b[1][0] + a[1][0] * b[1][1];
tmp[1][1] = a[0][1] * b[1][0] + a[1][1] * b[1][1];
copyMatrix(r, tmp);
}
void copyMatrix(Matrix out, Matrix in)
{
out[0][0] = in[0][0];
out[0][1] = in[0][1];
out[1][0] = in[1][0];
out[1][1] = in[1][1];
}
void my_range(int n, int *i1, int*i2)
{
int size, rank, chunk, rest;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
chunk = n / size;
rest = n % size;
if (rank < rest) {
chunk = chunk + 1;
*i1 = chunk * rank;
} else {
*i1 = chunk * rank + rest;
}
*i2 = *i1 + chunk - 1;
}