Tensor
A C++ expression template library for computations on N dimensional tensors
tensor_expression_static_cpu.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file Header file for tensor expressions static container and cpu specialization for tensor library.
3 // ----------------------------------------------------------------------------------------------------------
4 
5 /*
6  * ----------------------------------------------------------------------------------------------------------
7  * Tensor is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Tensor is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with tensor; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  * ----------------------------------------------------------------------------------------------------------
21  */
22 
23 #ifndef FTL_TENSOR_EXPRESSIONS_STATIC_CPU_HPP
24 #define FTL_TENSOR_EXPRESSIONS_STATIC_CPU_HPP
25 
27 
28 // NOTE: Some of the template parameters are abbreviated as (IMHO) it looks cleaner, so:
29 // - DT = Dtype = type of data used
30 // - SF = SizeFirst = size of the first dimension
31 // - SR = SizeRest = sizes of the rest of the dimensions
32 
33 namespace ftl {
34 
35 // Specialization for tensor expression with static container and cpu implementation traits
36 template <typename Expression, typename DT, size_t SF, size_t... SR>
37 class TensorExpression<Expression, TensorTraits<DT, CPU, SF, SR...>> {
38 public:
39  // ---------------------------------------- ALIAS'S -----------------------------------------------------
40  using traits = TensorTraits<DT, CPU, SF, SR...>;
41  using size_type = typename traits::size_type;
42  using data_type = typename traits::data_type;
43  using container_type = typename traits::container_type;
44  using data_container = typename traits::data_container;
45  using dim_container = typename traits::dim_container;
46  // ------------------------------------------------------------------------------------------------------
47 
48  // ------------------------------------------------------------------------------------------------------
49  /// @brief Gets a pointer to the expression.
50  /// @return A non-const pointer to the expression.
51  // ------------------------------------------------------------------------------------------------------
52  Expression* expression() { return static_cast<Expression*>(this); }
53 
54  // ------------------------------------------------------------------------------------------------------
55  /// @brief Gets a const pointer to the expression.
56  /// @return A const pointer to the expression.
57  // ------------------------------------------------------------------------------------------------------
58  const Expression* expression() const { return static_cast<const Expression*>(this); }
59 
60  // ------------------------------------------------------------------------------------------------------
61  //! @brief Gets a reference to the Tensor expression.
62  //! @return A reference to the Tensor expression E.
63  // ------------------------------------------------------------------------------------------------------
64  operator Expression&() { return static_cast<Expression&>(*this); }
65 
66  // ------------------------------------------------------------------------------------------------------
67  //! @brief Gets a constant reference to the Tensor expression.
68  //! @return A constant reference to the Tensror expression E.
69  // ------------------------------------------------------------------------------------------------------
70  operator Expression const&() const { return static_cast<const Expression&>(*this); }
71 
72  // ------------------------------------------------------------------------------------------------------
73  /// @brief Returns the size of the expression
74  /// @return The size of the expression
75  // ------------------------------------------------------------------------------------------------------
76  constexpr size_type size() const { return expression()->size(); }
77 
78  // ------------------------------------------------------------------------------------------------------
79  /// @brief Returns the rank of the expression
80  /// @return The rank of the expression
81  // ------------------------------------------------------------------------------------------------------
82  constexpr size_type rank() const { return expression()->rank(); }
83 
84  // ------------------------------------------------------------------------------------------------------
85  //! @brief Gets the sizes of the all the dimensions of the expression.
86  //! @return A constant reference to the dimension size vector of the expression
87  // ------------------------------------------------------------------------------------------------------
88  constexpr const dim_container& dim_sizes() const { return expression()->dim_sizes(); }
89 
90  // ------------------------------------------------------------------------------------------------------
91  //! @brief Gets and element from the Tensor expression data.
92  //! @param[in] i The element in the expression which must be fetched.
93  //! @return The value of the element at position i of the expression data.
94  // ------------------------------------------------------------------------------------------------------
95  inline data_type& operator[](size_type i) { return expression()->operator[](i); }
96 
97  // ------------------------------------------------------------------------------------------------------
98  //! @brief Gets and element from the Tensor expression data.
99  //! @param[in] i The element in the expression which must be fetched.
100  //! @return The value of the element at position i of the expression data.
101  // ------------------------------------------------------------------------------------------------------
102  inline const data_type& operator[](size_type i) const { return expression()->operator[](i); }
103 };
104 
105 } // End namespace ftl
106 #endif // FTL_TENSOR_EXPRESSIONS_STATIC_CPU_HPP
typename traits::data_type data_type
Definition: tensor_expression_static_cpu.hpp:42
Expression * expression()
Gets a pointer to the expression.
Definition: tensor_expression_static_cpu.hpp:52
data_type & operator[](size_type i)
Gets and element from the Tensor expression data.
Definition: tensor_expression_static_cpu.hpp:95
constexpr const dim_container & dim_sizes() const
Gets the sizes of the all the dimensions of the expression.
Definition: tensor_expression_static_cpu.hpp:88
const Expression * expression() const
Gets a const pointer to the expression.
Definition: tensor_expression_static_cpu.hpp:58
Definition: mapper.hpp:37
Defines a general tensor expression so that opertions on tensor expressions can be defined the syntax...
Definition: tensor_expression_interface.hpp:49
typename traits::size_type size_type
Definition: tensor_expression_static_cpu.hpp:41
typename traits::container_type container_type
Definition: tensor_expression_static_cpu.hpp:43
constexpr size_type size() const
Returns the size of the expression.
Definition: tensor_expression_static_cpu.hpp:76
const data_type & operator[](size_type i) const
Gets and element from the Tensor expression data.
Definition: tensor_expression_static_cpu.hpp:102
typename traits::dim_container dim_container
Definition: tensor_expression_static_cpu.hpp:45
typename traits::data_container data_container
Definition: tensor_expression_static_cpu.hpp:44
constexpr size_type rank() const
Returns the rank of the expression.
Definition: tensor_expression_static_cpu.hpp:82
Traits class which specifies parameters for a tensor, such as what type of container it uses and what...
Definition: tensor_traits.hpp:48