TiledArray  0.7.0
blk_tsr_engine.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2015 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  * blk_tsr_engine.h
22  * May 20, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_EXPRESSIONS_BLK_TSR_ENGINE_H__INCLUDED
27 #define TILEDARRAY_EXPRESSIONS_BLK_TSR_ENGINE_H__INCLUDED
28 
31 
32 namespace TiledArray {
33 
34  // Forward declaration
35  template <typename, typename> class DistArray;
36 
37  namespace expressions {
38 
39  // Forward declaration
40  template <typename, bool> class BlkTsrExpr;
41  template <typename, typename> class ScalBlkTsrExpr;
42  template <typename, typename, bool> class BlkTsrEngine;
43  template <typename, typename, typename> class ScalBlkTsrEngine;
44 
45  template <typename Tile, typename Policy, typename Result, bool Alias>
46  struct EngineTrait<BlkTsrEngine<DistArray<Tile, Policy>, Result, Alias> > {
47 
48  // Argument typedefs
50 
51  // Operational typedefs
53  Policy> >::type scalar_type;
54  typedef TiledArray::detail::Shift<Result, typename TiledArray::eval_trait<
55  typename array_type::value_type>::type, (! Alias) ||
62  typedef typename eval_trait<value_type>::type
64  typedef Policy policy;
67 
68  // Meta data typedefs
69  typedef typename policy::size_type size_type;
70  typedef typename policy::trange_type trange_type;
71  typedef typename policy::shape_type shape_type;
72  typedef typename policy::pmap_interface
74 
75  static constexpr bool consumable = true;
76  static constexpr unsigned int leaves = 1;
77  };
78 
79 
80  template <typename Tile, typename Policy, typename Scalar, typename Result>
81  struct EngineTrait<ScalBlkTsrEngine<DistArray<Tile, Policy>, Scalar, Result> >
82  {
83  // Argument typedefs
85 
86  // Operational typedefs
87  typedef Scalar scalar_type;
88  typedef TiledArray::detail::ScalShift<Result,
97  typedef typename eval_trait<value_type>::type
99  typedef Policy policy;
102 
103  // Meta data typedefs
104  typedef typename policy::size_type size_type;
105  typedef typename policy::trange_type trange_type;
106  typedef typename policy::shape_type shape_type;
107  typedef typename policy::pmap_interface
109 
110  static constexpr bool consumable = true;
111  static constexpr unsigned int leaves = 1;
112  };
113 
115 
117  template <typename Derived>
118  class BlkTsrEngineBase : public LeafEngine<Derived> {
119  public:
120  // Class hierarchy typedefs
123  typedef typename LeafEngine_::ExprEngine_
125 
126  // Argument typedefs
127  typedef typename EngineTrait<Derived>::array_type
129 
130  // Operational typedefs
131  typedef typename EngineTrait<Derived>::value_type
133  typedef typename EngineTrait<Derived>::op_type
135  typedef typename EngineTrait<Derived>::policy
137  typedef typename EngineTrait<Derived>::dist_eval_type
139 
140  // Meta data typedefs
141  typedef typename EngineTrait<Derived>::size_type
143  typedef typename EngineTrait<Derived>::trange_type
145  typedef typename EngineTrait<Derived>::shape_type
147  typedef typename EngineTrait<Derived>::pmap_interface
149 
150  protected:
151 
152  // Import base class variables to this scope
153  using ExprEngine_::world_;
154  using ExprEngine_::vars_;
155  using ExprEngine_::perm_;
156  using ExprEngine_::trange_;
157  using ExprEngine_::shape_;
158  using ExprEngine_::pmap_;
160  using LeafEngine_::array_;
161 
162  std::vector<std::size_t> lower_bound_;
163  std::vector<std::size_t> upper_bound_;
164 
165  public:
166 
167  template <typename Array, bool Alias>
169  LeafEngine_(expr),
170  lower_bound_(expr.lower_bound()), upper_bound_(expr.upper_bound())
171  { }
172 
173  template <typename Array, typename Scalar>
175  LeafEngine_(expr),
176  lower_bound_(expr.lower_bound()), upper_bound_(expr.upper_bound())
177  { }
178 
179 
181 
184  const unsigned int rank = array_.trange().tiles_range().rank();
185 
186  std::vector<TiledRange1> trange_data;
187  trange_data.reserve(rank);
188  std::vector<std::size_t> trange1_data;
189 
190  // Get temporary data pointers
191  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
192  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
193  const auto* MADNESS_RESTRICT const upper = upper_bound_.data();
194 
195  for(unsigned int d = 0u; d < rank; ++d) {
196  // Copy the tiling for the block
197  const auto lower_d = lower[d];
198  const auto upper_d = upper[d];
199 
200  // Copy and shift the tiling for the block
201  auto i = lower_d;
202  const auto base_d = trange[d].tile(i).first;
203  trange1_data.emplace_back(0ul);
204  for(; i < upper_d; ++i)
205  trange1_data.emplace_back(trange[d].tile(i).second - base_d);
206 
207  // Add the trange1 to the tiled range data
208  trange_data.emplace_back(trange1_data.begin(), trange1_data.end());
209  trange1_data.resize(0ul);
210  }
211 
212  return TiledRange(trange_data.begin(), trange_data.end());
213  }
214 
215 
217 
220  const unsigned int rank = array_.trange().tiles_range().rank();
221 
222  std::vector<TiledRange1> trange_data;
223  trange_data.reserve(rank);
224  std::vector<std::size_t> trange1_data;
225 
226  // Get temporary data pointers
227  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
228  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
229  const auto* MADNESS_RESTRICT const upper = upper_bound_.data();
230 
231  // Construct the inverse permutation
232  const Permutation inv_perm = -perm;
233  for(unsigned int d = 0u; d < rank; ++d) {
234  const auto inv_perm_d = inv_perm[d];
235 
236  // Copy the tiling for the block
237  const auto lower_i = lower[inv_perm_d];
238  const auto upper_i = upper[inv_perm_d];
239 
240  // Copy, shift, and permute the tiling of the block
241  auto i = lower_i;
242  const auto base_d = trange[inv_perm_d].tile(i).first;
243  trange1_data.emplace_back(0ul);
244  for(; i < upper_i; ++i)
245  trange1_data.emplace_back(trange[inv_perm_d].tile(i).second - base_d);
246 
247  // Add the trange1 to the tiled range data
248  trange_data.emplace_back(trange1_data.begin(), trange1_data.end());
249  trange1_data.resize(0ul);
250  }
251 
252  return TiledRange(trange_data.begin(), trange_data.end());
253  }
254 
255 
257  const std::shared_ptr<pmap_interface>& pmap)
258  {
260  (pmap ? pmap : policy::default_pmap(*world, trange_.tiles_range().volume())));
261  }
262 
265  // Define the distributed evaluator implementation type
267 
269  std::shared_ptr<impl_type> pimpl = std::make_shared<impl_type>(
272 
273  return dist_eval_type(pimpl);
274  }
275 
277 
279  std::string make_tag() const {
280  std::stringstream ss;
281  ss << "[Block ";
283  ss << " - ";
285  ss << "] ";
286  return ss.str();
287  }
288 
289  }; // class BlkTsrEngineBase
290 
291 
293 
298  template <typename Array, typename Result, bool Alias>
299  class BlkTsrEngine : public BlkTsrEngineBase<BlkTsrEngine<Array, Result, Alias> > {
300  public:
301  // Class hierarchy typedefs
302  typedef BlkTsrEngine<Array, Result, Alias>
306  typedef typename BlkTsrEngineBase_::LeafEngine_
308  typedef typename LeafEngine_::ExprEngine_
310 
311  // Argument typedefs
314 
315  // Operational typedefs
320  typedef typename EngineTrait<BlkTsrEngine_>::op_type
322  typedef typename EngineTrait<BlkTsrEngine_>::policy
326 
327  // Meta data typedefs
336 
337  protected:
338 
339  // Import base class variables to this scope
340  using ExprEngine_::world_;
341  using ExprEngine_::vars_;
342  using ExprEngine_::perm_;
343  using ExprEngine_::trange_;
344  using ExprEngine_::shape_;
345  using ExprEngine_::pmap_;
347  using LeafEngine_::array_;
350 
351  public:
352 
353  template <typename A>
355  BlkTsrEngineBase_(expr)
356  { }
357 
359 
362  return array_.shape().block(lower_bound_, upper_bound_);
363  }
364 
366 
370  return array_.shape().block(lower_bound_, upper_bound_, perm);
371  }
372 
374 
377  const unsigned int rank = trange_.tiles_range().rank();
378 
379  // Construct and allocate memory for the shift range
380  std::vector<long> range_shift;
381  range_shift.reserve(rank);
382 
383  // Get temporary data pointers
384  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
385  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
386 
387  // Initialize the range shift vector
388  for(unsigned int d = 0u; d < rank; ++d) {
389  const auto lower_d = lower[d];
390  const auto base_d = trange[d].tile(lower_d).first;
391  range_shift.emplace_back(-base_d);
392  }
393 
394  return op_type(op_base_type(range_shift));
395  }
396 
398 
402  const unsigned int rank = trange_.tiles_range().rank();
403 
404  // Construct and allocate memory for the shift range
405  std::vector<long> range_shift(rank, 0l);
406 
407  // Get temporary data pointers
408  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
409  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
410 
411  // Initialize the permuted range shift vector
412  for(unsigned int d = 0u; d < rank; ++d) {
413  const auto perm_d = perm[d];
414  const auto lower_d = lower[d];
415  const auto base_d = trange[d].tile(lower_d).first;
416  range_shift[perm_d] = -base_d;
417  }
418 
419  return op_type(op_base_type(range_shift), perm);
420  }
421 
422 
424 
426  std::string make_tag() const {
427  std::stringstream ss;
428  ss << "[block] ";
429  return BlkTsrEngineBase_::make_tag() + ss.str();
430  }
431 
432  }; // class BlkTsrEngine
433 
434 
435 
437 
441  template <typename Array, typename Scalar, typename Result>
442  class ScalBlkTsrEngine :
443  public BlkTsrEngineBase<ScalBlkTsrEngine<Array, Scalar, Result> >
444  {
445  public:
446  // Class hierarchy typedefs
447  typedef ScalBlkTsrEngine<Array, Scalar, Result>
451  typedef typename BlkTsrEngineBase_::LeafEngine_
453  typedef typename LeafEngine_::ExprEngine_
455 
456  // Argument typedefs
459 
460  // Operational typedefs
467 
468  // Meta data typedefs
473 
474  protected:
475 
476  // Import base class variables to this scope
477  using ExprEngine_::world_;
478  using ExprEngine_::vars_;
479  using ExprEngine_::perm_;
480  using ExprEngine_::trange_;
481  using ExprEngine_::shape_;
482  using ExprEngine_::pmap_;
484  using LeafEngine_::array_;
487 
489 
490  public:
491 
492  template <typename A, typename S>
494  BlkTsrEngineBase_(expr), factor_(expr.factor())
495  { }
496 
498 
501  return array_.shape().block(lower_bound_, upper_bound_, factor_);
502  }
503 
505 
508  shape_type
510  return array_.shape().block(lower_bound_, upper_bound_, factor_, perm);
511  }
512 
514 
517  const unsigned int rank = trange_.tiles_range().rank();
518 
519  // Construct and allocate memory for the shift range
520  std::vector<long> range_shift;
521  range_shift.reserve(rank);
522 
523  // Get temporary data pointers
524  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
525  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
526 
527  // Construct the inverse permutation
528  for(unsigned int d = 0u; d < rank; ++d) {
529  const auto lower_d = lower[d];
530  const auto base_d = trange[d].tile(lower_d).first;
531  range_shift.emplace_back(-base_d);
532  }
533 
534  return op_type(op_base_type(range_shift, factor_));
535  }
536 
538 
542  const unsigned int rank = trange_.tiles_range().rank();
543 
544  // Construct and allocate memory for the shift range
545  std::vector<long> range_shift(rank, 0l);
546 
547  // Get temporary data pointers
548  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
549  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
550 
551  // Initialize the permuted range shift vector
552  for(unsigned int d = 0u; d < rank; ++d) {
553  const auto perm_d = perm[d];
554  const auto lower_d = lower[d];
555  const auto base_d = trange[d].tile(lower_d).first;
556  range_shift[perm_d] = -base_d;
557  }
558 
559  return op_type(op_base_type(range_shift, factor_), perm);
560  }
561 
562 
564 
566  std::string make_tag() const {
567  std::stringstream ss;
568  ss << "[block] [" << factor_ << "] ";
569  return BlkTsrEngineBase_::make_tag() + ss.str();
570  }
571 
572  }; // class ScalBlkTsrEngine
573 
574 
575  } // namespace expressions
576 } // namespace TiledArray
577 
578 #endif // TILEDARRAY_EXPRESSIONS_BLK_TSR_ENGINE_H__INCLUDED
TiledArray::detail::LazyArrayTile< typename array_type::value_type, op_type > value_type
Tile type.
trange_type make_trange(const Permutation &perm) const
Permuting tiled range factory function.
EngineTrait< ScalBlkTsrEngine_ >::value_type value_type
Tensor value type.
BlkTsrEngineBase< ScalBlkTsrEngine_ > BlkTsrEngineBase_
Block tensor base class type.
EngineTrait< BlkTsrEngine_ >::size_type size_type
Size type.
Scaled tensor block expression engine.
EngineTrait< Derived >::pmap_interface pmap_interface
Process map interface type.
LeafEngine_::ExprEngine_ ExprEngine_
Expression engine base class.
const trange_type & trange() const
Tiled range accessor.
Definition: expr_engine.h:209
EngineTrait< ScalBlkTsrEngine_ >::pmap_interface pmap_interface
Process map interface type.
bool permute_tiles_
Result tile permutation flag (true == permute tile)
Definition: expr_engine.h:65
void init_distribution(World *world, const std::shared_ptr< pmap_interface > &pmap)
const Permutation & perm() const
Permutation accessor.
Definition: expr_engine.h:204
EngineTrait< Derived >::value_type value_type
Tensor value type.
BlkTsrEngineBase_::LeafEngine_ LeafEngine_
Leaf base class type.
Determine the object type used in the evaluation of tensor expressions.
Definition: type_traits.h:296
TiledArray::detail::UnaryWrapper< op_base_type > op_type
The tile operation.
EngineTrait< ScalBlkTsrEngine_ >::scalar_type scalar_type
Tile scalar type.
BlkTsrEngineBase(const ScalBlkTsrExpr< Array, Scalar > &expr)
BlkTsrEngine(const BlkTsrExpr< A, Alias > &expr)
Tensor expression engine.
BlkTsrEngineBase< BlkTsrEngine_ > BlkTsrEngineBase_
Block tensor base class type.
EngineTrait< Derived >::shape_type shape_type
Tensor shape type.
trange_type make_trange() const
Non-permuting tiled range factory function.
shape_type make_shape()
Non-permuting shape factory function.
LeafEngine_::ExprEngine_ ExprEngine_
Expression engine base class.
World * world() const
World accessor.
Definition: expr_engine.h:194
std::string make_tag() const
Expression identification tag.
EngineTrait< ScalBlkTsrEngine_ >::policy policy
The result policy type.
LeafEngine< Derived > LeafEngine_
Leaf base class type.
op_type make_op() const
Tile operation factory function.
Definition: expr_engine.h:178
EngineTrait< BlkTsrEngine_ >::value_type value_type
Tensor value type.
EngineTrait< BlkTsrEngine_ >::op_type op_type
Tile operation type.
op_type make_tile_op(const Permutation &perm) const
Permuting tile operation factory function.
array_type array_
The array object.
Definition: leaf_engine.h:74
Unary tile operation wrapper.
Definition: unary_wrapper.h:82
EngineTrait< BlkTsrEngine_ >::policy policy
The result policy type.
Type trait for extracting the scalar type of tensors and arrays.
Definition: type_traits.h:539
shape_type make_shape(const Permutation &perm)
Permuting shape factory function.
EngineTrait< ScalBlkTsrEngine_ >::trange_type trange_type
Tiled range type type.
const std::shared_ptr< pmap_interface > & pmap() const
Process map accessor.
Definition: expr_engine.h:219
shape_type make_shape()
Non-permuting shape factory function.
VariableList vars_
The variable list of this expression.
Definition: expr_engine.h:64
EngineTrait< ScalBlkTsrEngine_ >::dist_eval_type dist_eval_type
This expression&#39;s distributed evaluator type.
EngineTrait< BlkTsrEngine_ >::dist_eval_type dist_eval_type
This expression&#39;s distributed evaluator type.
Tile shift operation.
Definition: shift.h:46
BlkTsrEngine< Array, Result, Alias > BlkTsrEngine_
This class type.
EngineTrait< ScalBlkTsrEngine_ >::size_type size_type
Size type.
Lazy tile for on-the-fly evaluation of array tiles.
Definition: array_eval.h:42
EngineTrait< BlkTsrEngine_ >::pmap_interface pmap_interface
Process map interface type.
TiledArray::detail::UnaryWrapper< op_base_type > op_type
The tile operation.
ScalBlkTsrEngine< Array, Scalar, Result > ScalBlkTsrEngine_
This class type.
Tensor expression object.
Definition: dist_eval.h:241
EngineTrait< BlkTsrEngine_ >::trange_type trange_type
Tiled range type type.
EngineTrait< Derived >::trange_type trange_type
Tiled range type type.
void print_array(std::ostream &out, const A &a, const std::size_t n)
Print the content of an array like object.
Definition: utility.h:153
Forward declarations.
Definition: clone.h:32
std::string make_tag() const
Expression identification tag.
std::string make_tag() const
Expression identification tag.
EngineTrait< Derived >::op_type op_type
Tile operation type.
LeafEngine_::ExprEngine_ ExprEngine_
Expression engine base class.
std::vector< std::size_t > upper_bound_
Upper bound of the tile block.
EngineTrait< Derived >::size_type size_type
Size type.
EngineTrait< BlkTsrEngine_ >::shape_type shape_type
Tensor shape type.
std::shared_ptr< pmap_interface > pmap_
The process map for the result tensor.
Definition: expr_engine.h:69
Range data of a tiled array.
Definition: tiled_range.h:31
TiledArray::detail::DistEval< value_type, policy > dist_eval_type
The distributed evaluator type.
EngineTrait< ScalBlkTsrEngine_ >::shape_type shape_type
Tensor shape type.
World * world_
The world where this expression will be evaluated.
Definition: expr_engine.h:63
op_type make_tile_op() const
Non-permuting tile operation factory function.
op_type make_tile_op() const
Non-permuting tile operation factory function.
BlkTsrEngineBase(const BlkTsrExpr< Array, Alias > &expr)
EngineTrait< Derived >::dist_eval_type dist_eval_type
This expression&#39;s distributed evaluator type.
EngineTrait< Derived >::policy policy
The result policy type.
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
EngineTrait< ScalBlkTsrEngine_ >::op_type op_type
Tile operation type.
op_type make_tile_op(const Permutation &perm) const
Permuting tile operation factory function.
EngineTrait< BlkTsrEngine_ >::array_type array_type
The input array type.
Permutation perm_
The permutation that will be applied to the result.
Definition: expr_engine.h:66
Distributed evaluator for TiledArray::Array objects.
Definition: array_eval.h:138
TiledArray::detail::ScalShift< Result, typename TiledArray::eval_trait< typename array_type::value_type >::type, scalar_type, TiledArray::eval_trait< typename array_type::value_type >::is_consumable > op_base_type
The base tile operation.
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
TiledArray::detail::scalar_type< DistArray< Tile, Policy > >::type scalar_type
Tile shift operation.
Definition: shift.h:159
TiledArray::detail::DistEval< value_type, policy > dist_eval_type
The distributed evaluator type.
EngineTrait< ScalBlkTsrEngine_ >::op_base_type op_base_type
Tile base operation type.
EngineTrait< Derived >::array_type array_type
The input array type.
ScalBlkTsrEngine(const ScalBlkTsrExpr< A, S > &expr)
BlkTsrEngineBase< Derived > BlkTsrEngineBase_
This class type.
EngineTrait< ScalBlkTsrEngine_ >::array_type array_type
The input array type.
BlkTsrEngineBase_::LeafEngine_ LeafEngine_
Leaf base class type.
dist_eval_type make_dist_eval() const
Construct the distributed evaluator for array.
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:80
trange_type trange_
The tiled range of the result tensor.
Definition: expr_engine.h:67
TiledArray::detail::LazyArrayTile< typename array_type::value_type, op_type > value_type
Tile type.
shape_type make_shape(const Permutation &perm)
Permuting shape factory function.
std::vector< std::size_t > lower_bound_
Lower bound of the tile block.
EngineTrait< BlkTsrEngine_ >::op_base_type op_base_type
Tile base operation type.
Leaf expression engine.
Definition: leaf_engine.h:39