VTK  9.4.20250415
vtkIOSSReaderCommunication.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
4#include "vtkAbstractArray.h"
7
8#include <cassert>
9
10// Silence warnings that these functions are unused.
11// Because the functions are in an anonymous namespace, unused-function
12// function warnings are generated. But not every file that includes
13// this header needs to use every function.
14#if defined(__GNUC__) && !defined(__clang__)
15#pragma GCC diagnostic push
16#pragma GCC diagnostic ignored "-Wunused-function"
17#elif defined(__clang__)
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wunused-function"
20#pragma clang diagnostic ignored "-Wunused-template"
21#endif
22
23VTK_ABI_NAMESPACE_BEGIN
24namespace
25{
26template <typename T>
27bool Synchronize(vtkMultiProcessController* controller, T& data, T& result)
28{
29 if (controller == nullptr || controller->GetNumberOfProcesses() <= 1)
30 {
31 return true;
32 }
33
35 stream << data;
36
37 std::vector<vtkMultiProcessStream> all_streams;
38 if (controller->AllGather(stream, all_streams))
39 {
40 for (auto& s : all_streams)
41 {
42 s >> result;
43 }
44 return true;
45 }
46
47 return false;
48}
49
50template <typename T>
51bool Broadcast(vtkMultiProcessController* controller, T& data, int root)
52{
53 if (controller == nullptr || controller->GetNumberOfProcesses() <= 1)
54 {
55 return true;
56 }
57 if (controller->GetLocalProcessId() == root)
58 {
60 stream << data;
61 return controller->Broadcast(stream, root) != 0;
62 }
63 else
64 {
65 data = T();
67 if (controller->Broadcast(stream, root))
68 {
69 stream >> data;
70 return true;
71 }
72 return false;
73 }
74}
75
77 const std::vector<vtkSmartPointer<vtkAbstractArray>>& arrays)
78{
79 if (arrays.empty())
80 {
81 return nullptr;
82 }
83 else if (arrays.size() == 1)
84 {
85 return arrays[0];
86 }
87
88 vtkIdType numTuples = 0;
89 for (auto& array : arrays)
90 {
91 numTuples += array->GetNumberOfTuples();
92 }
93
95 result.TakeReference(arrays[0]->NewInstance());
96 result->CopyInformation(arrays[0]->GetInformation());
97 result->SetName(arrays[0]->GetName());
98 result->SetNumberOfComponents(arrays[0]->GetNumberOfComponents());
99 result->SetNumberOfTuples(numTuples);
100 vtkIdType offset = 0;
101 for (auto& array : arrays)
102 {
103 const auto count = array->GetNumberOfTuples();
104 result->InsertTuples(offset, count, 0, array);
105 offset += count;
106 }
107 result->Modified();
108 assert(offset == numTuples);
109 return result;
110}
111
112} // end of namespace {}
113VTK_ABI_NAMESPACE_END
114
115#if defined(__GNUC__) && !defined(__clang__)
116#pragma GCC diagnostic pop
117#elif defined(__clang__)
118#pragma clang diagnostic pop
119#endif
Multiprocessing communication superclass.
int GetNumberOfProcesses()
Set the number of processes you will be using.
int Broadcast(int *data, vtkIdType length, int srcProcessId)
Broadcast sends the array in the process with id srcProcessId to all of the other processes.
int GetLocalProcessId()
Tells you which process [0, NumProcess) you are in.
int AllGather(const int *sendBuffer, int *recvBuffer, vtkIdType length)
Same as gather except that the result ends up on all processes.
stream used to pass data across processes using vtkMultiProcessController.
Hold a reference to a vtkObjectBase instance.
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
@ offset
Definition vtkX3D.h:438
@ data
Definition vtkX3D.h:315
int vtkIdType
Definition vtkType.h:332