VTK  9.7.1
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 "vtkDeprecation.h" // For VTK_DEPRECATED_IN_9_6_0
7 #include "vtkMPIController.h" // For vtkMPIController
8 #include "vtkStringFormatter.h" // For vtk::print
9 
10 #include <cassert> // For assert
11 #include <string> // For std::string
12 
13 namespace vtkMPIUtilities
14 {
15 VTK_ABI_NAMESPACE_BEGIN
16 
18 
25 template <typename... T>
26 void Print(vtkMPIController* comm, const char* format, T&&... args)
27 {
28  assert(comm != nullptr);
29  if (comm->GetLocalProcessId() == 0)
30  {
31  vtk::print(vtk::runtime(format), std::forward<T>(args)...);
32  std::fflush(stdout);
33  }
34  comm->Barrier();
35 }
36 template <typename... T>
37 VTK_DEPRECATED_IN_9_6_0("Use vtkMPIUtilities::Print instead")
38 void Printf(vtkMPIController* comm, const char* formatArg, T&&... args)
39 {
40  std::string format = formatArg ? vtk::to_std_format(formatArg) : "";
41  assert(comm != nullptr);
42  vtkMPIUtilities::Print(comm, format.c_str(), std::forward<T>(args)...);
43 }
45 
47 
53 template <typename... T>
54 void SynchronizedPrint(vtkMPIController* comm, const char* format, T&&... args)
55 {
56  assert(comm != nullptr);
57  int rank = comm->GetLocalProcessId();
58  int numRanks = comm->GetNumberOfProcesses();
59 
61  int* nullmsg = nullptr;
62 
63  if (rank == 0)
64  {
65  // STEP 0: print message
66  vtk::print("[{:d}]: ", rank);
67  std::fflush(stdout);
68 
69  vtk::print(vtk::runtime(format), std::forward<T>(args)...);
70  std::fflush(stdout);
71 
72  // STEP 1: signal next process (if any) to print
73  if (numRanks > 1)
74  {
75  comm->NoBlockSend(nullmsg, 0, rank + 1, 0, rqst);
76  } // END if
77  } // END first rank
78  else if (rank == numRanks - 1)
79  {
80  // STEP 0: Block until previous process completes
81  comm->Receive(nullmsg, 0, rank - 1, 0);
82 
83  // STEP 1: print message
84  vtk::print("[{:d}]: ", rank);
85 
86  vtk::print(vtk::runtime(format), std::forward<T>(args)...);
87  std::fflush(stdout);
88  } // END last rank
89  else
90  {
91  // STEP 0: Block until previous process completes
92  comm->Receive(nullmsg, 0, rank - 1, 0);
93 
94  // STEP 1: print message
95  vtk::print("[{:d}]: ", rank);
96 
97  vtk::print(vtk::runtime(format), std::forward<T>(args)...);
98  std::fflush(stdout);
99 
100  // STEP 2: signal next process to print
101  comm->NoBlockSend(nullmsg, 0, rank + 1, 0, rqst);
102  }
103 
104  comm->Barrier();
105 }
106 template <typename... T>
107 VTK_DEPRECATED_IN_9_6_0("Use vtkMPIUtilities::SynchronizedPrint instead")
108 void SynchronizedPrintf(vtkMPIController* comm, const char* formatArg, T&&... args)
109 {
110  std::string format = formatArg ? vtk::to_std_format(formatArg) : "";
111  assert(comm != nullptr);
112  vtkMPIUtilities::SynchronizedPrint(comm, format.c_str(), std::forward<T>(args)...);
113 }
115 VTK_ABI_NAMESPACE_END
116 } // END namespace vtkMPIUtilities
117 
118 #endif // vtkMPIUtilities_h
119 // VTK-HeaderTest-Exclude: vtkMPIUtilities.h
VTKCOMMONCORE_EXPORT std::string to_std_format(const std::string &format)
Convert printf and std::format style format strings.
void SynchronizedPrintf(vtkMPIController *comm, const char *formatArg, T &&...args)
Each rank, r_0 to r_{N-1}, prints the formatted message to stdout in rank order.
void Printf(vtkMPIController *comm, const char *formatArg, T &&...args)
Rank 0 prints the user-supplied formatted message to stdout.
#define VTK_DEPRECATED_IN_9_6_0(reason)
int NoBlockSend(const int *data, int length, int remoteProcessId, int tag, vtkMPICommunicator::Request &req)
This method sends data to another process (non-blocking).
void Print(vtkMPIController *comm, const char *format, T &&...args)
Rank 0 prints the user-supplied formatted message to stdout.
Process communication using MPI.
void Barrier()
This method can be used to synchronize processes.
int GetNumberOfProcesses()
Set the number of processes you will be using.
int Receive(int *data, vtkIdType maxlength, int remoteProcessId, int tag)
This method receives data from a corresponding send.
int GetLocalProcessId()
Tells you which process [0, NumProcess) you are in.
Optimized C++ utilities for formatting values to strings and files.
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.