#include <deal.II/base/mpi.h>
#include <deal.II/base/conditional_ostream.h>
#include <fstream>
using namespace dealii;
template <int dim>
class MPI_TEST
{
public:
MPI_TEST();
void print_test();
private:
MPI_Comm mpi_communicator;
ConditionalOStream pcout;
};
template <int dim>
void MPI_TEST<dim>::print_test()
{
for (unsigned int i = 0;
i < Utilities::MPI::n_mpi_processes(mpi_communicator);
++i)
{
std::cout << "Hello world from process "
<< Utilities::MPI::this_mpi_process(mpi_communicator)
<< " of "
<< Utilities::MPI::n_mpi_processes(mpi_communicator)
<< std::endl;
}
}
template <int dim>
MPI_TEST<dim>::MPI_TEST()
: mpi_communicator(MPI_COMM_WORLD)
, pcout(std::cout, (Utilities::MPI::this_mpi_process(mpi_communicator) == 0))
{}
int main(int argc, char ** argv)
{
Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
MPI_TEST<2> hw_mpi;
hw_mpi.print_test();
return 0;
}
Hello world from process 1 of 3
Hello world from process 1 of 3
Hello world from process 1 of 3
Hello world from process 0 of 3
Hello world from process 0 of 3
Hello world from process 0 of 3
Hello world from process 2 of 3
Hello world from process 2 of 3
Hello world from process 2 of 3
Looks weird if you are not used to MPI but it is as expected.
1. You must call Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1); before you invoke any action of MPI. It will create an object that calls MPI_Init upon creation and MPI_Finalize in the destructor, i.e., when the object goes out of scope. Only then
2. When you implement MPI code it is important to get into the right mindset: You do not program code for one compute node only. You are programming code for all nodes (at the same time). In your example the for loop gets executed on all nodes( =3 here).
Hope that helps.
Best,
Konrad