FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
taskflow/taskflow/tensorframe/tensor.hpp at master · code-in-cpp/taskflow · GitHub
code-in-cpp
/
taskflow
Public
forked from
taskflow/taskflow
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
taskflow
/
taskflow
/
tensorframe
/
tensor.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
268 lines (194 loc) · 5.46 KB
Breadcrumbs
taskflow
/
taskflow
/
tensorframe
/
tensor.hpp
Copy path
File metadata and controls
268 lines (194 loc) · 5.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#
pragma
once
#
include
"
../taskflow.hpp
"
#
include
<
variant
>
#
include
<
filesystem
>
namespace
tf
{
enum
StorageLevel {
MEMORY
=
0
,
MEMORY_AND_DISK
=
1
};
/*
*
@class Tensor
@brief a tensor contains arithmetic data in N dimensions
*/
template
<
typename
T>
class
Tensor
{
template
<
typename
U>
friend
class
TensorNode
;
template
<
typename
U>
friend
class
TensorExpr
;
template
<
typename
U>
friend
class
TensorFrame
;
struct
Chunk
{
std::vector<T> data;
std::string location;
};
public:
Tensor
(
const
Tensor& tensor) =
delete
;
Tensor
(Tensor&& tensor) =
delete
;
Tensor
(std::vector<
size_t
> shape);
Tensor
(std::vector<
size_t
> shape,
size_t
max_chunk_size);
const
std::vector<
size_t
>&
shape
()
const
;
const
std::vector<
size_t
>&
chunk_shape
()
const
;
size_t
size
()
const
;
size_t
rank
()
const
;
size_t
chunk_size
()
const
;
size_t
num_chunks
()
const
;
StorageLevel
storage_level
()
const
;
void
dump
(std::ostream& ostream)
const
;
template
<
typename
... Is>
size_t
flat_chunk_index
(Is... indices)
const
;
template
<
typename
... Is>
size_t
flat_index
(Is... indices)
const
;
private:
StorageLevel _storage_level;
std::vector<
size_t
> _shape;
std::vector<
size_t
> _chunk_shape;
std::vector<
size_t
> _chunk_grid;
std::vector<Chunk> _chunks;
void
_make_chunks
(
size_t
=
65536
*
1024
);
//
65MB per chunk
size_t
_flat_chunk_index
(
size_t
&,
size_t
)
const
;
template
<
typename
... Is>
size_t
_flat_chunk_index
(
size_t
&,
size_t
, Is...)
const
;
size_t
_flat_index
(
size_t
&,
size_t
)
const
;
template
<
typename
... Is>
size_t
_flat_index
(
size_t
&,
size_t
, Is...)
const
;
};
template
<
typename
T>
Tensor<T>::Tensor(std::vector<
size_t
> shape) :
_shape {
std::move
(shape)},
_chunk_shape
(_shape.size()),
_chunk_grid
(_shape.size()) {
_make_chunks
();
}
template
<
typename
T>
Tensor<T>::Tensor(std::vector<
size_t
> shape,
size_t
max_chunk_size) :
_shape {
std::move
(shape)},
_chunk_shape
(_shape.size()),
_chunk_grid
(_shape.size()) {
_make_chunks
(
std::max
(
1ul
, max_chunk_size));
}
template
<
typename
T>
size_t
Tensor<T>::size()
const
{
return
std::accumulate
(
_shape.
begin
(), _shape.
end
(),
1
, std::multiplies<
size_t
>()
);
}
template
<
typename
T>
size_t
Tensor<T>::num_chunks()
const
{
return
_chunks.
size
();
}
template
<
typename
T>
size_t
Tensor<T>::chunk_size()
const
{
return
_chunks[
0
].
data
.
size
();
}
template
<
typename
T>
size_t
Tensor<T>::rank()
const
{
return
_shape.
size
();
}
template
<
typename
T>
const
std::vector<
size_t
>& Tensor<T>::shape()
const
{
return
_shape;
}
template
<
typename
T>
const
std::vector<
size_t
>& Tensor<T>::chunk_shape()
const
{
return
_chunk_shape;
}
template
<
typename
T>
template
<
typename
... Is>
size_t
Tensor<T>::flat_chunk_index(Is... rest)
const
{
if
(
sizeof
...(Is) !=
rank
()) {
TF_THROW
(
"
index rank dose not match tensor rank
"
);
}
size_t
offset;
return
_flat_chunk_index
(offset, rest...);
}
template
<
typename
T>
size_t
Tensor<T>::_flat_chunk_index(
size_t
& offset,
size_t
id)
const
{
offset =
1
;
return
id/_chunk_shape.
back
();
}
template
<
typename
T>
template
<
typename
... Is>
size_t
Tensor<T>::_flat_chunk_index(
size_t
& offset,
size_t
id, Is... rest
)
const
{
auto
i =
_flat_chunk_index
(offset, rest...);
offset *= _chunk_grid[_chunk_shape.
size
() - (
sizeof
...(Is))];
return
(id/_chunk_shape[_chunk_shape.
size
() -
sizeof
...(Is) -
1
])*offset + i;
}
template
<
typename
T>
template
<
typename
... Is>
size_t
Tensor<T>::flat_index(Is... rest)
const
{
if
(
sizeof
...(Is) !=
rank
()) {
TF_THROW
(
"
index rank dose not match tensor rank
"
);
}
size_t
offset;
return
_flat_index
(offset, rest...);
}
template
<
typename
T>
size_t
Tensor<T>::_flat_index(
size_t
& offset,
size_t
id)
const
{
offset =
1
;
return
id;
}
template
<
typename
T>
template
<
typename
... Is>
size_t
Tensor<T>::_flat_index(
size_t
& offset,
size_t
id, Is... rest)
const
{
auto
i =
_flat_index
(offset, rest...);
offset *= _shape[_shape.
size
() - (
sizeof
...(Is))];
return
id*offset + i;
}
template
<
typename
T>
void
Tensor<T>::dump(std::ostream& os)
const
{
os <<
"
Tensor<
"
<<
typeid
(T).
name
() <<
"
> {
\n
"
<<
"
shape=[
"
;
for
(
size_t
i=
0
; i<_shape.
size
(); ++i) {
if
(i) os <<
'
x
'
;
os << _shape[i];
}
os <<
"
], chunk=[
"
;
for
(
size_t
i=
0
; i<_chunk_shape.
size
(); ++i) {
if
(i) os <<
'
x
'
;
os << _chunk_shape[i];
}
os <<
"
], pgrid=[
"
;
for
(
size_t
i=
0
; i<_chunk_grid.
size
(); ++i) {
if
(i) os <<
'
x
'
;
os << _chunk_grid[i];
}
os <<
"
]
\n
}
\n
"
;
}
template
<
typename
T>
void
Tensor<T>::_make_chunks(
size_t
M) {
size_t
P =
1
;
size_t
N =
1
;
for
(
int
i=_shape.
size
()-
1
; i>=
0
; i--) {
if
(M >= _shape[i]) {
_chunk_shape[i] = _shape[i];
_chunk_grid[i] =
1
;
N *= _chunk_shape[i];
M /= _shape[i];
}
else
{
_chunk_shape[i] = M;
_chunk_grid[i] = (_shape[i] + _chunk_shape[i] -
1
) / _chunk_shape[i];
P *= _chunk_grid[i];
N *= _chunk_shape[i];
for
(i--; i>=
0
; i--) {
_chunk_shape[i] =
1
;
_chunk_grid[i] = _shape[i];
P *= _chunk_grid[i];
}
break
;
}
}
_chunks.
resize
(P);
//
we allocate the first data in memory
_chunks[
0
].
data
.
resize
(N);
//
TODO: the rest sits in the disk
for
(
size_t
i=
1
; i<_chunks.
size
(); ++i) {
}
//
assign the storage level
_storage_level = (_chunks.
size
() <=
1
) ?
MEMORY
:
MEMORY_AND_DISK
;
}
}
//
end of namespace tf -----------------------------------------------------
Back
|
FazBrowse Home
|
New Git URL