sparsity_pattern.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #ifndef LIBMESH_SPARSITY_PATTERN_H
20 #define LIBMESH_SPARSITY_PATTERN_H
21 
22 // Local Includes
23 #include "libmesh/elem_range.h"
26 
27 // C++ includes
28 #include <vector>
29 
30 namespace libMesh
31 {
32 
33 // Forward declarations
34 class MeshBase;
35 class DofMap;
36 class CouplingMatrix;
37 
48 namespace SparsityPattern // use a namespace so member classes can be forward-declared.
49 {
50 typedef std::vector<dof_id_type, Threads::scalable_allocator<dof_id_type>> Row;
51 class Graph : public std::vector<Row> {};
52 
53 class NonlocalGraph : public std::map<dof_id_type, Row> {};
54 
64 template<typename BidirectionalIterator>
65 static void sort_row (const BidirectionalIterator begin,
66  BidirectionalIterator middle,
67  const BidirectionalIterator end);
68 
80 class Build : public ParallelObject
81 {
82 private:
83  const MeshBase & mesh;
84  const DofMap & dof_map;
86  const std::set<GhostingFunctor *> & coupling_functors;
89 
90  void handle_vi_vj(const std::vector<dof_id_type> & element_dofs_i,
91  const std::vector<dof_id_type> & element_dofs_j);
92 
93  void sorted_connected_dofs(const Elem * elem,
94  std::vector<dof_id_type> & dofs_vi,
95  unsigned int vi);
96 
97 public:
98 
101 
102  std::vector<dof_id_type> n_nz;
103  std::vector<dof_id_type> n_oz;
104 
105  Build (const MeshBase & mesh_in,
106  const DofMap & dof_map_in,
107  const CouplingMatrix * dof_coupling_in,
108  std::set<GhostingFunctor *> coupling_functors_in,
109  const bool implicit_neighbor_dofs_in,
110  const bool need_full_sparsity_pattern_in);
111 
112  Build (Build & other, Threads::split);
113 
114  void operator()(const ConstElemRange & range);
115 
116  void join (const Build & other);
117 
118  void parallel_sync ();
119 };
120 
121 #if defined(__GNUC__) && (__GNUC__ < 4) && !defined(__INTEL_COMPILER)
122 
127 void _dummy_function(void);
128 #endif
129 
130 }
131 
132 
133 
134 // ------------------------------------------------------------
135 // SparsityPattern inline member functions
136 template<typename BidirectionalIterator>
137 inline
138 void SparsityPattern::sort_row (const BidirectionalIterator begin,
139  BidirectionalIterator middle,
140  const BidirectionalIterator end)
141 {
142  if ((begin == middle) || (middle == end)) return;
143 
144  libmesh_assert_greater (std::distance (begin, middle), 0);
145  libmesh_assert_greater (std::distance (middle, end), 0);
146  libmesh_assert (std::unique (begin, middle) == middle);
147  libmesh_assert (std::unique (middle, end) == end);
148 
149  while (middle != end)
150  {
151  BidirectionalIterator
152  b = middle,
153  a = b-1;
154 
155  // Bubble-sort the middle value downward
156  while (!(*a < *b)) // *a & *b are less-than comparable, so use <
157  {
158  std::swap (*a, *b);
159 
160 #if defined(__GNUC__) && (__GNUC__ < 4) && !defined(__INTEL_COMPILER)
161  /* Prohibit optimization at this point since gcc 3.3.5 seems
162  to have a bug. */
164 #endif
165 
166  if (a == begin) break;
167 
168  b=a;
169  --a;
170  }
171 
172  ++middle;
173  }
174 
175  // Assure the algorithm worked if we are in DEBUG mode
176 #ifdef DEBUG
177  {
178  // SGI STL extension!
179  // libmesh_assert (std::is_sorted(begin,end));
180 
181  BidirectionalIterator
182  prev = begin,
183  first = begin;
184 
185  for (++first; first != end; prev=first, ++first)
186  if (*first < *prev)
187  libmesh_assert(false);
188  }
189 #endif
190 
191  // Make sure the two ranges did not contain any common elements
192  libmesh_assert (std::unique (begin, end) == end);
193 }
194 
195 } // namespace libMesh
196 
197 #endif // LIBMESH_SPARSITY_PATTERN_H
SparsityPattern::Graph sparsity_pattern
const std::set< GhostingFunctor * > & coupling_functors
static const unsigned int prev[3]
The base class for all geometric element types.
Definition: elem.h:100
IterBase * end
Utility class for defining generic ranges for threading.
Definition: stored_range.h:52
Base class for Mesh.
Definition: mesh_base.h:77
Manages the degrees of freedom (DOFs) in a simulation.
Definition: dof_map.h:176
std::vector< dof_id_type, Threads::scalable_allocator< dof_id_type > > Row
void join(const Build &other)
const CouplingMatrix * dof_coupling
void sorted_connected_dofs(const Elem *elem, std::vector< dof_id_type > &dofs_vi, unsigned int vi)
std::vector< dof_id_type > n_nz
An object whose state is distributed along a set of processors.
SparsityPattern::NonlocalGraph nonlocal_pattern
void swap(Iterator &lhs, Iterator &rhs)
Build(const MeshBase &mesh_in, const DofMap &dof_map_in, const CouplingMatrix *dof_coupling_in, std::set< GhostingFunctor *> coupling_functors_in, const bool implicit_neighbor_dofs_in, const bool need_full_sparsity_pattern_in)
void handle_vi_vj(const std::vector< dof_id_type > &element_dofs_i, const std::vector< dof_id_type > &element_dofs_j)
void operator()(const ConstElemRange &range)
Defines the coupling between variables of a System.
static void sort_row(const BidirectionalIterator begin, BidirectionalIterator middle, const BidirectionalIterator end)
std::vector< dof_id_type > n_oz