0

I have to assemble a matrix using two for-loops, based on matrices from different libraries, using the following algorithm (here based on armadillo):

inline void loop_over_matrix_serial(const size_t &size, arma::mat &matrix)
{
    for (size_t i = 0; i < size; ++i)
        for (size_t j = 0; j < size; ++j)
            matrix(i, j) += position_function(i, j);
}

In order to speed that operation up I would like to use a reduction with OpenMP. There are already custom reduction operators in other questions, such as here: C++ Armadillo and OpenMp: Parallelization of summation of outer products - define reduction for Armadillo matrix, but it is targeted on full matrices, not on single elements. How could I define a custom operator for all matrices which are accessed in the shown way, but from different libraries, for the reduction? It will always be a double-value which is added from the right side.

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • 2
    I see no reduction going on in your code -- just the setting of each element in an array to a computed value. Reductions are called reductions because they reduce the dimensionality of data from input to output. So, for example, the `dot_product` reduces two vectors to a single scalar. Row-wise summation on a 2D structure produces a 1D structure. – High Performance Mark Aug 24 '17 at 13:29

1 Answers1

-1

If I were to optimize this code, I'll first get to know the size of matrix.

if it's big, I would consider writing a cache-friendly code.

in your case, if your several matrices can't be held in the cache, you also need do that. refer to this link, What is "cache-friendly" code?

omp reduction may be less important after cache optimizing.

Clark Lee
  • 169
  • 6