aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/Assign.h
blob: 53d6250f36a2fbec59b4fcaf29b01c2c2208f143 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2007 Michael Olbrich <michael.olbrich@gmx.net>
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_ASSIGN_H
#define EIGEN_ASSIGN_H

template<typename Derived1, typename Derived2, int UnrollCount>
struct ei_matrix_operator_equals_unroller
{
  enum {
    col = (UnrollCount-1) / Derived1::RowsAtCompileTime,
    row = (UnrollCount-1) % Derived1::RowsAtCompileTime
  };

  static void run(Derived1 &dst, const Derived2 &src)
  {
    ei_matrix_operator_equals_unroller<Derived1, Derived2, UnrollCount-1>::run(dst, src);
    dst.coeffRef(row, col) = src.coeff(row, col);
  }
};

template<typename Derived1, typename Derived2>
struct ei_matrix_operator_equals_unroller<Derived1, Derived2, 1>
{
  static void run(Derived1 &dst, const Derived2 &src)
  {
    dst.coeffRef(0, 0) = src.coeff(0, 0);
  }
};

// prevent buggy user code from causing an infinite recursion
template<typename Derived1, typename Derived2>
struct ei_matrix_operator_equals_unroller<Derived1, Derived2, 0>
{
  static void run(Derived1 &, const Derived2 &) {}
};

template<typename Derived1, typename Derived2>
struct ei_matrix_operator_equals_unroller<Derived1, Derived2, Dynamic>
{
  static void run(Derived1 &, const Derived2 &) {}
};

//----

template<typename Derived1, typename Derived2, int Index>
struct ei_matrix_operator_equals_packet_unroller
{
  enum {
    row = Derived1::Flags&RowMajorBit ? Index / Derived1::ColsAtCompileTime : Index % Derived1::RowsAtCompileTime,
    col = Derived1::Flags&RowMajorBit ? Index % Derived1::ColsAtCompileTime : Index / Derived1::RowsAtCompileTime
  };

  static void run(Derived1 &dst, const Derived2 &src)
  {
    ei_matrix_operator_equals_packet_unroller<Derived1, Derived2,
      Index-ei_packet_traits<typename Derived1::Scalar>::size>::run(dst, src);
    dst.writePacketCoeff(row, col, src.packetCoeff(row, col));
  }
};

template<typename Derived1, typename Derived2>
struct ei_matrix_operator_equals_packet_unroller<Derived1, Derived2, 0 >
{
  static void run(Derived1 &dst, const Derived2 &src)
  {
    dst.writePacketCoeff(0, 0, src.packetCoeff(0, 0));
  }
};

template<typename Derived1, typename Derived2>
struct ei_matrix_operator_equals_packet_unroller<Derived1, Derived2, Dynamic>
{
  static void run(Derived1 &, const Derived2 &) { ei_internal_assert(false && "ei_matrix_operator_equals_packet_unroller"); }
};

//----

template<typename Derived1, typename Derived2, int UnrollCount>
struct ei_vector_operator_equals_unroller
{
  enum { index = UnrollCount - 1 };

  static void run(Derived1 &dst, const Derived2 &src)
  {
    ei_vector_operator_equals_unroller<Derived1, Derived2, UnrollCount-1>::run(dst, src);
    dst.coeffRef(index) = src.coeff(index);
  }
};

// prevent buggy user code from causing an infinite recursion
template<typename Derived1, typename Derived2>
struct ei_vector_operator_equals_unroller<Derived1, Derived2, 0>
{
  static void run(Derived1 &, const Derived2 &) {}
};

template<typename Derived1, typename Derived2>
struct ei_vector_operator_equals_unroller<Derived1, Derived2, 1>
{
  static void run(Derived1 &dst, const Derived2 &src)
  {
    dst.coeffRef(0) = src.coeff(0);
  }
};

template<typename Derived1, typename Derived2>
struct ei_vector_operator_equals_unroller<Derived1, Derived2, Dynamic>
{
  static void run(Derived1 &, const Derived2 &) {}
};

template <typename Derived, typename OtherDerived,
bool Vectorize = (Derived::Flags & OtherDerived::Flags & VectorizableBit)
              && ((Derived::Flags&RowMajorBit)==(OtherDerived::Flags&RowMajorBit))>
struct ei_operator_equals_impl;

template<typename Derived>
template<typename OtherDerived>
Derived& MatrixBase<Derived>
  ::lazyAssign(const MatrixBase<OtherDerived>& other)
{
  ei_operator_equals_impl<Derived,OtherDerived>::execute(derived(),other.derived());
  return derived();
}

template<typename Derived>
template<typename OtherDerived>
Derived& MatrixBase<Derived>
  ::operator=(const MatrixBase<OtherDerived>& other)
{
  if(OtherDerived::Flags & EvalBeforeAssigningBit)
  {
    return lazyAssign(other.derived().eval());
  }
  else
    return lazyAssign(other.derived());
}

