On the usage of Utilities::MPI

25 views
Skip to first unread message

Yang Liu

unread,
Sep 11, 2019, 8:44:29 AM9/11/19
to dea...@googlegroups.com
Dear Deal.II developers,

Greetings!

I am a new user of Deal.II. Currently I would like to test the hello world program on Utilities::MPI.

A minimum working example is attached to this email.

However, when I run the command "mpirun -np 5 ./MPI_TEST", I get the following message,

Hello world from process 0 of 1
Hello world from process 0 of 1
Hello world from process 0 of 1
Hello world from process 0 of 1
Hello world from process 0 of 1

It seems that 5 cpu cores are used, however, they seemed to have the same rank.
Besides, my operating system is Ubuntu 16.04. Looking forward to hearing from you soon. Thank you!

Best Regards,

Yang Liu
CMakeLists.txt
MPI_TEST.cc

Bruno Turcksin

unread,
Sep 11, 2019, 9:05:05 AM9/11/19
to deal.II User Group
Yang,

Are sure that you compiled deal.II with MPI support? Also can you post  what you see when you do make VERBOSE=1

Best,

Bruno

Konrad

unread,
Sep 11, 2019, 9:33:01 AM9/11/19
to deal.II User Group
However, when I run the command "mpirun -np 5 ./MPI_TEST", I get the following message,

Hello world from process 0 of 1
Hello world from process 0 of 1
Hello world from process 0 of 1
Hello world from process 0 of 1
Hello world from process 0 of 1

It seems that 5 cpu cores are used, however, they seemed to have the same rank.
Besides, my operating system is Ubuntu 16.04. Looking forward to hearing from you soon. Thank you!

Hi Yang Liu,

Try that code (explanations below): 

#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;
}

The out put of mpirun -np 3 ./MPI_TEST is:

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

Reply all
Reply to author
Forward
0 new messages