DOLFIN
DOLFIN C++ interface
Vector.h
1 // Copyright (C) 2007 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 #ifndef __DOLFIN_VECTOR_H
19 #define __DOLFIN_VECTOR_H
20 
21 #include <string>
22 #include <utility>
23 #include <memory>
24 #include <dolfin/common/types.h>
25 #include "DefaultFactory.h"
26 #include "GenericVector.h"
27 
28 namespace dolfin
29 {
30 
31  template<typename T> class Array;
32 
35 
36  class Vector : public GenericVector
37  {
38  public:
39 
41  Vector() : Vector(MPI_COMM_WORLD) {}
42 
44  explicit Vector(MPI_Comm comm)
45  {
47  vector = factory.create_vector(comm);
48  }
49 
51  Vector(MPI_Comm comm, std::size_t N)
52  {
54  vector = factory.create_vector(comm);
55  vector->init(N);
56  }
57 
59  Vector(const Vector& x) : vector(x.vector->copy()) {}
60 
62  Vector(const GenericVector& x) : vector(x.copy()) {}
63 
64  //--- Implementation of the GenericTensor interface ---
65 
67  virtual std::shared_ptr<GenericVector> copy() const
68  {
69  std::shared_ptr<Vector> x(new Vector(*this));
70  return x;
71  }
72 
74  virtual void zero()
75  { vector->zero(); }
76 
78  virtual void apply(std::string mode)
79  { vector->apply(mode); }
80 
82  virtual MPI_Comm mpi_comm() const
83  { return vector->mpi_comm(); }
84 
86  virtual std::string str(bool verbose) const
87  { return "<Vector wrapper of " + vector->str(verbose) + ">"; }
88 
89  //--- Implementation of the GenericVector interface ---
90 
92  virtual void init(std::size_t N)
93  { vector->init(N); }
94 
96  virtual void init(std::pair<std::size_t, std::size_t> range)
97  { vector->init(range); }
98 
101  virtual void init(std::pair<std::size_t, std::size_t> range,
102  const std::vector<std::size_t>& local_to_global_map,
103  const std::vector<la_index>& ghost_indices)
104  { vector->init(range, local_to_global_map, ghost_indices); }
105 
106  // Bring init function from GenericVector into scope
107  using GenericVector::init;
108 
110  virtual bool empty() const
111  { return vector->empty(); }
112 
114  virtual std::size_t size() const
115  { return vector->size(); }
116 
118  virtual std::size_t local_size() const
119  { return vector->local_size(); }
120 
122  virtual std::pair<std::int64_t, std::int64_t> local_range() const
123  { return vector->local_range(); }
124 
126  virtual bool owns_index(std::size_t i) const
127  { return vector->owns_index(i); }
128 
131  virtual void get(double* block, std::size_t m,
132  const dolfin::la_index* rows) const
133  { vector->get(block, m, rows); }
134 
137  virtual void get_local(double* block, std::size_t m,
138  const dolfin::la_index* rows) const
139  { vector->get_local(block, m, rows); }
140 
142  virtual void set(const double* block, std::size_t m,
143  const dolfin::la_index* rows)
144  { vector->set(block, m, rows); }
145 
147  virtual void set_local(const double* block, std::size_t m,
148  const dolfin::la_index* rows)
149  { vector->set_local(block, m, rows); }
150 
152  virtual void add(const double* block, std::size_t m,
153  const dolfin::la_index* rows)
154  { vector->add(block, m, rows); }
155 
157  virtual void add_local(const double* block, std::size_t m,
158  const dolfin::la_index* rows)
159  { vector->add_local(block, m, rows); }
160 
162  virtual void get_local(std::vector<double>& values) const
163  { vector->get_local(values); }
164 
166  virtual void set_local(const std::vector<double>& values)
167  { vector->set_local(values); }
168 
170  virtual void add_local(const Array<double>& values)
171  { vector->add_local(values); }
172 
175  virtual void gather(GenericVector& x,
176  const std::vector<dolfin::la_index>& indices) const
177  { vector->gather(x, indices); }
178 
180  virtual void gather(std::vector<double>& x,
181  const std::vector<dolfin::la_index>& indices) const
182  { vector->gather(x, indices); }
183 
185  virtual void gather_on_zero(std::vector<double>& x) const
186  { vector->gather_on_zero(x); }
187 
189  virtual void axpy(double a, const GenericVector& x)
190  { vector->axpy(a, x); }
191 
193  virtual void abs()
194  { vector->abs(); }
195 
197  virtual double inner(const GenericVector& x) const
198  { return vector->inner(x); }
199 
201  virtual double norm(std::string norm_type) const
202  { return vector->norm(norm_type); }
203 
205  virtual double min() const
206  { return vector->min(); }
207 
209  virtual double max() const
210  { return vector->max(); }
211 
213  virtual double sum() const
214  { return vector->sum(); }
215 
216  virtual double sum(const Array<std::size_t>& rows) const
217  { return vector->sum(rows); }
218 
220  virtual const Vector& operator*= (double a)
221  { *vector *= a; return *this; }
222 
224  virtual const Vector& operator*= (const GenericVector& x)
225  { *vector *= x; return *this; }
226 
228  virtual const Vector& operator/= (double a)
229  { *this *= 1.0 / a; return *this; }
230 
232  virtual const Vector& operator+= (const GenericVector& x)
233  { axpy(1.0, x); return *this; }
234 
236  virtual const GenericVector& operator+= (double a)
237  { *vector += a; return *this; }
238 
240  virtual const Vector& operator-= (const GenericVector& x)
241  { axpy(-1.0, x); return *this; }
242 
244  virtual const GenericVector& operator-= (double a)
245  { *vector -= a; return *this; }
246 
248  virtual const GenericVector& operator= (const GenericVector& x)
249  { *vector = x; return *this; }
250 
252  const Vector& operator= (double a)
253  { *vector = a; return *this; }
254 
255  //--- Special functions ---
256 
259  { return vector->factory(); }
260 
261  //--- Special functions, intended for library use only ---
262 
264  virtual const GenericVector* instance() const
265  { return vector.get(); }
266 
269  { return vector.get(); }
270 
271  virtual std::shared_ptr<const LinearAlgebraObject> shared_instance() const
272  { return vector; }
273 
274  virtual std::shared_ptr<LinearAlgebraObject> shared_instance()
275  { return vector; }
276 
277  //--- Special Vector functions ---
278 
280  const Vector& operator= (const Vector& x)
281  { *vector = *x.vector; return *this; }
282 
283  private:
284 
285  // Pointer to concrete implementation
286  std::shared_ptr<GenericVector> vector;
287 
288  };
289 
290 }
291 
292 #endif
virtual const Vector & operator/=(double a)
Divide vector by given number.
Definition: Vector.h:228
virtual void init(const TensorLayout &tensor_layout)
Definition: GenericVector.h:58
virtual void gather_on_zero(std::vector< double > &x) const
Gather all entries into x on process 0.
Definition: Vector.h:185
virtual const GenericVector & operator=(const GenericVector &x)
Assignment operator.
Definition: Vector.h:248
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: Vector.h:86
virtual bool empty() const
Return true if vector is empty.
Definition: Vector.h:110
virtual double norm(std::string norm_type) const
Return norm of vector.
Definition: Vector.h:201
virtual void init(std::pair< std::size_t, std::size_t > range)
Initialize vector with given ownership range.
Definition: Vector.h:96
virtual double sum() const
Return sum of values of vector.
Definition: Vector.h:213
virtual std::size_t size() const
Return size of vector.
Definition: Vector.h:114
virtual void get_local(double *block, std::size_t m, const dolfin::la_index *rows) const
Definition: Vector.h:137
Definition: adapt.h:29
Vector()
Create empty vector.
Definition: Vector.h:41
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const
Definition: Vector.h:175
virtual void add_local(const Array< double > &values)
Add values to each entry on local process.
Definition: Vector.h:170
virtual std::shared_ptr< GenericVector > create_vector(MPI_Comm comm) const
Create empty vector.
Definition: DefaultFactory.cpp:37
Definition: Array.h:41
virtual std::pair< std::int64_t, std::int64_t > local_range() const
Return local ownership range of a vector.
Definition: Vector.h:122
virtual void axpy(double a, const GenericVector &x)
Add multiple of given vector (AXPY operation)
Definition: Vector.h:189
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: Vector.h:74
virtual std::shared_ptr< const LinearAlgebraObject > shared_instance() const
Return concrete shared ptr instance / unwrap (const version)
Definition: Vector.h:271
virtual bool owns_index(std::size_t i) const
Determine whether global vector index is owned by this process.
Definition: Vector.h:126
virtual const Vector & operator+=(const GenericVector &x)
Add given vector.
Definition: Vector.h:232
virtual void abs()
Replace all entries in the vector by their absolute values.
Definition: Vector.h:193
virtual const Vector & operator*=(double a)
Multiply vector by given number.
Definition: Vector.h:220
virtual const GenericVector * instance() const
Return concrete instance / unwrap (const version)
Definition: Vector.h:264
Vector(const Vector &x)
Copy constructor.
Definition: Vector.h:59
Vector(MPI_Comm comm, std::size_t N)
Create vector of size N.
Definition: Vector.h:51
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: Vector.h:82
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition: Vector.h:258
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using global indices.
Definition: Vector.h:152
virtual double sum(const Array< std::size_t > &rows) const
Definition: Vector.h:216
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using local indices.
Definition: Vector.h:157
virtual GenericVector * instance()
Return concrete instance / unwrap (non-const version)
Definition: Vector.h:268
virtual std::size_t local_size() const
Return local size of vector.
Definition: Vector.h:118
Definition: Vector.h:36
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition: Vector.h:78
virtual std::shared_ptr< LinearAlgebraObject > shared_instance()
Return concrete shared ptr instance / unwrap (non-const version)
Definition: Vector.h:274
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual double max() const
Return maximum value of vector.
Definition: Vector.h:209
virtual const Vector & operator-=(const GenericVector &x)
Subtract given vector.
Definition: Vector.h:240
virtual std::shared_ptr< GenericVector > copy() const
Return copy of vector.
Definition: Vector.h:67
virtual void gather(std::vector< double > &x, const std::vector< dolfin::la_index > &indices) const
Gather entries (given by global indices) into x.
Definition: Vector.h:180
virtual void init(std::size_t N)
Initialize vector to size N.
Definition: Vector.h:92
Vector(const GenericVector &x)
Create a Vector from a GenericVector.
Definition: Vector.h:62
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows)
Set block of values using local indices.
Definition: Vector.h:147
Vector(MPI_Comm comm)
Create empty vector.
Definition: Vector.h:44
virtual double min() const
Return minimum value of vector.
Definition: Vector.h:205
virtual void set_local(const std::vector< double > &values)
Set all values on local process.
Definition: Vector.h:166
This class defines a common interface for vectors.
Definition: GenericVector.h:47
virtual double inner(const GenericVector &x) const
Return inner product with given vector.
Definition: Vector.h:197
virtual void get_local(std::vector< double > &values) const
Get all values on local process.
Definition: Vector.h:162
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)
Definition: Vector.h:101
Default linear algebra factory based on global parameter "linear_algebra_backend".
Definition: DefaultFactory.h:35