Passing NULL to MPI Init
From HPCBugBase
HPCBugBase Menu
Submit feedback
Overview
Index
- Defect types (defect patterns)
- Specific defects (individual defects that belong to a defect type)
- Instances (code examples)
- Articles (various info)
- Templates
- Show all categories
- Show all pages
Index by Languages
Contents |
[edit] Fault Description
In the C version of MPI, MPI_Init takes two parameters: int MPI_Init(int *argc, char ***argv). Their intendend use was to pass the command-line arguments to allow an MPI implementation to make use of them. In MPI 1.1, calling MPI_Init with NULL parameters in come implementation can fail.
Citation from the MPI 2.0 spec: In MPI-1.1, it is explicitly stated that an implementation is allowed to require that the arguments argc and argv passed by an application to MPI_INIT in C be the same arguments passed into the application as the arguments to main. In MPI-2 implementations are not allowed to impose this requirement. Conforming implementations of MPI are required to allow applications to pass NULL for both the argc and argv arguments of main. In C++, there is an alternative binding for MPI::Init that does not have these arguments at all. Rationale. In some applications, libraries may be making the call to MPI_Init, and may not have access to argc and argv from main. It is anticipated that applications requiring special information about the environment or information supplied by mpiexec can get that information from environment variables. ( End of rationale.)
We consider this as a defect in a sense that it can cause a portability problem when, for example, a user reused the code on a different machine (with a different MPI implementation).
[edit] Statistics (Frequency)
[edit] Other Findings and Contexts
Below is a simple pseudo-code which contains this defect.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
MPI_Status status;
status = MPI_Init(NULL, NULL);
if (status != MPI_SUCCESS) {
return -1;
}
...
MPI_Finalize();
return 0;
}
In this case, fixing it is as easy as changing the line to:
status = MPI_Init(&argc, &argv);
However, MPI_Init may be called outside of main(). For example, in the following pseudo-code, MPI_Init is called inside the function init().
void init()
{
MPI_Status status;
status = MPI_Init(NULL, NULL);
if (status != MPI_SUCCESS) {
exit(-1);
}
}
void final()
{
MPI_Finalize();
}
int main(int argc, char **argv)
{
init();
...
final();
return 0;
}
To fix the defect in this case, init() must be changed to receive argc and argv, and main() has to call it with these parameters. It is still easy in this simple example, but when you are developing a bigger application, making this kind of modification can be more complicated.
- main() may call init() indirectly, in which case all functions in the middle (and all the locations calling them) must be modified too.
- main() and init() may be written by different developers. In this case changing both in this manner may not be possible.
[edit] References
|
Pages referring to this entry: Six Most Basic MPI Functions Use of Language Features |
