this is how we use MPI_Init function
int main(int argc, char **argv) { MPI_Init(&argc, &argv); … }
why does MPI_Init use pointers to argc and argv instead of values of argv?
The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.
Explanation of functions: MPI_Init(&argc, &argv) – Initializes the MPI execution environment. The variables argc and argv are pointers to command line arguments. MPI_Comm_size(comm, *size) – Gets the number of processes that are associated with a specific communicator.
What is Argc and Argv in C++ in Ubuntu 20.04? The parameter “argc” refers to the argument count, whereas “argv” refers to a character array that holds all the arguments that are passed to the “main()” function through the command line at the time of executing a program in C++.
— argv[argc] shall be a null pointer. The rationale for this is to provide a redundant check for the end of the argument list, on the basis of common practice (ref: Rationale for the ANSI C programming language (1990), 2.1. 2.2).
According to the answer stated here:
Passing arguments via command line with MPI
Most MPI implementations will remove all the mpirun-related arguments in this function so that, after calling it, you can address command line arguments as though it were a normal (non-mpirun) command execution.
i.e. after
mpirun -np 10 myapp myparam1 myparam2
argc = 7(?) because of the mpirun parameters (it also seems to add some) and the indices of myparam1 and myparam2 are unknown
but after
MPI_Init(&argc, &argv)
argc = 3 and myparam1 is at argv[1] and myparam2 is at argv[2]
Apparently this is outside the standard, but I've tested it on linux mpich and it certainly seems to be the case. Without this behaviour it would be very difficult (impossible?) to distinguish application parameters from mpirun parameters.
my guess to potentially allow to remove mpi arguments from commandline. passing argument count by pointer allows to modify its value from the point of main.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With