template <typename Derived, typename OtherDerived>
struct ei_operator_equals_impl<Derived, OtherDerived, false>
{
  static void execute(Derived & dst, const OtherDerived & src)
  {
    const bool unroll = Derived::SizeAtCompileTime * OtherDerived::CoeffReadCost <= EIGEN_UNROLLING_LIMIT;
    if(Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime)
      // copying a vector expression into a vector
    {
      ei_assert(dst.size() == src.size());
      if(unroll)
        ei_vector_operator_equals_unroller
          <Derived, OtherDerived,
          unroll ? Derived::SizeAtCompileTime : Dynamic
          >::run(dst.derived(), src.derived());
      else
      {
        #ifdef EIGEN_USE_OPENMPf
        if(Derived::Flags & OtherDerived::Flags & LargeBit)
        {
          #ifdef __INTEL_COMPILER
          #pragma omp parallel default(none) shared(other)
          #else
          #pragma omp parallel default(none)
          #endif
          {
            #pragma omp for
            for(int i = 0; i < dst.size(); i++)
              dst.coeffRef(i) = src.coeff(i);
          }
        }
        else
        #endif // EIGEN_USE_OPENMP
        {
          for(int i = 0; i < dst.size(); i++)
            dst.coeffRef(i) = src.coeff(i);
        }
      }
    }
    else // copying a matrix expression into a matrix
    {
      ei_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
      if(unroll)
      {
        ei_matrix_operator_equals_unroller
          <Derived, OtherDerived,
          unroll ? Derived::SizeAtCompileTime : Dynamic
          >::run(dst.derived(), src.derived());
      }
      else
      {
        if(Derived::ColsAtCompileTime == Dynamic || Derived::RowsAtCompileTime != Dynamic)
        {
          #ifdef EIGEN_USE_OPENMP
          if(Derived::Flags & OtherDerived::Flags & LargeBit)
          {
            #ifdef __INTEL_COMPILER
            #pragma omp parallel default(none) shared(other)
            #else
            #pragma omp parallel default(none)
            #endif
            {
              #pragma omp for
              for(int j = 0; j < dst.cols(); j++)
                for(int i = 0; i < dst.rows(); i++)
                  dst.coeffRef(i, j) = src.coeff(i, j);
            }
          }
          else
          #endif // EIGEN_USE_OPENMP
          {
            // traverse in column-major order
            for(int j = 0; j < dst.cols(); j++)
              for(int i = 0; i < dst.rows(); i++)
                dst.coeffRef(i, j) = src.coeff(i, j);
          }
        }
        else
        {
          #ifdef EIGEN_USE_OPENMP
          if(Derived::Flags & OtherDerived::Flags & LargeBit)
          {
            #ifdef __INTEL_COMPILER
            #pragma omp parallel default(none) shared(other)
            #else
            #pragma omp parallel default(none)
            #endif
            {
              #pragma omp for
              for(int i = 0; i < dst.rows(); i++)
                for(int j = 0; j < dst.cols(); j++)
                  dst.coeffRef(i, j) = src.coeff(i, j);
            }
          }
          else
          #endif // EIGEN_USE_OPENMP
          {
            // traverse in row-major order
            // in order to allow the compiler to unroll the inner loop
            for(int i = 0; i < dst.rows(); i++)
              for(int j = 0; j < dst.cols(); j++)
                dst.coeffRef(i, j) = src.coeff(i, j);
          }
        }
      }
    }
  }
};

template <typename Derived, typename OtherDerived>
struct ei_operator_equals_impl<Derived, OtherDerived, true>
{
  static void execute(Derived & dst, const OtherDerived & src)
  {
    const bool unroll = Derived::SizeAtCompileTime * OtherDerived::CoeffReadCost <= EIGEN_UNROLLING_LIMIT;
    ei_assert(dst.rows() == src.rows() && dst.cols() == src.cols());
    if(unroll)
    {
      ei_matrix_operator_equals_packet_unroller
        <Derived, OtherDerived,
          unroll && int(Derived::SizeAtCompileTime)>=ei_packet_traits<typename Derived::Scalar>::size
            ? Derived::SizeAtCompileTime-ei_packet_traits<typename Derived::Scalar>::size
            : Dynamic>::run(dst.const_cast_derived(), src.derived());
    }
    else
    {
      if(OtherDerived::Flags&RowMajorBit)
      {
        for(int i = 0; i < dst.rows(); i++)
          for(int j = 0; j < dst.cols(); j+=ei_packet_traits<typename Derived::Scalar>::size)
            dst.writePacketCoeff(i, j, src.packetCoeff(i, j));
      }
      else
      {
        for(int j = 0; j < dst.cols(); j++)
          for(int i = 0; i < dst.rows(); i+=ei_packet_traits<typename Derived::Scalar>::size)
            dst.writePacketCoeff(i, j, src.packetCoeff(i, j));
      }
    }
  }
};

#endif // EIGEN_ASSIGN_H