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

Time consuming when assign one matrix to another.

Tags: None
(comma "," separated)
qiang
Registered Member
Posts
3
Karma
0
Hi,

I've found matrix assignment is quite time consuming compare the other matrix operations, take the code below for example,

code 1: matrix_out = matrix_weight*matrix_init + matrix_bias;
code 2: matrix_weight*matrix_init + matrix_bias;

where, the dimension of matrix_weight is 20*10, matrix_init is 10*1, matrix_bias is 20*1, matrix_out is 20*1.

code 1 is much slower than code 2, according to my test, code 1 cost 12s, whereas , code 2 cost 0.2s.
(I've run code 1 and code 2 for many times.)

Does anyone know why assign one matrix to another so time consuming, and how to optimize the code.

Thanks.
twithaar
Registered Member
Posts
23
Karma
0
First of all, due to Eigen's template magic, some of the code might be optimized away if you don't assign the result.
The thing that typically takes time, is allocating memory.

Be sure to allocate memory beforehand, and re-use allocated memory within loops.
In C++, having a class which initializes member Matrices in the constructor and re-uses those matrices in member functions
is a scalable way to achieve that.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Because of expression templates, "code2" boils down to a no-op. Nonetheless, you can optimize code1 as:

matrix_out = matrix_bias;
matrix_out.noalias() += matrix_weight*matrix_init ;

In Eigen 3.3, this optimization is done for you.


Bookmarks



Who is online

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