cxx11_tensor_block_eval.cpp File Reference
#include "main.h"
#include <Eigen/CXX11/Tensor>

Classes

struct  TensorBlockParams< NumDims >
 
struct  SimpleTensorGenerator< T, NumDims >
 
struct  SimpleTensorGenerator< bool, NumDims >
 

Macros

#define CALL_SUBTEST_PART(PART)   CALL_SUBTEST_##PART
 
#define CALL_SUBTESTS_DIMS_LAYOUTS_TYPES(PART, NAME)
 
#define CALL_SUBTESTS_DIMS_LAYOUTS(PART, NAME)
 
#define CALL_SUBTESTS_LAYOUTS_TYPES(PART, NAME)
 

Functions

template<int NumDims>
static DSizes< Index, NumDims > RandomDims (Index min, Index max)
 
template<int Layout, int NumDims>
static TensorBlockParams< NumDims > RandomBlock (DSizes< Index, NumDims > dims, Index min, Index max)
 
template<int Layout, int NumDims>
static TensorBlockParams< NumDims > SkewedInnerBlock (DSizes< Index, NumDims > dims)
 
template<int NumDims>
static TensorBlockParams< NumDims > FixedSizeBlock (DSizes< Index, NumDims > dims)
 
Eigen::IndexList< Index, Eigen::type2index< 1 > > NByOne (Index n)
 
Eigen::IndexList< Eigen::type2index< 1 >, Index > OneByM (Index m)
 
template<typename T , int NumDims, int Layout, typename Expression , typename GenBlockParams >
static void VerifyBlockEvaluator (Expression expr, GenBlockParams gen_block)
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_block ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_unary_expr_block ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_binary_expr_block ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_binary_with_unary_expr_block ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_broadcast ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_reshape ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_cast ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_select ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_padding ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_chipping ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_generator ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_reverse ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_slice ()
 
template<typename T , int NumDims, int Layout>
static void test_eval_tensor_shuffle ()
 
template<typename T , int Layout>
static void test_eval_tensor_reshape_with_bcast ()
 
template<typename T , int Layout>
static void test_eval_tensor_forced_eval ()
 
template<typename T , int Layout>
static void test_eval_tensor_chipping_of_bcast ()
 
template<typename T , int NumDims, int Layout, int NumExprDims = NumDims, typename Expression , typename GenBlockParams >
static void VerifyBlockAssignment (Tensor< T, NumDims, Layout > &tensor, Expression expr, GenBlockParams gen_block)
 
template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor ()
 
template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_reshape ()
 
template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_chipping ()
 
template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_slice ()
 
template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_shuffle ()
 
 EIGEN_DECLARE_TEST (cxx11_tensor_block_eval)
 

Macro Definition Documentation

◆ CALL_SUBTEST_PART

#define CALL_SUBTEST_PART (   PART)    CALL_SUBTEST_##PART

◆ CALL_SUBTESTS_DIMS_LAYOUTS

#define CALL_SUBTESTS_DIMS_LAYOUTS (   PART,
  NAME 
)
Value:
CALL_SUBTEST_PART(PART)((NAME<float, 1, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 2, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 3, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 4, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 5, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 1, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 2, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 5, ColMajor>()))
#define CALL_SUBTEST_PART(PART)
Definition: cxx11_tensor_block_eval.cpp:714

◆ CALL_SUBTESTS_DIMS_LAYOUTS_TYPES

#define CALL_SUBTESTS_DIMS_LAYOUTS_TYPES (   PART,
  NAME 
)
Value:
CALL_SUBTEST_PART(PART)((NAME<float, 1, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 2, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 3, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 4, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 5, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 1, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 2, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, 5, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 1, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 2, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 3, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 4, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 5, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 1, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 2, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<int, 5, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 1, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 2, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 3, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 4, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 5, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 1, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 2, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 4, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, 5, ColMajor>()))

◆ CALL_SUBTESTS_LAYOUTS_TYPES

