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 
33 namespace detail {
34 template <typename DstTile, typename SrcTile, typename Op,
35  typename Enabler = void>
36 struct cast_then_op;
37 
38 template <typename Tile, typename Op>
39 struct cast_then_op<Tile, Tile, Op, void> {
40  cast_then_op(const Op& op) : op_(op) {}
41 
42  auto operator()(const Tile& tile) const { return op_(tile); }
43 
45 };
46 
47 template <typename DstTile, typename SrcTile, typename Op>
48 struct cast_then_op<DstTile, SrcTile, Op,
49  std::enable_if_t<!std::is_same<DstTile, SrcTile>::value>> {
50  cast_then_op(const Op& op) : op_(op) {}
51 
52  auto operator()(const SrcTile& tile) const {
53  return op_(Cast<DstTile, SrcTile>{}(tile));
54  }
55 
57 };
58 
59 } // namespace detail
60 
62 
70 template <typename Tile, typename ConvTile = Tile, typename Policy, typename Op>
71 inline decltype(auto) to_new_tile_type(DistArray<Tile, Policy> const& old_array,
72  Op&& op) {
73  using OutTileType = typename std::result_of<Op(ConvTile)>::type;
74 
75  static_assert(!std::is_same<Tile, OutTileType>::value,
76  "Can't call new tile type if tile type does not change.");
77 
79  cast_op(std::forward<Op>(op));
80 
81  return foreach<OutTileType>(
82  old_array, [cast_op](auto& out, const auto& in) { out = cast_op(in); });
83 }
84 
85 } // namespace TiledArray
86 #endif // TILEDARRAY_CONVERSIONS_TO_NEW_TILE_TYPE_H__INCLUDED
::blas::Op Op
Definition: blas.h:46
decltype(auto) 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.
Tile cast operation.
Definition: cast.h:168
Forward declarations.
Definition: dist_array.h:57
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82