rvalue_types.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Hauke Heibel <hauke.heibel@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #define EIGEN_RUNTIME_NO_MALLOC
11 
12 #include "main.h"
13 #if EIGEN_HAS_CXX11
14 #include "MovableScalar.h"
15 #endif
16 #include "SafeScalar.h"
17 
18 #include <Eigen/Core>
19 
20 using internal::UIntPtr;
21 
22 #if EIGEN_HAS_RVALUE_REFERENCES
23 template <typename MatrixType>
24 void rvalue_copyassign(const MatrixType& m)
25 {
26 
28 
29  // create a temporary which we are about to destroy by moving
30  MatrixType tmp = m;
31  UIntPtr src_address = reinterpret_cast<UIntPtr>(tmp.data());
32 
33  Eigen::internal::set_is_malloc_allowed(false); // moving from an rvalue reference shall never allocate
34  // move the temporary to n
35  MatrixType n = std::move(tmp);
36  UIntPtr dst_address = reinterpret_cast<UIntPtr>(n.data());
37  if (MatrixType::RowsAtCompileTime==Dynamic|| MatrixType::ColsAtCompileTime==Dynamic)
38  {
39  // verify that we actually moved the guts
40  VERIFY_IS_EQUAL(src_address, dst_address);
41  VERIFY_IS_EQUAL(tmp.size(), 0);
42  VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(tmp.data()), UIntPtr(0));
43  }
44 
45  // verify that the content did not change
46  Scalar abs_diff = (m-n).array().abs().sum();
47  VERIFY_IS_EQUAL(abs_diff, Scalar(0));
48  Eigen::internal::set_is_malloc_allowed(true);
49 }
50 template<typename TranspositionsType>
52 {
53  typedef typename TranspositionsType::IndicesType PermutationVectorType;
54 
55  PermutationVectorType vec;
57  TranspositionsType t0(vec);
58 
59  Eigen::internal::set_is_malloc_allowed(false); // moving from an rvalue reference shall never allocate
60 
61  UIntPtr t0_address = reinterpret_cast<UIntPtr>(t0.indices().data());
62 
63  // Move constructors:
64  TranspositionsType t1 = std::move(t0);
65  UIntPtr t1_address = reinterpret_cast<UIntPtr>(t1.indices().data());
66  VERIFY_IS_EQUAL(t0_address, t1_address);
67  // t0 must be de-allocated:
68  VERIFY_IS_EQUAL(t0.size(), 0);
69  VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(t0.indices().data()), UIntPtr(0));
70 
71 
72  // Move assignment:
73  t0 = std::move(t1);
74  t0_address = reinterpret_cast<UIntPtr>(t0.indices().data());
75  VERIFY_IS_EQUAL(t0_address, t1_address);
76  // t1 must be de-allocated:
77  VERIFY_IS_EQUAL(t1.size(), 0);
78  VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(t1.indices().data()), UIntPtr(0));
79 
80  Eigen::internal::set_is_malloc_allowed(true);
81 }
82 
83 template <typename MatrixType>
84 void rvalue_move(const MatrixType& m)
85 {
86  // lvalue reference is copied
87  MatrixType b(m);
89 
90  // lvalue reference is copied
91  MatrixType c{m};
93 
94  // lvalue reference is copied
95  MatrixType d = m;
97 
98  // rvalue reference is moved - copy constructor.
99  MatrixType e_src(m);
100  VERIFY_IS_EQUAL(e_src, m);
101  MatrixType e_dst(std::move(e_src));
102  VERIFY_IS_EQUAL(e_dst, m);
103 
104  // rvalue reference is moved - copy constructor.
105  MatrixType f_src(m);
106  VERIFY_IS_EQUAL(f_src, m);
107  MatrixType f_dst = std::move(f_src);
108  VERIFY_IS_EQUAL(f_dst, m);
109 
110  // rvalue reference is moved - copy assignment.
111  MatrixType g_src(m);
112  VERIFY_IS_EQUAL(g_src, m);
113  MatrixType g_dst;
114  g_dst = std::move(g_src);
115  VERIFY_IS_EQUAL(g_dst, m);
116 }
117 #else
118 template <typename MatrixType>
120 template<typename TranspositionsType>
122 template <typename MatrixType>
123 void rvalue_move(const MatrixType&) {}
124 #endif
125 
126 EIGEN_DECLARE_TEST(rvalue_types)
127 {
128  for(int i = 0; i < g_repeat; i++) {
129  CALL_SUBTEST_1(rvalue_copyassign( MatrixXf::Random(50,50).eval() ));
130  CALL_SUBTEST_1(rvalue_copyassign( ArrayXXf::Random(50,50).eval() ));
131 
134 
137 
141 
145 
150 
151 #if EIGEN_HAS_CXX11
155 #endif
156  }
157 }
array
int array[24]
Definition: Map_general_stride.cpp:1
Eigen::internal::UIntPtr
std::size_t UIntPtr
Definition: Meta.h:92
d
static const double d[K][N]
Definition: igam.h:11
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
Eigen::Transpositions
Represents a sequence of transpositions (row/column interchange)
Definition: Transpositions.h:155
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
n
int n
Definition: BiCGSTAB_simple.cpp:1
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
rvalue_move
void rvalue_move(const MatrixType &)
Definition: rvalue_types.cpp:123
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
rvalue_copyassign
void rvalue_copyassign(const MatrixType &)
Definition: rvalue_types.cpp:119
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
SafeScalar.h
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
SafeScalar
Definition: SafeScalar.h:4
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(rvalue_types)
Definition: rvalue_types.cpp:126
main.h
Eigen::PermutationMatrix< Dynamic, Dynamic, int >
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
Eigen::randomPermutationVector
void randomPermutationVector(PermutationVectorType &v, Index size)
Definition: main.h:693
Eigen::MovableScalar
Definition: MovableScalar.h:18
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
MovableScalar.h
rvalue_transpositions
void rvalue_transpositions(Index)
Definition: rvalue_types.cpp:121
eval
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:01