Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost_cuthill_mckee_ordering.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020: G-CSC, Goethe University Frankfurt
3 * Author: Lukas Larisch
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef UG_BASE_LIB_ALGEBRA_ORDERING_STRATEGIES_ALGORITHMS_BOOST_CUTHILL_MCKEE_ORDERING_H
34#define UG_BASE_LIB_ALGEBRA_ORDERING_STRATEGIES_ALGORITHMS_BOOST_CUTHILL_MCKEE_ORDERING_H
35
36#include <boost/graph/adjacency_list.hpp>
37#include <boost/graph/graph_traits.hpp>
38#include <boost/graph/properties.hpp>
39
40#include <boost/graph/cuthill_mckee_ordering.hpp>
41
42#include "IOrderingAlgorithm.h"
43#include "util.h"
44
45//debug
46#include "common/error.h"
47#include "common/log.h"
48
49namespace ug{
50
51
52template <typename G_t, typename M_t>
53void induced_subgraph(G_t& ind_g, M_t* A, const std::vector<size_t>& inv_map){
54 size_t n = A->num_rows();
55 size_t k = inv_map.size();
56 ind_g = G_t(k);
57
58 std::vector<int> ind_map(n, -1);
59 for(unsigned i = 0; i < k; ++i){
60 ind_map[inv_map[i]] = i;
61 }
62
63 typename boost::graph_traits<G_t>::adjacency_iterator nIt, nEnd;
64 for(unsigned i = 0; i < inv_map.size(); ++i){
65 for(typename M_t::row_iterator conn = A->begin_row(inv_map[i]); conn != A->end_row(inv_map[i]); ++conn){
66 if(conn.value() != 0.0 && conn.index() != i){
67 int idx = ind_map[conn.index()];
68 if(idx >= 0){
69 boost::add_edge(i, idx, ind_g);
70 }
71 }
72 }
73 }
74}
75
76
77
78#ifndef MCKEE_GRAPH_T
79#define MCKEE_GRAPH_T
80/* boost graph type for Cuthill-McKee */
81typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
82 boost::property<boost::vertex_color_t,
83 boost::default_color_type,
84 boost::property<boost::vertex_degree_t, int> > >
86#endif
87
88
89template <typename TAlgebra, typename O_t=std::vector<size_t> >
90class BoostCuthillMcKeeOrdering : public IOrderingAlgorithm<TAlgebra, O_t>
91{
92public:
93 typedef typename TAlgebra::matrix_type M_t;
94 typedef typename TAlgebra::vector_type V_t;
95 typedef Graph_t G_t;
96 typedef boost::graph_traits<G_t>::vertex_descriptor Vertex_t;
98
100
104
109
110 void compute(){
111 UG_COND_THROW(boost::num_vertices(g) == 0, name() << "::compute: Graph is empty!");
112
113 boost::property_map<G_t, boost::vertex_index_t>::type index_map = get(boost::vertex_index, g);
114
115 std::vector<Vertex_t> inv_perm(boost::num_vertices(g));
116
117 if(m_bReverse){
118 boost::cuthill_mckee_ordering(g, inv_perm.rbegin(), get(boost::vertex_color, g), boost::make_degree_map(g));
119 }
120 else{
121 boost::cuthill_mckee_ordering(g, inv_perm.begin(), get(boost::vertex_color, g), boost::make_degree_map(g));
122 }
123
124 o.resize(boost::num_vertices(g));
125
126 for(unsigned i = 0; i != inv_perm.size(); ++i){
127 o[index_map[inv_perm[i]]] = i;
128 }
129
130 g = G_t(0);
131
132 #ifdef UG_DEBUG
133 check();
134 #endif
135 }
136
137 void check(){
138 UG_COND_THROW(!is_permutation(o), name() << "::check: Not a permutation!");
139 }
140
141 O_t& ordering(){
142 return o;
143 }
144
145 void init(M_t* A, const V_t&){
146 init(A);
147 }
148
149 void init(M_t* A){
150 //TODO: replace this by UG_DLOG if permutation_util does not depend on this file anymore
151 #ifdef UG_ENABLE_DEBUG_LOGS
152 UG_LOG("Using " << name() << "\n");
153 #endif
154
155 unsigned rows = A->num_rows();
156
157 g = G_t(rows);
158
159 for(unsigned i = 0; i < rows; i++){
160 for(typename M_t::row_iterator conn = A->begin_row(i); conn != A->end_row(i); ++conn){
161 if(conn.value() != 0.0 && conn.index() != i){ //TODO: think about this!!
162 if(!boost::edge(i, conn.index(), g).second){
163 boost::add_edge(i, conn.index(), g);
164 }
165 }
166 }
167 }
168 }
169
170 void init(M_t* A, const V_t&, const O_t& inv_map){
171 init(A, inv_map);
172 }
173
174 void init(M_t* A, const O_t& inv_map){
175 //TODO: replace this by UG_DLOG if permutation_util does not depend on this file anymore
176 #ifdef UG_ENABLE_DEBUG_LOGS
177 UG_LOG("Using " << name() << " on induced matrix of size " << inv_map.size() << "\n");
178 #endif
179
180 induced_subgraph<G_t, M_t>(g, A, inv_map);
181 }
182
183 void set_reverse(bool b){
184 m_bReverse = b;
185 }
186
187 virtual const char* name() const {
188 if(m_bReverse){
189 return "ReverseBoostCuthillMcKeeOrdering";
190 }
191 else{
192 return "BoostCuthillMcKeeOrdering";
193 }
194 }
195
196private:
198 O_t o;
199
201};
202
203}
204
205#endif
Definition smart_pointer.h:108
Definition boost_cuthill_mckee_ordering.h:91
void set_reverse(bool b)
Definition boost_cuthill_mckee_ordering.h:183
boost::graph_traits< G_t >::vertex_descriptor Vertex_t
Definition boost_cuthill_mckee_ordering.h:96
O_t o
Definition boost_cuthill_mckee_ordering.h:198
IOrderingAlgorithm< TAlgebra, O_t > baseclass
Definition boost_cuthill_mckee_ordering.h:97
Graph_t G_t
Definition boost_cuthill_mckee_ordering.h:95
BoostCuthillMcKeeOrdering(const BoostCuthillMcKeeOrdering< TAlgebra, O_t > &parent)
clone constructor
Definition boost_cuthill_mckee_ordering.h:102
bool m_bReverse
Definition boost_cuthill_mckee_ordering.h:200
void init(M_t *A)
Definition boost_cuthill_mckee_ordering.h:149
void check()
Definition boost_cuthill_mckee_ordering.h:137
void init(M_t *A, const V_t &, const O_t &inv_map)
Definition boost_cuthill_mckee_ordering.h:170
virtual const char * name() const
Definition boost_cuthill_mckee_ordering.h:187
TAlgebra::vector_type V_t
Definition boost_cuthill_mckee_ordering.h:94
G_t g
Definition boost_cuthill_mckee_ordering.h:197
void init(M_t *A, const O_t &inv_map)
Definition boost_cuthill_mckee_ordering.h:174
SmartPtr< IOrderingAlgorithm< TAlgebra, O_t > > clone()
Definition boost_cuthill_mckee_ordering.h:105
TAlgebra::matrix_type M_t
Definition boost_cuthill_mckee_ordering.h:93
O_t & ordering()
Definition boost_cuthill_mckee_ordering.h:141
void init(M_t *A, const V_t &)
Definition boost_cuthill_mckee_ordering.h:145
void compute()
Definition boost_cuthill_mckee_ordering.h:110
BoostCuthillMcKeeOrdering()
Definition boost_cuthill_mckee_ordering.h:99
Definition IOrderingAlgorithm.h:52
virtual void init()
#define UG_LOG(msg)
Definition log.h:367
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition error.h:61
int num_vertices(ug::BidirectionalMatrix< T > const &M)
Definition bidirectional_boost.h:70
degree_property_map< ug::UndirectedMatrix< T > > make_degree_map(const ug::UndirectedMatrix< T > &g)
Definition undirected_boost.h:341
the ug namespace
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, boost::property< boost::vertex_color_t, boost::default_color_type, boost::property< boost::vertex_degree_t, int > > > Graph_t
Definition boost_cuthill_mckee_ordering.h:85
bool is_permutation(O_t &o)
Definition permutation_util.h:135
void induced_subgraph(G_t &ind_g, M_t *A, const std::vector< size_t > &inv_map)
Definition boost_cuthill_mckee_ordering.h:53
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition smart_pointer.h:836