Loading...
Searching...
No Matches
GenericVector.h
// Copyright (C) 2006-2010 Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Anders Logg 2006-2011
// Modified by Kent-Andre Mardal 2008
// Modified by Ola Skavhaug 2008
// Modified by Martin Sandve Alnes 2009
// Modified by Johan Hake 2009-2010
//
// First added: 2006-04-25
// Last changed: 2011-11-11
#ifndef __GENERIC_VECTOR_H
#define __GENERIC_VECTOR_H
#include <algorithm>
#include <cstdint>
#include <utility>
#include <vector>
#include <dolfin/common/ArrayView.h>
#include <dolfin/common/types.h>
#include <dolfin/log/log.h>
#include "IndexMap.h"
#include "TensorLayout.h"
#include "GenericTensor.h"
namespace dolfin
{
template<typename T> class Array;
class GenericVector : public GenericTensor
{
public:
virtual ~GenericVector() {}
//--- Implementation of the GenericTensor interface ---
virtual void init(const TensorLayout& tensor_layout)
{
if (!empty())
{
dolfin_error("GenericVector.h",
"initialize vector",
"Vector cannot be initialised more than once");
}
std::vector<dolfin::la_index> ghosts;
std::vector<std::size_t> local_to_global(tensor_layout.index_map(0)->size(IndexMap::MapSize::ALL));
// FIXME: should just pass index_map to init()
for (std::size_t i = 0; i != local_to_global.size(); ++i)
local_to_global[i] = tensor_layout.index_map(0)->local_to_global(i);
// FIXME: temporary hack - needs passing tensor layout directly to backend
if (tensor_layout.is_ghosted() == TensorLayout::Ghosts::GHOSTED)
{
const std::size_t nowned
= tensor_layout.index_map(0)->size(IndexMap::MapSize::OWNED);
const std::size_t nghosts
= tensor_layout.index_map(0)->size(IndexMap::MapSize::UNOWNED);
ghosts.resize(nghosts);
for (std::size_t i = 0; i != nghosts; ++i)
ghosts[i] = local_to_global[i + nowned];
}
init(tensor_layout.local_range(0), local_to_global, ghosts);
zero();
}
virtual std::size_t rank() const
{ return 1; }
virtual std::size_t size(std::size_t dim) const
{ dolfin_assert(dim == 0); return size(); }
virtual std::pair<std::int64_t, std::int64_t>
local_range(std::size_t dim) const
{ dolfin_assert(dim == 0); return local_range(); }
virtual void get(double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows) const
{ get(block, num_rows[0], rows[0]); }
virtual void get_local(double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows) const
{ get_local(block, num_rows[0], rows[0]); }
virtual void set(const double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ set(block, num_rows[0], rows[0]); }
virtual void set_local(const double* block,
const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ set_local(block, num_rows[0], rows[0]); }
virtual void add(const double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ add(block, num_rows[0], rows[0]); }
virtual void add_local(const double* block,
const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ add_local(block, num_rows[0], rows[0]); }
virtual void
add(const double* block,
const std::vector<ArrayView<const dolfin::la_index>>& rows)
{ add(block, rows[0].size(), rows[0].data()); }
virtual void
add_local(const double* block,
const std::vector<ArrayView<const dolfin::la_index>>& rows)
{ add_local(block, rows[0].size(), rows[0].data()); }
virtual void zero() = 0;
virtual void apply(std::string mode) = 0;
virtual std::string str(bool verbose) const = 0;
//--- Vector interface ---
virtual std::shared_ptr<GenericVector> copy() const = 0;
virtual void init(std::size_t N) = 0;
virtual void init(std::pair<std::size_t, std::size_t> range) = 0;
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 std::size_t size() const = 0;
virtual std::size_t local_size() const = 0;
virtual std::pair<std::int64_t, std::int64_t> local_range() const = 0;
virtual bool owns_index(std::size_t i) const = 0;
virtual void get(double* block, std::size_t m,
const dolfin::la_index* rows) const = 0;
virtual void get_local(double* block, std::size_t m,
const dolfin::la_index* rows) const = 0;
virtual void set(const double* block, std::size_t m,
const dolfin::la_index* rows) = 0;
virtual void set_local(const double* block, std::size_t m,
const dolfin::la_index* rows) = 0;
virtual void add(const double* block, std::size_t m,
const dolfin::la_index* rows) = 0;
virtual void add_local(const double* block, std::size_t m,
const dolfin::la_index* rows) = 0;
virtual void get_local(std::vector<double>& values) const = 0;
virtual void set_local(const std::vector<double>& values) = 0;
virtual void add_local(const Array<double>& values) = 0;
virtual void gather(GenericVector& x,
const std::vector<dolfin::la_index>& indices) const = 0;
virtual void gather(std::vector<double>& x,
const std::vector<dolfin::la_index>& indices) const = 0;
virtual void gather_on_zero(std::vector<double>& x) const = 0;
virtual void axpy(double a, const GenericVector& x) = 0;
virtual void abs() = 0;
virtual double inner(const GenericVector& x) const = 0;
virtual double norm(std::string norm_type) const = 0;
virtual double min() const = 0;
virtual double max() const = 0;
virtual double sum() const = 0;
virtual double sum(const Array<std::size_t>& rows) const = 0;
std::shared_ptr<GenericVector> operator+ (const GenericVector& x)
{
auto y = this->copy();
dolfin_assert(y);
*y += x;
return y;
}
std::shared_ptr<GenericVector> operator+ (double a)
{
auto y = this->copy();
dolfin_assert(y);
*y += a;
return y;
}
std::shared_ptr<GenericVector> operator* (double a)
{
auto y = this->copy();
dolfin_assert(y);
*y *= a;
return y;
}
virtual const GenericVector& operator*= (double a) = 0;
virtual const GenericVector& operator*= (const GenericVector& x) = 0;
virtual const GenericVector& operator/= (double a) = 0;
virtual const GenericVector& operator+= (const GenericVector& x) = 0;
virtual const GenericVector& operator+= (double a) = 0;
virtual const GenericVector& operator-= (const GenericVector& x) = 0;
virtual const GenericVector& operator-= (double a) = 0;
virtual const GenericVector& operator= (const GenericVector& x) = 0;
virtual const GenericVector& operator= (double a) = 0;
//--- Convenience functions ---
virtual double operator[] (dolfin::la_index i) const
{ double value(0); get_local(&value, 1, &i); return value; }
virtual double getitem(dolfin::la_index i) const
{ double value(0); get_local(&value, 1, &i); return value; }
virtual void setitem(dolfin::la_index i, double value)
{ set(&value, 1, &i); }
};
}
#endif
dolfin::ArrayView
Definition ArrayView.h:32
dolfin::Array
Definition Array.h:42
dolfin::GenericTensor
A common interface for arbitrary rank tensors.
Definition GenericTensor.h:49
dolfin::GenericTensor::empty
virtual bool empty() const =0
Return true if empty.
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::GenericVector::init
virtual void init(std::size_t N)=0
Initialize vector to global size N.
dolfin::GenericVector::add
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
dolfin::GenericVector::gather
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const =0
dolfin::GenericVector::~GenericVector
virtual ~GenericVector()
Destructor.
Definition GenericVector.h:52
dolfin::GenericVector::set_local
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Set block of values using local indices.
dolfin::GenericVector::abs
virtual void abs()=0
Replace all entries in the vector by their absolute values.
dolfin::GenericVector::operator+=
virtual const GenericVector & operator+=(const GenericVector &x)=0
Add given vector.
dolfin::GenericVector::set
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Set block of values using global indices.
dolfin::GenericVector::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition GenericVector.h:100
dolfin::GenericVector::min
virtual double min() const =0
Return minimum value of vector.
dolfin::GenericVector::add_local
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Add block of values using local indices.
dolfin::GenericVector::set_local
virtual void set_local(const std::vector< double > &values)=0
Set all values on local process.
dolfin::GenericVector::get_local
virtual void get_local(double *block, std::size_t m, const dolfin::la_index *rows) const =0
dolfin::GenericVector::max
virtual double max() const =0
Return maximum value of vector.
dolfin::GenericVector::operator*=
virtual const GenericVector & operator*=(double a)=0
Multiply vector by given number.
dolfin::GenericVector::add
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
dolfin::GenericVector::get
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows) const =0
dolfin::GenericVector::copy
virtual std::shared_ptr< GenericVector > copy() const =0
Return copy of vector.
dolfin::GenericVector::setitem
virtual void setitem(dolfin::la_index i, double value)
Definition GenericVector.h:324
dolfin::GenericVector::norm
virtual double norm(std::string norm_type) const =0
Return norm of vector.
dolfin::GenericVector::set_local
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
dolfin::GenericVector::zero
virtual void zero()=0
Set all entries to zero and keep any sparse structure.
dolfin::GenericVector::init
virtual void init(std::pair< std::size_t, std::size_t > range)=0
Initialize vector with given local ownership range.
dolfin::GenericVector::sum
virtual double sum(const Array< std::size_t > &rows) const =0
dolfin::GenericVector::operator/=
virtual const GenericVector & operator/=(double a)=0
Divide vector by given number.
dolfin::GenericVector::init
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
dolfin::GenericVector::set
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
dolfin::GenericVector::operator[]
virtual double operator[](dolfin::la_index i) const
Get value of given entry.
Definition GenericVector.h:315
dolfin::GenericVector::apply
virtual void apply(std::string mode)=0
Finalize assembly of tensor.
dolfin::GenericVector::owns_index
virtual bool owns_index(std::size_t i) const =0
Determine whether global vector index is owned by this process.
dolfin::GenericVector::get_local
virtual void get_local(std::vector< double > &values) const =0
Get all values on local process.
dolfin::GenericVector::add
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows)=0
Add block of values using global indices.
dolfin::GenericVector::init
virtual void init(const TensorLayout &tensor_layout)
Definition GenericVector.h:58
dolfin::GenericVector::add_local
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
dolfin::GenericVector::operator+
std::shared_ptr< GenericVector > operator+(const GenericVector &x)
Sum two vectors (returns a new vector).
Definition GenericVector.h:259
dolfin::GenericVector::size
virtual std::size_t size() const =0
Return global size of vector.
dolfin::GenericVector::operator*
std::shared_ptr< GenericVector > operator*(double a)
Multiply vector by a scalar (returns a new vector).
Definition GenericVector.h:277
dolfin::GenericVector::size
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition GenericVector.h:95
dolfin::GenericVector::local_size
virtual std::size_t local_size() const =0
Return local size of vector.
dolfin::GenericVector::axpy
virtual void axpy(double a, const GenericVector &x)=0
Add multiple of given vector (AXPY operation).
dolfin::GenericVector::operator=
virtual const GenericVector & operator=(const GenericVector &x)=0
Assignment operator.
dolfin::GenericVector::gather_on_zero
virtual void gather_on_zero(std::vector< double > &x) const =0
Gather all entries into x on process 0.
dolfin::GenericVector::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range() const =0
Return local ownership range of a vector.
dolfin::GenericVector::operator-=
virtual const GenericVector & operator-=(const GenericVector &x)=0
Subtract given vector.
dolfin::GenericVector::add_local
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
dolfin::GenericVector::inner
virtual double inner(const GenericVector &x) const =0
Return inner product with given vector.
dolfin::GenericVector::rank
virtual std::size_t rank() const
Return tensor rank (number of dimensions).
Definition GenericVector.h:91
dolfin::GenericVector::get_local
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
dolfin::GenericVector::sum
virtual double sum() const =0
Return sum of vector.
dolfin::GenericVector::getitem
virtual double getitem(dolfin::la_index i) const
Get value of given entry.
Definition GenericVector.h:319
dolfin::GenericVector::add_local
virtual void add_local(const Array< double > &values)=0
Add values to each entry on local process.
dolfin::GenericVector::str
virtual std::string str(bool verbose) const =0
Return informal string representation (pretty-print).
dolfin::GenericVector::get
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
dolfin::GenericVector::gather
virtual void gather(std::vector< double > &x, const std::vector< dolfin::la_index > &indices) const =0
Gather entries (given by global indices) into x.
dolfin::TensorLayout
Definition TensorLayout.h:42
dolfin::TensorLayout::index_map
std::shared_ptr< const IndexMap > index_map(std::size_t i) const
Return IndexMap for dimension.
Definition TensorLayout.h:96
dolfin::TensorLayout::local_range
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
dolfin::TensorLayout::is_ghosted
Ghosts is_ghosted() const
Require ghosts.
Definition TensorLayout.h:103
dolfin
Definition adapt.h:30
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32