truncate.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  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * truncate.h
22  * Apr 15, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_CONVERSIONS_TRUNCATE_H__INCLUDED
27 #define TILEDARRAY_CONVERSIONS_TRUNCATE_H__INCLUDED
28 
30 
31 namespace TiledArray {
32 
34 template <typename, typename>
35 class DistArray;
36 class DensePolicy;
37 class SparsePolicy;
38 
40 
45 template <typename Tile, typename Policy>
46 inline std::enable_if_t<is_dense_v<Policy>, void> truncate(
48  typename Policy::shape_type::value_type = 0) {}
49 
51 
55 template <typename Tile, typename Policy>
56 inline std::enable_if_t<!is_dense_v<Policy>, void> truncate(
58  typename Policy::shape_type::value_type thresh =
59  Policy::shape_type::threshold()) {
60  TA_ASSERT(thresh >= 0);
61  const typename Policy::shape_type::value_type previous_thresh =
62  Policy::shape_type::threshold();
63  const auto need_to_change_thresh = (thresh != previous_thresh);
64  if (need_to_change_thresh)
65  array.world().gop.serial_invoke(
66  [thresh] { Policy::shape_type::threshold(thresh); });
67  typedef typename DistArray<Tile, Policy>::value_type value_type;
68  array = foreach (array,
69  [](value_type& result_tile, const value_type& arg_tile) ->
70  typename Policy::shape_type::value_type {
71  using result_type =
72  typename Policy::shape_type::value_type;
73  result_type arg_tile_norm;
74  norm(arg_tile, arg_tile_norm);
75  result_tile = arg_tile; // Assume this is shallow copy
76  return arg_tile_norm;
77  });
78  if (need_to_change_thresh)
79  array.world().gop.serial_invoke(
80  [previous_thresh] { Policy::shape_type::threshold(previous_thresh); });
81 }
82 
83 } // namespace TiledArray
84 
85 #endif // TILEDARRAY_CONVERSIONS_TRUNCATE_H__INCLUDED
decltype(auto) norm(const Tile< Arg > &arg)
Vector 2-norm of a tile.
Definition: tile.h:1527
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
Forward declarations.
Definition: dist_array.h:57
World & world() const
World accessor.
Definition: dist_array.h:1007
std::enable_if_t< is_dense_v< Policy >, void > truncate(DistArray< Tile, Policy > &array, typename Policy::shape_type::value_type=0)
Truncate a dense Array.
Definition: truncate.h:46
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82