VTK  9.5.20251214
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_5_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
14{
15VTK_ABI_NAMESPACE_BEGIN
16
18
25template <typename... T>
26void Print(vtkMPIController* comm, const char* format, T&&... args)
27{
28 assert(comm != nullptr);
29 if (comm->GetLocalProcessId() == 0)
30 {
31 vtk::print(format, std::forward<T>(args)...);
32 std::fflush(stdout);
33 }
34 comm->Barrier();
35}
36template <typename... T>
37VTK_DEPRECATED_IN_9_6_0("Use vtkMPIUtilities::Print instead")
38void 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}
44
45
47
53template <typename... T>
54void 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(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(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(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}
106template <typename... T>
107VTK_DEPRECATED_IN_9_6_0("Use vtkMPIUtilities::SynchronizedPrint instead")
108void 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}
114
115VTK_ABI_NAMESPACE_END
116} // END namespace vtkMPIUtilities
117
118#endif // vtkMPIUtilities_h
119// 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 Printf(vtkMPIController *comm, const char *formatArg, T &&... args)
Rank 0 prints the user-supplied formatted message to stdout.
void Print(vtkMPIController *comm, const char *format, T &&... args)
Rank 0 prints the user-supplied formatted message to stdout.
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.
VTKCOMMONCORE_EXPORT std::string to_std_format(const std::string &format)
Convert printf and std::format style format strings.
#define VTK_DEPRECATED_IN_9_6_0(reason)
Optimized C++ utilities for formatting values to strings and files.