TiledArray  0.7.0
to_new_tile_type.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  * Jan 22, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_CONVERSIONS_TO_NEW_TILE_TYPE_H__INCLUDED
27 #define TILEDARRAY_CONVERSIONS_TO_NEW_TILE_TYPE_H__INCLUDED
28 
29 #include "../dist_array.h"
30 
31 namespace TiledArray {
32 
34 
40  template <typename Tile, typename Policy, typename Op>
41  inline DistArray<typename std::result_of<Op(Tile)>::type, Policy>
42  to_new_tile_type(DistArray<Tile, Policy> const &old_array, Op &&op) {
43  using OutTileType = typename std::result_of<Op(Tile)>::type;
44  using OutArray = DistArray<OutTileType, Policy>;
45 
46  static_assert(!std::is_same<Tile, OutTileType>::value,
47  "Can't call new tile type if tile type does not change.");
48 
49  auto &world = old_array.world();
50 
51  // Create new array
52  OutArray new_array(world, old_array.trange(), old_array.shape(), old_array.pmap());
53 
54  using pmap_iter = decltype(old_array.pmap()->begin());
55  pmap_iter it = old_array.pmap()->begin();
56  pmap_iter end = old_array.pmap()->end();
57 
58  for(; it != end; ++it) {
59  // Must check for zero because pmap_iter does not.
60  if(!old_array.is_zero(*it)) {
61  // Spawn a task to evaluate the tile
62  Future<OutTileType> tile = world.taskq.add(op, old_array.find(*it));
63  new_array.set(*it, tile);
64  }
65  }
66 
67  return new_array;
68  }
69 
70 } // namespace TiledArray
71 #endif // TILEDARRAY_CONVERSIONS_TO_NEW_TILE_TYPE_H__INCLUDED
Future< value_type > find(const Index &i) const
Find local or remote tile.
Definition: dist_array.h:324
const shape_type & shape() const
Shape accessor.
Definition: dist_array.h:668
const trange_type & trange() const
Tiled range accessor.
Definition: dist_array.h:547
DistArray< typename std::result_of< Op(Tile)>::type, Policy > to_new_tile_type(DistArray< Tile, Policy > const &old_array, Op &&op)
Function to convert an array to a new array with a different tile type.
bool is_zero(const Index &i) const
Check for zero tiles.
Definition: dist_array.h:723
Forward declarations.
Definition: clone.h:32
World & world() const
World accessor.
Definition: dist_array.h:633
const std::shared_ptr< pmap_interface > & pmap() const
Process map accessor.
Definition: dist_array.h:647