1

How do I read arguments from the command line in C++?

I currently have this code:

int data_size = 0;
std::cout << "Please enter an integer value: ";
std::cin >> data_size;
std::cout << "The value you entered is " << data_size;

Main :

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

        int data_size = 0;
        std::cout << "Please enter an integer value: ";
        std::cin >> data_size;
        std::cout << "The value you entered is " << data_size; 


    // initialise the MPI library
    MPI_Init(NULL, NULL);

    // determine the world size
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // determine our rank in the world
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    std::cout << "rank " << world_rank << " size " << world_size << std::endl;

    if (world_rank == 0){
        coordinator(world_size);
    }
    else{
        participant(world_rank, world_size);
    }

    MPI_Finalize();

    return 0;
}

It works but it keeps asking me to enter an integer value 4 times then when I enter it a number it the command line freezes.

here is what i get in the command line

C:\Users\Roland\Documents\Visual Studio 2013\Projects\DistributedSystems\Debug>m
piexec -n 4 .\DistributedSystems.exe
Please enter an integer value:
Please enter an integer value:
Please enter an integer value:
Please enter an integer value: 
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Roland
  • 47
  • 2
  • 9
  • 3
    See this: http://stackoverflow.com/q/3024197/10077 – Fred Larson Mar 09 '15 at 21:54
  • 2
    Do you actually want to use a comment line argument or do you have trouble with reading input via `std::cin`? If the latter is the case provide an [SSCCE](http://www.sscce.org). – Baum mit Augen Mar 09 '15 at 21:57
  • yea i have to use the command line for the input, but i still get the same problem – Roland Mar 09 '15 at 22:25
  • 1
    The code you show is not the reason why you're asked 4 times. You can access the command line arguments from `int main(int argc, char **argv)`, remembering that `argv[0]` is usually the name of the program and `argv[argc] == nullptr` (and hence `argc >= 1`, and you should normally process only `for (int i = 1; i < argc; i++) { ...argv[i]... }`). You should show what you're trying w.r.t command line arguments. Note that command line arguments are C-style null-terminated strings. If you want an integer, you'll need to convert an argument to an integer, maybe using `strtol()` from ``. – Jonathan Leffler Mar 09 '15 at 22:32
  • Is `mpiexec` your program? Is the `mpiexec -n 4` the reason you get your program running 4 times? – Jonathan Leffler Mar 09 '15 at 22:33
  • Please reduce your program to minimal standalone program and provide us ( with main function). That is needed to answer your question. – qqqqq Mar 09 '15 at 22:34
  • @JonathanLeffler `mpiexec` is used to start MPI jobs. `mpiexec -n k` spawns `k` instances of the given program with the given command line arguments (in this case, none), which then can communicate via MPI. – Baum mit Augen Mar 09 '15 at 22:36
  • So, the reason you get asked 4 times is because `mpiexec` runs 4 copies of your program. That is expected behaviour, therefore (or, if it is unexpected, you need to work on adjusting your expectations). If you don't enter a number, the programs will hang waiting until you do enter a number. You read into `data_size`; you then initialize the system with `world_size`, which is uninitialized (though maybe the function called does set it). You never use `data_size` after reading it, which is a bug of some sort. – Jonathan Leffler Mar 09 '15 at 22:43
  • Btw, you should do `MPI_Init(&argc, &argv);`, although `MPI_Init` promises not to change or interpret those. @JonathanLeffler The `world_size` stuff is fine, it is an output parameter for the function. C-interfaces ftw! :( – Baum mit Augen Mar 09 '15 at 22:55
  • @BaummitAugen: I wondered, and left myself wriggle-room. The `data_size` is not visibly used once it is initialized -- I hope that means the relevant code was excised when the MCVE was created. – Jonathan Leffler Mar 09 '15 at 22:58

1 Answers1

3

With MPI programs, reading stuff with std::cin is not a good idea. I have no idea how you could make it work that way and you just shouldn't.

Here are your alternatives though:

If the input to your code is small enough to be passed as command line argument, do so. In your example, your input code block would change to

// Do some error handling if needed, then
int data_size = std::atoi(argv[1]);

and the start the job like

mpiexec -n 4 .\DistributedSystems.exe k

with k being the number you want data_size to be.

If you should get to a point where the amount input is to large for convenient use like this, write it in a file and pass the input filename as above. Then, every process can open that file in its own std::ifstream and read the data from there.

According to Rob Latham, this working is an implementation specific behavior. You can however generally expect this to work if your system uses a command line interface.

Community
  • 1
  • 1
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Getting `std::cin` to work with MPI is fairly easy: Just use `if(world_rank == 0) { std::cin << data_size; } MPI_Bcast(&data_size, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);` On the other hand, command line arguments are better in this case, yes. – wolfPack88 Mar 10 '15 at 14:41
  • byt he strict letter of the MPI standard, command line arguments are only gauranteed to be meaningful on rank 0. in practice, your approach works fine. – Rob Latham Mar 10 '15 at 18:21
  • @RobLatham I did not know that. Do you have a standard quote or link to documentation for that? – Baum mit Augen Mar 10 '15 at 18:28
  • Section 8.7, Startup (page 357 of MPI-3.0): "One goal of MPI is to achieve source code portability. [Such a] program written using MPI and complying with the relevant language standards is portable as written, and must not require any source code changes when moved from one system to another. This explicitly does not say anything about how an MPI program is started or launched from the command line, nor what the user must do to set up the environment in which an MPI program will run." also 8.8 "the range of environments is so diverse (e.g., there may not even be a command line interface)". – Rob Latham Mar 10 '15 at 18:39
  • @RobLatham Ok, thank you. I will add a remark to my answer. I do not see the guarantee for rank 0 in your quote yet, but goes out of the scope of comments anyway. A new question might be in order for that. – Baum mit Augen Mar 11 '15 at 12:01
  • It is purely pedantic. At this point it would only be some obscure implementation – Rob Latham Mar 11 '15 at 12:03