Re: [eigen] Aliasing bug with Transpose, possibly a ReturnByValue problem

[ Thread Index | Date Index | More Archives ]


sorry for the very late reply. Basically, dealing with aliasing issue is the responsibility of the user. There only a very few cases where Eigen explicitly consider aliasing:

- plain matrix-matrix products, which assumes aliasing by default (in my opinion this should be changed to noaliasing by default, but that's not the point here)
- A = A.transpose() (and some variants, which trigger a runtime assertion in debug mode
- A = A * permutation, which automatically fallbacks to an inplace implementation. Only works if the types of both 'A' are the same.

All other cases will silently fail. It would ben ice to implement a mechanism to automatically detect all possible cases of aliasing, but I don't see any lightweight solution for that.


On Mon, Jan 19, 2015 at 7:57 AM, Adrien Escande <adrien.escande@xxxxxxxxx> wrote:
Dear all,

while browsing through Eigen's code, I found the following aliasing problem:

#include <Eigen/Eigen>
#include <iostream>

using namespace Eigen;

int main()
  VectorXd v = VectorXd::LinSpaced(15, 1, 15);
  MatrixXd M = Map<MatrixXd>(, 5, 3);

  Transpositions<Dynamic> T(3);
  T[0] = 2; T[1] = 0; T[2] = 0;
  MatrixXd M2 = T*M.topLeftCorner(3, 3);
  std::cout << M2 << std::endl << std::endl;
  M.bottomRightCorner(3, 3) = T*M.topLeftCorner(3, 3);
  std::cout << M.bottomRightCorner(3, 3) << std::endl << std::endl;

In the first case, we get
 1  6 11
 3  8 13
 2  7 12
which is correct

In the second case
 1  6 11
 1  6 11
 2  7 12
which is not.

I know that tracking all aliasing problems is difficult and this might just be a silly example. However, with my limited understanding of Eigen's internal, it looks to me more like a problem with the ReturnByValue class. In particular, it looks like, by using evalTo, the implementation of MatrixBase<Derived>::operator=(const ReturnByValue<OtherDerived>& other) in assign.h is ignoring the possibility of aliasing. That would mean that expressions with aliasing nested in ReturnByValue would always be an issue.

Adrien Escande

Mail converted by MHonArc 2.6.19+