KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
distributed_numbering.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 #if !defined(KRATOS_DISTRIBUTED_NUMBERING_H_INCLUDED )
13 #define KRATOS_DISTRIBUTED_NUMBERING_H_INCLUDED
14 
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 
20 
21 // External includes
22 
23 
24 // Project includes
25 #include "includes/define.h"
28 
29 namespace Kratos
30 {
33 
36 
40 
44 
48 
52 
54 template<class TIndexType=std::size_t>
56 {
57 public:
60  typedef TIndexType IndexType;
61  typedef int MpiIndexType;
62 
65 
69 
70  //constructor by local size - computes cpu bounds automatically
72  const DataCommunicator& rComm,
73  const IndexType LocalSize
74  )
76  {}
77 
79  const DataCommunicator* pComm,
80  const IndexType LocalSize)
81  :
82  mpComm(pComm)
83  {
84  mCpuBounds.resize(pComm->Size()+1);
85 
86  std::vector<IndexType> send_vect{LocalSize};
87  std::vector<IndexType> local_sizes = pComm->AllGather(send_vect);
88  mCpuBounds[0] = 0;
89  for(unsigned int i=1; i<mCpuBounds.size(); ++i)
90  mCpuBounds[i] = mCpuBounds[i-1] + local_sizes[i-1];
91  }
92 
93  //constructor by global sizes and nranks - Requires no communication
95  const DataCommunicator& rComm,
96  const IndexType TotalSize,
97  const MpiIndexType Nranks //note that we could obtain this by the rComm, but we need to distinguish this constructor from the previous
98  )
99  : DistributedNumbering(&rComm, TotalSize,Nranks)
100  {}
101 
103  const DataCommunicator* pComm,
104  const IndexType TotalSize,
105  const MpiIndexType Nranks //note that we could obtain this by the rComm, but we need to distinguish this constructor from the previous
106  )
107  :
108  mpComm(pComm)
109  {
110  KRATOS_ERROR_IF(pComm->Size() != Nranks) << "We expect Nranks to be the same as pComm->size()" << std::endl;
111 
112  mCpuBounds.resize(pComm->Size()+1);
113 
114  const IndexType local_size = TotalSize / Nranks;
115  mCpuBounds[0] = 0;
116  mCpuBounds[Nranks] = TotalSize;
117  for (int i=1; i<Nranks; i++) {
118  mCpuBounds[i] = mCpuBounds[i-1] + local_size;
119  }
120  }
121 
123  const DataCommunicator* pComm,
124  const std::vector<IndexType>& CpuBounds)
125  :
126  mpComm(pComm), mCpuBounds(CpuBounds)
127  {
128  }
129 
131  const DataCommunicator& rComm,
132  const std::vector<IndexType>& CpuBounds)
133  :
134  mpComm(&rComm), mCpuBounds(CpuBounds)
135  {
136  }
137 
140  :
141  mpComm(rOther.mpComm), mCpuBounds(rOther.GetCpuBounds())
142  {
143  }
144 
147 
151  const DataCommunicator& GetComm() const
152  {
153  return *mpComm;
154  }
155 
156  const DataCommunicator* pGetComm() const
157  {
158  return mpComm;
159  }
160 
161  inline IndexType LocalSize() const
162  {
163  const auto k = GetComm().Rank();
164  return mCpuBounds[k+1]-mCpuBounds[k];
165  }
166 
167  inline IndexType Size() const
168  {
169  return mCpuBounds[mCpuBounds.size()-1];
170  }
171 
172  inline bool IsLocal(const IndexType I) const
173  {
174  const auto k = GetComm().Rank();
175  return (I>=mCpuBounds[k] && I<mCpuBounds[k+1]);
176  }
177 
178  inline IndexType LocalId(const IndexType rGlobalId) const
179  {
180  const auto k = GetComm().Rank();
181  return rGlobalId-mCpuBounds[k];
182  }
183 
184  inline IndexType GlobalId(const IndexType rLocalId) const
185  {
186  const auto k = GetComm().Rank();
187  return rLocalId+mCpuBounds[k];
188  }
189 
190  inline IndexType RemoteLocalId(const IndexType rGlobalId, const IndexType rOwnerRank) const
191  {
192  return rGlobalId-mCpuBounds[rOwnerRank];
193  }
194 
195  inline IndexType RemoteGlobalId(const IndexType rRemoteLocalId, const IndexType rOwnerRank) const
196  {
197  return rRemoteLocalId+mCpuBounds[rOwnerRank];
198  }
199 
200  inline IndexType OwnerRank(const IndexType RowIndex) const
201  {
202  //position of element just larger than RowIndex in mCpuBounds
203  auto it = std::upper_bound(mCpuBounds.begin(), mCpuBounds.end(), RowIndex);
204 
205  KRATOS_DEBUG_ERROR_IF(it == mCpuBounds.end()) <<
206  "row RowIndex " << RowIndex <<
207  " is not owned by any processor " << std::endl;
208 
209  IndexType owner_rank = (it-mCpuBounds.begin()-1);
210 
211  return owner_rank;
212 
213  }
214 
215  const std::vector<IndexType>& GetCpuBounds() const
216  {
217  return mCpuBounds;
218  }
219 
221  std::string Info() const
222  {
223  std::stringstream buffer;
224  buffer << "DistributedNumbering: CpuBounds = " << mCpuBounds << std::endl;
225  return buffer.str();
226  }
227 
229  void PrintInfo(std::ostream& rOStream) const
230  {
231  rOStream << "DistributedNumbering";
232  }
233 
235  void PrintData(std::ostream& rOStream) const {
236  rOStream << "DistributedNumbering: CpuBounds = " << mCpuBounds << std::endl;
237  }
238 
239 
240 protected:
241 
242 private:
243  const DataCommunicator* mpComm;
244  std::vector<IndexType> mCpuBounds;
245 
247  DistributedNumbering& operator=(DistributedNumbering const& rOther)
248  {
249  mpComm = rOther.mpComm;
250  this->GetCpuBounds() = rOther.GetCpuBounds();
251  }
252 
254 
255 }; // Class DistributedNumbering
256 
258 
261 
262 
266 
267 
269 template<class TDataType, class TIndexType>
270 inline std::istream& operator >> (std::istream& rIStream,
272 {
273  return rIStream;
274 }
275 
277 template<class TDataType, class TIndexType>
278 inline std::ostream& operator << (std::ostream& rOStream,
280 {
281  rThis.PrintInfo(rOStream);
282  rOStream << std::endl;
283  rThis.PrintData(rOStream);
284 
285  return rOStream;
286 }
288 
290 } // namespace Kratos.
291 
292 #endif // KRATOS_DISTRIBUTED_NUMBERING_H_INCLUDED defined
293 
294 
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
virtual int Size() const
Get the parallel size of this DataCommunicator.
Definition: data_communicator.h:597
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
This function provides essential capabilities for mapping between local and global ids in a distribut...
Definition: distributed_numbering.h:56
DistributedNumbering(const DataCommunicator *pComm, const std::vector< IndexType > &CpuBounds)
Definition: distributed_numbering.h:122
KRATOS_CLASS_POINTER_DEFINITION(DistributedNumbering)
Pointer definition of DistributedNumbering.
TIndexType IndexType
Definition: distributed_numbering.h:60
DistributedNumbering(const DataCommunicator *pComm, const IndexType TotalSize, const MpiIndexType Nranks)
Definition: distributed_numbering.h:102
DistributedNumbering(const DataCommunicator *pComm, const IndexType LocalSize)
Definition: distributed_numbering.h:78
IndexType OwnerRank(const IndexType RowIndex) const
Definition: distributed_numbering.h:200
IndexType LocalSize() const
Definition: distributed_numbering.h:161
DistributedNumbering(const DataCommunicator &rComm, const IndexType TotalSize, const MpiIndexType Nranks)
Definition: distributed_numbering.h:94
IndexType RemoteGlobalId(const IndexType rRemoteLocalId, const IndexType rOwnerRank) const
Definition: distributed_numbering.h:195
IndexType RemoteLocalId(const IndexType rGlobalId, const IndexType rOwnerRank) const
Definition: distributed_numbering.h:190
int MpiIndexType
Definition: distributed_numbering.h:61
DistributedNumbering(const DataCommunicator &rComm, const IndexType LocalSize)
Definition: distributed_numbering.h:71
IndexType LocalId(const IndexType rGlobalId) const
Definition: distributed_numbering.h:178
DistributedNumbering(const DistributedNumbering &rOther)
Copy constructor.
Definition: distributed_numbering.h:139
DistributedNumbering(const DataCommunicator &rComm, const std::vector< IndexType > &CpuBounds)
Definition: distributed_numbering.h:130
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: distributed_numbering.h:235
std::string Info() const
Turn back information as a string.
Definition: distributed_numbering.h:221
const std::vector< IndexType > & GetCpuBounds() const
Definition: distributed_numbering.h:215
~DistributedNumbering()
Destructor.
Definition: distributed_numbering.h:146
bool IsLocal(const IndexType I) const
Definition: distributed_numbering.h:172
IndexType GlobalId(const IndexType rLocalId) const
Definition: distributed_numbering.h:184
IndexType Size() const
Definition: distributed_numbering.h:167
const DataCommunicator & GetComm() const
Definition: distributed_numbering.h:151
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: distributed_numbering.h:229
const DataCommunicator * pGetComm() const
Definition: distributed_numbering.h:156
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_DEBUG_ERROR_IF(conditional)
Definition: exception.h:171
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
int local_size
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:17
int k
Definition: quadrature.py:595
integer i
Definition: TensorModule.f:17