#define CALL_SUBTESTS_LAYOUTS_TYPES (   PART,
  NAME 
)
Value:
CALL_SUBTEST_PART(PART)((NAME<float, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, ColMajor>()))

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_block_eval  )
766  {
767  // clang-format off
782 
786 
792 
793  // Force CMake to split this test.
794  // EIGEN_SUFFIXES;1;2;3;4;5;6;7;8
795 
796  // clang-format on
797 }
static void test_eval_tensor_chipping()
Definition: cxx11_tensor_block_eval.cpp:345
static void test_eval_tensor_generator()
Definition: cxx11_tensor_block_eval.cpp:400
static void test_eval_tensor_block()
Definition: cxx11_tensor_block_eval.cpp:201
#define CALL_SUBTESTS_DIMS_LAYOUTS_TYPES(PART, NAME)
Definition: cxx11_tensor_block_eval.cpp:716
static void test_eval_tensor_padding()
Definition: cxx11_tensor_block_eval.cpp:308
static void test_eval_tensor_select()
Definition: cxx11_tensor_block_eval.cpp:294
static void test_eval_tensor_reverse()
Definition: cxx11_tensor_block_eval.cpp:414
static void test_eval_tensor_reshape()
Definition: cxx11_tensor_block_eval.cpp:267
static void test_assign_to_tensor_chipping()
Definition: cxx11_tensor_block_eval.cpp:635
static void test_assign_to_tensor()
Definition: cxx11_tensor_block_eval.cpp:604
static void test_eval_tensor_unary_expr_block()
Definition: cxx11_tensor_block_eval.cpp:211
static void test_assign_to_tensor_shuffle()
Definition: cxx11_tensor_block_eval.cpp:690
static void test_assign_to_tensor_reshape()
Definition: cxx11_tensor_block_eval.cpp:615
static void test_eval_tensor_reshape_with_bcast()
Definition: cxx11_tensor_block_eval.cpp:477
static void test_eval_tensor_binary_expr_block()
Definition: cxx11_tensor_block_eval.cpp:220
static void test_eval_tensor_slice()
Definition: cxx11_tensor_block_eval.cpp:430
static void test_eval_tensor_broadcast()
Definition: cxx11_tensor_block_eval.cpp:241
static void test_eval_tensor_binary_with_unary_expr_block()
Definition: cxx11_tensor_block_eval.cpp:230
static void test_eval_tensor_forced_eval()
Definition: cxx11_tensor_block_eval.cpp:499
static void test_eval_tensor_chipping_of_bcast()
Definition: cxx11_tensor_block_eval.cpp:520
static void test_eval_tensor_cast()
Definition: cxx11_tensor_block_eval.cpp:284
static void test_assign_to_tensor_slice()
Definition: cxx11_tensor_block_eval.cpp:663
static void test_eval_tensor_shuffle()
Definition: cxx11_tensor_block_eval.cpp:453
#define CALL_SUBTESTS_DIMS_LAYOUTS(PART, NAME)
Definition: cxx11_tensor_block_eval.cpp:748
#define CALL_SUBTESTS_LAYOUTS_TYPES(PART, NAME)
Definition: cxx11_tensor_block_eval.cpp:760

References CALL_SUBTESTS_DIMS_LAYOUTS, CALL_SUBTESTS_DIMS_LAYOUTS_TYPES, CALL_SUBTESTS_LAYOUTS_TYPES, test_assign_to_tensor(), test_assign_to_tensor_chipping(), test_assign_to_tensor_reshape(), test_assign_to_tensor_shuffle(), test_assign_to_tensor_slice(), test_eval_tensor_binary_expr_block(), test_eval_tensor_binary_with_unary_expr_block(), test_eval_tensor_block(), test_eval_tensor_broadcast(), test_eval_tensor_cast(), test_eval_tensor_chipping(), test_eval_tensor_chipping_of_bcast(), test_eval_tensor_forced_eval(), test_eval_tensor_generator(), test_eval_tensor_padding(), test_eval_tensor_reshape(), test_eval_tensor_reshape_with_bcast(), test_eval_tensor_reverse(), test_eval_tensor_select(), test_eval_tensor_shuffle(), test_eval_tensor_slice(), and test_eval_tensor_unary_expr_block().

◆ FixedSizeBlock()

template<int NumDims>
static TensorBlockParams<NumDims> FixedSizeBlock ( DSizes< Index, NumDims >  dims)
static
97  {
98  DSizes<Index, NumDims> offsets;
99  for (int i = 0; i < NumDims; ++i) offsets[i] = 0;
100 
101  return {offsets, dims, TensorBlockDescriptor<NumDims, Index>(0, dims)};
102 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Definition: TensorBlock.h:171

References i.

Referenced by test_assign_to_tensor(), test_assign_to_tensor_chipping(), and test_eval_tensor_chipping().

◆ NByOne()

Eigen::IndexList<Index, Eigen::type2index<1> > NByOne ( Index  n)
inline
104  {
106  ret.set(0, n);
107  return ret;
108 }
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
Definition: TensorIndexList.h:271

References n, and ret.

Referenced by Eigen::TensorEvaluator< const TensorReshapingOp< NewDimensions, ArgType >, Device >::block(), test_eval_tensor_forced_eval(), and test_eval_tensor_reshape_with_bcast().

◆ OneByM()

Eigen::IndexList<Eigen::type2index<1>, Index> OneByM ( Index  m)
inline
109  {
111  ret.set(1, m);
112  return ret;
113 }
int * m
Definition: level2_cplx_impl.h:294
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83

References m, and ret.

Referenced by test_eval_tensor_forced_eval(), and test_eval_tensor_reshape_with_bcast().

◆ RandomBlock()

template<int Layout, int NumDims>
static TensorBlockParams<NumDims> RandomBlock ( DSizes< Index, NumDims >  dims,
Index  min,
Index  max 
)
static
38  {
39  // Choose random offsets and sizes along all tensor dimensions.
40  DSizes<Index, NumDims> offsets(RandomDims<NumDims>(min, max));
41  DSizes<Index, NumDims> sizes(RandomDims<NumDims>(min, max));
42 
43  // Make sure that offset + size do not overflow dims.
44  for (int i = 0; i < NumDims; ++i) {
45  offsets[i] = numext::mini(dims[i] - 1, offsets[i]);
46  sizes[i] = numext::mini(sizes[i], dims[i] - offsets[i]);
47  }
48 
49  Index offset = 0;
50  DSizes<Index, NumDims> strides = Eigen::internal::strides<Layout>(dims);
51  for (int i = 0; i < NumDims; ++i) {
52  offset += strides[i] * offsets[i];
53  }
54 
55  return {offsets, sizes, TensorBlockDescriptor<NumDims, Index>(offset, sizes)};
56 }
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
std::vector< Array2i > sizes
Definition: dense_solvers.cpp:12
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:29
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920

References i, max, min, Eigen::numext::mini(), sizes, and Eigen::internal::strides().

◆ RandomDims()

template<int NumDims>
static DSizes<Index, NumDims> RandomDims ( Index  min,
Index  max 
)
static
20  {
22  for (int i = 0; i < NumDims; ++i) {
23  dims[i] = internal::random<Index>(min, max);
24  }
25  return DSizes<Index, NumDims>(dims);
26 }

References i, max, and min.

◆ SkewedInnerBlock()

template<int Layout, int NumDims>
static TensorBlockParams<NumDims> SkewedInnerBlock ( DSizes< Index, NumDims >  dims)
static
61  {
62  using BlockMapper = internal::TensorBlockMapper<NumDims, Layout, Index>;
63  BlockMapper block_mapper(
64  dims,
65  {internal::TensorBlockShapeType::kSkewedInnerDims, internal::random<size_t>(1, dims.TotalSize()), {0, 0, 0}});
66 
67  Index total_blocks = block_mapper.blockCount();
68  Index block_index = internal::random<Index>(0, total_blocks - 1);
69  auto block = block_mapper.blockDescriptor(block_index);
70  DSizes<Index, NumDims> sizes = block.dimensions();
71 
72  auto strides = internal::strides<Layout>(dims);
73  DSizes<Index, NumDims> offsets;
74 
75  // Compute offsets for the first block coefficient.
76  Index index = block.offset();
77  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
78  for (int i = NumDims - 1; i > 0; --i) {
79  const Index idx = index / strides[i];
80  index -= idx * strides[i];
81  offsets[i] = idx;
82  }
83  if (NumDims > 0) offsets[0] = index;
84  } else {
85  for (int i = 0; i < NumDims - 1; ++i) {
86  const Index idx = index / strides[i];
87  index -= idx * strides[i];
88  offsets[i] = idx;
89  }
90  if (NumDims > 0) offsets[NumDims - 1] = index;
91  }
92 
93  return {offsets, sizes, block};
94 }
m m block(1, 0, 2, 2)<< 4
@ ColMajor
Definition: Constants.h:318
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize() const
Definition: TensorDimensions.h:167

