matrix multiply with mpi

60 views Asked by At

I have a problem with the result of my m1 function when I check that some of the array between rank 0 and the last rank is empty and unfortunately none of the workarounds help to solve this problem. Can anyone help me with this? Where is the problem in this code? And how can it be solved? This is the code: this is the code:

#include <stdlib.h>
#include <time.h>
#include <mpi.h>

#define N 1000
#define M 1000 / 2

int A[N][N], B[N][N], C[N][N];
int m1[M][M], m2[M][M], m3[M][M], m4[M][M], m5[M][M], m6[M][M], m7[M][M];
int A11[M][M], A12[M][M], A21[M][M], A22[M][M], B11[M][M], B12[M][M], B21[M][M], B22[M][M];
int C11[M][M], C12[M][M], C21[M][M], C22[M][M];
int rank, size, start_row, end_row;

void multiplym1(int mySize, int AA[M][M], int BB[M][M], int CC[M][M], int DD[M][M], int resfinal[M][M], int mystart_row, int myend_row)
{
    int result1[mySize][mySize], result2[mySize][mySize];

    for (int i = mystart_row; i < myend_row; i++)
    {
        for (int j = 0; j < mySize; j++)
        {
            result1[i][j] = AA[i][j] + BB[i][j];
            result2[i][j] = CC[i][j] + DD[i][j];
        }
    }

    for (int i = mystart_row; i < myend_row; i++)
    {
        for (int j = 0; j < mySize; j++)
        {
            resfinal[i][j] = 0;
            for (int k = 0; k < mySize; k++)
            {
                resfinal[i][j] += (result1[i][k] * result2[k][j]);
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    srand(time(NULL));

    printf("\n------------------------* Initializing matrices *----------------------\n");

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    start_row = rank * (N / size);
    if (rank + 1 == size)
    {
        end_row = N;
    }
    else
    {
        end_row = (rank + 1) * (N / size);
    }

    // printf("#%d: start is %d and end is %d\n", rank, start_row, end_row);

    for (int i = start_row; i < end_row; i++)
    {
        for (int j = 0; j < N; j++)
        {
            A[i][j] = rand() % 50;
            B[i][j] = rand() % 20;
            C[i][j] = 0;
            // printf("#%d: A[%d][%d] = %d\n", rank, i, j, A[i][j]);
        }
    }
    // printf("#%d: Done\n\n", rank);

    MPI_Barrier(MPI_COMM_WORLD);

    // printf("#%d: start \n\n", rank);
    start_row = 0;
    end_row = 0;

    start_row = rank * (M / size);
    if (rank + 1 == size)
    {
        end_row = M;
    }
    else
    {
        end_row = (rank + 1) * (M / size);
    }
    // printf("#%d: start is %d and end is %d\n", rank, start_row, end_row);

    for (int i = start_row; i < end_row; i++)
    {
        for (int j = 0; j < M; j++)
        {

            A11[i][j] = A[i][j];
            A12[i][j] = A[i][j + M];
            A21[i][j] = A[i + M][j];
            A22[i][j] = A[i + M][j + M];

            B11[i][j] = B[i][j];
            B12[i][j] = B[i][j + M];
            B21[i][j] = B[i + M][j];
            B22[i][j] = B[i + M][j + M];
        }
    }

    // printf("#%d: Done\n\n", rank);

    MPI_Barrier(MPI_COMM_WORLD);

    // printf("#%d: start For M1\n\n", rank);
    start_row = 0;
    end_row = 0;

    start_row = rank * (M / size);
    if (rank + 1 == size)
    {
        end_row = M;
    }
    else
    {
        end_row = (rank + 1) * (M / size);
    }
    printf("#%d: start is %d and end is %d\n", rank, start_row, end_row);

    multiplym1(M, A11, A22, B11, B22, m1, start_row, end_row);

    MPI_Barrier(MPI_COMM_WORLD);

    int *counts = malloc(size * sizeof(int));
    int *displs = malloc(size * sizeof(int));

    for (int i = 0; i < size; i++)
    {
        counts[i] = (M / size) * M;
        displs[i] = i * (M / size) * M;
    }
    counts[size - 1] = ((M / size) + (M % size)) * M;

    MPI_Gatherv(&m1[start_row][0], counts[rank], MPI_INT, m1, counts, displs, MPI_INT, 0, MPI_COMM_WORLD);

    printf("#%d: M1 DONE!!\n", rank);

    if (rank == 0)
    {
        for (int i = 0; i < M; i += 49)
        {
            for (int j = 0; j < M; j += 100)
            {
                printf("#%d: m1[%d][%d] = %d\n", rank, i, j, m1[i][j]);
            }
        }
    }

    MPI_Finalize();
    return 0;
}```
`
0

There are 0 answers