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 
24 #include "error.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::index1_type index1_type;
46  typedef typename Policy::ordinal_type ordinal_type;
47  typedef typename Policy::shape_type shape_type;
48  typedef typename Policy::pmap_interface
50 
51  private:
52  World& world_;
53  const trange_type trange_;
54  const shape_type shape_;
55  std::shared_ptr<pmap_interface> pmap_;
56 
57  public:
59 
68  const std::shared_ptr<pmap_interface>& pmap)
69  : world_(world), trange_(trange), shape_(shape), pmap_(pmap) {
70  // Validate input data.
71  TA_ASSERT(pmap_);
72  TA_ASSERT(pmap_->size() == trange_.tiles_range().volume());
73  TA_ASSERT(pmap_->rank() ==
74  typename pmap_interface::size_type(world_.rank()));
75  TA_ASSERT(pmap_->procs() ==
76  typename pmap_interface::size_type(world_.size()));
77  TA_ASSERT(shape_.validate(trange_.tiles_range()));
78  // ensure that shapes are identical on every rank
80  }
81 
83  virtual ~TensorImpl() {}
84 
86 
89  const std::shared_ptr<pmap_interface>& pmap() const { return pmap_; }
90 
92 
95  const range_type& tiles_range() const { return trange_.tiles_range(); }
96 
98 
101  ordinal_type size() const { return trange_.tiles_range().volume(); }
102 
104 
110  return static_cast<ordinal_type>(pmap_->local_size());
111  }
112 
114 
121  template <typename Index>
122  ProcessID owner(const Index& i) const {
123  TA_ASSERT(trange_.tiles_range().includes(i));
124  return pmap_->owner(trange_.tiles_range().ordinal(i));
125  }
126 
128 
133  template <typename Index>
134  bool is_local(const Index& i) const {
135  TA_ASSERT(trange_.tiles_range().includes(i));
136  return pmap_->is_local(trange_.tiles_range().ordinal(i));
137  }
138 
140 
146  template <typename Index>
147  bool is_zero(const Index& i) const {
148  TA_ASSERT(trange_.tiles_range().includes(i));
149  return shape_.is_zero(trange_.tiles_range().ordinal(i));
150  }
151 
153 
156  bool is_dense() const { return shape_.is_dense(); }
157 
159 
162  const shape_type& shape() const { return shape_; }
163 
165 
167  const trange_type& trange() const { return trange_; }
168 
170  [[deprecated]] World& get_world() const { return world_; }
171 
173 
175  World& world() const { return world_; }
176 
177 }; // class TensorImpl
178 
179 #ifndef TILEDARRAY_HEADER_ONLY
180 
181 extern template class TensorImpl<DensePolicy>;
182 extern template class TensorImpl<SparsePolicy>;
183 
184 #endif // TILEDARRAY_HEADER_ONLY
185 
186 } // namespace detail
187 } // namespace TiledArray
188 
189 #endif // TILEDARRAY_TENSOR_IMPL_H__INCLUDED
Policy policy_type
Policy type.
Definition: tensor_impl.h:42
World & world() const
World accessor.
Definition: tensor_impl.h:175
virtual ~TensorImpl()
Virtual destructor.
Definition: tensor_impl.h:83
ordinal_type size() const
Tensor tile volume accessor.
Definition: tensor_impl.h:101
TensorImpl< Policy > TensorImpl_
Definition: tensor_impl.h:41
bool is_zero(const Index &i) const
Query for a zero tile.
Definition: tensor_impl.h:147
bool is_dense() const
Query the density of the tensor.
Definition: tensor_impl.h:156
const range_type & tiles_range() const
Tiles range accessor.
Definition: tensor_impl.h:95
const std::shared_ptr< pmap_interface > & pmap() const
Tensor process map accessor.
Definition: tensor_impl.h:89
Tensor implementation and base for other tensor implementation objects.
Definition: tensor_impl.h:39
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
ProcessID owner(const Index &i) const
Query a tile owner.
Definition: tensor_impl.h:122
Policy::ordinal_type ordinal_type
Ordinal type.
Definition: tensor_impl.h:46
Policy::shape_type shape_type
Tensor shape type.
Definition: tensor_impl.h:47
Policy::range_type range_type
Element/tile range type.
Definition: tensor_impl.h:44
Policy::trange_type trange_type
Tiled range type.
Definition: tensor_impl.h:43
ordinal_type local_size() const
Local element count.
Definition: tensor_impl.h:109
TensorImpl(World &world, const trange_type &trange, const shape_type &shape, const std::shared_ptr< pmap_interface > &pmap)
Constructor.
Definition: tensor_impl.h:67
Policy::pmap_interface pmap_interface
Process map interface type.
Definition: tensor_impl.h:49
Policy::index1_type index1_type
1-index type
Definition: tensor_impl.h:45
constexpr bool is_replicated(World &world, const DenseShape &t)
Definition: dense_shape.h:386
const trange_type & trange() const
Tiled range accessor.
Definition: tensor_impl.h:167
const shape_type & shape() const
Tensor shape accessor.
Definition: tensor_impl.h:162
bool is_local(const Index &i) const
Query for a locally owned tile.
Definition: tensor_impl.h:134