References block(), Eigen::ColMajor, i, sizes, Eigen::internal::strides(), and Eigen::DSizes< DenseIndex, NumDims >::TotalSize().

◆ test_assign_to_tensor()

template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor ( )
static
604  {
605  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
606  Tensor<T, NumDims, Layout> tensor(dims);
607 
608  TensorMap<Tensor<T, NumDims, Layout>> map(tensor.data(), dims);
609 
610  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map, [&dims]() { return RandomBlock<Layout>(dims, 10, 20); });
611  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map, [&dims]() { return FixedSizeBlock(dims); });
612 }
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
The tensor class.
Definition: Tensor.h:68
static TensorBlockParams< NumDims > FixedSizeBlock(DSizes< Index, NumDims > dims)
Definition: cxx11_tensor_block_eval.cpp:97

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), and FixedSizeBlock().

Referenced by EIGEN_DECLARE_TEST().

◆ test_assign_to_tensor_chipping()

template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_chipping ( )
static
635  {
636  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
637  Tensor<T, NumDims, Layout> tensor(dims);
638 
639  Index chip_dim = internal::random<int>(0, NumDims - 1);
640  Index chip_offset = internal::random<Index>(0, dims[chip_dim] - 2);
641 
642  DSizes<Index, NumDims - 1> chipped_dims;
643  for (Index i = 0; i < chip_dim; ++i) {
644  chipped_dims[i] = dims[i];
645  }
646  for (Index i = chip_dim + 1; i < NumDims; ++i) {
647  chipped_dims[i - 1] = dims[i];
648  }
649 
650  TensorMap<Tensor<T, NumDims, Layout>> map(tensor.data(), dims);
651 
652  VerifyBlockAssignment<T, NumDims, Layout, NumDims - 1>(
653  tensor, map.chip(chip_offset, chip_dim), [&chipped_dims]() { return RandomBlock<Layout>(chipped_dims, 1, 10); });
654 
655  VerifyBlockAssignment<T, NumDims, Layout, NumDims - 1>(
656  tensor, map.chip(chip_offset, chip_dim), [&chipped_dims]() { return SkewedInnerBlock<Layout>(chipped_dims); });
657 
658  VerifyBlockAssignment<T, NumDims, Layout, NumDims - 1>(tensor, map.chip(chip_offset, chip_dim),
659  [&chipped_dims]() { return FixedSizeBlock(chipped_dims); });
660 }
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
static void VerifyBlockAssignment(Tensor< T, NumDims, Layout > &tensor, Expression expr, GenBlockParams gen_block)
Definition: cxx11_tensor_block_eval.cpp:549

References Eigen::TensorBase< Derived, AccessLevel >::chip(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), FixedSizeBlock(), i, and VerifyBlockAssignment().

Referenced by EIGEN_DECLARE_TEST().

◆ test_assign_to_tensor_reshape()

template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_reshape ( )
static
615  {
616  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
617  Tensor<T, NumDims, Layout> tensor(dims);
618 
619  TensorMap<Tensor<T, NumDims, Layout>> map(tensor.data(), dims);
620 
621  DSizes<Index, NumDims> shuffled = dims;
622  std::shuffle(&shuffled[0], &shuffled[NumDims - 1], std::mt19937(g_seed));
623 
624  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.reshape(shuffled),
625  [&shuffled]() { return RandomBlock<Layout>(shuffled, 1, 10); });
626 
627  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.reshape(shuffled),
628  [&shuffled]() { return SkewedInnerBlock<Layout>(shuffled); });
629 
630  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.reshape(shuffled),
631  [&shuffled]() { return FixedSizeBlock(shuffled); });
632 }
EIGEN_STRONG_INLINE Packet2d shuffle(const Packet2d &m, const Packet2d &n, int mask)
Definition: LSX/PacketMath.h:150
static unsigned int g_seed
Definition: main.h:192

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::g_seed, Eigen::TensorBase< Derived, AccessLevel >::reshape(), and Eigen::internal::shuffle().

