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

2 times slowdown using Eigen's column expressions

Tags: None
(comma "," separated)
hijokpayne
Registered Member
Posts
25
Karma
0
I have the following unexpected 2X slow down, when I am using Eigen's column expression instead of manual looping through each element of the column. I am using clang on Mac with compile flags -O3 -DNDEBUG -march=native. I am using the latest trunk version of Eigen.

MethodA: 1.89s
MethodB: 0.83s

Code: Select all
Eigen::MatrixXf compute(const Eigen::MatrixXf& in, const Eigen::MatrixXi& offset, const Eigen::MatrixXf& weight) {
  assert(offset.size() == weight.size());
  Eigen::MatrixXf values(in.rows(), 2000);
  for (int j = 0; j < in.cols(); ++j) {
   
    for (int i = 0; i < offset.rows(); ++i) {
      int o = offset(i, j);
      float w = weight(i, j);

       // MethodA: Eigen Col Expression
       values.col(o) += w * in.col(j) ;

      // MethodB: Raw Loops
      //for (int k = 0; k < in.rows(); ++k)
      //   values(k, o) += w * in(k, j);
    }
  }
  return values;
}


The full code to reproduce the problem is https://gist.github.com/anonymous/29560512fb748bcbd706
tienhung
Registered Member
Posts
29
Karma
0
I guess methodA is slower due to the vector resizing.
hijokpayne
Registered Member
Posts
25
Karma
0
tienhung wrote:I guess methodA is slower due to the vector resizing.


Hi Tienhung, what do you mean by resizing?
tienhung
Registered Member
Posts
29
Karma
0
hijokpayne wrote:
tienhung wrote:I guess methodA is slower due to the vector resizing.


Hi Tienhung, what do you mean by resizing?

in.col(j) and values.col(o) return column vectors, so their size must be verified before the addition and assignment. Eigen resizes values.col(o) on the left hand side.
You can refer the link below for more information.
http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
hijokpayne
Registered Member
Posts
25
Karma
0
tienhung wrote: in.col(j) and values.col(o) return column vectors, so their size must be verified before the addition and assignment. Eigen resizes values.col(o) on the left hand side.
You can refer the link below for more information.
http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html


I still don't understand why there should be resizing? Both values and in has same number of rows. Also since I am using non-debug mode, I expect no overhead of size check assertions.
qiang
Registered Member
Posts
3
Karma
0
Hi,

Did you figure out why method 1 is more time consuming, and how do you optimize the code when assign one matrix to another?

Thanks.
hijokpayne
Registered Member
Posts
25
Karma
0
When I compiled the same with gcc, the differences go away. So the problem is on Clang (I am on Mac OSX).


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar