I use Armadillo as an interface to OpenBLAS. In my current program, I have a loop, in which I do multiplications of the form
for(long t = t0; t < t1; t+=tStep)
{
stateMatrix %= elementWiseEvolutionMatrix;
}
The operator %
is an element-wise multiplication operator. The problem here is that for matrices of side length of 500+ (the ones I have at hand), I can see that there is no parallelization whatsoever. Now I would like to point out that normal matrix multiplication is parallelized. But this kind of element-wise multiplication is not parallelized.
How do I know that? Because I go to htop
in my linux system, and I see that only one core is busy, while if I do the same with normal matrix multiplication, I see that all cores go busy.
Now I tried to manually parallelize this with OpenMP, but no luck. I tried:
for(long t = t0; t < t1; t+=tStep)
{
#pragma omp parallel for
for(long i = 0; i < static_cast<long>(stateMatrix.n_rows); i++)
{
stat1eMatrix.row(i) %= elementWiseEvolutionMatrix.row(i);
}
}
But this got all the cores busy, but the program became about a factor of 10 slower.
My question: How can I get the element-wise multiplication to be as fast as possible with parallelization?
Thanks.
EDIT: I would like to point out that I'm more than happy to use another library for the element-wise multiplication, if necessary.
-O3
optimization switch in GCC or clang (or the equivalent in MSVC) to enable auto-vectorization. This will make Armadillo use SSE2 instructions. For even more speed, use-O3 -march=native
, which will enable AVX instructions. More information is on the Armadillo FAQ page. $\endgroup$