sparse_to_dense.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  * Drew Lewis
19  * Department of Chemistry, Virginia Tech
20  *
21  * sparse_to_dense.h
22  * Feb 02, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_CONVERSIONS_SPARSE_TO_DENSE_H__INCLUDED
27 #define TILEDARRAY_CONVERSIONS_SPARSE_TO_DENSE_H__INCLUDED
28 
29 #include "../dist_array.h"
30 
31 namespace TiledArray {
32 
33 template <typename Tile, typename ResultPolicy = DensePolicy,
34  typename ArgPolicy>
35 inline std::enable_if_t<is_dense_v<ResultPolicy> && !is_dense_v<ArgPolicy>,
36  DistArray<Tile, ResultPolicy>>
37 to_dense(DistArray<Tile, ArgPolicy> const& sparse_array) {
38  typedef DistArray<Tile, ResultPolicy> ArrayType;
39  ArrayType dense_array(sparse_array.world(), sparse_array.trange());
40 
41  typedef typename ArrayType::pmap_interface pmap_interface;
42  std::shared_ptr<pmap_interface> const& pmap = dense_array.pmap();
43 
44  typename pmap_interface::const_iterator end = pmap->end();
45 
46  // iterate over sparse tiles
47  for (typename pmap_interface::const_iterator it = pmap->begin(); it != end;
48  ++it) {
49  const std::size_t ord = *it;
50  if (!sparse_array.is_zero(ord)) {
51  // clone because tiles are shallow copied
52  Tile tile(sparse_array.find(ord).get().clone());
53  dense_array.set(ord, tile);
54  } else {
55  // see DistArray::set(ordinal, element_type)
56  dense_array.set(ord, 0);
57  }
58  }
59 
60  return dense_array;
61 }
62 
63 // If array is already dense just use the copy constructor.
64 template <typename Tile, typename Policy>
65 inline std::enable_if_t<is_dense_v<Policy>, DistArray<Tile, Policy>> to_dense(
66  DistArray<Tile, Policy> const& other) {
67  return other;
68 }
69 
70 } // namespace TiledArray
71 
72 #endif // TILEDARRAY_CONVERSIONS_SPARSE_TO_DENSE_H__INCLUDED
std::enable_if_t< is_dense_v< ResultPolicy > &&!is_dense_v< ArgPolicy >, DistArray< Tile, ResultPolicy > > to_dense(DistArray< Tile, ArgPolicy > const &sparse_array)
const trange_type & trange() const
Tiled range accessor.
Definition: dist_array.h:917
Future< value_type > find(const Index &i) const
Find local or remote tile by index.
Definition: dist_array.h:524
Forward declarations.
Definition: dist_array.h:57
World & world() const
World accessor.
Definition: dist_array.h:1007
bool is_zero(const Index &i) const
Check for zero tiles.
Definition: dist_array.h:1137
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82