Tensor
A C++ expression template library for computations on N dimensional tensors
tensor_expression_dynamic_cpu.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file Header file for tensor expressions dynamic 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_DYNAMIC_CPU_HPP
24 #define FTL_TENSOR_EXPRESSIONS_DYNAMIC_CPU_HPP
25 
27 
28 namespace ftl {
29 
30 // Specialization for tensor expression with dynamic container and cpu implemenation traits
31 template <typename Expression, typename Dtype>
32 class TensorExpression<Expression, TensorTraits<Dtype, CPU>> {
33 public:
34  // ---------------------------------------- ALIAS'S -----------------------------------------------------
36  using size_type = typename traits::size_type;
37  using data_type = typename traits::data_type;
38  using container_type = typename traits::container_type;
39  using data_container = typename traits::data_container;
40  using dim_container = typename traits::dim_container;
41  // ------------------------------------------------------------------------------------------------------
42 
43  // ------------------------------------------------------------------------------------------------------
44  /// @brief Gets a pointer to the expression.
45  /// @return A non-const pointer to the expression.
46  // ------------------------------------------------------------------------------------------------------
47  Expression* expression() { return static_cast<Expression*>(this); }
48 
49  // ------------------------------------------------------------------------------------------------------
50  /// @brief Gets a const pointer to the expression.
51  /// @return A const pointer to the expression.
52  // ------------------------------------------------------------------------------------------------------
53  const Expression* expression() const { return static_cast<const Expression*>(this); }
54 
55  // ------------------------------------------------------------------------------------------------------
56  //! @brief Gets a reference to the Tensor expression.
57  //! @return A reference to the Tensor expression E.
58  // ------------------------------------------------------------------------------------------------------
59  operator Expression&() { return static_cast<Expression&>(*this); }
60 
61  // ------------------------------------------------------------------------------------------------------
62  //! @brief Gets a constant reference to the Tensor expression.
63  //! @return A constant reference to the Tensror expression E.
64  // ------------------------------------------------------------------------------------------------------
65  operator Expression const&() const { return static_cast<const Expression&>(*this); }
66 
67  // ------------------------------------------------------------------------------------------------------
68  //! @brief Returns the size of the expression
69  //! @return The size of the tensor_expression
70  // ------------------------------------------------------------------------------------------------------
71  size_type size() const { return expression()->size(); }
72 
73  // ------------------------------------------------------------------------------------------------------
74  /// @brief Returns the rank of the expression
75  /// @return The rank of the expression
76  // ------------------------------------------------------------------------------------------------------
77  size_type rank() const { return expression()->rank(); }
78 
79  // ------------------------------------------------------------------------------------------------------
80  //! @brief Gets the sizes of the all the dimensions of the expression.
81  //! @return A constant reference to the dimension size vector of the expression
82  // ------------------------------------------------------------------------------------------------------
83  const dim_container& dim_sizes() const { return expression()->dim_sizes(); }
84 
85  // ------------------------------------------------------------------------------------------------------
86  //! @brief Gets and element from the Tensor expression data.
87  //! @param[in] i The element in the expression which must be fetched.
88  //! @return The value of the element at position i of the expression data.
89  // ------------------------------------------------------------------------------------------------------
90  inline data_type& operator[](size_type i) { return expression()->operator[](i); }
91 
92  // ------------------------------------------------------------------------------------------------------
93  //! @brief Gets and element from the Tensor expression data.
94  //! @param[in] i The element in the expression which must be fetched.
95  //! @return The value of the element at position i of the expression data.
96  // ------------------------------------------------------------------------------------------------------
97  inline const data_type& operator[](size_type i) const { return expression()->operator[](i); }
98 };
99 
100 } // End namespace ftl
101 #endif // FTL_TENSOR_EXPRESSIONS_DYNAMIC_CPU_HPP
typename traits::dim_container dim_container
Definition: tensor_expression_dynamic_cpu.hpp:40
Expression * expression()
Gets a pointer to the expression.
Definition: tensor_expression_dynamic_cpu.hpp:47
data_type & operator[](size_type i)
Gets and element from the Tensor expression data.
Definition: tensor_expression_dynamic_cpu.hpp:90
typename traits::size_type size_type
Definition: tensor_expression_dynamic_cpu.hpp:36
size_type size() const
Returns the size of the expression.
Definition: tensor_expression_dynamic_cpu.hpp:71
const Expression * expression() const
Gets a const pointer to the expression.
Definition: tensor_expression_dynamic_cpu.hpp:53
const data_type & operator[](size_type i) const
Gets and element from the Tensor expression data.
Definition: tensor_expression_dynamic_cpu.hpp:97
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
size_type rank() const
Returns the rank of the expression.
Definition: tensor_expression_dynamic_cpu.hpp:77
typename traits::container_type container_type
Definition: tensor_expression_dynamic_cpu.hpp:38
typename traits::data_type data_type
Definition: tensor_expression_dynamic_cpu.hpp:37
typename traits::data_container data_container
Definition: tensor_expression_dynamic_cpu.hpp:39
const dim_container & dim_sizes() const
Gets the sizes of the all the dimensions of the expression.
Definition: tensor_expression_dynamic_cpu.hpp:83
Traits class which specifies parameters for a tensor, such as what type of container it uses and what...
Definition: tensor_traits.hpp:48