TiledArray  0.7.0
tensor_impl.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU 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  * This program 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_TENSOR_IMPL_H__INCLUDED
21 #define TILEDARRAY_TENSOR_IMPL_H__INCLUDED
22 
23 #include "error.h"
24 #include "madness.h"
25 #include "policies/dense_policy.h"
26 #include "policies/sparse_policy.h"
27 
28 namespace TiledArray {
29  namespace detail {
30 
32 
38  template <typename Policy>
39  class TensorImpl : private NO_DEFAULTS {
40  public:
42  typedef Policy policy_type;
43  typedef typename Policy::trange_type trange_type;
44  typedef typename Policy::range_type range_type;
45  typedef typename Policy::size_type size_type;
46  typedef typename Policy::shape_type shape_type;
47  typedef typename Policy::pmap_interface pmap_interface;
48 
49  private:
50  World& world_;
51  const trange_type trange_;
52  const shape_type shape_;
53  std::shared_ptr<pmap_interface> pmap_;
54 
55  public:
56 
58 
67  const std::shared_ptr<pmap_interface>& pmap) :
68  world_(world), trange_(trange), shape_(shape), pmap_(pmap)
69  {
70  // Validate input data.
71  TA_ASSERT(pmap_);
72  TA_ASSERT(pmap_->size() == trange_.tiles_range().volume());
73  TA_ASSERT(pmap_->rank() == typename pmap_interface::size_type(world_.rank()));
74  TA_ASSERT(pmap_->procs() == typename pmap_interface::size_type(world_.size()));
75  TA_ASSERT(shape_.validate(trange_.tiles_range()));
76  }
77 
79  virtual ~TensorImpl() { }
80 
82 
85  const std::shared_ptr<pmap_interface>& pmap() const { return pmap_; }
86 
88 
91  const range_type& tiles_range() const { return trange_.tiles_range(); }
92 
94 
97  size_type size() const { return trange_.tiles_range().volume(); }
98 
100 
105  size_type local_size() const { return pmap_->local_size(); }
106 
108 
115  template <typename Index>
116  ProcessID owner(const Index& i) const {
117  TA_ASSERT(trange_.tiles_range().includes(i));
118  return pmap_->owner(trange_.tiles_range().ordinal(i));
119  }
120 
122 
127  template <typename Index>
128  bool is_local(const Index& i) const {
129  TA_ASSERT(trange_.tiles_range().includes(i));
130  return pmap_->is_local(trange_.tiles_range().ordinal(i));
131  }
132 
134 
140  template <typename Index>
141  bool is_zero(const Index& i) const {
142  TA_ASSERT(trange_.tiles_range().includes(i));
143  return shape_.is_zero(trange_.tiles_range().ordinal(i));
144  }
145 
147 
150  bool is_dense() const { return shape_.is_dense(); }
151 
153 
156  const shape_type& shape() const { return shape_; }
157 
159 
161  const trange_type& trange() const { return trange_; }
162 
164  DEPRECATED World& get_world() const { return world_; }
165 
167 
169  World& world() const { return world_; }
170 
171  }; // class TensorImpl
172 
173 
174 #ifndef TILEDARRAY_HEADER_ONLY
175 
176  extern template
177  class TensorImpl<DensePolicy>;
178  extern template
179  class TensorImpl<SparsePolicy>;
180 
181 #endif // TILEDARRAY_HEADER_ONLY
182 
183  } // namespace detail
184 } // namespace TiledArray
185 
186 #endif // TILEDARRAY_TENSOR_IMPL_H__INCLUDED
const shape_type & shape() const
Tensor shape accessor.
Definition: tensor_impl.h:156
const trange_type & trange() const
Tiled range accessor.
Definition: tensor_impl.h:161
Policy::range_type range_type
Element/tile range type.
Definition: tensor_impl.h:44
#define DEPRECATED
Definition: error.h:148
Policy::pmap_interface pmap_interface
Process map interface type.
Definition: tensor_impl.h:47
bool is_dense() const
Query the density of the tensor.
Definition: tensor_impl.h:150
size_type local_size() const
Local element count.
Definition: tensor_impl.h:105
Tensor implementation and base for other tensor implementation objects.
Definition: tensor_impl.h:39
Policy policy_type
Policy type.
Definition: tensor_impl.h:42
virtual ~TensorImpl()
Virtual destructor.
Definition: tensor_impl.h:79
Policy::trange_type trange_type
Tiled range type.
Definition: tensor_impl.h:43
DEPRECATED World & get_world() const
Definition: tensor_impl.h:164
TensorImpl(World &world, const trange_type &trange, const shape_type &shape, const std::shared_ptr< pmap_interface > &pmap)
Constructor.
Definition: tensor_impl.h:66
ProcessID owner(const Index &i) const
Query a tile owner.
Definition: tensor_impl.h:116
bool is_local(const Index &i) const
Query for a locally owned tile.
Definition: tensor_impl.h:128
#define TA_ASSERT(a)
Definition: error.h:107
bool is_zero(const Index &i) const
Query for a zero tile.
Definition: tensor_impl.h:141
Policy::size_type size_type
Size type.
Definition: tensor_impl.h:45
size_type size() const
Tensor tile volume accessor.
Definition: tensor_impl.h:97
TensorImpl< Policy > TensorImpl_
Definition: tensor_impl.h:41
World & world() const
World accessor.
Definition: tensor_impl.h:169
const range_type & tiles_range() const
Tiles range accessor.
Definition: tensor_impl.h:91
const std::shared_ptr< pmap_interface > & pmap() const
Tensor process map accessor.
Definition: tensor_impl.h:85
Policy::shape_type shape_type
Tensor shape type.
Definition: tensor_impl.h:46