Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
block_indices.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2020 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_block_indices_h
17 #define dealii_block_indices_h
18 
19 
20 #include <deal.II/base/config.h>
21 
23 #include <deal.II/base/logstream.h>
25 #include <deal.II/base/utilities.h>
26 
27 #include <cstddef>
28 #include <vector>
29 
31 
32 
60 class BlockIndices : public Subscriptor
61 {
62 public:
67 
71  BlockIndices();
72 
78  BlockIndices(const std::vector<size_type> &block_sizes);
79 
84  BlockIndices(BlockIndices &&b) noexcept;
85 
89  BlockIndices(const BlockIndices &) = default;
90 
94  explicit BlockIndices(const unsigned int n_blocks,
95  const size_type block_size = 0);
96 
101  void
102  reinit(const unsigned int n_blocks, const size_type n_elements_per_block);
103 
110  void
111  reinit(const std::vector<size_type> &block_sizes);
112 
116  void
117  push_back(const size_type size);
118 
123 
127  unsigned int
128  size() const;
129 
134  size_type
135  total_size() const;
136 
140  size_type
141  block_size(const unsigned int i) const;
142 
148  std::string
149  to_string() const;
150 
152 
162 
168  std::pair<unsigned int, size_type>
169  global_to_local(const size_type i) const;
170 
174  size_type
175  local_to_global(const unsigned int block, const size_type index) const;
176 
180  size_type
181  block_start(const unsigned int i) const;
183 
187  BlockIndices &
188  operator=(const BlockIndices &b);
189 
194  BlockIndices &
195  operator=(BlockIndices &&) noexcept;
196 
201  bool
202  operator==(const BlockIndices &b) const;
203 
207  void
208  swap(BlockIndices &b);
209 
214  std::size_t
215  memory_consumption() const;
216 
217 private:
222  unsigned int n_blocks;
223 
228  std::vector<size_type> start_indices;
229 };
230 
231 
240 inline LogStream &
241 operator<<(LogStream &s, const BlockIndices &bi)
242 {
243  const unsigned int n = bi.size();
244  s << n << ":[";
245  // Write first size without leading space
246  if (n > 0)
247  s << bi.block_size(0);
248  // Write all other sizes
249  for (unsigned int i = 1; i < n; ++i)
250  s << ' ' << bi.block_size(i);
251  s << "]->" << bi.total_size();
252  return s;
253 }
254 
255 
256 
257 /* ---------------------- template and inline functions ------------------- */
258 
259 inline void
260 BlockIndices::reinit(const unsigned int nb, const size_type block_size)
261 {
262  n_blocks = nb;
263  start_indices.resize(n_blocks + 1);
264  for (size_type i = 0; i <= n_blocks; ++i)
265  start_indices[i] = i * block_size;
266 }
267 
268 
269 
270 inline void
271 BlockIndices::reinit(const std::vector<size_type> &block_sizes)
272 {
273  if (start_indices.size() != block_sizes.size() + 1)
274  {
275  n_blocks = static_cast<unsigned int>(block_sizes.size());
276  start_indices.resize(n_blocks + 1);
277  }
278  start_indices[0] = 0;
279  for (size_type i = 1; i <= n_blocks; ++i)
280  start_indices[i] = start_indices[i - 1] + block_sizes[i - 1];
281 }
282 
283 
285  : n_blocks(0)
286  , start_indices(1, 0)
287 {}
288 
289 
290 
291 inline BlockIndices::BlockIndices(const unsigned int n_blocks,
292  const size_type block_size)
293  : n_blocks(n_blocks)
294  , start_indices(n_blocks + 1)
295 {
296  for (size_type i = 0; i <= n_blocks; ++i)
297  start_indices[i] = i * block_size;
298 }
299 
300 
301 
302 inline BlockIndices::BlockIndices(const std::vector<size_type> &block_sizes)
303  : n_blocks(static_cast<unsigned int>(block_sizes.size()))
304  , start_indices(block_sizes.size() + 1)
305 {
306  reinit(block_sizes);
307 }
308 
309 
310 
312  : n_blocks(b.n_blocks)
313  , start_indices(std::move(b.start_indices))
314 {
315  b.n_blocks = 0;
316  b.start_indices = std::vector<size_type>(1, 0);
317 }
318 
319 
320 
321 inline void
323 {
324  start_indices.push_back(start_indices[n_blocks] + sz);
325  ++n_blocks;
327 }
328 
329 
330 inline std::pair<unsigned int, BlockIndices::size_type>
332 {
334  Assert(n_blocks > 0, ExcLowerRangeType<size_type>(i, size_type(1)));
335 
336  // start_indices[0] == 0 so we might as well start from the next one
337  const auto it =
338  --std::upper_bound(++start_indices.begin(), start_indices.end(), i);
339 
340  return {std::distance(start_indices.begin(), it), i - *it};
341 }
342 
343 
345 BlockIndices::local_to_global(const unsigned int block,
346  const size_type index) const
347 {
348  AssertIndexRange(block, n_blocks);
349  AssertIndexRange(index, start_indices[block + 1] - start_indices[block]);
350 
351  return start_indices[block] + index;
352 }
353 
354 
355 inline unsigned int
357 {
358  return n_blocks;
359 }
360 
361 
362 
365 {
366  if (n_blocks == 0)
367  return 0;
368  return start_indices[n_blocks];
369 }
370 
371 
372 
374 BlockIndices::block_size(const unsigned int block) const
375 {
376  AssertIndexRange(block, n_blocks);
377  return start_indices[block + 1] - start_indices[block];
378 }
379 
380 
381 
382 inline std::string
384 {
385  std::string result = "[" + Utilities::int_to_string(n_blocks) + "->";
386  for (unsigned int i = 0; i < n_blocks; ++i)
387  {
388  if (i > 0)
389  result += ',';
390  result += std::to_string(block_size(i));
391  }
392  result += "|" + std::to_string(total_size()) + ']';
393  return result;
394 }
395 
396 
397 
399 BlockIndices::block_start(const unsigned int block) const
400 {
401  AssertIndexRange(block, n_blocks);
402  return start_indices[block];
403 }
404 
405 
406 
407 inline BlockIndices &
409 {
410  start_indices = b.start_indices;
411  n_blocks = b.n_blocks;
412  return *this;
413 }
414 
415 
416 
417 inline BlockIndices &
419 {
420  start_indices = std::move(b.start_indices);
421  n_blocks = b.n_blocks;
422 
423  b.start_indices = std::vector<size_type>(1, 0);
424  b.n_blocks = 0;
425 
426  return *this;
427 }
428 
429 
430 
431 inline bool
433 {
434  if (n_blocks != b.n_blocks)
435  return false;
436 
437  for (size_type i = 0; i <= n_blocks; ++i)
438  if (start_indices[i] != b.start_indices[i])
439  return false;
440 
441  return true;
442 }
443 
444 
445 
446 inline void
448 {
449  std::swap(n_blocks, b.n_blocks);
450  std::swap(start_indices, b.start_indices);
451 }
452 
453 
454 
455 inline std::size_t
457 {
458  return (sizeof(*this) + start_indices.size() * sizeof(start_indices[0]));
459 }
460 
461 
462 
463 /* ----------------- global functions ---------------------------- */
464 
465 
474 inline void
476 {
477  u.swap(v);
478 }
479 
480 
481 
483 
484 #endif
BlockIndices::push_back
void push_back(const size_type size)
Definition: block_indices.h:322
CUDAWrappers::block_size
constexpr int block_size
Definition: cuda_size.h:29
BlockIndices::size
unsigned int size() const
Definition: block_indices.h:356
Utilities::int_to_string
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:474
utilities.h
BlockIndices::block_start
size_type block_start(const unsigned int i) const
Definition: block_indices.h:399
AssertIndexRange
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
Patterns::Tools::to_string
std::string to_string(const T &t)
Definition: patterns.h:2360
BlockIndices::block_size
size_type block_size(const unsigned int i) const
Definition: block_indices.h:374
BlockIndices::total_size
size_type total_size() const
Definition: block_indices.h:364
BlockIndices::start_indices
std::vector< size_type > start_indices
Definition: block_indices.h:228
Subscriptor
Definition: subscriptor.h:62
MatrixFreeOperators::BlockHelper::n_blocks
std::enable_if< IsBlockVector< VectorType >::value, unsigned int >::type n_blocks(const VectorType &vector)
Definition: operators.h:49
BlockIndices::local_to_global
size_type local_to_global(const unsigned int block, const size_type index) const
Definition: block_indices.h:345
types::global_dof_index
unsigned int global_dof_index
Definition: types.h:76
subscriptor.h
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
Physics::Elasticity::Kinematics::b
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
BlockIndices::swap
void swap(BlockIndices &u, BlockIndices &v)
Definition: block_indices.h:475
AssertDimension
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1579
BlockIndices::operator=
BlockIndices & operator=(const BlockIndices &b)
Definition: block_indices.h:408
exceptions.h
BlockIndices::BlockIndices
BlockIndices()
Definition: block_indices.h:284
BlockIndices::size_type
types::global_dof_index size_type
Definition: block_indices.h:66
unsigned int
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
BlockIndices::reinit
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
Definition: block_indices.h:260
LogStream
Definition: logstream.h:83
config.h
BlockIndices::operator==
bool operator==(const BlockIndices &b) const
Definition: block_indices.h:432
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
swap
void swap(BlockIndices &u, BlockIndices &v)
Definition: block_indices.h:475
logstream.h
BlockIndices::n_blocks
unsigned int n_blocks
Definition: block_indices.h:222
BlockIndices::global_to_local
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
Definition: block_indices.h:331
BlockIndices::to_string
std::string to_string() const
Definition: block_indices.h:383
BlockIndices::swap
void swap(BlockIndices &b)
Definition: block_indices.h:447
BlockIndices
Definition: block_indices.h:60
BlockIndices::memory_consumption
std::size_t memory_consumption() const
Definition: block_indices.h:456
int