DOLFIN
DOLFIN C++ interface
Loading...
Searching...
No Matches
GenericVector.h
1// Copyright (C) 2006-2010 Garth N. Wells
2//
3// This file is part of DOLFIN.
4//
5// DOLFIN is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// DOLFIN is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17//
18// Modified by Anders Logg 2006-2011
19// Modified by Kent-Andre Mardal 2008
20// Modified by Ola Skavhaug 2008
21// Modified by Martin Sandve Alnes 2009
22// Modified by Johan Hake 2009-2010
23//
24// First added: 2006-04-25
25// Last changed: 2011-11-11
26
27#ifndef __GENERIC_VECTOR_H
28#define __GENERIC_VECTOR_H
29
30#include <algorithm>
31#include <cstdint>
32#include <utility>
33#include <vector>
34#include <dolfin/common/ArrayView.h>
35#include <dolfin/common/types.h>
36#include <dolfin/log/log.h>
37#include "IndexMap.h"
38#include "TensorLayout.h"
39#include "GenericTensor.h"
40
41namespace dolfin
42{
43 template<typename T> class Array;
44
46
48 {
49 public:
50
52 virtual ~GenericVector() {}
53
54 //--- Implementation of the GenericTensor interface ---
55
58 virtual void init(const TensorLayout& tensor_layout)
59 {
60 if (!empty())
61 {
62 dolfin_error("GenericVector.h",
63 "initialize vector",
64 "Vector cannot be initialised more than once");
65 }
66
67 std::vector<dolfin::la_index> ghosts;
68 std::vector<std::size_t> local_to_global(tensor_layout.index_map(0)->size(IndexMap::MapSize::ALL));
69
70 // FIXME: should just pass index_map to init()
71 for (std::size_t i = 0; i != local_to_global.size(); ++i)
72 local_to_global[i] = tensor_layout.index_map(0)->local_to_global(i);
73
74 // FIXME: temporary hack - needs passing tensor layout directly to backend
75 if (tensor_layout.is_ghosted() == TensorLayout::Ghosts::GHOSTED)
76 {
77 const std::size_t nowned
78 = tensor_layout.index_map(0)->size(IndexMap::MapSize::OWNED);
79 const std::size_t nghosts
80 = tensor_layout.index_map(0)->size(IndexMap::MapSize::UNOWNED);
81 ghosts.resize(nghosts);
82 for (std::size_t i = 0; i != nghosts; ++i)
83 ghosts[i] = local_to_global[i + nowned];
84 }
85
86 init(tensor_layout.local_range(0), local_to_global, ghosts);
87 zero();
88 }
89
91 virtual std::size_t rank() const
92 { return 1; }
93
95 virtual std::size_t size(std::size_t dim) const
96 { dolfin_assert(dim == 0); return size(); }
97
99 virtual std::pair<std::int64_t, std::int64_t>
100 local_range(std::size_t dim) const
101 { dolfin_assert(dim == 0); return local_range(); }
102
104 virtual void get(double* block, const dolfin::la_index* num_rows,
105 const dolfin::la_index * const * rows) const
106 { get(block, num_rows[0], rows[0]); }
107
109 virtual void get_local(double* block, const dolfin::la_index* num_rows,
110 const dolfin::la_index * const * rows) const
111 { get_local(block, num_rows[0], rows[0]); }
112
114 virtual void set(const double* block, const dolfin::la_index* num_rows,
115 const dolfin::la_index * const * rows)
116 { set(block, num_rows[0], rows[0]); }
117
119 virtual void set_local(const double* block,
120 const dolfin::la_index* num_rows,
121 const dolfin::la_index * const * rows)
122 { set_local(block, num_rows[0], rows[0]); }
123
125 virtual void add(const double* block, const dolfin::la_index* num_rows,
126 const dolfin::la_index * const * rows)
127 { add(block, num_rows[0], rows[0]); }
128
130 virtual void add_local(const double* block,
131 const dolfin::la_index* num_rows,
132 const dolfin::la_index * const * rows)
133 { add_local(block, num_rows[0], rows[0]); }
134
136 virtual void
137 add(const double* block,
138 const std::vector<ArrayView<const dolfin::la_index>>& rows)
139 { add(block, rows[0].size(), rows[0].data()); }
140
142 virtual void
143 add_local(const double* block,
144 const std::vector<ArrayView<const dolfin::la_index>>& rows)
145 { add_local(block, rows[0].size(), rows[0].data()); }
146
148 virtual void zero() = 0;
149
151 virtual void apply(std::string mode) = 0;
152
154 virtual std::string str(bool verbose) const = 0;
155
156 //--- Vector interface ---
157
159 virtual std::shared_ptr<GenericVector> copy() const = 0;
160
162 virtual void init(std::size_t N) = 0;
163
165 virtual void init(std::pair<std::size_t, std::size_t> range) = 0;
166
170 virtual void init(std::pair<std::size_t, std::size_t> range,
171 const std::vector<std::size_t>& local_to_global_map,
172 const std::vector<la_index>& ghost_indices) = 0;
173
175 virtual std::size_t size() const = 0;
176
178 virtual std::size_t local_size() const = 0;
179
181 virtual std::pair<std::int64_t, std::int64_t> local_range() const = 0;
182
184 virtual bool owns_index(std::size_t i) const = 0;
185
188 virtual void get(double* block, std::size_t m,
189 const dolfin::la_index* rows) const = 0;
190
193 virtual void get_local(double* block, std::size_t m,
194 const dolfin::la_index* rows) const = 0;
195
197 virtual void set(const double* block, std::size_t m,
198 const dolfin::la_index* rows) = 0;
199
201 virtual void set_local(const double* block, std::size_t m,
202 const dolfin::la_index* rows) = 0;
203
205 virtual void add(const double* block, std::size_t m,
206 const dolfin::la_index* rows) = 0;
207
209 virtual void add_local(const double* block, std::size_t m,
210 const dolfin::la_index* rows) = 0;
211
213 virtual void get_local(std::vector<double>& values) const = 0;
214
216 virtual void set_local(const std::vector<double>& values) = 0;
217
219 virtual void add_local(const Array<double>& values) = 0;
220
223 virtual void gather(GenericVector& x,
224 const std::vector<dolfin::la_index>& indices) const = 0;
225
227 virtual void gather(std::vector<double>& x,
228 const std::vector<dolfin::la_index>& indices) const = 0;
229
231 virtual void gather_on_zero(std::vector<double>& x) const = 0;
232
234 virtual void axpy(double a, const GenericVector& x) = 0;
235
237 virtual void abs() = 0;
238
240 virtual double inner(const GenericVector& x) const = 0;
241
243 virtual double norm(std::string norm_type) const = 0;
244
246 virtual double min() const = 0;
247
249 virtual double max() const = 0;
250
252 virtual double sum() const = 0;
253
256 virtual double sum(const Array<std::size_t>& rows) const = 0;
257
259 std::shared_ptr<GenericVector> operator+ (const GenericVector& x)
260 {
261 auto y = this->copy();
262 dolfin_assert(y);
263 *y += x;
264 return y;
265 }
266
268 std::shared_ptr<GenericVector> operator+ (double a)
269 {
270 auto y = this->copy();
271 dolfin_assert(y);
272 *y += a;
273 return y;
274 }
275
277 std::shared_ptr<GenericVector> operator* (double a)
278 {
279 auto y = this->copy();
280 dolfin_assert(y);
281 *y *= a;
282 return y;
283 }
284
286 virtual const GenericVector& operator*= (double a) = 0;
287
289 virtual const GenericVector& operator*= (const GenericVector& x) = 0;
290
292 virtual const GenericVector& operator/= (double a) = 0;
293
295 virtual const GenericVector& operator+= (const GenericVector& x) = 0;
296
298 virtual const GenericVector& operator+= (double a) = 0;
299
301 virtual const GenericVector& operator-= (const GenericVector& x) = 0;
302
304 virtual const GenericVector& operator-= (double a) = 0;
305
307 virtual const GenericVector& operator= (const GenericVector& x) = 0;
308
310 virtual const GenericVector& operator= (double a) = 0;
311
312 //--- Convenience functions ---
313
315 virtual double operator[] (dolfin::la_index i) const
316 { double value(0); get_local(&value, 1, &i); return value; }
317
319 virtual double getitem(dolfin::la_index i) const
320 { double value(0); get_local(&value, 1, &i); return value; }
321
324 virtual void setitem(dolfin::la_index i, double value)
325 { set(&value, 1, &i); }
326
327 };
328
329}
330
331#endif
Definition SystemAssembler.h:44
Definition SubDomain.h:36
A common interface for arbitrary rank tensors.
Definition GenericTensor.h:49
virtual bool empty() const =0
Return true if empty.
This class defines a common interface for vectors.
Definition GenericVector.h:48
virtual void init(std::size_t N)=0
Initialize vector to global size N.
virtual void add(const double *block, const std::vector< ArrayView< const dolfin::la_index > > &rows)
Add block of values using global indices.
Definition GenericVector.h:137
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const =0
virtual ~GenericVector()
Destructor.
Definition GenericVector.h:52
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Set block of values using local indices.
virtual void abs()=0
Replace all entries in the vector by their absolute values.
virtual const GenericVector & operator+=(const GenericVector &x)=0
Add given vector.
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Set block of values using global indices.
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition GenericVector.h:100
virtual double min() const =0
Return minimum value of vector.
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Add block of values using local indices.
virtual void set_local(const std::vector< double > &values)=0
Set all values on local process.
virtual void get_local(double *block, std::size_t m, const dolfin::la_index *rows) const =0
virtual double max() const =0
Return maximum value of vector.
virtual const GenericVector & operator*=(double a)=0
Multiply vector by given number.
virtual void add(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using global indices.
Definition GenericVector.h:125
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows) const =0
virtual std::shared_ptr< GenericVector > copy() const =0
Return copy of vector.
virtual void setitem(dolfin::la_index i, double value)
Definition GenericVector.h:324
virtual double norm(std::string norm_type) const =0
Return norm of vector.
virtual void set_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using local indices.
Definition GenericVector.h:119
virtual void zero()=0
Set all entries to zero and keep any sparse structure.
virtual void init(std::pair< std::size_t, std::size_t > range)=0
Initialize vector with given local ownership range.
virtual double sum(const Array< std::size_t > &rows) const =0
virtual const GenericVector & operator/=(double a)=0
Divide vector by given number.
virtual void init(std::pair< std::size_t, std::size_t > range, const std::vector< std::size_t > &local_to_global_map, const std::vector< la_index > &ghost_indices)=0
virtual void set(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using global indices.
Definition GenericVector.h:114
virtual double operator[](dolfin::la_index i) const
Get value of given entry.
Definition GenericVector.h:315
virtual void apply(std::string mode)=0
Finalize assembly of tensor.
virtual bool owns_index(std::size_t i) const =0
Determine whether global vector index is owned by this process.
virtual void get_local(std::vector< double > &values) const =0
Get all values on local process.
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Add block of values using global indices.
virtual void init(const TensorLayout &tensor_layout)
Definition GenericVector.h:58
virtual void add_local(const double *block, const std::vector< ArrayView< const dolfin::la_index > > &rows)
Add block of values using local indices.
Definition GenericVector.h:143
std::shared_ptr< GenericVector > operator+(const GenericVector &x)
Sum two vectors (returns a new vector)
Definition GenericVector.h:259
virtual std::size_t size() const =0
Return global size of vector.
std::shared_ptr< GenericVector > operator*(double a)
Multiply vector by a scalar (returns a new vector)
Definition GenericVector.h:277
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition GenericVector.h:95
virtual std::size_t local_size() const =0
Return local size of vector.
virtual void axpy(double a, const GenericVector &x)=0
Add multiple of given vector (AXPY operation)
virtual const GenericVector & operator=(const GenericVector &x)=0
Assignment operator.
virtual void gather_on_zero(std::vector< double > &x) const =0
Gather all entries into x on process 0.
virtual std::pair< std::int64_t, std::int64_t > local_range() const =0
Return local ownership range of a vector.
virtual const GenericVector & operator-=(const GenericVector &x)=0
Subtract given vector.
virtual void add_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using local indices.
Definition GenericVector.h:130
virtual double inner(const GenericVector &x) const =0
Return inner product with given vector.
virtual std::size_t rank() const
Return tensor rank (number of dimensions)
Definition GenericVector.h:91
virtual void get_local(double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows) const
Get block of values using local indices.
Definition GenericVector.h:109
virtual double sum() const =0
Return sum of vector.
virtual double getitem(dolfin::la_index i) const
Get value of given entry.
Definition GenericVector.h:319
virtual void add_local(const Array< double > &values)=0
Add values to each entry on local process.
virtual std::string str(bool verbose) const =0
Return informal string representation (pretty-print)
virtual void get(double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows) const
Get block of values using global indices.
Definition GenericVector.h:104
virtual void gather(std::vector< double > &x, const std::vector< dolfin::la_index > &indices) const =0
Gather entries (given by global indices) into x.
Definition TensorLayout.h:42
std::shared_ptr< const IndexMap > index_map(std::size_t i) const
Return IndexMap for dimension.
Definition TensorLayout.h:96
std::pair< std::size_t, std::size_t > local_range(std::size_t dim) const
Return local range for dimension dim.
Definition TensorLayout.cpp:76
Ghosts is_ghosted() const
Require ghosts.
Definition TensorLayout.h:103
Definition adapt.h:30
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition types.h:32