VTK  9.7.1
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 
23 VTK_ABI_NAMESPACE_BEGIN
24 namespace
25 {
26 template <typename T>
27 bool Synchronize(vtkMultiProcessController* controller, T& data, T& result)
28 {
29  if (controller == nullptr || controller->GetNumberOfProcesses() <= 1)
30  {
31  return true;
32  }
33 
34  vtkMultiProcessStream stream;
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 
50 template <typename T>
51 bool 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  {
59  vtkMultiProcessStream stream;
60  stream << data;
61  return controller->Broadcast(stream, root) != 0;
62  }
63  else
64  {
65  data = T();
66  vtkMultiProcessStream stream;
67  if (controller->Broadcast(stream, root))
68  {
69  stream >> data;
70  return true;
71  }
72  return false;
73  }
74 }
75 
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 {}
113 VTK_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
void Modified() override
Removes out-of-date PER_COMPONENT() and PER_FINITE_COMPONENT() values.
virtual void SetNumberOfTuples(vtkIdType numTuples)
Set the number of tuples (a component group) in the array.
stream used to pass data across processes using vtkMultiProcessController.
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 vtkIdType
Definition: vtkType.h:363
virtual void SetName(const char *)
Set/get array's name.
virtual int CopyInformation(vtkInformation *infoFrom, vtkTypeBool deep=1)
Copy information instance.
int AllGather(const int *sendBuffer, int *recvBuffer, vtkIdType length)
Same as gather except that the result ends up on all processes.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
virtual void SetNumberOfComponents(int)
Set/Get the dimension (n) of the components.
void TakeReference(T *t)
Transfer ownership of one reference to the given VTK object to this smart pointer.
int GetNumberOfProcesses()
Set the number of processes you will be using.
int GetLocalProcessId()
Tells you which process [0, NumProcess) you are in.
virtual void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source)=0
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
Multiprocessing communication superclass.