Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i return a Eigen::Matrix from a method such it do not copy the data when returning

Tags:

eigen

I have:

Eigen::MatrixXf load_from_gpu()
{
    Eigen::MatrixXf mat(m_rows,m_cols);
    clEnqueueReadBuffer(m_manager->m_gpu_queue_loader, m_buffer, CL_TRUE, 0, sizeof(float)*numel(), mat.data(), 0, NULL, NULL); 
    return mat; 
}

I belive that when i call this method the data is stored to one mat and then copyed to mat2 : Eigen::MatrixXf mat2 = load_from_gpu();

Is it possible to make it write the data into the matrix which is the rhs of the function call of load_from_gpu()

like image 866
Poul K. Sørensen Avatar asked Mar 11 '13 01:03

Poul K. Sørensen


2 Answers

Your compiler should be able to do this for you, using the common Return Value Optimization method. Basically what this does, is that the compiler rewrites load_from_gpu to take a pointer to an Eigen::MatrixXf as a parameter, and fill that matrix directly.

Note that it can only do this because it can see that mat will always be the return value, if there are several matrices in the methods and one gets returned based on some condition, the compiler doesn't know which one to replace with the hidden pointer parameter. In this case you have to resort to doing it manually, like in alrikai's answer.

To enable the optimization you have to compile with -O2 with GCC.

like image 115
sgvd Avatar answered Oct 12 '22 07:10

sgvd


I haven't used Eigen much, but can't you pass your Matrix as a reference parameter and assign it in load_from_gpu()? That is,

void load_from_gpu(Eigen::MatrixXf& mat)
{
    clEnqueueReadBuffer(m_manager->m_gpu_queue_loader, m_buffer, CL_TRUE, 0, sizeof(float)*numel(), mat.data(), 0, NULL, NULL);
}
like image 24
alrikai Avatar answered Oct 12 '22 06:10

alrikai