沿多维数组的任意轴进行归约(求和)

Tom*_*eus 5 c++ algorithm

我想沿着可能具有任意维度的多维矩阵的任意轴(例如 10 维数组的轴 5)执行求和缩减。矩阵使用行主格式存储,即vector与沿每个轴的步幅一起存储。

我知道如何使用嵌套循环执行此缩减(请参见下面的示例),但这样做会产生硬编码轴(缩减沿下面的轴 1)和任意数量的维度(下面的 4)。在不使用嵌套循环的情况下如何概括这一点?


#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;
}
Run Code Online (Sandbox Code Playgroud)

注意:我想避免“解压缩”和“压缩”计数器。我的意思是,我可以用伪代码执行以下操作:

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)
}
Run Code Online (Sandbox Code Playgroud)

vis*_*hwa 4

我不知道这段代码的效率如何,但在我看来,它肯定是精确的。

这是怎么回事?

一点adjusted_strides

对于axis_count = 4adjusted_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;
Run Code Online (Sandbox Code Playgroud)

让我们以维数为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. 压缩轴之前的所有轴都具有压缩轴的形状作为其乘积的一部分。在我们的示例中,我们必须删除 axis 1,因此第一个轴之前的所有轴(这里只有一个:由0th axis) 表示的i) 都n1作为乘积 ( ) 的一部分i*n1*n2*n3

  2. 压缩轴之后的所有轴均不受影响。

  3. 最后,我们需要做两件事:

    1. 隔离要压缩的轴索引之前的轴索引并删除该轴的形状:

      整数除法:( idx / (n1*n2*n3);) == idx / adjusted_strides[1]

      我们只剩下i,它可以根据新的形状重新调整(通过乘以n2*n3):我们得到

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

    2. 我们隔离压缩轴之后的轴,这些轴不受其形状的影响。

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

      这给了我们k*n3 + l.

    3. 添加步骤 i 的结果ii . 结果是:

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

      这与 相同new_idx。所以,我们的转变是正确的:)。

代码:

注:ninew_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];
  }
Run Code Online (Sandbox Code Playgroud)

输出(轴 = 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
...
Run Code Online (Sandbox Code Playgroud)

在这里测试过。

如需进一步阅读,请参阅