Write to std::cout using MPI from subroutine

31 views
Skip to first unread message

Alex Quinlan

unread,
Apr 24, 2024, 3:17:42 PM4/24/24
to deal.II User Group
Hi everyone,

I am looking to replicate the functionality of a ConditionalOstream in one of my subroutines.  In my main file, I use the stream "pcout" based on Step-17.  However, pcout is not passed in to my subroutine.

I have tried to guard my code using something like:
if (dealii::Utilities::MPI::this_mpi_process == 0)
std::cout << "Text" << std::endl;

This is the approach that is used in the ConditionalOstream pcout.  However, the text never actually prints, and instead I'm given a warning when I compile:

warning: the address of ‘unsigned int dealii::Utilities::MPI::this_mpi_process(MPI_Comm)’ will never be NULL [-Waddress]

I'm unclear why the mpi_process will never be NULL; it works fine in the rest of my code, just not in an external file.

Can you think of an effective way to print for only one process that won't cause this warning?

Best regards,
Alex

Wolfgang Bangerth

unread,
Apr 24, 2024, 3:32:36 PM4/24/24
to dea...@googlegroups.com

> if (dealii::Utilities::MPI::this_mpi_process == 0)
> [...]
> warning: the address of ‘unsigned int
> dealii::Utilities::MPI::this_mpi_process(MPI_Comm)’ will never be NULL

You need to say
Utilities::MPI::this_mpi_process()
instead of
Utilities::MPI::this_mpi_process
The former calls the function. The latter takes the address of the function.

Best
W.

Alex Quinlan

unread,
Apr 24, 2024, 3:42:56 PM4/24/24
to deal.II User Group
Thanks!

I changed it to
dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0
and now it's working.  I appreciate the help

Thanks,
Alex
Reply all
Reply to author
Forward
0 new messages