VTK  9.7.20260711
vtkMPIUtilities.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#ifndef vtkMPIUtilities_h
4#define vtkMPIUtilities_h
5
6#include "vtkMPIController.h" // For vtkMPIController
7#include "vtkStringFormatter.h" // For vtk::print
8
9#include <cassert> // For assert
10#include <string> // For std::string
11
13{
14VTK_ABI_NAMESPACE_BEGIN
15
17
24template <typename... T>
25void Print(vtkMPIController* comm, const char* format, T&&... args)
26{
27 assert(comm != nullptr);
28 if (comm->GetLocalProcessId() == 0)
29 {
30 vtk::print(vtk::runtime(format), std::forward<T>(args)...);
31 std::fflush(stdout);
32 }
33 comm->Barrier();
34}
35
36
38
44template <typename... T>
45void SynchronizedPrint(vtkMPIController* comm, const char* format, T&&... args)
46{
47 assert(comm != nullptr);
48 int rank = comm->GetLocalProcessId();
49 int numRanks = comm->GetNumberOfProcesses();
50
52 int* nullmsg = nullptr;
53
54 if (rank == 0)
55 {
56 // STEP 0: print message
57 vtk::print("[{:d}]: ", rank);
58 std::fflush(stdout);
59
60 vtk::print(vtk::runtime(format), std::forward<T>(args)...);
61 std::fflush(stdout);
62
63 // STEP 1: signal next process (if any) to print
64 if (numRanks > 1)
65 {
66 comm->NoBlockSend(nullmsg, 0, rank + 1, 0, rqst);
67 } // END if
68 } // END first rank
69 else if (rank == numRanks - 1)
70 {
71 // STEP 0: Block until previous process completes
72 comm->Receive(nullmsg, 0, rank - 1, 0);
73
74 // STEP 1: print message
75 vtk::print("[{:d}]: ", rank);
76
77 vtk::print(vtk::runtime(format), std::forward<T>(args)...);
78 std::fflush(stdout);
79 } // END last rank
80 else
81 {
82 // STEP 0: Block until previous process completes
83 comm->Receive(nullmsg, 0, rank - 1, 0);
84
85 // STEP 1: print message
86 vtk::print("[{:d}]: ", rank);
87
88 vtk::print(vtk::runtime(format), std::forward<T>(args)...);
89 std::fflush(stdout);
90
91 // STEP 2: signal next process to print
92 comm->NoBlockSend(nullmsg, 0, rank + 1, 0, rqst);
93 }
94
95 comm->Barrier();
96}
97
99VTK_ABI_NAMESPACE_END
100} // END namespace vtkMPIUtilities
101
102#endif // vtkMPIUtilities_h
103// VTK-HeaderTest-Exclude: vtkMPIUtilities.h
Process communication using MPI.
int NoBlockSend(const int *data, int length, int remoteProcessId, int tag, vtkMPICommunicator::Request &req)
This method sends data to another process (non-blocking).
int GetNumberOfProcesses()
Set the number of processes you will be using.
void Barrier()
This method can be used to synchronize processes.
int GetLocalProcessId()
Tells you which process [0, NumProcess) you are in.
int Receive(int *data, vtkIdType maxlength, int remoteProcessId, int tag)
This method receives data from a corresponding send.
void SynchronizedPrint(vtkMPIController *comm, const char *format, T &&... args)
Each rank, r_0 to r_{N-1}, prints the formatted message to stdout in rank order.
void Print(vtkMPIController *comm, const char *format, T &&... args)
Rank 0 prints the user-supplied formatted message to stdout.
Optimized C++ utilities for formatting values to strings and files.