makemake I benchmarked the default function for element by element product of Boost Ublas Matrix and found that element_prod was way slower than if I wrote my own implementation with simple for loops. So, I decided to write my own version.
I am trying to achieve code that will do element by element matrix multiplication with the help of a statement as followed :
matrix m1, m2, m3;
m3 = m1 * m2;
Here, I would like to make use of C++11 move semantics with regards to efficiently returning the output of the multiplication.
This is what I have so far.
#include "boost\numeric\ublas\matrix.hpp"
#include <Windows.h>
typedef boost::numeric::ublas::matrix<float> matrix;
void ElemProd();
const size_t X_SIZE = 400;
const size_t Y_SIZE = 400;
const size_t ITERATIONS = 500;
matrix operator*(const matrix &m1, const matrix &m2)
{
size_t rows = m1.size1();
size_t cols = m2.size2();
matrix temp(rows,cols);
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
temp(i, j) = m1(i, j) * m2(i, j);
}
}
//return std::move(temp);
return temp;
}
void ElemProd()
{
matrix m1(X_SIZE, Y_SIZE);
matrix m2(X_SIZE, Y_SIZE);
for (size_t i = 0; i < X_SIZE; i++)
{
for (size_t j = 0; j < Y_SIZE; j++)
{
m1(i, j) = 2;
m2(i, j) = 10;
}
}
matrix m3 = m1; // simply to allocate the right amount of memory for m3, to be overwritten.
m3 = m1 * m2;
}
Here, in the operator* overload, I had to create a temp matrix to store the result of the calculation. I think this is adding a significant overhead. Any suggestions how to work around this?
Another option is to make the arguments to the overload as not const, and overwrite one of the matrices and return it, but I think this is very risky long term, I would prefer to avoid it.
Consider the case where I want something like this :
matrix m = m1 * m2 * m3 * m4 * m5 * m6;
Here, you can see that I am allocating memory for temp 6 times in my implementation. m should only have to be allocated once. Further allocations are simply overhead.