This forum has been archived. All content is frozen. Please use KDE Discuss instead.

aliasing bug?

Tags: None
(comma "," separated)
raman
Registered Member
Posts
8
Karma
0
OS

aliasing bug?

Tue Sep 08, 2009 4:25 pm
Hi, I decided to use Eigen2 to solve a particular problem I am working on and came across issues with aliasing. I'm using the latest version 2.0.5. Here's the code saved as eigenbug.cc:

Code: Select all
#include <Eigen/Core>

// import most common Eigen types
USING_PART_OF_NAMESPACE_EIGEN

int main(int, char *[])
{   
  MatrixXf Q(5,5);

  for (int i=0; i<Q.rows(); i++)
    for (int j=0; j<Q.cols(); j++)
      Q(i,j)=i+j*Q.rows();
     
  std::cout << Q << "\n\n";

  MatrixXf R=Q.corner(Eigen::TopLeft, Q.rows()-1,  Q.cols()-1);
  std::cout << R << "\n\n"; // prints correct result

  Q=Q.corner(Eigen::TopLeft, Q.rows()-1, Q.cols()-1);
  std::cout << Q << "\n\n"; // prints incorrect result

  // Causes aliasing:
  R.row(3) = R.col(0);
  std::cout << R << "\n\n";

  return 0;
}


Output:
Code: Select all
raman@lanai:~/Work$ g++ --version
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

raman@lanai:~/Work$ g++ -I eigen2 eigenbug.cc -o eigenbug && ./eigenbug
0 5 10 15 20
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24

0 5 10 15
1 6 11 16
2 7 12 17
3 8 13 18

-2.771e-05 4 8 12
-2.771e-05 5 9 13
2 6 10 14
3 7 11 15

0 5 10 15
1 6 11 16
2 7 12 17
0 1 2 0


It looks like the line
Code: Select all
Q=Q.corner(Eigen::TopLeft, Q.rows()-1, Q.cols()-1);

causes aliasing. I can fix it by using an eval() before assignment but I thought that the default behaviour should take care of that. Is this expected behaviour or a bug?

This also occurs when I use any of the other corners (TopRight, BottomLeft, BottomLeft), general blocks using the block() method, or if I copy a column to a row.

Thanks in advance!
Raman
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: aliasing bug?

Tue Sep 08, 2009 4:36 pm
Is this expected behaviour or a bug?


This is expected behaviour. I agree that it's terrible, but there simply is no way to make this work automatically, without giving up the benefit of expression templates in other use cases, and without adding a permanent performance overhead. The only way forward is to make sure that the user knows about these issues and how to deal with them. Since you had the idea of using eval() there, which is exactly the right thing to do, I'm afraid I don't have anything more to teach you :)


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]