Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MPI_Init accept pointers to argc and argv?

Tags:

argv

argc

mpi

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?

like image 677
Rohit Banga Avatar asked Apr 15 '10 05:04

Rohit Banga


People also ask

Why do we use argc and 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.

What is Mpi_init (& argc &argv?

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 the use of argc and argv in C++?

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++.

Why is argv argc null pointer?

— 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).


2 Answers

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.

like image 113
NickTee Avatar answered Oct 07 '22 15:10

NickTee


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.

like image 23
Anycorn Avatar answered Oct 07 '22 15:10

Anycorn