abhimanyu bahree
abhimanyu bahree

Reputation: 83

Stack Smashing Error MPI_Send() and MPI_Recv()

I want to resolve stack smashing error in the code

I have tried running the code with mpicc and mpiexec

#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
    int rank,size,x,status;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(rank==0)
    {
        scanf("%d",&x);
        MPI_Send(&x,1,MPI_INT,1,1,MPI_COMM_WORLD);
        printf("I have send %d from process 0\n",x);
        //fflush(stdout);
    }
    else
    {
        MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
        printf("I have received %d in process 1\n",x);
        //fflush(stdout);
    }
    MPI_Finalize();
    return 0;
}

I expected the code to print the receiving and sending statements but the actual output prints the receiving and sending statements and gives

* stack smashing error *

I can't understand why does it happen?

Upvotes: 0

Views: 312

Answers (1)

Duck Dodgers
Duck Dodgers

Reputation: 3461

You need to use MPI_Status instead of int for declaring the status variable, as shown in the code below.

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

int main(int argc, char *argv[])
{
    int rank,size,x;

    MPI_Status status;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(rank==0)
    {
        scanf("%d",&x);
        MPI_Send(&x,1,MPI_INT,1,1,MPI_COMM_WORLD);
        printf("I have send %d from process 0\n",x);
        //fflush(stdout);
    }
    else
    {
        MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
        printf("I have received %d in process 1\n",x);
        //fflush(stdout);
    }
    MPI_Finalize();
    return 0;
}

Upvotes: 4

Related Questions