Reduction (sum) along arbitrary axes of a multidimensional array

I want to reduce the sum over arbitrary axes of a multidimensional matrix, which can have arbitrary dimensions (for example, axis 5 of a 10-dimensional array). The matrix is ​​saved using the format of the main row, i.e. As vectorwith steps along each axis.

I know how to perform this reduction using nested loops (see the example below), but this leads to a hard-coded axis (reduction along axis 1 below) and an arbitrary number of dimensions (see below). How can I generalize this without using nested loops?


#include <iostream>
#include <vector>

int main()
{
  // shape, stride & data of the matrix

  size_t shape  [] = { 2, 3, 4, 5};
  size_t strides[] = {60,20, 5, 1};

  std::vector<double> data(2*3*4*5);

  for ( size_t i = 0 ; i < data.size() ; ++i ) data[i] = 1.;

  // shape, stride & data (zero-initialized) of the reduced matrix

  size_t rshape  [] = { 2, 4, 5};
  size_t rstrides[] = {20, 5, 1};

  std::vector<double> rdata(2*4*5, 0.0);

  // compute reduction

  for ( size_t a = 0 ; a < shape[0] ; ++a )
    for ( size_t c = 0 ; c < shape[2] ; ++c )
      for ( size_t d = 0 ; d < shape[3] ; ++d )
        for ( size_t b = 0 ; b < shape[1] ; ++b )
          rdata[ a*rstrides[0]                 + c*rstrides[1] + d*rstrides[2] ] += \
          data [ a*strides [0] + b*strides [1] + c*strides [2] + d*strides [3] ];

  // print resulting reduced matrix

  for ( size_t a = 0 ; a < rshape[0] ; ++a )
    for ( size_t b = 0 ; b < rshape[1] ; ++b )
      for ( size_t c = 0 ; c < rshape[2] ; ++c )
        std::cout << "(" << a << "," << b << "," << c << ") " << \
        rdata[ a*rstrides[0] + b*rstrides[1] + c*rstrides[2] ] << std::endl;

  return 0;
}

Note. I want to avoid the "decompression" and "compression" of the counter. By this I mean that I could do in pseudo-code:

for ( size_t i = 0 ; i < data.size() ; ++i ) 
{
  i -> {a,b,c,d}

  discard "b" (axis 1) -> {a,c,d}

  rdata(a,c,d) += data(a,b,c,d)
}
+4
source share
2

, , , , .

?

adjusted_strides:

axis_count = 4, adjusted_strides 5, :

 adjusted_strides[0] = shape[0]*shape[1]*shape[2]*shape[3];
 adjusted_strides[1] = shape[1]*shape[2]*shape[3];
 adjusted_strides[2] = shape[2]*shape[3];
 adjusted_strides[3] = shape[3];
 adjusted_strides[4] = 1;

, 4, (A) n0, n1, n2, n3.

(B) : n0, n2, n3 ( axis = 1 (0-based)), :

A B. A[i][j][k][l] - A. flat_A A[i*n1*n2*n3 + j*n2*n3 + k*n3 + l]

idx = i*n1*n2*n3 + j*n2*n3 + k*n3 + l;

B ( ), B[i][k][l]. flat_B new_idx = i*n2*n3 + k*n3 + l;.

new_idx idx?

  • . 1, , 1- ( : 0th axis), i), n1 (i*n1*n2*n3).

  • .

  • , :

    • , :

      : idx / (n1*n2*n3); (== idx / adjusted_strides[1]).

      i, ( n2*n3):

      i*n2*n3 (== i * adjusted_strides[2]).

    • , .

      idx % (n2*n3) (== idx % adjusted_strides[2])

      k*n3 + l.

    • i. ii. :

      computed_idx = i*n2*n3 + k*n3 + l;

      , new_idx. , :).

:

: ni new_idx.

  size_t cmp_axis = 1, axis_count = sizeof shape/ sizeof *shape;
  std::vector<size_t> adjusted_strides;
  //adjusted strides is basically same as strides
  //only difference being that the first element is the 
  //total number of elements in the n dim array.

  //The only reason to introduce this array was
  //so that I don't have to write any if-elses
  adjusted_strides.push_back(shape[0]*strides[0]);
  adjusted_strides.insert(adjusted_strides.end(), strides, strides + axis_count);
  for(size_t i = 0; i < data.size(); ++i) {
    size_t ni = i/adjusted_strides[cmp_axis]*adjusted_strides[cmp_axis+1] + i%adjusted_strides[cmp_axis+1];
    rdata[ni] += data[i];
  }

( = 1)

(0,0,0) 3
(0,0,1) 3
(0,0,2) 3
(0,0,3) 3
(0,0,4) 3
(0,1,0) 3
(0,1,1) 3
(0,1,2) 3
(0,1,3) 3
(0,1,4) 3
(0,2,0) 3
(0,2,1) 3
(0,2,2) 3
(0,2,3) 3
(0,2,4) 3
(0,3,0) 3
(0,3,1) 3
(0,3,2) 3
...

.

this.

+3

, :

#include <iostream>
#include <vector>

int main()
{
  // shape, stride & data of the matrix
  size_t shape  [] = {  2, 3, 4, 5};
  size_t strides[] = {60, 20, 5, 1};
  std::vector<double> data(2 * 3 * 4 * 5);

  size_t rshape  [] = { 2, 4, 5};
  size_t rstrides[] = {3, 5, 1};
  std::vector<double> rdata(2 * 4 * 5, 0.0);

  const unsigned int NDIM = 4;
  unsigned int axis = 1;

  for (size_t i = 0 ; i < data.size() ; ++i) data[i] = 1;

  // How many elements to advance after each reduction
  size_t step_axis = strides[NDIM - 1];
  if (axis == NDIM - 1)
  {
      step_axis = strides[NDIM - 2];
  }
  // Position of the first element of the current reduction
  size_t offset_base = 0;
  size_t offset = 0;
  size_t s = 0;
  for (auto &v : rdata)
  {
      // Current reduced element
      size_t offset_i = offset;
      for (unsigned int i = 0; i < shape[axis]; i++)
      {
          // Reduce
          v += *(data.data() + offset_i);
          // Advance to next element
          offset_i += strides[axis];
      }
      s = (s + 1) % strides[axis];
      if (s == 0)
      {
          offset_base += strides[axis - 1];
          offset = offset_base;
      }
      else
      {
          offset += step_axis;
      }
  }

  // Print
  for ( size_t a = 0 ; a < rshape[0] ; ++a )
    for ( size_t b = 0 ; b < rshape[1] ; ++b )
      for ( size_t c = 0 ; c < rshape[2] ; ++c )
        std::cout << "(" << a << "," << b << "," << c << ") " << \
        rdata[ a*rstrides[0] + b*rstrides[1] + c*rstrides[2] ] << std::endl;

  return 0;
}

:

(0,0,0) 3
(0,0,1) 3
(0,0,2) 3
(0,0,3) 3
(0,0,4) 3
(0,1,0) 3
(0,1,1) 3
(0,1,2) 3
(0,1,3) 3
(0,1,4) 3
(0,2,0) 3
(0,2,1) 3
(0,2,2) 3
// ...

axis = 3 :

(0,0,0) 5
(0,0,1) 5
(0,0,2) 5
(0,0,3) 5
(0,0,4) 5
(0,1,0) 5
(0,1,1) 5
(0,1,2) 5
(0,1,3) 5
(0,1,4) 5
(0,2,0) 5
(0,2,1) 5
(0,2,2) 5
(0,2,3) 5
// ...
+1

Source: https://habr.com/ru/post/1696281/


All Articles