TiledArray  0.7.0
binary_engine.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2014 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  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * binary_engine.h
22  * Mar 31, 2014
23  *
24  */
25 
26 #ifndef TILEDARRAY_EXPRESSIONS_BINARY_ENGINE_H__INCLUDED
27 #define TILEDARRAY_EXPRESSIONS_BINARY_ENGINE_H__INCLUDED
28 
31 
32 namespace TiledArray {
33  namespace expressions {
34 
35  // Forward declarations
36  template <typename> class BinaryExpr;
37  template <typename> class BinaryEngine;
38 
39  template <typename Derived>
40  class BinaryEngine : public ExprEngine<Derived> {
41  public:
42  // Class hierarchy typedefs
45 
46  // Argument typedefs
49 
50  // Operational typedefs
55 
56  // Meta data typedefs
61 
62 
64  static constexpr unsigned int leaves = EngineTrait<Derived>::leaves;
65 
66  protected:
67 
68  // Import base class variables to this scope
69  using ExprEngine_::world_;
70  using ExprEngine_::vars_;
71  using ExprEngine_::perm_;
73  using ExprEngine_::shape_;
74  using ExprEngine_::pmap_;
76 
79 
80  public:
81 
82  template <typename D>
83  BinaryEngine(const BinaryExpr<D>& expr) :
84  ExprEngine_(expr), left_(expr.left()), right_(expr.right())
85  { }
86 
88 
94  void perm_vars(const VariableList& target_vars) {
96  TA_ASSERT(left_.vars().dim() == target_vars.dim());
97  TA_ASSERT(right_.vars().dim() == target_vars.dim());
98 
99  // Determine the equality of the variable lists
100  bool left_target = true, right_target = true, left_right = true;
101  for(unsigned int i = 0u; i < target_vars.dim(); ++i) {
102  left_target = left_target && left_.vars()[i] == target_vars[i];
103  right_target = right_target && right_.vars()[i] == target_vars[i];
104  left_right = left_right && left_.vars()[i] == right_.vars()[i];
105  }
106 
107  if(left_right) {
108  vars_ = left_.vars();
109  } else {
110  // Determine which argument will be permuted
111  const bool perm_left = (right_target || ((! (left_target || right_target))
112  && (left_type::leaves <= right_type::leaves)));
113 
114  if(perm_left) {
115  vars_ = right_.vars();
116  left_.perm_vars(right_.vars());
117  } else {
118  vars_ = left_.vars();
119  right_.perm_vars(left_.vars());
120  }
121  }
122  }
123 
125 
127  void init_vars(const VariableList& target_vars) {
128  left_.init_vars(target_vars);
129  right_.init_vars(target_vars);
130  perm_vars(target_vars);
131  }
132 
133 
135  void init_vars() {
136  if(left_type::leaves <= right_type::leaves) {
137  left_.init_vars();
138  vars_ = left_.vars();
139  right_.init_vars(vars_);
140  } else {
141  right_.init_vars();
142  vars_ = right_.vars();
143  left_.init_vars(vars_);
144  }
145  }
146 
148 
152  void init_struct(const VariableList& target_vars) {
153  left_.init_struct(ExprEngine_::vars());
154  right_.init_struct(ExprEngine_::vars());
155 #ifndef NDEBUG
156  if(left_.trange() != right_.trange()) {
157  if(TiledArray::get_default_world().rank() == 0) {
159  "The TiledRanges of the left- and right-hand arguments of the " \
160  "binary operation are not equal:" \
161  << "\n left = " << left_.trange() \
162  << "\n right = " << right_.trange() );
163  }
164 
165  TA_EXCEPTION("The TiledRanges of the left- and right-hand arguments " \
166  "of the binary operation are not equal.");
167  }
168 #endif // NDEBUG
169  ExprEngine_::init_struct(target_vars);
170  }
171 
173 
179  const std::shared_ptr<pmap_interface>& pmap)
180  {
181  left_.init_distribution(world, pmap);
182  right_.init_distribution(world, left_.pmap());
184  }
185 
187 
189  trange_type make_trange() const { return left_.trange(); }
190 
192 
196  return perm * left_.trange();
197  }
198 
200 
203  typedef TiledArray::detail::BinaryEvalImpl<typename left_type::dist_eval_type,
204  typename right_type::dist_eval_type, op_type, policy> impl_type;
205 
206  // Construct left and right distributed evaluators
207  const typename left_type::dist_eval_type left = left_.make_dist_eval();
208  const typename right_type::dist_eval_type right = right_.make_dist_eval();
209 
210  // Construct the distributed evaluator type
211  std::shared_ptr<impl_type> pimpl =
212  std::make_shared<impl_type>(left, right, *world_, trange_, shape_,
214 
215  return dist_eval_type(pimpl);
216  }
217 
219 
222  void print(ExprOStream os, const VariableList& target_vars) const {
223  ExprEngine_::print(os, target_vars);
224  os.inc();
225  left_.print(os, vars_);
226  right_.print(os, vars_);
227  os.dec();
228  }
229  }; // class BinaryEngine
230 
231  } // namespace expressions
232 } // namespace TiledArray
233 
234 #endif // TILEDARRAY_EXPRESSIONS_BINARY_ENGINE_H__INCLUDED
dist_eval_type make_dist_eval() const
Construct the distributed evaluator for this expression.
right_type right_
The right-hand argument.
Definition: binary_engine.h:78
bool permute_tiles_
Result tile permutation flag (true == permute tile)
Definition: expr_engine.h:65
void init_struct(const VariableList &target_vars)
Initialize result tensor structure.
const Permutation & perm() const
Permutation accessor.
Definition: expr_engine.h:204
BinaryEngine< Derived > BinaryEngine_
This class type.
Definition: binary_engine.h:43
EngineTrait< Derived >::shape_type shape_type
Shape type.
Definition: binary_engine.h:59
EngineTrait< Derived >::policy policy
The result policy type.
Definition: binary_engine.h:53
World * world() const
World accessor.
Definition: expr_engine.h:194
op_type make_op() const
Tile operation factory function.
Definition: expr_engine.h:178
EngineTrait< Derived >::op_type op_type
The tile operation type.
Definition: binary_engine.h:52
void init_distribution(World *world, const std::shared_ptr< pmap_interface > &pmap)
Initialize result tensor distribution.
const std::shared_ptr< pmap_interface > & pmap() const
Process map accessor.
Definition: expr_engine.h:219
VariableList vars_
The variable list of this expression.
Definition: expr_engine.h:64
static constexpr unsigned int leaves
Definition: binary_engine.h:64
EngineTrait< Derived >::size_type size_type
Size type.
Definition: binary_engine.h:57
EngineTrait< Derived >::trange_type trange_type
Tiled range type.
Definition: binary_engine.h:58
EngineTrait< Derived >::dist_eval_type dist_eval_type
The distributed evaluator type.
Definition: binary_engine.h:54
unsigned int dim() const
Returns the number of strings in the variable list.
const VariableList & vars() const
Variable list accessor.
Definition: expr_engine.h:199
void init_struct(const VariableList &target_vars)
Initialize result tensor structure.
Definition: expr_engine.h:130
EngineTrait< Derived >::pmap_interface pmap_interface
Process map interface type.
Definition: binary_engine.h:60
void init_vars()
Initialize the variable list of this expression.
BinaryEngine(const BinaryExpr< D > &expr)
Definition: binary_engine.h:83
Binary expression object.
Definition: binary_engine.h:36
Variable list manages a list variable strings.
#define TA_ASSERT(a)
Definition: error.h:107
void perm_vars(const VariableList &target_vars)
Set the variable list for this expression.
Definition: binary_engine.h:94
std::shared_ptr< pmap_interface > pmap_
The process map for the result tensor.
Definition: expr_engine.h:69
World * world_
The world where this expression will be evaluated.
Definition: expr_engine.h:63
void inc()
Increment the number of tabs.
Definition: expr_trace.h:70
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
trange_type make_trange() const
Non-permuting tiled range factory function.
#define TA_EXCEPTION(m)
Definition: error.h:72
EngineTrait< Derived >::left_type left_type
The left-hand expression type.
Definition: binary_engine.h:47
Binary, distributed tensor evaluator.
Definition: binary_eval.h:38
Permutation perm_
The permutation that will be applied to the result.
Definition: expr_engine.h:66
ExprEngine< Derived > ExprEngine_
Base class type.
Definition: binary_engine.h:44
void init_distribution(World *world, const std::shared_ptr< pmap_interface > &pmap)
Initialize result tensor distribution.
Definition: expr_engine.h:151
shape_type shape_
The shape of the result tensor.
Definition: expr_engine.h:68
Expression output stream.
Definition: expr_trace.h:39
trange_type make_trange(const Permutation &perm) const
Permuting tiled range factory function.
EngineTrait< Derived >::value_type value_type
The result tile type.
Definition: binary_engine.h:51
trange_type trange_
The tiled range of the result tensor.
Definition: expr_engine.h:67
#define TA_USER_ERROR_MESSAGE(m)
Definition: error.h:118
left_type left_
The left-hand argument.
Definition: binary_engine.h:77
void print(ExprOStream os, const VariableList &target_vars) const
Expression print.
void print(ExprOStream &os, const VariableList &target_vars) const
Expression print.
Definition: expr_engine.h:230
EngineTrait< Derived >::right_type right_type
The right-hand expression type.
Definition: binary_engine.h:48
void dec()
Decrement the number of tabs.
Definition: expr_trace.h:73
void init_vars(const VariableList &target_vars)
Initialize the variable list of this expression.