Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
dynamic_sparsity_pattern.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2019 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 #include <deal.II/base/memory_consumption.h>
17 #include <deal.II/base/utilities.h>
18 
19 #include <deal.II/lac/dynamic_sparsity_pattern.h>
20 #include <deal.II/lac/sparsity_pattern.h>
21 
22 #include <algorithm>
23 #include <cmath>
24 #include <functional>
25 #include <numeric>
26 #include <set>
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 
31 
32 template <typename ForwardIterator>
33 void
35  ForwardIterator end,
36  const bool indices_are_sorted)
37 {
38  const int n_elements = end - begin;
39  if (n_elements <= 0)
40  return;
41 
42  const size_type stop_size = entries.size() + n_elements;
43 
44  if (indices_are_sorted == true && n_elements > 3)
45  {
46  // in debug mode, check whether the
47  // indices really are sorted.
48 #ifdef DEBUG
49  {
50  ForwardIterator test = begin, test1 = begin;
51  ++test1;
52  for (; test1 != end; ++test, ++test1)
53  Assert(*test1 > *test, ExcInternalError());
54  }
55 #endif
56 
57  if (entries.size() == 0 || entries.back() < *begin)
58  {
59  entries.insert(entries.end(), begin, end);
60  return;
61  }
62 
63  // find a possible insertion point for
64  // the first entry. check whether the
65  // first entry is a duplicate before
66  // actually doing something.
67  ForwardIterator my_it = begin;
68  size_type col = *my_it;
69  std::vector<size_type>::iterator it =
70  Utilities::lower_bound(entries.begin(), entries.end(), col);
71  while (*it == col)
72  {
73  ++my_it;
74  if (my_it == end)
75  break;
76  col = *my_it;
77  // check the very next entry in the
78  // current array
79  ++it;
80  if (it == entries.end())
81  break;
82  if (*it > col)
83  break;
84  if (*it == col)
85  continue;
86  // ok, it wasn't the very next one, do a
87  // binary search to find the insert point
88  it = Utilities::lower_bound(it, entries.end(), col);
89  if (it == entries.end())
90  break;
91  }
92  // all input entries were duplicates.
93  if (my_it == end)
94  return;
95 
96  // resize vector by just inserting the
97  // list
98  const size_type pos1 = it - entries.begin();
99  Assert(pos1 <= entries.size(), ExcInternalError());
100  entries.insert(it, my_it, end);
101  it = entries.begin() + pos1;
102  Assert(entries.size() >= static_cast<size_type>(it - entries.begin()),
103  ExcInternalError());
104 
105  // now merge the two lists.
106  std::vector<size_type>::iterator it2 = it + (end - my_it);
107 
108  // as long as there are indices both in
109  // the end of the entries list and in the
110  // input list
111  while (my_it != end && it2 != entries.end())
112  {
113  if (*my_it < *it2)
114  *it++ = *my_it++;
115  else if (*my_it == *it2)
116  {
117  *it++ = *it2++;
118  ++my_it;
119  }
120  else
121  *it++ = *it2++;
122  }
123  // in case there are indices left in the
124  // input list
125  while (my_it != end)
126  *it++ = *my_it++;
127 
128  // in case there are indices left in the
129  // end of entries
130  while (it2 != entries.end())
131  *it++ = *it2++;
132 
133  // resize and return
134  const size_type new_size = it - entries.begin();
135  Assert(new_size <= stop_size, ExcInternalError());
136  entries.resize(new_size);
137  return;
138  }
139 
140  // unsorted case or case with too few
141  // elements
142  ForwardIterator my_it = begin;
143 
144  // If necessary, increase the size of the
145  // array.
146  if (stop_size > entries.capacity())
147  entries.reserve(stop_size);
148 
149  size_type col = *my_it;
150  std::vector<size_type>::iterator it, it2;
151  // insert the first element as for one
152  // entry only first check the last
153  // element (or if line is still empty)
154  if ((entries.size() == 0) || (entries.back() < col))
155  {
156  entries.push_back(col);
157  it = entries.end() - 1;
158  }
159  else
160  {
161  // do a binary search to find the place
162  // where to insert:
163  it2 = Utilities::lower_bound(entries.begin(), entries.end(), col);
164 
165  // If this entry is a duplicate, continue
166  // immediately Insert at the right place
167  // in the vector. Vector grows
168  // automatically to fit elements. Always
169  // doubles its size.
170  if (*it2 != col)
171  it = entries.insert(it2, col);
172  else
173  it = it2;
174  }
175 
176  ++my_it;
177  // Now try to be smart and insert with
178  // bias in the direction we are
179  // walking. This has the advantage that
180  // for sorted lists, we always search in
181  // the right direction, what should
182  // decrease the work needed in here.
183  for (; my_it != end; ++my_it)
184  {
185  col = *my_it;
186  // need a special insertion command when
187  // we're at the end of the list
188  if (col > entries.back())
189  {
190  entries.push_back(col);
191  it = entries.end() - 1;
192  }
193  // search to the right (preferred search
194  // direction)
195  else if (col > *it)
196  {
197  it2 = Utilities::lower_bound(it++, entries.end(), col);
198  if (*it2 != col)
199  it = entries.insert(it2, col);
200  }
201  // search to the left
202  else if (col < *it)
203  {
204  it2 = Utilities::lower_bound(entries.begin(), it, col);
205  if (*it2 != col)
206  it = entries.insert(it2, col);
207  }
208  // if we're neither larger nor smaller,
209  // then this was a duplicate and we can
210  // just continue.
211  }
212 }
213 
214 
217 {
218  return entries.capacity() * sizeof(size_type) + sizeof(Line);
219 }
220 
221 
223  : have_entries(false)
224  , rows(0)
225  , cols(0)
226  , rowset(0)
227 {}
228 
229 
230 
232  : Subscriptor()
233  , have_entries(false)
234  , rows(0)
235  , cols(0)
236  , rowset(0)
237 {
238  (void)s;
239  Assert(s.rows == 0 && s.cols == 0,
240  ExcMessage(
241  "This constructor can only be called if the provided argument "
242  "is the sparsity pattern for an empty matrix. This constructor can "
243  "not be used to copy-construct a non-empty sparsity pattern."));
244 }
245 
246 
247 
249  const size_type n,
250  const IndexSet &rowset_)
251  : have_entries(false)
252  , rows(0)
253  , cols(0)
254  , rowset(0)
255 {
256  reinit(m, n, rowset_);
257 }
258 
259 
261  : have_entries(false)
262  , rows(0)
263  , cols(0)
264  , rowset(0)
265 {
266  reinit(rowset_.size(), rowset_.size(), rowset_);
267 }
268 
269 
271  : have_entries(false)
272  , rows(0)
273  , cols(0)
274  , rowset(0)
275 {
276  reinit(n, n);
277 }
278 
279 
280 
283 {
284  (void)s;
285  Assert(s.rows == 0 && s.cols == 0,
286  ExcMessage(
287  "This operator can only be called if the provided argument "
288  "is the sparsity pattern for an empty matrix. This operator can "
289  "not be used to copy a non-empty sparsity pattern."));
290 
291  Assert(rows == 0 && cols == 0,
292  ExcMessage("This operator can only be called if the current object is"
293  "empty."));
294 
295  return *this;
296 }
297 
298 
299 
300 void
302  const size_type n,
303  const IndexSet &rowset_)
304 {
305  have_entries = false;
306  rows = m;
307  cols = n;
308  rowset = rowset_;
309 
310  Assert(rowset.size() == 0 || rowset.size() == m,
311  ExcMessage(
312  "The IndexSet argument to this function needs to either "
313  "be empty (indicating the complete set of rows), or have size "
314  "equal to the desired number of rows as specified by the "
315  "first argument to this function. (Of course, the number "
316  "of indices in this IndexSet may be less than the number "
317  "of rows, but the *size* of the IndexSet must be equal.)"));
318 
319  std::vector<Line> new_lines(rowset.size() == 0 ? rows : rowset.n_elements());
320  lines.swap(new_lines);
321 }
322 
323 
324 
325 void
327 {}
328 
329 
330 
331 bool
333 {
334  return ((rows == 0) && (cols == 0));
335 }
336 
337 
338 
341 {
342  if (!have_entries)
343  return 0;
344 
345  size_type m = 0;
346  for (const auto &line : lines)
347  {
348  m = std::max(m, static_cast<size_type>(line.entries.size()));
349  }
350 
351  return m;
352 }
353 
354 
355 
356 bool
358 {
359  Assert(i < rows, ExcIndexRange(i, 0, rows));
360  Assert(j < cols, ExcIndexRange(j, 0, cols));
361  Assert(
362  rowset.size() == 0 || rowset.is_element(i),
363  ExcMessage(
364  "The row IndexSet does not contain the index i. This sparsity pattern "
365  "object cannot know whether the entry (i, j) exists or not."));
366 
367  // Avoid a segmentation fault in below code if the row index happens to
368  // not be present in the IndexSet rowset:
369  if (!(rowset.size() == 0 || rowset.is_element(i)))
370  return false;
371 
372  if (!have_entries)
373  return false;
374 
375  const size_type rowindex =
376  rowset.size() == 0 ? i : rowset.index_within_set(i);
377 
378  return std::binary_search(lines[rowindex].entries.begin(),
379  lines[rowindex].entries.end(),
380  j);
381 }
382 
383 
384 
385 void
387 {
389 
390  // loop over all elements presently
391  // in the sparsity pattern and add
392  // the transpose element. note:
393  //
394  // 1. that the sparsity pattern
395  // changes which we work on, but
396  // not the present row
397  //
398  // 2. that the @p{add} function can
399  // be called on elements that
400  // already exist without any harm
401  for (size_type row = 0; row < lines.size(); ++row)
402  {
403  const size_type rowindex =
404  rowset.size() == 0 ? row : rowset.nth_index_in_set(row);
405 
406  for (std::vector<size_type>::const_iterator j =
407  lines[row].entries.begin();
408  j != lines[row].entries.end();
409  ++j)
410  // add the transpose entry if
411  // this is not the diagonal
412  if (rowindex != *j)
413  add(*j, rowindex);
414  }
415 }
416 
417 
418 
419 void
421 {
422  AssertIndexRange(row, n_rows());
423  if (!have_entries)
424  return;
425 
426  if (rowset.size() > 0 && !rowset.is_element(row))
427  return;
428 
429  const size_type rowindex =
430  rowset.size() == 0 ? row : rowset.index_within_set(row);
431 
432  AssertIndexRange(rowindex, lines.size());
433  lines[rowindex].entries = std::vector<size_type>();
434 }
435 
436 
437 
440 {
442  view.reinit(rows.n_elements(), this->n_cols());
443  AssertDimension(rows.size(), this->n_rows());
444 
445  const auto end = rows.end();
447  for (auto it = rows.begin(); it != end; ++it, ++view_row)
448  {
449  const size_type rowindex =
450  rowset.size() == 0 ? *it : rowset.index_within_set(*it);
451 
452  view.lines[view_row].entries = lines[rowindex].entries;
453  view.have_entries |= (lines[rowindex].entries.size() > 0);
454  }
455  return view;
456 }
457 
458 
459 
460 template <typename SparsityPatternTypeLeft, typename SparsityPatternTypeRight>
461 void
463  const SparsityPatternTypeLeft & sp_A,
464  const SparsityPatternTypeRight &sp_B)
465 {
466  Assert(sp_A.n_rows() == sp_B.n_rows(),
467  ExcDimensionMismatch(sp_A.n_rows(), sp_B.n_rows()));
468 
469  this->reinit(sp_A.n_cols(), sp_B.n_cols());
470  // we will go through all the
471  // rows in the matrix A, and for each column in a row we add the whole
472  // row of matrix B with that row number. This means that we will insert
473  // a lot of entries to each row, which is best handled by the
474  // DynamicSparsityPattern class.
475 
476  std::vector<size_type> new_cols;
477  new_cols.reserve(sp_B.max_entries_per_row());
478 
479  // C_{kl} = A_{ik} B_{il}
480  for (size_type i = 0; i < sp_A.n_rows(); ++i)
481  {
482  // get all column numbers from sp_B in a temporary vector:
483  new_cols.resize(sp_B.row_length(i));
484  {
485  const auto last_il = sp_B.end(i);
486  auto * col_ptr = new_cols.data();
487  for (auto il = sp_B.begin(i); il != last_il; ++il)
488  *col_ptr++ = il->column();
489  }
490  std::sort(new_cols.begin(), new_cols.end());
491 
492  // now for each k, add new_cols to the target sparsity
493  const auto last_ik = sp_A.end(i);
494  for (auto ik = sp_A.begin(i); ik != last_ik; ++ik)
495  this->add_entries(ik->column(), new_cols.begin(), new_cols.end(), true);
496  }
497 }
498 
499 
500 
501 template <typename SparsityPatternTypeLeft, typename SparsityPatternTypeRight>
502 void
504  const SparsityPatternTypeLeft & left,
505  const SparsityPatternTypeRight &right)
506 {
507  Assert(left.n_cols() == right.n_rows(),
508  ExcDimensionMismatch(left.n_cols(), right.n_rows()));
509 
510  this->reinit(left.n_rows(), right.n_cols());
511 
512  typename SparsityPatternTypeLeft::iterator it_left = left.begin(),
513  end_left = left.end();
514  for (; it_left != end_left; ++it_left)
515  {
516  const unsigned int j = it_left->column();
517 
518  // We are sitting on entry (i,j) of the left sparsity pattern. We then
519  // need to add all entries (i,k) to the final sparsity pattern where (j,k)
520  // exists in the right sparsity pattern -- i.e., we need to iterate over
521  // row j.
522  typename SparsityPatternTypeRight::iterator it_right = right.begin(j),
523  end_right = right.end(j);
524  for (; it_right != end_right; ++it_right)
525  this->add(it_left->row(), it_right->column());
526  }
527 }
528 
529 
530 
531 void
532 DynamicSparsityPattern::print(std::ostream &out) const
533 {
534  for (size_type row = 0; row < lines.size(); ++row)
535  {
536  out << '[' << (rowset.size() == 0 ? row : rowset.nth_index_in_set(row));
537 
538  for (const auto entry : lines[row].entries)
539  out << ',' << entry;
540 
541  out << ']' << std::endl;
542  }
543 
544  AssertThrow(out, ExcIO());
545 }
546 
547 
548 
549 void
551 {
552  for (size_type row = 0; row < lines.size(); ++row)
553  {
554  const size_type rowindex =
555  rowset.size() == 0 ? row : rowset.nth_index_in_set(row);
556 
557  for (const auto entry : lines[row].entries)
558  // while matrix entries are usually
559  // written (i,j), with i vertical and
560  // j horizontal, gnuplot output is
561  // x-y, that is we have to exchange
562  // the order of output
563  out << entry << " " << -static_cast<signed int>(rowindex) << std::endl;
564  }
565 
566 
567  AssertThrow(out, ExcIO());
568 }
569 
570 
571 
574 {
575  size_type b = 0;
576  for (size_type row = 0; row < lines.size(); ++row)
577  {
578  const size_type rowindex =
579  rowset.size() == 0 ? row : rowset.nth_index_in_set(row);
580 
581  for (const auto entry : lines[row].entries)
582  if (static_cast<size_type>(
583  std::abs(static_cast<int>(rowindex - entry))) > b)
584  b = std::abs(static_cast<signed int>(rowindex - entry));
585  }
586 
587  return b;
588 }
589 
590 
591 
594 {
595  if (!have_entries)
596  return 0;
597 
598  size_type n = 0;
599  for (const auto &line : lines)
600  {
601  n += line.entries.size();
602  }
603 
604  return n;
605 }
606 
607 
608 
609 IndexSet
611 {
612  std::set<types::global_dof_index> cols;
613  for (const auto &line : lines)
614  cols.insert(line.entries.begin(), line.entries.end());
615 
616  IndexSet res(this->n_cols());
617  res.add_indices(cols.begin(), cols.end());
618  return res;
619 }
620 
621 
622 
623 IndexSet
625 {
626  const IndexSet all_rows = complete_index_set(this->n_rows());
627  const IndexSet &locally_stored_rows = rowset.size() == 0 ? all_rows : rowset;
628 
629  std::vector<types::global_dof_index> rows;
630  auto line = lines.begin();
631  AssertDimension(locally_stored_rows.n_elements(), lines.size());
632  for (const auto &row : locally_stored_rows)
633  {
634  if (line->entries.size() > 0)
635  rows.push_back(row);
636 
637  ++line;
638  }
639 
640  IndexSet res(this->n_rows());
641  res.add_indices(rows.begin(), rows.end());
642  return res;
643 }
644 
645 
646 
649 {
650  size_type mem = sizeof(DynamicSparsityPattern) +
652  sizeof(rowset);
653 
654  for (const auto &line : lines)
656 
657  return mem;
658 }
659 
660 
661 
665  const DynamicSparsityPattern::size_type col) const
666 {
667  Assert(row < n_rows(),
668  ExcIndexRangeType<DynamicSparsityPattern::size_type>(row,
669  0,
670  n_rows()));
671  Assert(col < n_cols(),
672  ExcIndexRangeType<DynamicSparsityPattern::size_type>(row,
673  0,
674  n_cols()));
675  Assert(rowset.size() == 0 || rowset.is_element(row), ExcInternalError());
676 
677  const DynamicSparsityPattern::size_type local_row =
678  rowset.size() ? rowset.index_within_set(row) : row;
679 
680  // now we need to do a binary search. Note that col indices are assumed to
681  // be sorted.
682  const auto &cols = lines[local_row].entries;
683  auto it = Utilities::lower_bound(cols.begin(), cols.end(), col);
684 
685  if ((it != cols.end()) && (*it == col))
686  return (it - cols.begin());
687  else
689 }
690 
691 
692 
693 // explicit instantiations
694 template void
695 DynamicSparsityPattern::Line::add_entries(size_type *, size_type *, const bool);
696 template void
698  const size_type *,
699  const bool);
700 #ifndef DEAL_II_VECTOR_ITERATOR_IS_POINTER
701 template void
702 DynamicSparsityPattern::Line::add_entries(std::vector<size_type>::iterator,
703  std::vector<size_type>::iterator,
704  const bool);
705 template void
707  std::vector<size_type>::const_iterator,
708  std::vector<size_type>::const_iterator,
709  const bool);
710 #endif
711 
712 template void
714  const DynamicSparsityPattern &);
715 template void
717  const SparsityPattern &);
718 template void
720  const DynamicSparsityPattern &);
721 template void
723  const SparsityPattern &);
724 
725 template void
727  const SparsityPattern &);
728 template void
730  const SparsityPattern &);
731 template void
733  const DynamicSparsityPattern &);
734 template void
736  const DynamicSparsityPattern &);
737 
738 DEAL_II_NAMESPACE_CLOSE
Iterator lower_bound(Iterator first, Iterator last, const T &val)
Definition: utilities.h:1067
const types::global_dof_index invalid_size_type
Definition: types.h:182
void add_entries(const size_type row, ForwardIterator begin, ForwardIterator end, const bool indices_are_unique_and_sorted=false)
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
ElementIterator end() const
Definition: index_set.h:1546
void clear_row(const size_type row)
size_type nth_index_in_set(const unsigned int local_index) const
Definition: index_set.h:1780
static ::ExceptionBase & ExcIO()
void add(const size_type i, const size_type j)
#define AssertIndexRange(index, range)
Definition: exceptions.h:1637
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
Definition: index_set.h:1641
#define AssertThrow(cond, exc)
Definition: exceptions.h:1519
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
size_type size() const
Definition: index_set.h:1600
static ::ExceptionBase & ExcMessage(std::string arg1)
void compute_Tmmult_pattern(const SparsityPatternTypeLeft &left, const SparsityPatternTypeRight &right)
DynamicSparsityPattern get_view(const IndexSet &rows) const
#define Assert(cond, exc)
Definition: exceptions.h:1407
void add_entries(ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted)
void reinit(const size_type m, const size_type n, const IndexSet &rowset=IndexSet())
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
size_type index_within_set(const size_type global_index) const
Definition: index_set.h:1821
void print_gnuplot(std::ostream &out) const
void compute_mmult_pattern(const SparsityPatternTypeLeft &left, const SparsityPatternTypeRight &right)
static ::ExceptionBase & ExcNotQuadratic()
unsigned int global_dof_index
Definition: types.h:89
types::global_dof_index size_type
DynamicSparsityPattern & operator=(const DynamicSparsityPattern &)
void print(std::ostream &out) const
bool is_element(const size_type index) const
Definition: index_set.h:1665
bool exists(const size_type i, const size_type j) const
size_type column_index(const size_type row, const size_type col) const
size_type n_elements() const
Definition: index_set.h:1732
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
static ::ExceptionBase & ExcInternalError()