1

I am trying to run the following MPI code. The problem is with the scanf. The command keep on taking input and does not anywhere. It is supposed to take one input string only.

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

int main(int argc,char * argv[])
{
    int npes, myrank, length = 10;
    char string[length+1];      // array size changed to length +1 as suggested in comments.
    memset(string, 0, length+1);
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &npes);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    if (myrank == 0) {
        printf("Please enter the string of length %d or enter 0 to generate string randomly:\n", length);
        scanf ("%10s", string);     // format changed as suggested in comments
        printf("%s\n", string);
    }

    MPI_Finalize();
    return 0;
}

Output:

enter image description here

Platform: Mac OS X 10.10

MPI Version : Open MPI: 1.8.3

System Info: Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.0.0 Thread model: posix

Please correct me if I am wrong somewhere.

Harshit Gupta
  • 1,200
  • 12
  • 27
  • 1
    Leave some place for the null terminating `\0` character at the end of all strings : `char string[length+1];` Does it change the behavior ? You may specify the maximum number of characters to be read : `scanf ("%10s", string);` – francis Oct 29 '14 at 17:25
  • didn't work @francis. – Harshit Gupta Oct 29 '14 at 17:50
  • 1
    What "didn't work"? Post the results of the change. Did you 1) Change to `char string[length+1];` 2) change to `scanf ("%10s", string);` or 3) both? You need both. – chux - Reinstate Monica Oct 29 '14 at 19:17
  • I applied all the cases 1), 2) and both. No change in behavior. When it asks for input, i type something and press enter and then it does nothing. Only thing you can do there, is keep on supplying the input hoping it would stop somewhere. – Harshit Gupta Oct 29 '14 at 19:50
  • none of the return values from the called functions are being saved/tested, so the actual point where the code is failing could be anywhere. For a start, add a printf, before the if(myrank ==0) to display the actual value (and indicate if the code every gets there. Since there is no message(s) on the console about unrecognized commands, it is for sure that the program is hanging in one of the called functions – user3629249 Oct 30 '14 at 04:28
  • Which Open MPI version is that? Works for me on Linux. – Hristo Iliev Oct 30 '14 at 15:51
  • it works fine for me on ubuntu as well. But my working system is Mac OS x – Harshit Gupta Oct 30 '14 at 19:02
  • How exactly are you starting the program? Are you running it in Terminal or using some sort of IDE? (hard to tell from the screenshot) – Hristo Iliev Oct 31 '14 at 09:40

1 Answers1

1

Reading from stdin is generally inadvisable in MPI programs due to the complexities of forwarding the input to all of the processes. It's not something that will be portable between different implementations.

Usually, the way people get input for their applications is to read input files. That works everywhere and all you have to do is make the file available at all processes.

Wesley Bland
  • 8,816
  • 3
  • 44
  • 59