Registered Member
|
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. |
Registered Member
|
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. |
Moderator
|
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. |
Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar