Help with Hello World Exercise - Intro to MPI

For anyone willing to help, I have recently started to learn how to use MPI through the course offered on this website, and I seem to have a problem with the first exercise. Although I followed the tips and instructions to create the HelloWorld Program, every time I run my code, I get these messages:

Standard Output
Compiling
Compilation is OK
Execution …
Standard Error
mpirun noticed that the job aborted, but has no info as to the process
that caused that situation.

Process 0
*** The MPI_comm_size() function was called before MPI_INIT was invoked.
→ ERROR, Expected : ‘Hello world, from process #0

Further, this message prints out for my other 3 processes. I would have wanted to send pictures of my error messages, but I can’t because the system says I’m a new member. My code is below as well. Any assistance would be appreciated.

#include
#include <mpi.h>

int main(int argc, char** argv) {
// Initialisation
int MPI_Init(int *argc, char ***argv);

// Reading size and rank
int size, rank;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

// Printing
std::cout << “Hello world, from process #” << rank << std::endl;

// Finalisation
int MPI_Finalize();

return 0;
}

Try

MPI_Init(&argc, &argv);

and

MPI_Finalize();

without int

I just tried it, and it worked fine. Thanks so much. I get why we would use the & symbol for the arguments, but do the MPI_Init and MPI_Finalize not need the int. The information that I read gave these as their signatures:
int MPI_Init(int *argc, char ***argv);
int MPI_Finalize();

I am not an expert in the field, I just used the code given in the tutorial a few pages later :man_shrugging:

A signature can be considered the contract/definition/calling convention of that function. In C++ a signature is of the form: returntype name(parameters).

So for the given signature int MPI_Init(int *argc, char ***argv); the components of it are:

returntype = int
name = MPI_Init
parameters = a list of 2: [ a pointer to an integer, a pointer to a pointer to a pointer to a character ]

When you want/need to use/call a function, you must use it’s name and provide arguments for the expected parameters. You may “catch” the result of the function into a variable with a type matching the returntype.

So when calling MPI_Init we could do something like this:

int initialization_result;
initialization_result = MPI_Init(&argc, &argv);
// Do something more with initialization_result, for example
if (initialization_result != 0) {
    std::cerr << "Could not initialize MPI... aborting" << std::endl;
    return 0;
}

I’m not familiar with MPI, so I’ve just made a guess as to what the return value integer would mean. Likely a value of 0 means “success”, and a different value corresponds to an error code which hopefully is documented in the MPI documentation somewhere.

But as you’ve noticed, you can just as well ignore the return value by not assigning the result of MPI_Init.

Note that as of C++17 it is possible for a function signature to be decorated with [[nodiscard]] which means that a compilation warning shall be triggered if the return value of this function is not used.

1 Like

Okay that makes sense. Thanks for the help.