unary_eval.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_DIST_EVAL_UNARY_EVAL_H__INCLUDED
21 #define TILEDARRAY_DIST_EVAL_UNARY_EVAL_H__INCLUDED
22 
24 
26 
27 namespace TiledArray {
28 namespace detail {
29 
31 
37 template <typename Arg, typename Op, typename Policy>
39  : public DistEvalImpl<typename Op::result_type, Policy>,
40  public std::enable_shared_from_this<UnaryEvalImpl<Arg, Op, Policy>> {
41  public:
45  typedef typename DistEvalImpl_::TensorImpl_
47  typedef Arg arg_type;
51  typedef typename DistEvalImpl_::pmap_interface
53  typedef
56  typedef
58  typedef Op op_type;
59 
61 
69  template <typename Perm, typename = std::enable_if_t<
70  TiledArray::detail::is_permutation_v<Perm>>>
71  UnaryEvalImpl(const arg_type& arg, World& world, const trange_type& trange,
72  const shape_type& shape,
73  const std::shared_ptr<pmap_interface>& pmap, const Perm& perm,
74  const op_type& op)
76  arg_(arg),
77  op_(op) {}
78 
80  virtual ~UnaryEvalImpl() {}
81 
83 
91  const auto source = arg_.owner(DistEvalImpl_::perm_index_to_source(i));
92  const madness::DistributedID key(DistEvalImpl_::id(), i);
93  return TensorImpl_::world().gop.template recv<value_type>(source, key);
94  }
95 
97 
101  virtual void discard_tile(ordinal_type i) const { get_tile(i); }
102 
103  private:
105 
108  typedef typename std::conditional<
109  op_type::is_consumable, typename arg_type::value_type&,
110  const typename arg_type::value_type&>::type tile_argument_type;
111 
113 
114 #ifdef TILEDARRAY_HAS_CUDA
115  template <typename U = value_type>
118  std::enable_if_t<detail::is_cuda_tile_v<U>, void> eval_tile(
119  const ordinal_type i, tile_argument_type tile) {
120  // TODO avoid copy Op object
121  auto result_tile =
122  madness::add_cuda_task(DistEvalImpl_::world(), op_, tile);
123  DistEvalImpl_::set_tile(i, result_tile);
124  }
125 
128  template <typename U = value_type>
129  std::enable_if_t<!detail::is_cuda_tile_v<U>, void> eval_tile(
130  const ordinal_type i, tile_argument_type tile) {
131  DistEvalImpl_::set_tile(i, op_(tile));
132  }
133 #else
134  void eval_tile(const ordinal_type i, tile_argument_type tile) {
137  DistEvalImpl_::set_tile(i, op_(tile));
138  }
139 #endif
140 
147  virtual int internal_eval() {
148  // Convert pimpl to this object type so it can be used in tasks
149  std::shared_ptr<UnaryEvalImpl_> self =
150  std::enable_shared_from_this<UnaryEvalImpl_>::shared_from_this();
151 
152  // Evaluate argument
153  arg_.eval();
154 
155  // Counter for the number of tasks submitted by this object
156  ordinal_type task_count = 0ul;
157 
158  // Make sure all local tiles are present.
159  const typename pmap_interface::const_iterator end = arg_.pmap()->end();
160  typename pmap_interface::const_iterator it = arg_.pmap()->begin();
161  for (; it != end; ++it) {
162  // Get argument tile index
163  const auto index = *it;
164 
165  if (!arg_.is_zero(index)) {
166  // Get target tile index
167  const auto target_index = DistEvalImpl_::perm_index_to_target(index);
168 
169  // Schedule tile evaluation task
170 #ifdef TILEDARRAY_HAS_CUDA
171  TensorImpl_::world().taskq.add(self,
172  &UnaryEvalImpl_::template eval_tile<>,
173  target_index, arg_.get(index));
174 #else
175  TensorImpl_::world().taskq.add(self, &UnaryEvalImpl_::eval_tile,
176  target_index, arg_.get(index));
177 #endif
178 
179  ++task_count;
180  }
181  }
182 
183  // Wait for local tiles of argument to be evaluated
184  arg_.wait();
185 
186  return task_count;
187  }
188 
189  arg_type arg_;
190  op_type op_;
191 }; // class UnaryEvalImpl
192 
193 } // namespace detail
194 } // namespace TiledArray
195 
196 #endif // TILEDARRAY_DIST_EVAL_UNARY_EVAL_H__INCLUDED
virtual Future< value_type > get_tile(ordinal_type i) const
Get tile at index i.
Definition: unary_eval.h:88
::blas::Op Op
Definition: blas.h:46
World & world() const
World accessor.
Definition: tensor_impl.h:175
Arg arg_type
The argument tensor type.
Definition: unary_eval.h:47
bool is_zero(const Index &i) const
Query for a zero tile.
Definition: tensor_impl.h:147
ordinal_type perm_index_to_target(ordinal_type index) const
Permute index from a source index to a target index.
Definition: dist_eval.h:85
DistEvalImpl_::eval_type eval_type
Tile evaluation type.
Definition: unary_eval.h:57
DistEvalImpl_::shape_type shape_type
Shape type.
Definition: unary_eval.h:50
void set_tile(ordinal_type i, const value_type &value)
Set tensor value.
Definition: dist_eval.h:154
Tensor that is composed from an argument tensor.
Definition: unary_eval.h:40
UnaryEvalImpl(const arg_type &arg, World &world, const trange_type &trange, const shape_type &shape, const std::shared_ptr< pmap_interface > &pmap, const Perm &perm, const op_type &op)
Constructor.
Definition: unary_eval.h:71
DistEvalImpl_::trange_type trange_type
Tiled range type.
Definition: unary_eval.h:54
DistEvalImpl_::ordinal_type ordinal_type
Ordinal type.
Definition: unary_eval.h:48
DistEvalImpl_::TensorImpl_ TensorImpl_
The base, base class type.
Definition: unary_eval.h:46
eval_trait< value_type >::type eval_type
Tile evaluation type.
Definition: dist_eval.h:62
const std::shared_ptr< pmap_interface > & pmap() const
Tensor process map accessor.
Definition: tensor_impl.h:89
DistEvalImpl_::range_type range_type
Range type.
Definition: unary_eval.h:49
Distributed evaluator implementation object.
Definition: dist_eval.h:46
constexpr auto end(Eigen::Matrix< _Scalar, _Rows, 1, _Options, _MaxRows, 1 > &m)
Definition: eigen.h:51
auto outer(const Permutation &p)
Definition: permutation.h:820
Tensor implementation and base for other tensor implementation objects.
Definition: tensor_impl.h:39
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
ordinal_type perm_index_to_source(ordinal_type index) const
Permute index from a target index to a source index.
Definition: dist_eval.h:94
DistEvalImpl< typename Op::result_type, Policy > DistEvalImpl_
The base class type.
Definition: unary_eval.h:44
TensorImpl_::ordinal_type ordinal_type
Ordinal type.
Definition: dist_eval.h:52
DistEvalImpl_::value_type value_type
Tile type.
Definition: unary_eval.h:55
DistEvalImpl_::pmap_interface pmap_interface
Process map interface type.
Definition: unary_eval.h:52
virtual ~UnaryEvalImpl()
Virtual destructor.
Definition: unary_eval.h:80
TensorImpl_::range_type range_type
Range type this tensor.
Definition: dist_eval.h:56
TensorImpl_::shape_type shape_type
Shape type.
Definition: dist_eval.h:57
TensorImpl_::pmap_interface pmap_interface
process map interface type
Definition: dist_eval.h:59
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
UnaryEvalImpl< Arg, Op, Policy > UnaryEvalImpl_
This object type.
Definition: unary_eval.h:42
Op op_type
Tile evaluation operator type.
Definition: unary_eval.h:58
const madness::uniqueidT & id() const
Unique object id accessor.
Definition: dist_eval.h:133
TensorImpl_::trange_type trange_type
Tiled range type for this object.
Definition: dist_eval.h:54
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82
bool is_local(const Index &i) const
Query for a locally owned tile.
Definition: tensor_impl.h:134
virtual void discard_tile(ordinal_type i) const
Discard a tile that is not needed.
Definition: unary_eval.h:101