Referenced by EIGEN_DECLARE_TEST().

◆ test_assign_to_tensor_shuffle()

template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_shuffle ( )
static
690  {
691  DSizes<Index, NumDims> dims = RandomDims<NumDims>(5, 15);
692  Tensor<T, NumDims, Layout> tensor(dims);
693 
695  for (int i = 0; i < NumDims; ++i) shuffle[i] = i;
696 
697  TensorMap<Tensor<T, NumDims, Layout>> map(tensor.data(), dims);
698 
699  do {
700  DSizes<Index, NumDims> shuffled_dims;
701  for (int i = 0; i < NumDims; ++i) shuffled_dims[i] = dims[shuffle[i]];
702 
703  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.shuffle(shuffle),
704  [&shuffled_dims]() { return FixedSizeBlock(shuffled_dims); });
705 
706  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.shuffle(shuffle),
707  [&shuffled_dims]() { return RandomBlock<Layout>(shuffled_dims, 1, 5); });
708 
709  } while (std::next_permutation(&shuffle[0], &shuffle[0] + NumDims));
710 }

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), i, Eigen::internal::shuffle(), and Eigen::TensorBase< Derived, AccessLevel >::shuffle().

Referenced by EIGEN_DECLARE_TEST().

◆ test_assign_to_tensor_slice()

