Xped
Loading...
Searching...
No Matches
TensorInterface_Cyclops_impl.hpp
Go to the documentation of this file.
1#ifndef TENSOR_INTERFACE_CYCLOPS_IMPL_H_
2#define TENSOR_INTERFACE_CYCLOPS_IMPL_H_
3
4#include <ctf.hpp>
5
6namespace Xped {
7
8struct TensorInterface
9{
10 // typedefs
11 template <typename Scalar, std::size_t Rank>
12 using TType = CTF::Tensor<Scalar>;
13 template <typename Scalar, std::size_t Rank>
14 using cTType = const CTF::Tensor<Scalar>;
15
16 template <typename Scalar, std::size_t Rank>
17 using MapTType = CTF::Tensor<Scalar>;
18 template <typename Scalar, std::size_t Rank>
19 using cMapTType = const CTF::Tensor<Scalar>;
20
21 using Indextype = int;
22
23 // utility functions
24 static constexpr char idx(const Indextype& i)
25 {
26 if(i == 0) { return 'i'; }
27 if(i == 1) { return 'j'; }
28 if(i == 2) { return 'k'; }
29 if(i == 3) { return 'l'; }
30 if(i == 4) { return 'm'; }
31 if(i == 5) { return 'n'; }
32 if(i == 6) { return 'o'; }
33 if(i == 100) { return 'z'; }
34 if(i == 101) { return 'y'; }
35 if(i == 102) { return 'x'; }
36 if(i == 103) { return 'w'; }
37 if(i == 104) { return 'v'; }
38 if(i == 105) { return 'u'; }
39 if(i == 106) { return 't'; }
40 return 'e';
41 }
42
43 template <std::size_t Rank>
44 static constexpr std::array<char, Rank> get_idx(std::size_t shift = 0)
45 {
46 std::array<char, Rank> out;
47 for(std::size_t i = 0; i < Rank; i++) { out[i] = idx(i + shift); }
48 return out;
49 }
50
51 // constructors
52 template <typename Scalar, std::size_t Rank>
53 static TType<Scalar, Rank> construct(const std::array<Indextype, Rank>& dims, CTF::World& world);
54
55 template <typename Scalar, int Rank>
57
58 // template <typename Scalar, int Rank>
59 // static cTType<Scalar, Rank> construct(const cMapTType<Scalar, Rank>& map)
60 // {
61 // return cTType<Scalar, Rank>(map);
62 // }
63
64 // map constructors
65 // template <typename Scalar, std::size_t Rank>
66 // static cMapTType<Scalar, Rank> cMap(const Scalar* data, const std::array<Indextype, Rank>& dims)
67 // {
68 // MapTType<Scalar, Rank> out(Rank, dims.data());
69 // int64_t nvals;
70 // int64_t* indices;
71 // Scalar* new_data;
72 // out.get_local_data(&nvals, &indices, &new_data);
73 // for(int i = 0; i < nvals; i++) { new_data[i] = data[indices[i]]; }
74 // out.write(nvals, indices, new_data);
75 // free(indices);
76 // delete[] new_data;
77 // return out;
78 // }
79
80 // template <typename Scalar, std::size_t Rank>
81 // static MapTType<Scalar, Rank> Map(Scalar* data, const std::array<Indextype, Rank>& dims)
82 // {
83 // MapTType<Scalar, Rank> out(Rank, dims.data());
84 // int64_t nvals;
85 // int64_t* indices;
86 // Scalar* new_data;
87 // out.get_local_data(&nvals, &indices, &new_data);
88 // for(int i = 0; i < nvals; i++) { new_data[i] = data[indices[i]]; }
89 // out.write(nvals, indices, new_data);
90 // free(indices);
91 // delete[] new_data;
92 // return out;
93 // }
94
95 // initialization
96 template <typename Scalar, int Rank>
98
99 template <typename Scalar, int Rank>
101
102 template <typename Scalar, int Rank>
103 static void setConstant(TType<Scalar, Rank>& T, const Scalar& val);
104
105 template <typename Scalar, int Rank>
106 static void setVal(TType<Scalar, Rank>& T, const std::array<Indextype, Rank>& index, const Scalar& val);
107
108 template <typename Scalar, int Rank>
109 static Scalar getVal(const TType<Scalar, Rank>& T, const std::array<Indextype, Rank>& index);
110
111 // raw data
112 template <typename Scalar, int Rank>
113 static Scalar* get_raw_data(const TType<Scalar, Rank>& T);
114
115 // shape info
116 template <typename Scalar, int Rank>
117 static std::array<Indextype, Rank> dimensions(const TType<Scalar, Rank>& T);
118
119 // tensorProd
120 template <typename Scalar, int Rank>
122
123 template <typename Scalar, std::size_t Rank, typename Expr1, typename Expr2>
124 static void addScale(const Expr1& src, Expr2& dst, const Scalar& scale);
125
126 // methods rvalue
127
128 template <typename Scalar, std::size_t Rank1, std::size_t Rank2, Indextype... Is1, Indextype... Is2, Indextype... Ist>
129 static TType<Scalar, sizeof...(Ist)> contract_helper(TType<Scalar, Rank1>& T1,
131 seq::iseq<Indextype, Is1...> S1,
132 seq::iseq<Indextype, Is2...> S2,
133 seq::iseq<Indextype, Ist...> St);
134
135 template <typename Scalar, std::size_t Rank1, std::size_t Rank2, Indextype... Is>
136 static TType<Scalar, Rank1 + Rank2 - sizeof...(Is)> contract(TType<Scalar, Rank1>& T1, TType<Scalar, Rank2>& T2);
137
138 template <typename Scalar, std::size_t Rank, Indextype... p>
140
141 template <typename Scalar, std::size_t Rank, Indextype... p>
142 static TType<Scalar, Rank> shuffle(TType<Scalar, Rank>& T, seq::iseq<Indextype, p...> s);
143
144 template <typename Expr, Indextype... p>
145 static Expr shuffle_view(const Expr& T);
146
147 template <typename Scalar, int Rank1, std::size_t Rank2>
148 static TType<Scalar, Rank2> reshape(const TType<Scalar, Rank1>& T, const std::array<Indextype, Rank2>& dims);
149
150 // methods lvalue
151 template <typename Scalar, std::size_t Rank1, std::size_t Rank2>
153 const std::array<Indextype, Rank2>& offsets,
154 const std::array<Indextype, Rank2>& extents,
155 const TType<Scalar, Rank1>& S);
156
157 template <typename Scalar, int Rank1, std::size_t Rank2>
159 slice(TType<Scalar, Rank1>& T, const std::array<Indextype, Rank2>& offsets, const std::array<Indextype, Rank2>& extents);
160
161 template <typename Scalar, std::size_t Rank>
162 static std::string print(const TType<Scalar, Rank>& T);
163};
164
165} // namespace Xped
166
167#ifndef XPED_COMPILED_LIB
169#endif
170
171#endif
Definition: bench.cpp:62
CTF::Tensor< Scalar > TType
Definition: PlainInterface_Cyclops_impl.cpp:13
static auto shuffle_view(const Expr &T)
Definition: TensorInterface_Array_impl.hpp:300
nda::dense_array< Scalar, Rank > TType
Definition: TensorInterface_Array_impl.hpp:44
static void setZero(TType< Scalar, Rank > &T)
const nda::dense_array< Scalar, Rank > cTType
Definition: TensorInterface_Array_impl.hpp:46
static void setConstant(TType< Scalar, Rank > &T, const Scalar &val)
static TType< Scalar, Rank > construct(const MapTType< Scalar, Rank > &map)
static constexpr std::array< char, Rank > get_idx(std::size_t shift=0)
Definition: TensorInterface_Cyclops_impl.hpp:44
static auto contract_helper(const TType< Scalar, Rank1 > &T1, const TType< Scalar, Rank2 > &T2, seq::iseq< Indextype, Is1... > S1, seq::iseq< Indextype, Is2... > S2, seq::iseq< Indextype, Ist... > St)
Definition: TensorInterface_Array_impl.hpp:237
static void addScale(const Expr1 &src, Expr2 &dst, const Scalar &scale)
static std::array< Indextype, Rank > dimensions(const TType< Scalar, Rank > &T)
static TType< Scalar, Rank2 > reshape(const TType< Scalar, Rank1 > &T, const std::array< Indextype, Rank2 > &dims)
static void setSubTensor(TType< Scalar, Rank1 > &T, const std::array< Indextype, Rank2 > &offsets, const std::array< Indextype, Rank2 > &extents, const TType< Scalar, Rank1 > &S)
static void setVal(TType< Scalar, Rank > &T, const std::array< Indextype, Rank > &index, const Scalar &val)
static TType< Scalar, Rank > shuffle(const TType< Scalar, Rank > &T)
Definition: TensorInterface_Array_impl.hpp:307
nda::index_t Indextype
Definition: TensorInterface_Array_impl.hpp:54
static constexpr char idx(const Indextype &i)
Definition: TensorInterface_Cyclops_impl.hpp:24
static std::string print(const TType< Scalar, Rank > &T)
static auto slice(TType< Scalar, Rank1 > &T, const std::array< Indextype, Rank2 > &offsets, const std::array< Indextype, Rank2 > &extents)
Definition: TensorInterface_Array_impl.hpp:358
static Scalar getVal(const TType< Scalar, Rank > &T, const std::array< Indextype, Rank > &index)
nda::dense_array_ref< Scalar, Rank > MapTType
Definition: TensorInterface_Array_impl.hpp:49
static void setRandom(TType< Scalar, Rank > &T)
static TType< Scalar, Rank > construct(const std::array< Indextype, Rank > &dims, mpi::XpedWorld &world=mpi::getUniverse())
Definition: TensorInterface_Array_impl.hpp:58
static const Scalar * get_raw_data(const TType< Scalar, Rank > &T)
Definition: TensorInterface_Array_impl.hpp:130
static TType< Scalar, Rank > tensorProd(const TType< Scalar, Rank > &T1, const TType< Scalar, Rank > &T2)
Definition: TensorInterface_Array_impl.hpp:153
static TType< Scalar, Rank1+Rank2 - sizeof...(Is)> contract(const TType< Scalar, Rank1 > &T1, const TType< Scalar, Rank2 > &T2)
Definition: TensorInterface_Array_impl.hpp:247
nda::const_dense_array_ref< Scalar, Rank > cMapTType
Definition: TensorInterface_Array_impl.hpp:51