VTK  9.5.20250830
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 ? formatArg : "";
41 if (vtk::is_printf_format(format))
42 {
43 format = vtk::printf_to_std_format(format);
44 }
45 assert(comm != nullptr);
46 vtkMPIUtilities::Print(comm, format.c_str(), std::forward<T>(args)...);
47}
49
51
57template <typename... T>
58void SynchronizedPrint(vtkMPIController* comm, const char* format, T&&... args)
59{
60 assert(comm != nullptr);
61 int rank = comm->GetLocalProcessId();
62 int numRanks = comm->GetNumberOfProcesses();
63
65 int* nullmsg = nullptr;
66
67 if (rank == 0)
68 {
69 // STEP 0: print message
70 vtk::print("[{:d}]: ", rank);
71 std::fflush(stdout);
72
73 vtk::print(format, std::forward<T>(args)...);
74 std::fflush(stdout);
75
76 // STEP 1: signal next process (if any) to print
77 if (numRanks > 1)
78 {
79 comm->NoBlockSend(nullmsg, 0, rank + 1, 0, rqst);
80 } // END if
81 } // END first rank
82 else if (rank == numRanks - 1)
83 {
84 // STEP 0: Block until previous process completes
85 comm->Receive(nullmsg, 0, rank - 1, 0);
86
87 // STEP 1: print message
88 vtk::print("[{:d}]: ", rank);
89
90 vtk::print(format, std::forward<T>(args)...);
91 std::fflush(stdout);
92 } // END last rank
93 else
94 {
95 // STEP 0: Block until previous process completes
96 comm->Receive(nullmsg, 0, rank - 1, 0);
97
98 // STEP 1: print message
99 vtk::print("[{:d}]: ", rank);
100
101 vtk::print(format, std::forward<T>(args)...);
102 std::fflush(stdout);
103
104 // STEP 2: signal next process to print
105 comm->NoBlockSend(nullmsg, 0, rank + 1, 0, rqst);
106 }
107
108 comm->Barrier();
109}
110template <typename... T>
111VTK_DEPRECATED_IN_9_6_0("Use vtkMPIUtilities::SynchronizedPrint instead")
112void SynchronizedPrintf(vtkMPIController* comm, const char* formatArg, T&&... args)
113{
114 std::string format = formatArg ? formatArg : "";
115 if (vtk::is_printf_format(format))
116 {
117 format = vtk::printf_to_std_format(format);
118 }
119 assert(comm != nullptr);
120 vtkMPIUtilities::SynchronizedPrint(comm, format.c_str(), std::forward<T>(args)...);
121}
123VTK_ABI_NAMESPACE_END
124} // END namespace vtkMPIUtilities
125
126#endif // vtkMPIUtilities_h
127// 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 printf_to_std_format(const std::string &printf_format)
Convert a printf style format to a std::format style format.
VTKCOMMONCORE_EXPORT bool is_printf_format(const std::string &format)
Check if the given string is a printf style format.
#define VTK_DEPRECATED_IN_9_6_0(reason)
Optimized C++ utilities for formatting values to strings and files.