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>
36 class DistArray;
37 
38 namespace expressions {
39 
40 // Forward declaration
41 template <typename, bool>
42 class BlkTsrExpr;
43 template <typename, typename>
44 class ScalBlkTsrExpr;
45 template <typename, typename, bool>
46 class BlkTsrEngine;
47 template <typename, typename, typename>
48 class ScalBlkTsrEngine;
49 
50 template <typename Tile, typename Policy, typename Result, bool Alias>
51 struct EngineTrait<BlkTsrEngine<DistArray<Tile, Policy>, Result, Alias>> {
52  // Argument typedefs
54 
55  // Operational typedefs
56  typedef
60  Result,
62  (!Alias) || TiledArray::eval_trait<
63  typename array_type::value_type>::is_consumable>
68  op_type>
70  typedef typename eval_trait<value_type>::type
72  typedef Policy policy;
75 
76  // Meta data typedefs
77  typedef typename policy::ordinal_type size_type;
78  typedef typename policy::trange_type trange_type;
79  typedef typename policy::shape_type shape_type;
80  typedef typename policy::pmap_interface
82 
83  static constexpr bool consumable = true;
84  static constexpr unsigned int leaves = 1;
85 };
86 
87 template <typename Tile, typename Policy, typename Scalar, typename Result>
88 struct EngineTrait<ScalBlkTsrEngine<DistArray<Tile, Policy>, Scalar, Result>> {
89  // Argument typedefs
91 
92  // Operational typedefs
93  typedef Scalar scalar_type;
95  Result,
103  op_type>
105  typedef typename eval_trait<value_type>::type
107  typedef Policy policy;
110 
111  // Meta data typedefs
112  typedef typename policy::ordinal_type size_type;
113  typedef typename policy::trange_type trange_type;
114  typedef typename policy::shape_type shape_type;
115  typedef typename policy::pmap_interface
117 
118  static constexpr bool consumable = true;
119  static constexpr unsigned int leaves = 1;
120 };
121 
123 
125 template <typename Derived>
126 class BlkTsrEngineBase : public LeafEngine<Derived> {
127  public:
128  // Class hierarchy typedefs
131  typedef typename LeafEngine_::ExprEngine_
133 
134  // Argument typedefs
135  typedef typename EngineTrait<Derived>::array_type
137 
138  // Operational typedefs
139  typedef typename EngineTrait<Derived>::value_type
141  typedef
143  typedef
145  typedef typename EngineTrait<Derived>::dist_eval_type
147 
148  // Meta data typedefs
150  typedef typename EngineTrait<Derived>::trange_type
152  typedef typename EngineTrait<Derived>::shape_type
154  typedef typename EngineTrait<Derived>::pmap_interface
156 
157  protected:
158  // Import base class variables to this scope
159  using ExprEngine_::indices_;
160  using ExprEngine_::perm_;
162  using ExprEngine_::pmap_;
163  using ExprEngine_::shape_;
164  using ExprEngine_::trange_;
165  using ExprEngine_::world_;
166  using LeafEngine_::array_;
167 
172 
173  public:
174  template <typename Array, bool Alias>
176  : LeafEngine_(expr),
177  lower_bound_(expr.lower_bound()),
178  upper_bound_(expr.upper_bound()) {}
179 
180  template <typename Array, typename Scalar>
182  : LeafEngine_(expr),
183  lower_bound_(expr.lower_bound()),
184  upper_bound_(expr.upper_bound()) {}
185 
187 
190  const unsigned int rank = array_.trange().tiles_range().rank();
191 
192  std::vector<TiledRange1> trange_data;
193  trange_data.reserve(rank);
194  std::vector<std::size_t> trange1_data;
195 
196  // Get temporary data pointers
197  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
198  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
199  const auto* MADNESS_RESTRICT const upper = upper_bound_.data();
200 
201  for (unsigned int d = 0u; d < rank; ++d) {
202  // Copy the tiling for the block
203  const auto lower_d = lower[d];
204  const auto upper_d = upper[d];
205 
206  // Copy and shift the tiling for the block
207  auto i = lower_d;
208  const auto base_d = trange[d].tile(i).first;
209  trange1_data.emplace_back(0ul);
210  for (; i < upper_d; ++i)
211  trange1_data.emplace_back(trange[d].tile(i).second - base_d);
212 
213  // Add the trange1 to the tiled range data
214  trange_data.emplace_back(trange1_data.begin(), trange1_data.end());
215  trange1_data.resize(0ul);
216  }
217 
218  return TiledRange(trange_data.begin(), trange_data.end());
219  }
220 
222 
225  const unsigned int rank = array_.trange().tiles_range().rank();
226 
227  std::vector<TiledRange1> trange_data;
228  trange_data.reserve(rank);
229  std::vector<std::size_t> trange1_data;
230 
231  // Get temporary data pointers
232  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
233  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
234  const auto* MADNESS_RESTRICT const upper = upper_bound_.data();
235 
236  // Construct the inverse permutation
237  const auto inv_perm = -perm;
238  for (unsigned int d = 0u; d < rank; ++d) {
239  const auto inv_perm_d = inv_perm[d];
240 
241  // Copy the tiling for the block
242  const auto lower_i = lower[inv_perm_d];
243  const auto upper_i = upper[inv_perm_d];
244 
245  // Copy, shift, and permute the tiling of the block
246  auto i = lower_i;
247  const auto base_d = trange[inv_perm_d].tile(i).first;
248  trange1_data.emplace_back(0ul);
249  for (; i < upper_i; ++i)
250  trange1_data.emplace_back(trange[inv_perm_d].tile(i).second - base_d);
251 
252  // Add the trange1 to the tiled range data
253  trange_data.emplace_back(trange1_data.begin(), trange1_data.end());
254  trange1_data.resize(0ul);
255  }
256 
257  return TiledRange(trange_data.begin(), trange_data.end());
258  }
259 
261  const std::shared_ptr<pmap_interface>& pmap) {
263  world,
264  (pmap ? pmap
265  : policy::default_pmap(*world, trange_.tiles_range().volume())));
266  }
267 
270  // Define the distributed evaluator implementation type
272  impl_type;
273 
275  std::shared_ptr<impl_type> pimpl = std::make_shared<impl_type>(
278 
279  return dist_eval_type(pimpl);
280  }
281 
283 
285  std::string make_tag() const {
286  std::stringstream ss;
287  ss << "[Block ";
289  ss << " - ";
291  ss << "] ";
292  return ss.str();
293  }
294 
295 }; // class BlkTsrEngineBase
296 
298 
303 template <typename Array, typename Result, bool Alias>
305  : public BlkTsrEngineBase<BlkTsrEngine<Array, Result, Alias>> {
306  public:
307  // Class hierarchy typedefs
312  typedef typename BlkTsrEngineBase_::LeafEngine_
314  typedef typename LeafEngine_::ExprEngine_
316 
317  // Argument typedefs
320 
321  // Operational typedefs
326  typedef typename EngineTrait<BlkTsrEngine_>::op_type
328  typedef typename EngineTrait<BlkTsrEngine_>::policy
332 
333  // Meta data typedefs
334  typedef
342 
343  protected:
344  // Import base class variables to this scope
347  using ExprEngine_::indices_;
348  using ExprEngine_::perm_;
350  using ExprEngine_::pmap_;
351  using ExprEngine_::shape_;
352  using ExprEngine_::trange_;
353  using ExprEngine_::world_;
354  using LeafEngine_::array_;
355 
356  public:
357  template <typename A>
359 
361 
364  return array_.shape().block(lower_bound_, upper_bound_);
365  }
366 
368 
372  return array_.shape().block(lower_bound_, upper_bound_, perm);
373  }
374 
376 
379  const unsigned int rank = trange_.tiles_range().rank();
380 
381  // Construct and allocate memory for the shift range
382  std::vector<long> range_shift;
383  range_shift.reserve(rank);
384 
385  // Get temporary data pointers
386  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
387  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
388 
389  // Initialize the range shift vector
390  for (unsigned int d = 0u; d < rank; ++d) {
391  const auto lower_d = lower[d];
392  const auto base_d = trange[d].tile(lower_d).first;
393  range_shift.emplace_back(-base_d);
394  }
395 
396  return op_type(op_base_type(range_shift));
397  }
398 
400 
403  template <typename Perm, typename = std::enable_if_t<
404  TiledArray::detail::is_permutation_v<Perm>>>
405  op_type make_tile_op(const Perm& perm) const {
406  const unsigned int rank = trange_.tiles_range().rank();
407 
408  // Construct and allocate memory for the shift range
409  std::vector<long> range_shift(rank, 0l);
410 
411  // Get temporary data pointers
412  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
413  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
414 
415  // Initialize the permuted range shift vector
416  auto outer_perm = outer(perm);
417  TA_ASSERT(outer_perm.size() == rank);
418  for (unsigned int d = 0u; d < rank; ++d) {
419  const auto perm_d = outer_perm[d];
420  const auto lower_d = lower[d];
421  const auto base_d = trange[d].tile(lower_d).first;
422  range_shift[perm_d] = -base_d;
423  }
424 
425  return op_type(op_base_type(range_shift), perm);
426  }
427 
429 
431  std::string make_tag() const {
432  std::stringstream ss;
433  ss << "[block] ";
434  return BlkTsrEngineBase_::make_tag() + ss.str();
435  }
436 
437 }; // class BlkTsrEngine
438 
440 
444 template <typename Array, typename Scalar, typename Result>
446  : public BlkTsrEngineBase<ScalBlkTsrEngine<Array, Scalar, Result>> {
447  public:
448  // Class hierarchy typedefs
453  typedef typename BlkTsrEngineBase_::LeafEngine_
455  typedef typename LeafEngine_::ExprEngine_
457 
458  // Argument typedefs
461 
462  // Operational typedefs
475 
476  // Meta data typedefs
485 
486  protected:
487  // Import base class variables to this scope
490  using ExprEngine_::indices_;
491  using ExprEngine_::perm_;
493  using ExprEngine_::pmap_;
494  using ExprEngine_::shape_;
495  using ExprEngine_::trange_;
496  using ExprEngine_::world_;
497  using LeafEngine_::array_;
498 
500 
501  public:
502  template <typename A, typename S>
504  : BlkTsrEngineBase_(expr), factor_(expr.factor()) {}
505 
507 
510  return array_.shape().block(lower_bound_, upper_bound_, factor_);
511  }
512 
514 
518  return array_.shape().block(lower_bound_, upper_bound_, factor_, perm);
519  }
520 
522 
525  const unsigned int rank = trange_.tiles_range().rank();
526 
527  // Construct and allocate memory for the shift range
528  std::vector<long> range_shift;
529  range_shift.reserve(rank);
530 
531  // Get temporary data pointers
532  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
533  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
534 
535  // Construct the inverse permutation
536  for (unsigned int d = 0u; d < rank; ++d) {
537  const auto lower_d = lower[d];
538  const auto base_d = trange[d].tile(lower_d).first;
539  range_shift.emplace_back(-base_d);
540  }
541 
542  return op_type(op_base_type(range_shift, factor_));
543  }
544 
546 
549  template <typename Perm, typename = std::enable_if_t<
550  TiledArray::detail::is_permutation_v<Perm>>>
551  op_type make_tile_op(const Perm& perm) const {
552  const unsigned int rank = trange_.tiles_range().rank();
553 
554  // Construct and allocate memory for the shift range
555  std::vector<long> range_shift(rank, 0l);
556 
557  // Get temporary data pointers
558  const auto* MADNESS_RESTRICT const trange = array_.trange().data().data();
559  const auto* MADNESS_RESTRICT const lower = lower_bound_.data();
560 
561  // Initialize the permuted range shift vector
562  auto outer_perm = outer(perm);
563  TA_ASSERT(outer_perm.size() == rank);
564  for (unsigned int d = 0u; d < rank; ++d) {
565  const auto perm_d = outer_perm[d];
566  const auto lower_d = lower[d];
567  const auto base_d = trange[d].tile(lower_d).first;
568  range_shift[perm_d] = -base_d;
569  }
570 
571  return op_type(op_base_type(range_shift, factor_), perm);
572  }
573 
575 
577  std::string make_tag() const {
578  std::stringstream ss;
579  ss << "[block] [" << factor_ << "] ";
580  return BlkTsrEngineBase_::make_tag() + ss.str();
581  }
582 
583 }; // class ScalBlkTsrEngine
584 
585 } // namespace expressions
586 } // namespace TiledArray
587 
588 #endif // TILEDARRAY_EXPRESSIONS_BLK_TSR_ENGINE_H__INCLUDED
EngineTrait< BlkTsrEngine_ >::trange_type trange_type
Tiled range type type.
array_type array_
The array object.
Definition: leaf_engine.h:81
boost::container::small_vector< T, N > svector
Definition: vector.h:43
EngineTrait< ScalBlkTsrEngine_ >::shape_type shape_type
Tensor shape type.
EngineTrait< BlkTsrEngine_ >::pmap_interface pmap_interface
Process map interface type.
std::string make_tag() const
Expression identification tag.
EngineTrait< Derived >::op_type op_type
Tile operation type.
EngineTrait< BlkTsrEngine_ >::dist_eval_type dist_eval_type
This expression's distributed evaluator type.
World * world_
The world where this expression will be evaluated.
Definition: expr_engine.h:73
World * world() const
World accessor.
Definition: expr_engine.h:219
op_type make_tile_op(const Perm &perm) const
Permuting tile operation factory function.
EngineTrait< ScalBlkTsrEngine_ >::pmap_interface pmap_interface
Process map interface type.
array_type array_
The array object.
Definition: leaf_engine.h:81
TiledArray::detail::LazyArrayTile< typename array_type::value_type, op_type > value_type
Tile type.
shape_type shape_
The shape of the result tensor.
Definition: expr_engine.h:82
ScalBlkTsrEngine(const ScalBlkTsrExpr< A, S > &expr)
trange_type trange_
The tiled range of the result tensor.
Definition: expr_engine.h:81
trange_type trange_
The tiled range of the result tensor.
Definition: expr_engine.h:81
trange_type trange_
The tiled range of the result tensor.
Definition: expr_engine.h:81
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:130
BlkTsrEngine(const BlkTsrExpr< A, Alias > &expr)
Type trait for extracting the scalar type of tensors and arrays.
Definition: type_traits.h:744
auto outer(const IndexList &p)
Definition: index_list.h:879
op_type make_tile_op() const
Non-permuting tile operation factory function.
std::shared_ptr< pmap_interface > pmap_
The process map for the result tensor.
Definition: expr_engine.h:84
BlkTsrEngineBase(const ScalBlkTsrExpr< Array, Scalar > &expr)
Distributed evaluator for TiledArray::Array objects.
Definition: array_eval.h:173
array_type array_
The array object.
Definition: leaf_engine.h:81
EngineTrait< Derived >::array_type array_type
The input array type.
World * world_
The world where this expression will be evaluated.
Definition: expr_engine.h:73
trange_type make_trange(const Permutation &perm) const
Permuting tiled range factory function.
LeafEngine_::ExprEngine_ ExprEngine_
Expression engine base class.
EngineTrait< Derived >::trange_type trange_type
Tiled range type type.
EngineTrait< BlkTsrEngine_ >::op_type op_type
Tile operation type.
std::string make_tag() const
Expression identification tag.
shape_type make_shape()
Non-permuting shape factory function.
EngineTrait< BlkTsrEngine_ >::array_type array_type
The input array type.
TiledArray::detail::scalar_type< DistArray< Tile, Policy > >::type scalar_type
ScalBlkTsrEngine< Array, Scalar, Result > ScalBlkTsrEngine_
This class type.
Lazy tile for on-the-fly evaluation of array tiles.
Definition: array_eval.h:42
EngineTrait< Derived >::policy policy
The result policy type.
EngineTrait< ScalBlkTsrEngine_ >::dist_eval_type dist_eval_type
This expression's distributed evaluator type.
BlkTsrEngine< Array, Result, Alias > BlkTsrEngine_
This class type.
BlkTsrEngineBase< BlkTsrEngine_ > BlkTsrEngineBase_
Block tensor base class type.
EngineTrait< BlkTsrEngine_ >::policy policy
The result policy type.
LeafEngine_::ExprEngine_ ExprEngine_
Expression engine base class.
TiledArray::detail::UnaryWrapper< op_base_type > op_type
The tile operation.
Leaf expression engine.
Definition: leaf_engine.h:39
auto rank(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1617
op_type make_tile_op(const Perm &perm) const
Permuting tile operation factory function.
container::svector< std::size_t > upper_bound_
Upper bound of the tile block.
TiledArray::detail::DistEval< value_type, policy > dist_eval_type
The distributed evaluator type.
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.
LeafEngine_::ExprEngine_ ExprEngine_
Expression engine base class.
container::svector< std::size_t > lower_bound_
Lower bound of the tile block.
EngineTrait< ScalBlkTsrEngine_ >::size_type size_type
Size type.
shape_type make_shape()
Non-permuting shape factory function.
EngineTrait< BlkTsrEngine_ >::shape_type shape_type
Tensor shape type.
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
TiledArray::detail::LazyArrayTile< typename array_type::value_type, op_type > value_type
Tile type.
LeafEngine< Derived > LeafEngine_
Leaf base class type.
EngineTrait< ScalBlkTsrEngine_ >::op_base_type op_base_type
Tile base operation type.
EngineTrait< ScalBlkTsrEngine_ >::op_type op_type
Tile operation type.
container::svector< std::size_t > lower_bound_
Lower bound of the tile block.
Range data of a tiled array.
Definition: tiled_range.h:32
BlkTsrEngineBase_::LeafEngine_ LeafEngine_
Leaf base class type.
container::svector< std::size_t > upper_bound_
Upper bound of the tile block.
EngineTrait< Derived >::shape_type shape_type
Tensor shape type.
const BipartitePermutation & perm() const
Permutation accessor.
Definition: expr_engine.h:229
EngineTrait< ScalBlkTsrEngine_ >::array_type array_type
The input array type.
Scaled tensor block expression engine.
dist_eval_type make_dist_eval() const
Construct the distributed evaluator for array.
EngineTrait< BlkTsrEngine_ >::size_type size_type
Size type.
std::shared_ptr< pmap_interface > pmap_
The process map for the result tensor.
Definition: expr_engine.h:84
Forward declarations.
Definition: dist_array.h:57
void init_distribution(World *world, const std::shared_ptr< pmap_interface > &pmap)
Initialize result tensor distribution.
Definition: expr_engine.h:171
op_type make_tile_op() const
Non-permuting tile operation factory function.
EngineTrait< ScalBlkTsrEngine_ >::policy policy
The result policy type.
trange_type make_trange() const
Non-permuting tiled range factory function.
array_type array_
The array object.
Definition: leaf_engine.h:81
EngineTrait< Derived >::pmap_interface pmap_interface
Process map interface type.
container::svector< std::size_t > upper_bound_
Upper bound of the tile block.
EngineTrait< ScalBlkTsrEngine_ >::trange_type trange_type
Tiled range type type.
trange_type trange_
The tiled range of the result tensor.
Definition: expr_engine.h:81
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:48
Unary tile operation wrapper.
Definition: unary_wrapper.h:83
shape_type make_shape(const Permutation &perm)
Permuting shape factory function.
const trange_type & trange() const
Tiled range accessor.
Definition: expr_engine.h:234
TiledArray::detail::UnaryWrapper< op_base_type > op_type
The tile operation.
std::string make_tag() const
Expression identification tag.
EngineTrait< Derived >::value_type value_type
Tensor value type.
void init_distribution(World *world, const std::shared_ptr< pmap_interface > &pmap)
EngineTrait< Derived >::size_type size_type
Size type.
EngineTrait< ScalBlkTsrEngine_ >::scalar_type scalar_type
Tile scalar type.
EngineTrait< Derived >::dist_eval_type dist_eval_type
This expression's distributed evaluator type.
BlkTsrEngineBase< Derived > BlkTsrEngineBase_
This class type.
BipartitePermutation perm_
The permutation that will be applied to the outer tensor of tensors.
Definition: expr_engine.h:80
EngineTrait< ScalBlkTsrEngine_ >::value_type value_type
Tensor value type.
shape_type shape_
The shape of the result tensor.
Definition: expr_engine.h:82
Determine the object type used in the evaluation of tensor expressions.
Definition: type_traits.h:580
op_type make_op() const
Tile operation factory function.
Definition: expr_engine.h:199
BlkTsrEngineBase(const BlkTsrExpr< Array, Alias > &expr)
EngineTrait< BlkTsrEngine_ >::value_type value_type
Tensor value type.
container::svector< std::size_t > lower_bound_
Lower bound of the tile block.
Tile shift operation.
Definition: shift.h:155
shape_type make_shape(const Permutation &perm)
Permuting shape factory function.
BlkTsrEngineBase_::LeafEngine_ LeafEngine_
Leaf base class type.
TiledArray::detail::DistEval< value_type, policy > dist_eval_type
The distributed evaluator type.
Tensor expression object.
Definition: dist_eval.h:247
Tile shift operation.
Definition: shift.h:46
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82
BlkTsrEngineBase< ScalBlkTsrEngine_ > BlkTsrEngineBase_
Block tensor base class type.
const std::shared_ptr< pmap_interface > & pmap() const
Process map accessor.
Definition: expr_engine.h:244
BipartitePermutation perm_
The permutation that will be applied to the outer tensor of tensors.
Definition: expr_engine.h:80
EngineTrait< BlkTsrEngine_ >::op_base_type op_base_type
Tile base operation type.