I can use MPI_Comm_size to get the number of total processors.
But how can I get the number of real physical machine?
If by physical machine you mean a set of processing elements, sharing a common memory address space, then the MPI-3 split-by-type operation MPI_COMM_SPLIT_TYPE could be used to portably obtain the number of such machines:
int getNodeCount(void)
{
   int rank, is_rank0, nodes;
   MPI_Comm shmcomm;
   MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0,
                       MPI_INFO_NULL, &shmcomm);
   MPI_Comm_rank(shmcomm, &rank);
   is_rank0 = (rank == 0) ? 1 : 0;
   MPI_Allreduce(&is_rank0, &nodes, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
   MPI_Comm_free(&shmcomm);
   return nodes;
}
And in Fortran:
subroutine getNodeCount(count)
  use mpi
  implicit none
  integer, intent(out) :: count
  integer :: shmcomm, rank, is_rank0, ierr
  call MPI_COMM_SPLIT_TYPE(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0, &
                           MPI_INFO_NULL, shmcomm, ierr)
  call MPI_COMM_RANK(shmcomm, rank, ierr)
  if (rank == 0) then
     is_rank0 = 1
  else
     is_rank0 = 0
  end if
  call MPI_ALLREDUCE(is_rank0, count, 1, MPI_INTEGER, MPI_SUM, &
                     MPI_COMM_WORLD, ierr)
  call MPI_COMM_FREE(shmcomm, ierr)
end subroutine getNodeCount
The function first splits the world communicator into groups of ranks capable of creating shared memory regions, i.e. one group per physical machine (give the definition above). It then counts the number of such groups by summing the number of rank-0 entities. Due to the use of collective operations, the function must be called by all ranks in the world group.
Disclaimer: untested code - use at your own risk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With