TiledArray  0.7.0
scal.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 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  * scal.h
22  * June 20, 2013
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_OP_SCAL_H__INCLUDED
27 #define TILEDARRAY_TILE_OP_SCAL_H__INCLUDED
28 
29 #include <type_traits>
30 #include "../tile_interface/scale.h"
32 
33 namespace TiledArray {
34  namespace detail {
35 
37 
45  template <typename Result, typename Arg, typename Scalar, bool Consumable>
46  class Scal {
47  public:
49  typedef Arg argument_type;
50  typedef Scalar scalar_type;
51  typedef Result result_type;
52 
53  static constexpr bool is_consumable =
54  Consumable && std::is_same<result_type, argument_type>::value;
55 
56  private:
57 
58  scalar_type factor_;
59 
60  // Permuting tile evaluation function
61  // These operations cannot consume the argument tile since this operation
62  // requires temporary storage space.
63 
64  result_type eval(const Arg& arg, const Permutation& perm) const {
65  using TiledArray::scale;
66  return scale(arg, factor_, perm);
67  }
68 
69  // Non-permuting tile evaluation functions
70  // The compiler will select the correct functions based on the
71  // consumability of the arguments.
72 
73  template <bool C, typename std::enable_if<!C>::type* = nullptr>
74  result_type eval(const argument_type& arg) const {
75  using TiledArray::scale;
76  return scale(arg, factor_);
77  }
78 
79  template <bool C, typename std::enable_if<C>::type* = nullptr>
80  result_type eval(argument_type& arg) const {
82  return scale_to(arg, factor_);
83  }
84 
85  public:
86 
87  // Compiler generated functions
88  Scal(const Scal_&) = default;
89  Scal(Scal_&&) = default;
90  ~Scal() = default;
91  Scal_& operator=(const Scal_&) = default;
92  Scal_& operator=(Scal_&&) = default;
93 
95 
98  explicit Scal(const scalar_type factor) : factor_(factor) { }
99 
101 
106  operator()(const argument_type& arg, const Permutation& perm) const {
107  return eval(arg, perm);
108  }
109 
111 
115  template <typename A>
116  result_type operator()(A&& arg) const {
117  return Scal_::template eval<is_consumable>(std::forward<A>(arg));
118  }
119 
121 
125  constexpr bool can_consume = is_consumable_tile<argument_type>::value &&
126  std::is_same<result_type, argument_type>::value;
127  return Scal_::template eval<can_consume>(arg);
128  }
129 
130  }; // class Scal
131 
132  } // namespace detail
133 } // namespace TiledArray
134 
135 #endif // TILEDARRAY_TILE_OP_SCAL_H__INCLUDED
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:725
void scale(DistArray< Tile, Policy > &a, typename DistArray< Tile, Policy >::element_type scaling_factor)
Definition: utils.h:108
result_type operator()(const argument_type &arg, const Permutation &perm) const
Scale and permute operator.
Definition: scal.h:106
Scal_ & operator=(const Scal_ &)=default
Tile scaling operation.
Definition: scal.h:46
static constexpr bool is_consumable
Definition: scal.h:53
Scal(const scalar_type factor)
Constructor.
Definition: scal.h:98
result_type operator()(A &&arg) const
Consuming scale operation.
Definition: scal.h:116
result_type consume(argument_type &arg) const
Explicit consuming scale operation.
Definition: scal.h:124
Result result_type
The result tile type.
Definition: scal.h:51
Arg argument_type
The argument type.
Definition: scal.h:49
Scalar scalar_type
The scaling factor type.
Definition: scal.h:50
Consumable tile type trait.
Definition: type_traits.h:406
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
Scal(const Scal_ &)=default
Scal< Result, Arg, Scalar, Consumable > Scal_
This object type.
Definition: scal.h:48