Skip to content

Print / PrintToFile ignore constructor MPI_Comm when deciding emitting rank #5064

@WeiqunZhang

Description

@WeiqunZhang
  • Type: Correctness / Rank-space mismatch
  • Severity: Medium
  • Component: Print utilities
  • Location:
    • Src/Base/AMReX_Print.H:64
    • Src/Base/AMReX_Print.H:70
    • Src/Base/AMReX_Print.H:145
    • Src/Base/AMReX_Print.H:151
    • Src/Base/AMReX_Print.H:184

Problem

Both classes accept a user-provided MPI_Comm and store it in comm, but rank checks use ParallelContext::MyProcSub() unconditionally:

if (rank == AllProcs || rank == ParallelContext::MyProcSub())

This ignores the supplied communicator and compares rank in the wrong rank space.

Impact

  • Printing can happen on the wrong rank (or not happen) when using non-default communicators.
  • Behavior contradicts the API contract documented in constructors taking (rank, comm).

Suggested patch

Use communicator-relative rank for filtering, and only use sub-context file stream plumbing when communicator matches sub-communicator.

--- a/Src/Base/AMReX_Print.H
+++ b/Src/Base/AMReX_Print.H
@@
-            if (rank == AllProcs || rank == ParallelContext::MyProcSub()) {
+            int const my_rank = ParallelDescriptor::MyProc(comm);
+            if (rank == AllProcs || rank == my_rank) {
@@
-            if (rank == AllProcs || rank == ParallelContext::MyProcSub()) {
+            int const my_rank = ParallelDescriptor::MyProc(comm);
+            if (rank == AllProcs || rank == my_rank) {
@@
-            int my_proc = ParallelContext::MyProcSub();
+            int my_proc = ParallelDescriptor::MyProc(comm);

Prepared by Codex

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions