17#ifndef TMVA_DNN_ARCHITECTURES_CPU_CPUTENSOR
18#define TMVA_DNN_ARCHITECTURES_CPU_CPUTENSOR
39template <
typename AFloat>
65 if (memlayout == MemoryLayout::RowMajor)
73 {depth, height,
width, bsize}, memlayout)
75 if (memlayout == MemoryLayout::RowMajor)
92 for (
size_t i = 0; i < this->
GetSize(); ++i) container[i] = data[i];
115 if (this->
GetLayout() == MemoryLayout::ColumnMajor) {
116 shape.insert(shape.end(),dim-2, 1);
118 shape.insert(shape.begin(), dim - 2, 1);
158 return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape.back() : shape.front();
164 if (shape.size() == 2)
return 1;
165 return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape.front() : shape[1];
171 if (shape.size() == 2)
return shape[0];
172 if (shape.size() == 3)
return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape[0] : shape[1] ;
173 if (shape.size() >= 4)
return shape[2] ;
180 if (shape.size() == 2)
return shape[1];
181 if (shape.size() == 3)
return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape[1] : shape[2] ;
182 if (shape.size() >= 4)
return shape[3] ;
202 for (
auto& shape_i : shape){
207 assert(ndims <= 2 && shape.size() > 1);
215 x.ReshapeInplace(shape);
225 Shape_t sliced_shape = (layout == MemoryLayout::RowMajor) ?
Shape_t(shape.begin() + 1, shape.end())
226 :
Shape_t(shape.begin(), shape.end() - 1);
228 size_t buffsize = (layout == MemoryLayout::RowMajor) ? this->
GetStrides().front() : this->
GetStrides().back();
229 size_t offset = i * buffsize;
239 return At(i).GetMatrix();
246 for (
size_t i = 0; i < this->
GetSize(); ++i)
254 assert(shape.size() == 2);
264 assert(shape.size() == 3);
267 ? (*(this->
GetContainer()))[i * shape[1] * shape[2] + j * shape[2] + k]
268 : (*(this->
GetContainer()))[i * shape[0] * shape[1] + k * shape[0] + j];
275 assert(shape.size() == 2);
277 : (this->
GetData())[j * shape[0] + i];
283 assert(shape.size() == 3);
286 ? (this->
GetData())[i * shape[1] * shape[2] + j * shape[2] + k]
287 : (this->
GetData())[i * shape[0] * shape[1] + k * shape[0] + j];
292 template <
typename Function_t>
293 void Map(Function_t &
f);
297 template <
typename Function_t>
306 for (
size_t i = 0; i < this->
GetSize(); i++)
307 std::cout << (this->
GetData())[i] <<
" ";
308 std::cout << std::endl;
312 std::string memlayout = (
GetLayout() == MemoryLayout::RowMajor) ?
"RowMajor" :
"ColMajor";
313 std::cout <<
name <<
" shape : { ";
315 for (
size_t i = 0; i < shape.size() - 1; ++i)
316 std::cout << shape[i] <<
" , ";
317 std::cout << shape.back() <<
" } "
318 <<
" Layout : " << memlayout << std::endl;
323template <
typename AFloat>
324template <
typename Function_t>
327 AFloat *data = GetRawDataPointer();
328 size_t nelements = GetNoElements();
331 auto ff = [data, &nsteps, &nelements, &
f](
UInt_t workerID) {
332 size_t jMax = std::min(workerID + nsteps, nelements);
333 for (
size_t j = workerID; j < jMax; ++j) {
334 data[j] =
f(data[j]);
339 if (nsteps < nelements) {
352template <
typename AFloat>
353template <
typename Function_t>
356 AFloat *dataB = GetRawDataPointer();
359 size_t nelements = GetNoElements();
363 auto ff = [&dataB, &dataA, &nsteps, &nelements, &
f](
UInt_t workerID) {
364 size_t jMax = std::min(workerID + nsteps, nelements);
365 for (
size_t j = workerID; j < jMax; ++j) {
366 dataB[j] =
f(dataA[j]);
370 if (nsteps < nelements) {
include TDocParser_001 C image html pict1_TDocParser_001 png width
A pseudo container class which is a generator of indices.
Executor & GetThreadExecutor()
Get executor class for multi-thread usage In case when MT is not enabled will return a serial executo...
static Config & Instance()
static function: returns TMVA instance
static size_t GetNWorkItems(size_t nelements)
size_t GetBufferUseCount() const
AFloat operator()(size_t i, size_t j, size_t k) const
TCpuTensor(size_t n, size_t m, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from n m
TCpuTensor(size_t bsize, size_t depth, size_t height, size_t width, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from batch size, depth, height, width
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
size_t GetNoElements() const
void Map(Function_t &f)
Map the given function over the matrix elements.
const TCpuBuffer< AFloat > & GetDeviceBuffer() const
TCpuTensor(size_t bsize, size_t depth, size_t hw, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from batch size, depth, height*width
TCpuMatrix< AFloat > operator[](size_t i) const
const AFloat * GetRawDataPointer() const
TCpuBuffer< AFloat > & GetDeviceBuffer()
AFloat & operator()(size_t i, size_t j, size_t k)
TCpuTensor(const TCpuBuffer< AFloat > &buffer, Shape_t shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a TCpuBuffer and a shape
void MapFrom(Function_t &f, const TCpuTensor< AFloat > &A)
Same as maps but takes the input values from the tensor A and writes the results in this tensor.
AFloat operator()(size_t i, size_t j) const
TCpuTensor(const TCpuMatrix< AFloat > &matrix, size_t dim=3, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a TCpuMatrix.
TCpuMatrix< AFloat > GetMatrix() const
TCpuTensor< AFloat > At(size_t i) const
TCpuTensor(Shape_t shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a shape.
size_t GetFirstSize() const
void PrintShape(const char *name="Tensor") const
AFloat & operator()(size_t i, size_t j)
TCpuTensor< AFloat > At(size_t i)
friend class TCpuMatrix< AFloat >
MemoryLayout GetLayout() const
void Print(const char *name="Tensor") const
TCpuTensor< AFloat > Reshape(Shape_t shape) const
typename TMVA::Experimental::RTensor< AFloat >::Shape_t Shape_t
TCpuTensor(AFloat *data, const Shape_t &shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
void Foreach(Function func, unsigned int nTimes, unsigned nChunks=0)
wrap TExecutor::Foreach
RTensor is a container with contiguous memory and shape information.
void ReshapeInplace(const Shape_t &shape)
Reshape tensor in place.
MemoryLayout GetMemoryLayout() const
std::shared_ptr< Container_t > GetContainer()
const Shape_t & GetStrides() const
std::size_t GetSize() const
RTensor(Value_t *data, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
const Shape_t & GetShape() const
std::vector< std::size_t > Shape_t
MemoryLayout
Memory layout type (copy from RTensor.hxx)
create variable transformations