Rheolef  7.1
an efficient C++ finite element environment
asr.h
Go to the documentation of this file.
1 # ifndef _RHEO_NEW_ASR_H
2 # define _RHEO_NEW_ASR_H
3 //
4 // This file is part of Rheolef.
5 //
6 // Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
7 //
8 // Rheolef is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rheolef is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rheolef; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // =========================================================================
23 #include "rheolef/disarray.h"
24 #include "rheolef/pair_set.h"
25 #include "rheolef/diststream.h"
26 // -------------------------------------------------------------
27 // the asr class
28 // -------------------------------------------------------------
29 namespace rheolef {
30 
31 template<class T, class M> class csr;
32 template<class T, class M> class csr_rep;
33 
34 /*Class:asr
35 NAME: @code{asr} - associative sparse matrix (@PACKAGE@-@VERSION@)
36 SYNOPSIS:
37  Associative sparse matrix container, used during FEM assembling process.
38 IMPLEMENTATION NOTE:
39  Elements are stored row by row using the pair_set class bqsed on the STL map class.
40  Implementation of asr uses disarray<pair_set>
41 AUTHORS:
42  LMC-IMAG, 38041 Grenoble cedex 9, France
43  | Pierre.Saramito@imag.fr
44 DATE: 6 january 1999, last update 20 may 2012.
45 End:
46 */
47 //<verbatim:
48 template<class T, class M = rheo_default_memory_model, class A = std::allocator<T> >
49 class asr : public disarray<pair_set<T,A>, M, A> {
50 public:
51 // typedefs:
52 
55  typedef typename base::size_type size_type;
56  typedef M memory_type;
57 
58  struct dis_reference {
59  dis_reference (typename base::dis_reference row_dis_i, size_type dis_j)
60  : _row_dis_i(row_dis_i), _dis_j(dis_j) {}
61 
63  _row_dis_i += std::pair<size_type,T>(_dis_j,value);
64  return *this;
65  }
66  typename base::dis_reference _row_dis_i;
68  };
69 
70 // allocators/deallocators:
71 
72  asr (const A& alloc = A())
73  : base(distributor(), row_type(alloc), alloc), _col_ownership(), _nnz(0), _dis_nnz(0) {}
74 
75  asr (const distributor& row_ownership, const distributor& col_ownership, const A& alloc = A())
76  : base(row_ownership, row_type(alloc), alloc), _col_ownership(col_ownership), _nnz(0), _dis_nnz(0) {}
77 
78  asr (const csr_rep<T,M>&, const A& alloc = A());
79  asr (const csr<T,M>&, const A& alloc = A());
80  void build_from_csr (const csr_rep<T,M>&);
81 
83  {
84  base::resize (row_ownership);
86  _nnz = _dis_nnz = 0;
87  }
88 
89 // accessors:
90 
91  const communicator& comm() const { return base::comm(); }
92 
93  size_type nrow () const { return base::size(); }
94  size_type ncol () const { return _col_ownership.size(); }
95  size_type nnz () const { return _nnz; }
96 
97  size_type dis_nrow () const { return base::dis_size(); }
98  size_type dis_ncol () const { return _col_ownership.dis_size(); }
99  size_type dis_nnz () const { return _dis_nnz; }
100  const distributor& row_ownership() const { return base::ownership(); }
101  const distributor& col_ownership() const { return _col_ownership; }
102 
103 // modifiers:
104 
105  T operator() (size_type i, size_type dis_j) const;
106  T& semi_dis_entry (size_type i, size_type dis_j);
107  dis_reference dis_entry (size_type dis_i, size_type dis_j);
108 
109  // dis_entry_assembly_end is redefined in order to recompute _nnz and _dis_nnz
110  void dis_entry_assembly_begin() { base::dis_entry_assembly_begin (pair_set_add_op<row_type>()); }
111  void dis_entry_assembly_end() { base::dis_entry_assembly_end (pair_set_add_op<row_type>()); _recompute_nnz(); }
113 
114 // io:
115  odiststream& put (odiststream& ops) const;
116  idiststream& get (idiststream& ips);
117 
118 // internal:
119  odiststream& put_mpi (odiststream& ops) const;
120  odiststream& put_seq (odiststream& ops, size_type first_dis_i = 0) const;
121  odiststream& put_seq_sparse_matlab (odiststream& ops, size_type first_dis_i = 0) const;
122  odiststream& put_seq_matrix_market (odiststream& ops, size_type first_dis_i = 0) const;
123 protected:
124  void _recompute_nnz();
125 // data:
129 };
130 //>verbatim:
131 // ----------------------------------------------------------------------------
132 // inlined
133 // ----------------------------------------------------------------------------
134 template <class T, class M, class A>
135 inline
136 asr<T,M,A>::asr (const csr<T,M>& a, const A& alloc)
137  : base(a.row_ownership(), row_type(alloc), alloc),
138  _col_ownership(a.col_ownership()),
139  _nnz(a.nnz()),
140  _dis_nnz(a.dis_nnz())
141 {
142  build_from_csr (a.data());
143 }
144 template <class T, class M, class A>
145 inline
146 asr<T,M,A>::asr (const csr_rep<T,M>& a, const A& alloc)
147  : base(a.row_ownership(), row_type(alloc), alloc),
148  _col_ownership(a.col_ownership()),
149  _nnz(a.nnz()),
150  _dis_nnz(a.dis_nnz())
151 {
152  build_from_csr (a);
153 }
154 template <class T, class M, class A>
155 inline
156 idiststream&
157 operator>> (idiststream& s, asr<T,M,A>& x)
158 {
159  return x.get(s);
160 }
161 template <class T, class M, class A>
162 inline
164 operator<< (odiststream& s, const asr<T,M,A>& x)
165 {
166  return x.put(s);
167 }
168 template <class T, class M, class A>
169 inline
170 T
172 {
173  typename row_type::const_iterator pos_aij = base::operator[](i).find(dis_j);
174  if (pos_aij != base::operator[](i).end()) {
175  return (*pos_aij).second;
176  } else {
177  return T(0);
178  }
179 }
180 template <class T, class M, class A>
181 inline
182 T&
184 {
185  row_type& row_i = base::operator[](i);
186  std::pair<typename row_type::iterator,bool> status
187  = row_i.insert (std::pair<size_type,T>(dis_j,T(0)));
188  return (*(status.first)).second;
189 }
190 template <class T, class M, class A>
191 inline
194 {
195  assert_macro (dis_i < dis_nrow() && dis_j < dis_ncol(),
196  "indexes ("<<dis_i<<" "<<dis_j<<") out of range [0:"
197  << dis_nrow() << "[x[0:" << dis_ncol() << "[");
198  return dis_reference (base::dis_entry (dis_i), dis_j);
199 }
200 
201 } // namespace rheolef
202 # endif // _RHEO_NEW_ASR_H
field::size_type size_type
Definition: branch.cc:425
size_type dis_nnz() const
Definition: asr.h:99
void dis_entry_assembly_end()
Definition: asr.h:111
T & semi_dis_entry(size_type i, size_type dis_j)
Definition: asr.h:183
disarray< row_type, M, A > base
Definition: asr.h:54
void dis_entry_assembly()
Definition: asr.h:112
odiststream & put_seq_sparse_matlab(odiststream &ops, size_type first_dis_i=0) const
Definition: asr.cc:85
const distributor & col_ownership() const
Definition: asr.h:101
void dis_entry_assembly_begin()
Definition: asr.h:110
idiststream & get(idiststream &ips)
Definition: asr.cc:183
pair_set< T, A > row_type
Definition: asr.h:53
size_type _nnz
Definition: asr.h:127
size_type dis_ncol() const
Definition: asr.h:98
odiststream & put_mpi(odiststream &ops) const
Definition: asr.cc:144
M memory_type
Definition: asr.h:56
odiststream & put(odiststream &ops) const
Definition: asr.cc:173
base::size_type size_type
Definition: asr.h:55
size_type nrow() const
Definition: asr.h:93
void build_from_csr(const csr_rep< T, M > &)
Definition: asr.cc:37
dis_reference dis_entry(size_type dis_i, size_type dis_j)
Definition: asr.h:193
asr(const A &alloc=A())
Definition: asr.h:72
size_type ncol() const
Definition: asr.h:94
void _recompute_nnz()
Definition: asr.cc:65
const distributor & row_ownership() const
Definition: asr.h:100
size_type _dis_nnz
Definition: asr.h:128
odiststream & put_seq(odiststream &ops, size_type first_dis_i=0) const
Definition: asr.cc:134
T operator()(size_type i, size_type dis_j) const
Definition: asr.h:171
const communicator & comm() const
Definition: asr.h:91
size_type nnz() const
Definition: asr.h:95
distributor _col_ownership
Definition: asr.h:126
asr(const distributor &row_ownership, const distributor &col_ownership, const A &alloc=A())
Definition: asr.h:75
void resize(const distributor &row_ownership, const distributor &col_ownership)
Definition: asr.h:82
odiststream & put_seq_matrix_market(odiststream &ops, size_type first_dis_i=0) const
Definition: asr.cc:110
size_type dis_nrow() const
Definition: asr.h:97
see the disarray page for the full documentation
Definition: disarray.h:459
rep::base::size_type size_type
Definition: disarray.h:463
see the distributor page for the full documentation
Definition: distributor.h:62
size_type dis_size() const
global and local sizes
Definition: distributor.h:207
size_type size(size_type iproc) const
Definition: distributor.h:163
odiststream: see the diststream page for the full documentation
Definition: diststream.h:126
base::const_iterator const_iterator
Definition: pair_set.h:84
rheolef::std value
#define assert_macro(ok_condition, message)
Definition: dis_macros.h:113
Expr1::float_type T
Definition: field_expr.h:261
This file is part of Rheolef.
std::istream & operator>>(std::istream &is, const catchmark &m)
Definition: catchmark.h:88
std::ostream & operator<<(std::ostream &os, const catchmark &m)
Definition: catchmark.h:99
base::dis_reference _row_dis_i
Definition: asr.h:66
dis_reference(typename base::dis_reference row_dis_i, size_type dis_j)
Definition: asr.h:59
dis_reference & operator+=(const T &value)
Definition: asr.h:62
Expr1::memory_type M
Definition: vec_expr_v2.h:416