template<typename T , int NumDims, int Layout>
static void test_assign_to_tensor_slice ( )
static
663  {
664  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
665  Tensor<T, NumDims, Layout> tensor(dims);
666 
667  // Pick a random slice of tensor.
668  DSizes<Index, NumDims> slice_start = RandomDims<NumDims>(5, 10);
669  DSizes<Index, NumDims> slice_size = RandomDims<NumDims>(5, 10);
670 
671  // Make sure that slice start + size do not overflow tensor dims.
672  for (int i = 0; i < NumDims; ++i) {
673  slice_start[i] = numext::mini(dims[i] - 1, slice_start[i]);
674  slice_size[i] = numext::mini(slice_size[i], dims[i] - slice_start[i]);
675  }
676 
677  TensorMap<Tensor<T, NumDims, Layout>> map(tensor.data(), dims);
678 
679  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.slice(slice_start, slice_size),
680  [&slice_size]() { return RandomBlock<Layout>(slice_size, 1, 10); });
681 
682  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.slice(slice_start, slice_size),
683  [&slice_size]() { return SkewedInnerBlock<Layout>(slice_size); });
684 
685  VerifyBlockAssignment<T, NumDims, Layout>(tensor, map.slice(slice_start, slice_size),
686  [&slice_size]() { return FixedSizeBlock(slice_size); });
687 }

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), i, Eigen::numext::mini(), and Eigen::TensorBase< Derived, AccessLevel >::slice().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_binary_expr_block()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_binary_expr_block ( )
static
220  {
221  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
222  Tensor<T, NumDims, Layout> lhs(dims), rhs(dims);
223  lhs.setRandom();
224  rhs.setRandom();
225 
226  VerifyBlockEvaluator<T, NumDims, Layout>(lhs * rhs, [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
227 }

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_binary_with_unary_expr_block()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_binary_with_unary_expr_block ( )
static
230  {
231  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
232  Tensor<T, NumDims, Layout> lhs(dims), rhs(dims);
233  lhs.setRandom();
234  rhs.setRandom();
235 
236  VerifyBlockEvaluator<T, NumDims, Layout>((lhs.abs() + rhs.abs()).sqrt(),
237  [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
238 }

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_block()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_block ( )
static
201  {
202  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
203  Tensor<T, NumDims, Layout> input(dims);
204  input.setRandom();
205 
206  // Identity tensor expression transformation.
207  VerifyBlockEvaluator<T, NumDims, Layout>(input, [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
208 }

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_broadcast()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_broadcast ( )
static
241  {
242  DSizes<Index, NumDims> dims = RandomDims<NumDims>(1, 10);
243  Tensor<T, NumDims, Layout> input(dims);
244  input.setRandom();
245 
246  DSizes<Index, NumDims> bcast = RandomDims<NumDims>(1, 5);
247 
248  DSizes<Index, NumDims> bcasted_dims;
249  for (int i = 0; i < NumDims; ++i) bcasted_dims[i] = dims[i] * bcast[i];
250 
251  VerifyBlockEvaluator<T, NumDims, Layout>(input.broadcast(bcast),
252  [&bcasted_dims]() { return SkewedInnerBlock<Layout>(bcasted_dims); });
253 
254  VerifyBlockEvaluator<T, NumDims, Layout>(input.broadcast(bcast),
255  [&bcasted_dims]() { return RandomBlock<Layout>(bcasted_dims, 5, 10); });
256 
257  VerifyBlockEvaluator<T, NumDims, Layout>(input.broadcast(bcast),
258  [&bcasted_dims]() { return FixedSizeBlock(bcasted_dims); });
259 
260  // Check that desc.destination() memory is not shared between two broadcast
261  // materializations.
262  VerifyBlockEvaluator<T, NumDims, Layout>(input.broadcast(bcast) * input.abs().broadcast(bcast),
263  [&bcasted_dims]() { return SkewedInnerBlock<Layout>(bcasted_dims); });
264 }

References i, and Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_cast()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_cast ( )
static
284  {
285  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
286  Tensor<T, NumDims, Layout> input(dims);
287  input.setRandom();
288 
289  VerifyBlockEvaluator<T, NumDims, Layout>(input.template cast<int>().template cast<T>(),
290  [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
291 }

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_chipping()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_chipping ( )
static
345  {
346  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
347  Tensor<T, NumDims, Layout> input(dims);
348  input.setRandom();
349 
350  Index chip_dim = internal::random<int>(0, NumDims - 1);
351  Index chip_offset = internal::random<Index>(0, dims[chip_dim] - 2);
352 
353  DSizes<Index, NumDims - 1> chipped_dims;
354  for (Index i = 0; i < chip_dim; ++i) {
355  chipped_dims[i] = dims[i];
356  }
357  for (Index i = chip_dim + 1; i < NumDims; ++i) {
358  chipped_dims[i - 1] = dims[i];
359  }
360 
361  // Block buffer forwarding.
362  VerifyBlockEvaluator<T, NumDims - 1, Layout>(input.chip(chip_offset, chip_dim),
363  [&chipped_dims]() { return FixedSizeBlock(chipped_dims); });
364 
365  VerifyBlockEvaluator<T, NumDims - 1, Layout>(input.chip(chip_offset, chip_dim),
366  [&chipped_dims]() { return RandomBlock<Layout>(chipped_dims, 1, 10); });
367 
368  // Block expression assignment.
369  VerifyBlockEvaluator<T, NumDims - 1, Layout>(input.abs().chip(chip_offset, chip_dim),
370  [&chipped_dims]() { return FixedSizeBlock(chipped_dims); });
371 
372  VerifyBlockEvaluator<T, NumDims - 1, Layout>(input.abs().chip(chip_offset, chip_dim),
373  [&chipped_dims]() { return RandomBlock<Layout>(chipped_dims, 1, 10); });
374 }
static void VerifyBlockEvaluator(Expression expr, GenBlockParams gen_block)
Definition: cxx11_tensor_block_eval.cpp:120

References Eigen::TensorBase< Derived, AccessLevel >::chip(), FixedSizeBlock(), i, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VerifyBlockEvaluator().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_chipping_of_bcast()

template<typename T , int Layout>
static void test_eval_tensor_chipping_of_bcast ( )
static
520  {
521  if (Layout != static_cast<int>(RowMajor)) return;
522 
523  Index dim0 = internal::random<Index>(1, 10);
524  Index dim1 = internal::random<Index>(1, 10);
525  Index dim2 = internal::random<Index>(1, 10);
526 
527  Tensor<T, 3, Layout> input(1, dim1, dim2);
528  input.setRandom();
529 
530  Eigen::array<Index, 3> bcast = {{dim0, 1, 1}};
531  DSizes<Index, 2> chipped_dims(dim0, dim2);
532 
533  VerifyBlockEvaluator<T, 2, Layout>(input.broadcast(bcast).chip(0, 1),
534  [chipped_dims]() { return FixedSizeBlock(chipped_dims); });
535 
536  VerifyBlockEvaluator<T, 2, Layout>(input.broadcast(bcast).chip(0, 1),
537  [chipped_dims]() { return SkewedInnerBlock<Layout, 2>(chipped_dims); });
538 
539  VerifyBlockEvaluator<T, 2, Layout>(input.broadcast(bcast).chip(0, 1),
540  [chipped_dims]() { return RandomBlock<Layout, 2>(chipped_dims, 1, 5); });
541 }
@ RowMajor
Definition: Constants.h:320
std::array< T, N > array
Definition: EmulateArray.h:231

References Eigen::TensorBase< Derived, AccessLevel >::chip(), Eigen::RowMajor, and Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_forced_eval()

template<typename T , int Layout>
static void test_eval_tensor_forced_eval ( )
static
499  {
500  Index dim = internal::random<Index>(1, 100);
501 
502  Tensor<T, 2, Layout> lhs(dim, 1);
503  Tensor<T, 2, Layout> rhs(1, dim);
504  lhs.setRandom();
505  rhs.setRandom();
506 
507  auto bcastLhs = OneByM(dim);
508  auto bcastRhs = NByOne(dim);
509 
510  DSizes<Index, 2> dims(dim, dim);
511 
512  VerifyBlockEvaluator<T, 2, Layout>((lhs.broadcast(bcastLhs) * rhs.broadcast(bcastRhs)).eval().reshape(dims),
513  [dims]() { return SkewedInnerBlock<Layout, 2>(dims); });
514 
515  VerifyBlockEvaluator<T, 2, Layout>((lhs.broadcast(bcastLhs) * rhs.broadcast(bcastRhs)).eval().reshape(dims),
516  [dims]() { return RandomBlock<Layout, 2>(dims, 1, 50); });
517 }
Eigen::IndexList< Eigen::type2index< 1 >, Index > OneByM(Index m)
Definition: cxx11_tensor_block_eval.cpp:109
Eigen::IndexList< Index, Eigen::type2index< 1 > > NByOne(Index n)
Definition: cxx11_tensor_block_eval.cpp:104

References NByOne(), OneByM(), and Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_generator()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_generator ( )
static
400  {
401  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
402  Tensor<T, NumDims, Layout> input(dims);
403  input.setRandom();
404 
405  auto generator = SimpleTensorGenerator<T, NumDims>();
406 
407  VerifyBlockEvaluator<T, NumDims, Layout>(input.generate(generator), [&dims]() { return FixedSizeBlock(dims); });
408 
409  VerifyBlockEvaluator<T, NumDims, Layout>(input.generate(generator),
410  [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
411 }
Definition: cxx11_tensor_block_eval.cpp:377

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_padding()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_padding ( )
static
308  {
309  const int inner_dim = Layout == static_cast<int>(ColMajor) ? 0 : NumDims - 1;
310 
311  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
312  Tensor<T, NumDims, Layout> input(dims);
313  input.setRandom();
314 
315  DSizes<Index, NumDims> pad_before = RandomDims<NumDims>(0, 4);
316  DSizes<Index, NumDims> pad_after = RandomDims<NumDims>(0, 4);
317  array<std::pair<Index, Index>, NumDims> paddings;
318  for (int i = 0; i < NumDims; ++i) {
319  paddings[i] = std::make_pair(pad_before[i], pad_after[i]);
320  }
321 
322  // Test squeezing reads from inner dim.
323  if (internal::random<bool>()) {
324  pad_before[inner_dim] = 0;
325  pad_after[inner_dim] = 0;
326  paddings[inner_dim] = std::make_pair(0, 0);
327  }
328 
329  DSizes<Index, NumDims> padded_dims;
330  for (int i = 0; i < NumDims; ++i) {
331  padded_dims[i] = dims[i] + pad_before[i] + pad_after[i];
332  }
333 
334  VerifyBlockEvaluator<T, NumDims, Layout>(input.pad(paddings),
335  [&padded_dims]() { return FixedSizeBlock(padded_dims); });
336 
337  VerifyBlockEvaluator<T, NumDims, Layout>(input.pad(paddings),
338  [&padded_dims]() { return RandomBlock<Layout>(padded_dims, 1, 10); });
339 
340  VerifyBlockEvaluator<T, NumDims, Layout>(input.pad(paddings),
341  [&padded_dims]() { return SkewedInnerBlock<Layout>(padded_dims); });
342 }

References Eigen::ColMajor, i, and Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_reshape()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_reshape ( )
static
267  {
268  DSizes<Index, NumDims> dims = RandomDims<NumDims>(1, 10);
269 
270  DSizes<Index, NumDims> shuffled = dims;
271  std::shuffle(&shuffled[0], &shuffled[NumDims - 1], std::mt19937(g_seed));
272 
273  Tensor<T, NumDims, Layout> input(dims);
274  input.setRandom();
275 
276  VerifyBlockEvaluator<T, NumDims, Layout>(input.reshape(shuffled),
277  [&shuffled]() { return RandomBlock<Layout>(shuffled, 1, 10); });
278 
279  VerifyBlockEvaluator<T, NumDims, Layout>(input.reshape(shuffled),
280  [&shuffled]() { return SkewedInnerBlock<Layout>(shuffled); });
281 }

References Eigen::g_seed, Eigen::TensorBase< Derived, AccessLevel >::reshape(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and Eigen::internal::shuffle().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_reshape_with_bcast()

template<typename T , int Layout>
static void test_eval_tensor_reshape_with_bcast ( )
static
477  {
478  Index dim = internal::random<Index>(1, 100);
479 
480  Tensor<T, 2, Layout> lhs(1, dim);
481  Tensor<T, 2, Layout> rhs(dim, 1);
482  lhs.setRandom();
483  rhs.setRandom();
484 
485  auto reshapeLhs = NByOne(dim);
486  auto reshapeRhs = OneByM(dim);
487 
488  auto bcastLhs = OneByM(dim);
489  auto bcastRhs = NByOne(dim);
490 
491  DSizes<Index, 2> dims(dim, dim);
492 
493  VerifyBlockEvaluator<T, 2, Layout>(
494  lhs.reshape(reshapeLhs).broadcast(bcastLhs) * rhs.reshape(reshapeRhs).broadcast(bcastRhs),
495  [dims]() { return SkewedInnerBlock<Layout, 2>(dims); });
496 }

References NByOne(), OneByM(), Eigen::TensorBase< Derived, AccessLevel >::reshape(), and Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_reverse()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_reverse ( )
static
414  {
415  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
416  Tensor<T, NumDims, Layout> input(dims);
417  input.setRandom();
418 
419  // Randomly reverse dimensions.
421  for (int i = 0; i < NumDims; ++i) reverse[i] = internal::random<bool>();
422 
423  VerifyBlockEvaluator<T, NumDims, Layout>(input.reverse(reverse), [&dims]() { return FixedSizeBlock(dims); });
424 
425  VerifyBlockEvaluator<T, NumDims, Layout>(input.reverse(reverse),
426  [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
427 }
void reverse(const MatrixType &m)
Definition: array_reverse.cpp:17

References i, reverse(), Eigen::TensorBase< Derived, AccessLevel >::reverse(), and Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_select()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_select ( )
static
294  {
295  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
296  Tensor<T, NumDims, Layout> lhs(dims);
297  Tensor<T, NumDims, Layout> rhs(dims);
299  lhs.setRandom();
300  rhs.setRandom();
301  cond.setRandom();
302 
303  VerifyBlockEvaluator<T, NumDims, Layout>(cond.select(lhs, rhs),
304  [&dims]() { return RandomBlock<Layout>(dims, 1, 20); });
305 }

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_shuffle()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_shuffle ( )
static
453  {
454  DSizes<Index, NumDims> dims = RandomDims<NumDims>(5, 15);
455  Tensor<T, NumDims, Layout> input(dims);
456  input.setRandom();
457 
459  for (int i = 0; i < NumDims; ++i) shuffle[i] = i;
460 
461  do {
462  DSizes<Index, NumDims> shuffled_dims;
463  for (int i = 0; i < NumDims; ++i) shuffled_dims[i] = dims[shuffle[i]];
464 
465  VerifyBlockEvaluator<T, NumDims, Layout>(input.shuffle(shuffle),
466  [&shuffled_dims]() { return FixedSizeBlock(shuffled_dims); });
467 
468  VerifyBlockEvaluator<T, NumDims, Layout>(input.shuffle(shuffle),
469  [&shuffled_dims]() { return RandomBlock<Layout>(shuffled_dims, 1, 5); });
470 
471  break;
472 
473  } while (std::next_permutation(&shuffle[0], &shuffle[0] + NumDims));
474 }

References i, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::internal::shuffle(), and Eigen::TensorBase< Derived, AccessLevel >::shuffle().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_slice()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_slice ( )
static
430  {
431  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
432  Tensor<T, NumDims, Layout> input(dims);
433  input.setRandom();
434 
435  // Pick a random slice of an input tensor.
436  DSizes<Index, NumDims> slice_start = RandomDims<NumDims>(5, 10);
437  DSizes<Index, NumDims> slice_size = RandomDims<NumDims>(5, 10);
438 
439  // Make sure that slice start + size do not overflow tensor dims.
440  for (int i = 0; i < NumDims; ++i) {
441  slice_start[i] = numext::mini(dims[i] - 1, slice_start[i]);
442  slice_size[i] = numext::mini(slice_size[i], dims[i] - slice_start[i]);
443  }
444 
445  VerifyBlockEvaluator<T, NumDims, Layout>(input.slice(slice_start, slice_size),
446  [&slice_size]() { return FixedSizeBlock(slice_size); });
447 
448  VerifyBlockEvaluator<T, NumDims, Layout>(input.slice(slice_start, slice_size),
449  [&slice_size]() { return RandomBlock<Layout>(slice_size, 1, 10); });
450 }

References i, Eigen::numext::mini(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and Eigen::TensorBase< Derived, AccessLevel >::slice().

Referenced by EIGEN_DECLARE_TEST().

◆ test_eval_tensor_unary_expr_block()

template<typename T , int NumDims, int Layout>
static void test_eval_tensor_unary_expr_block ( )
static
211  {
212  DSizes<Index, NumDims> dims = RandomDims<NumDims>(10, 20);
213  Tensor<T, NumDims, Layout> input(dims);
214  input.setRandom();
215 
216  VerifyBlockEvaluator<T, NumDims, Layout>(input.abs(), [&dims]() { return RandomBlock<Layout>(dims, 1, 10); });
217 }

References Eigen::TensorBase< Derived, AccessLevel >::setRandom().

Referenced by EIGEN_DECLARE_TEST().

◆ VerifyBlockAssignment()

template<typename T , int NumDims, int Layout, int NumExprDims = NumDims, typename Expression , typename GenBlockParams >
static void VerifyBlockAssignment ( Tensor< T, NumDims, Layout > &  tensor,
Expression  expr,
GenBlockParams  gen_block 
)
static
549  {
550  using Device = DefaultDevice;
551  auto d = Device();
552 
553  // We use tensor evaluator as a target for block and slice assignments.
555 
556  // Generate a random block, or choose a block that fits in full expression.
557  TensorBlockParams<NumExprDims> block_params = gen_block();
558 
559  // Generate random data of the selected block size.
561  block.setRandom();
562 
563  // ************************************************************************ //
564  // (1) Assignment from a block.
565 
566  // Construct a materialize block from a random generated block tensor.
567  internal::TensorMaterializedBlock<T, NumExprDims, Layout> blk(internal::TensorBlockKind::kView, block.data(),
568  block.dimensions());
569 
570  // Reset all underlying tensor values to zero.
571  tensor.setZero();
572 
573  // Use evaluator to write block into a tensor.
574  eval.writeBlock(block_params.desc, blk);
575 
576  // Make a copy of the result after assignment.
577  Tensor<T, NumDims, Layout> block_assigned = tensor;
578 
579  // ************************************************************************ //
580  // (2) Assignment to a slice
581 
582  // Reset all underlying tensor values to zero.
583  tensor.setZero();
584 
585  // Assign block to a slice of original expression
586  auto s_expr = expr.slice(block_params.offsets, block_params.sizes);
587 
588  // Explicitly use coefficient assignment to evaluate slice expression.
589  using SliceAssign = TensorAssignOp<decltype(s_expr), const decltype(block)>;
591  SliceExecutor::run(SliceAssign(s_expr, block), d);
592 
593  // Make a copy of the result after assignment.
594  Tensor<T, NumDims, Layout> slice_assigned = tensor;
595 
596  for (Index i = 0; i < tensor.dimensions().TotalSize(); ++i) {
597  VERIFY_IS_EQUAL(block_assigned.coeff(i), slice_assigned.coeff(i));
598  }
599 }
Definition: TensorAssign.h:57
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & setZero()
Definition: TensorBase.h:1041
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: Tensor.h:100
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const
Definition: Tensor.h:112
const Dimensions & dimensions() const
Definition: TensorBlock.h:271
Definition: TensorExecutor.h:78
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
@ kView
Definition: TensorBlock.h:545
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:47
Definition: TensorDeviceDefault.h:19
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31
Definition: cxx11_tensor_block_eval.cpp:31
TensorBlockDescriptor< NumDims, Index > desc
Definition: cxx11_tensor_block_eval.cpp:34
DSizes< Index, NumDims > offsets
Definition: cxx11_tensor_block_eval.cpp:32
DSizes< Index, NumDims > sizes
Definition: cxx11_tensor_block_eval.cpp:33
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317

References block(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::coeff(), TensorBlockParams< NumDims >::desc, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimensions(), Eigen::internal::TensorBlockDescriptor< NumDims, IndexType >::dimensions(), eval(), i, Eigen::internal::kView, TensorBlockParams< NumDims >::offsets, run(), Eigen::TensorBase< Derived, AccessLevel >::setZero(), TensorBlockParams< NumDims >::sizes, and VERIFY_IS_EQUAL.

Referenced by test_assign_to_tensor_chipping().

◆ VerifyBlockEvaluator()

template<typename T , int NumDims, int Layout, typename Expression , typename GenBlockParams >
static void VerifyBlockEvaluator ( Expression  expr,
GenBlockParams  gen_block 
)
static
120  {
121  using Device = DefaultDevice;
122  auto d = Device();
123 
124  // Scratch memory allocator for block evaluation.
125  typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
126  TensorBlockScratch scratch(d);
127 
128  // TensorEvaluator is needed to produce tensor blocks of the expression.
130  eval.evalSubExprsIfNeeded(nullptr);
131 
132  // Choose a random offsets, sizes and TensorBlockDescriptor.
133  TensorBlockParams<NumDims> block_params = gen_block();
134 
135  // Evaluate TensorBlock expression into a tensor.
137 
138  // Dimensions for the potential destination buffer.
139  DSizes<Index, NumDims> dst_dims;
140  if (internal::random<bool>()) {
141  dst_dims = block_params.desc.dimensions();
142  } else {
143  for (int i = 0; i < NumDims; ++i) {
144  Index extent = internal::random<Index>(0, 5);
145  dst_dims[i] = block_params.desc.dimension(i) + extent;
146  }
147  }
148 
149  // Maybe use this tensor as a block desc destination.
150  Tensor<T, NumDims, Layout> dst(dst_dims);
151  dst.setZero();
152  if (internal::random<bool>()) {
153  block_params.desc.template AddDestinationBuffer<Layout>(dst.data(), internal::strides<Layout>(dst.dimensions()));
154  }
155 
156  const bool root_of_expr = internal::random<bool>();
157  auto tensor_block = eval.block(block_params.desc, scratch, root_of_expr);
158 
159  if (tensor_block.kind() == internal::TensorBlockKind::kMaterializedInOutput) {
160  // Copy data from destination buffer.
161  if (dimensions_match(dst.dimensions(), block.dimensions())) {
162  block = dst;
163  } else {
164  DSizes<Index, NumDims> offsets;
165  for (int i = 0; i < NumDims; ++i) offsets[i] = 0;
166  block = dst.slice(offsets, block.dimensions());
167  }
168 
169  } else {
170  // Assign to block from expression.
171  auto b_expr = tensor_block.expr();
172 
173  // We explicitly disable vectorization and tiling, to run a simple coefficient
174  // wise assignment loop, because it's very simple and should be correct.
175  using BlockAssign = TensorAssignOp<decltype(block), const decltype(b_expr)>;
177  BlockExecutor::run(BlockAssign(block, b_expr), d);
178  }
179 
180  // Cleanup temporary buffers owned by a tensor block.
181  tensor_block.cleanup();
182 
183  // Compute a Tensor slice corresponding to a Tensor block.
184  Tensor<T, NumDims, Layout> slice(block_params.desc.dimensions());
185  auto s_expr = expr.slice(block_params.offsets, block_params.sizes);
186 
187  // Explicitly use coefficient assignment to evaluate slice expression.
188  using SliceAssign = TensorAssignOp<decltype(slice), const decltype(s_expr)>;
190  SliceExecutor::run(SliceAssign(slice, s_expr), d);
191 
192  // Tensor block and tensor slice must be the same.
193  for (Index i = 0; i < block.dimensions().TotalSize(); ++i) {
194  VERIFY_IS_EQUAL(block.coeff(i), slice.coeff(i));
195  }
196 }
IndexType dimension(int index) const
Definition: TensorBlock.h:272
@ kMaterializedInOutput
Definition: TensorBlock.h:559
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool dimensions_match(Dims1 dims1, Dims2 dims2)
Definition: TensorDimensions.h:322

References block(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::coeff(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), TensorBlockParams< NumDims >::desc, Eigen::internal::TensorBlockDescriptor< NumDims, IndexType >::dimension(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimensions(), Eigen::internal::TensorBlockDescriptor< NumDims, IndexType >::dimensions(), Eigen::dimensions_match(), eval(), i, Eigen::internal::kMaterializedInOutput, TensorBlockParams< NumDims >::offsets, run(), Eigen::TensorBase< Derived, AccessLevel >::setZero(), TensorBlockParams< NumDims >::sizes, Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_EQUAL.

Referenced by test_eval_tensor_chipping().