retile.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2020 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  * retile.h
19  * May 11, 2020
20  */
21 
22 #ifndef TILEDARRAY_RETILE_H
23 #define TILEDARRAY_RETILE_H
24 
27 
30 
38 
39 namespace TiledArray {
40 
41 template <typename TileType, typename PolicyType>
43  const TiledRange& new_trange) {
44  // Make sure ranks match
45  auto rank = new_trange.rank();
46  auto tensor_rank = tensor.trange().rank();
47  assert((rank == tensor_rank) && "TiledRanges are of different ranks");
48 
49  // Makes the annotations for the contraction step
50  auto annotations =
51  [&](std::size_t target_dim) -> std::tuple<std::string, std::string> {
52  std::ostringstream final, switcher;
53  switcher << "i" << target_dim << ",iX";
54  if (target_dim == 0) {
55  final << "iX";
56  } else {
57  final << "i0";
58  }
59  for (unsigned int d = 1; d < rank; ++d) {
60  if (d == target_dim) {
61  final << ",iX";
62  } else {
63  final << ",i" << d;
64  }
65  }
66  return {final.str(), switcher.str()};
67  };
68 
69  // Check the different dimensions and contract when needed
70  using tensor_type = DistArray<TileType, PolicyType>;
71  auto start = detail::dummy_annotation(rank);
72  tensor_type output_tensor;
73  for (auto i = 0; i < rank; ++i) {
74  if (i == 0) { output_tensor(start) = tensor(start); }
75  if (new_trange.dim(i) != tensor.trange().dim(i)) {
76  // Make identity for contraction
77  TiledRange retiler{tensor.trange().dim(i), new_trange.dim(i)};
78  auto identity = diagonal_array<tensor_type>(tensor.world(), retiler);
79 
80  // Make indices for contraction
81  auto [finish, change] = annotations(i);
82 
83  // Retile
84  output_tensor(finish) = output_tensor(start) * identity(change);
85  }
86  }
87 
88  return output_tensor;
89 }
90 
91 } // namespace TiledArray
92 
93 
94 #endif // TILEDARRAY_RETILE_H
std::string dummy_annotation(unsigned int n_outer_size, unsigned int n_inner_size=0)
Definition: annotation.h:35
const TiledRange1 & dim(std::size_t d) const
Accessor of the tiled range for one of the dimensions.
Definition: tiled_range.h:283
auto rank(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1617
std::size_t rank() const
The rank accessor.
Definition: tiled_range.h:277
Range data of a tiled array.
Definition: tiled_range.h:32
const trange_type & trange() const
Tiled range accessor.
Definition: dist_array.h:917
Forward declarations.
Definition: dist_array.h:57
World & world() const
World accessor.
Definition: dist_array.h:1007
auto retile(const DistArray< TileType, PolicyType > &tensor, const TiledRange &new_trange)
Definition: retile.h:42
T identity()
identity for group of objects of type T