Tensor
A C++ expression template library for computations on N dimensional tensors
tensor_traits.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file Header file for tensor traits 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_TRAITS_HPP
24 #define FTL_TENSOR_TRAITS_HPP
25 
26 #include "tensor_container.hpp"
27 
28 namespace ftl {
29 
30 // So that the code is more readable -- using an int rather than an enum since it will make
31 // for easy integration with other libraries that want to enable compile time device selection
32 using device = short;
33 
34 static constexpr device CPU = 0;
35 static constexpr device GPU = 1;
36 
37 // ----------------------------------------------------------------------------------------------------------
38 /// @struct TensorTraits
39 /// @brief Traits class which specifies parameters for a tensor, such as what type of container it uses
40 /// and what type of device the computations should be performed on
41 /// @tparam Dtype The type of data used by the container
42 /// @tparam DeviceType The type of device used for computation -- CPU or GPU
43 /// @tparam DimSizes The sizes of each of the dimensions of the tensor -- an optional parameters.
44 /// This parameter is used to determine the container type -- static if the dimension sizes are
45 /// specified or dynamic if they are not.
46 // ----------------------------------------------------------------------------------------------------------
47 template <typename Dtype, device DeviceType, size_t... DimSizes>
48 struct TensorTraits;
49 
50 // Specialize for static container
51 template <typename Dtype, device DeviceType, size_t SizeFirst, size_t... SizeRest>
52 struct TensorTraits<Dtype, DeviceType, SizeFirst, SizeRest...> {
53  // ---------------------------------------- ALIAS'S -----------------------------------------------------
54  using data_type = Dtype;
55  using container_type = TensorContainer<Dtype, SizeFirst, SizeRest...>;
59  // ------------------------------------------------------------------------------------------------------
60  static constexpr device device_type = DeviceType;
61 };
62 
63 // Specialize for dynamic container
64 template <typename Dtype, device DeviceType>
65 struct TensorTraits<Dtype, DeviceType> {
66  // ---------------------------------------- ALIAS'S -----------------------------------------------------
67  using data_type = Dtype;
72  // ------------------------------------------------------------------------------------------------------};
73  static constexpr device device_type = DeviceType;
74 };
75 
76 } // End namespace ftl
77 #endif // FTL_TENSOR_TRAITS_HPP
typename data_container::size_type size_type
Definition: tensor_container.hpp:122
typename container_type::data_container data_container
Definition: tensor_traits.hpp:69
typename container_type::size_type size_type
Definition: tensor_traits.hpp:58
std::vector< size_type > dim_container
Definition: tensor_container.hpp:123
Container for tensor data depending on if the tensor is static (dimension sizes, and hence the total ...
Definition: tensor_container.hpp:42
Dtype data_type
Definition: tensor_traits.hpp:67
Definition: mapper.hpp:37
typename container_type::size_type size_type
Definition: tensor_traits.hpp:71
typename nano::runtime_converter< dimension_sizes >::array_type dim_container
Definition: tensor_container.hpp:53
Definition: tensor_container.hpp:46
short device
Definition: tensor_traits.hpp:32
Definition: tensor_container.hpp:117
typename container_type::dim_container dim_container
Definition: tensor_traits.hpp:70
typename data_container::size_type size_type
Definition: tensor_container.hpp:54
std::array< data_type, dimension_product::result > data_container
Definition: tensor_container.hpp:52
std::vector< data_type > data_container
Definition: tensor_container.hpp:121
typename container_type::data_container data_container
Definition: tensor_traits.hpp:56
typename container_type::dim_container dim_container
Definition: tensor_traits.hpp:57
Traits class which specifies parameters for a tensor, such as what type of container it uses and what...
Definition: tensor_traits.hpp:48