TiledArray  0.7.0
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>
34  DistArray<Tile, DensePolicy>
35  to_dense(DistArray<Tile, SparsePolicy> const& sparse_array) {
36  typedef DistArray<Tile, DensePolicy> ArrayType;
37  ArrayType dense_array(sparse_array.world(), sparse_array.trange());
38 
39  typedef typename ArrayType::pmap_interface pmap_interface;
40  std::shared_ptr<pmap_interface> const& pmap = dense_array.pmap();
41 
42  typename pmap_interface::const_iterator end = pmap->end();
43 
44  // iterate over sparse tiles
45  for (typename pmap_interface::const_iterator it = pmap->begin(); it != end;
46  ++it) {
47  const std::size_t ord = *it;
48  if (!sparse_array.is_zero(ord)) {
49  // clone because tiles are shallow copied
50  Tile tile(sparse_array.find(ord).get().clone());
51  dense_array.set(ord, tile);
52  } else {
53  // see DistArray::set(ordinal, element_type)
54  dense_array.set(ord, 0);
55  }
56  }
57 
58  return dense_array;
59  }
60 
61  // If array is already dense just use the copy constructor.
62  template <typename Tile>
63  DistArray<Tile, DensePolicy>
65  return other;
66  }
67 
68 } // namespace TiledArray
69 
70 #endif // TILEDARRAY_CONVERSIONS_SPARSE_TO_DENSE_H__INCLUDED
Future< value_type > find(const Index &i) const
Find local or remote tile.
Definition: dist_array.h:324
const trange_type & trange() const
Tiled range accessor.
Definition: dist_array.h:547
bool is_zero(const Index &i) const
Check for zero tiles.
Definition: dist_array.h:723
Forward declarations.
Definition: clone.h:32
DistArray< Tile, DensePolicy > to_dense(DistArray< Tile, SparsePolicy > const &sparse_array)
World & world() const
World accessor.
Definition: dist_array.h:633
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:80
const std::shared_ptr< pmap_interface > & pmap() const
Process map accessor.
Definition: dist_array.h:647