ahat long
ahat long

Reputation: 31

Segmentation fault in matrix multiplication using mpi

I am trying to write an mpi program for multiplication of 2 matrix . If I give the size of the matrix lower that 800 the code works but when I give it higher I am getting segmentation fault and I am not able to figure out why . I am new to MPI so still trying to understand everything. If possible please help.

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

#define N 1000

int main(int argc, char* argv[]) {
    int rank, size;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    double a[N][N], b[N][N], c[N][N];
    int i, j, k;

    // Initialize the matrices with random values
    if (rank == 0) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                a[i][j] = (double)rand() / RAND_MAX;
                b[i][j] = (double)rand() / RAND_MAX;
            }
        }
    }

    // Broadcast the matrices to all ranks
    MPI_Bcast(a, N*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    MPI_Bcast(b, N*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    // Each rank calculates a portion of the output matrix
    int rows_per_rank = N / size;
    int start_row = rows_per_rank * rank;
    int end_row = start_row + rows_per_rank;
    for (i = start_row; i < end_row; i++) {
        for (j = 0; j < N; j++) {
            c[i][j] = 0;
            for (k = 0; k < N; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    // Gather the output matrix from all ranks
    double* c_buffer = (double*) malloc(N*N*sizeof(double));
    MPI_Gather(c, rows_per_rank*N, MPI_DOUBLE, c_buffer, rows_per_rank*N, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    // Print the output matrix
    if (rank == 0) {
        printf("Output matrix C:\n");
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                printf("%lf ", c_buffer[i*N + j]);
            }
            printf("\n");
        }
    }

    free(c_buffer);
    MPI_Finalize();
    return 0;
}

Upvotes: 2

Views: 79

Answers (1)

pm100
pm100

Reputation: 50110

this line

double a[N][N], b[N][N], c[N][N];

with N = 1000 requires 24mb of stack space. Thats almost certainly larger than whats available. Either allocate them statically (place the kw static before them) or dynamically on the heap

Upvotes: 1

Related Questions