0
votes

My program is compiling and showing abnormal behaviour.

Used following commands for compiling

mpicc one.c -o one

mpiexec -n 2 ./one

I tried to debug it but show no compilation error. I can't understand why my program is behaving abnormally

#include<mpi.h>
#include<stdio.h>
#include<string.h>
int main(int argc,char * argv[])
{

    char str[434];
    int n;
    int rank,size; MPI_Status status;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    if(rank == 0)
    {//Sender process
        scanf("%s",&str);

        MPI_Ssend(&n,1,MPI_INT,1,0,MPI_COMM_WORLD);
        MPI_Ssend(&str,n,MPI_CHAR,1,0,MPI_COMM_WORLD);
        printf("Sending word %s in process 0\n",str);
    }
    else
    {//Receiver process
        MPI_Recv(&n,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
        MPI_Recv(&str,n,MPI_CHAR,0,0,MPI_COMM_WORLD,&status);
        printf("Receiving word %s in process 1\n",str);
    }
    MPI_Finalize();


    return 0;
}

Input:

haha

Actual result

Sending word haha in process 0

Receiving word haha in process 1

Exchange result

Sending word haha in process 0

Receiving word �������� in process 1

1
Enable compiler warnings. You are passing an address of a pointer to an array in e.g. scanf where you should simply pass pointer to the array str. The same problems occurs later in your code.pbn
You never initialize n. And you should not use &str which is a pointer to the array (and of type char (*)[434]). Use either &str[0] or plain str (which decays to &str[0]). This use of &str is wrong everywhere you use it.Some programmer dude

1 Answers

0
votes

As people have kindly pointed out in the comments to your post, you can't use str the way you were, because scanf() expects a char *, while you are passing it char (*)[434], which are not the same.

You would see this error (or warning), if you compiled with the "-Werror -Wformat" flags.

After modifying your code to use an pointer and not a char array, it looks as below:

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

int main(int argc, char * argv[])
{

    const int size = 434;
    char* str_send = (char*) malloc(size * sizeof(char)); // = "haha";
    char* str_recv = (char*) malloc(size * sizeof(char));
    int n = size;
    int rank;
    MPI_Status status;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    if(rank == 0)
    {//Sender process
        scanf("%s", str_send);

        MPI_Ssend(&n,1,MPI_INT,1,0,MPI_COMM_WORLD);
        MPI_Ssend(str_send,n,MPI_CHAR,1,0,MPI_COMM_WORLD);
        printf("Sending word %s in process 0\n",str_send);
    }
    else
    {//Receiver process
        MPI_Recv(&n,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
        MPI_Recv(str_recv,n,MPI_CHAR,0,0,MPI_COMM_WORLD,&status);
        printf("Receiving word %s in process 1\n",str_recv);
    }
    MPI_Finalize();
    return 0;
}

Which gives the output:

junglefox@ubuntu:~/test_programs$ mpicc main.c

junglefox@ubuntu:~/test_programs$ mpiexec -n 2 ./a.out

haha

Sending word haha in process 0

Receiving word haha in process 1

Since, I feel you want to test the MPI library and not get bogged down into other intricacies, I would suggest using a fixed input and looking for the fixed output, instead of using scanf. As an example,

const char* str_send = "haha";

And then remove or comment out the scanf line.

// scanf("%s", str_send);