I'm trying to implement matrix multiplication by graph topology for example, but this code doesn't work. The ring topology works perfectly. What is wrong?
My Code:
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
#define M 20
#define N 5
static double A[N][M], B[M], C[N];
int main(int argc, char** argv)
{
int rank, size, *index, *edges, i, j, v, reord = 0;
double rt, t1, t2;
MPI_Comm comm_gr;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
index = new int[size]; // (int*)malloc(size * sizeof(int));
edges = new int[((size - 1) + (size - 1) * 3)]; // (int*)malloc(((size - 1) + (size - 1) * 3) * sizeof(int));size = 4, 12, 0..3,
index[0] = size - 1;
for (i = 0; i < size - 1; i++) {
edges[i] = i + 1;
}
v = 0;
for (i = 1; i < size; i++)
{
index[i] = (size - 1) + i * 3;
edges[(size - 1) + v++] = 0;
edges[(size - 1) + v++] = ((i - 2) + (size - 1)) % (size - 1) + 1;
edges[(size - 1) + v++] = i % (size - 1) + 1;
}
MPI_Graph_create(MPI_COMM_WORLD, size, index, edges, reord, &comm_gr);
MPI_Comm_rank(comm_gr, &rank);
for (i = 0; i < M; j++)
{
for (j = 0; j < N; i++)
{
A[j][i] = 3.0;
C[j] = 0.0;
}
B[i] = 2.0;
}
t1 = MPI_Wtime();
for (j = 0; j < N; j++)
{
for (i = 0; i < M; i++)
C[j] += A[j][i] * B[i];
}
MPI_Allgather(C, N, MPI_DOUBLE, B, N, MPI_DOUBLE, comm_gr);
t2 = MPI_Wtime();
rt = t2 - t1;
printf("rank = %d Time = %f\n", rank, rt);
if (rank == 0)
{
for (i = 0; i < M; i++)
printf("B = %6.2f\n", B[i]);
}
MPI_Finalize();
return(0);
}
Error:
mpiexec -np 4 Ex2.exe job aborted: [ranks] message
[0-2] terminated [3] process exited without calling finalize ---- error analysis ----- [3] on DESKTOP Ex2.exe ended prematurely and may have crashed. exit code 0xc0000005 ---- error analysis -----
Please tell me how to fix it?
User contributions licensed under CC BY-SA 3.0