【问题标题】:Why is grouped summation slower with sorted groups than unsorted groups?为什么已排序组的分组求和比未排序组慢?
【发布时间】:2020-02-21 09:32:53
【问题描述】:

我有 2 列制表符分隔的整数,第一列是随机整数,第二列是标识组的整数,可以由该程序生成。 (generate_groups.cc)

#include <cstdlib>
#include <iostream>
#include <ctime>

int main(int argc, char* argv[]) {
  int num_values = atoi(argv[1]);
  int num_groups = atoi(argv[2]);

  int group_size = num_values / num_groups;
  int group = -1;

  std::srand(42);

  for (int i = 0; i < num_values; ++i) {
    if (i % group_size == 0) {
      ++group;
    }
    std::cout << std::rand() << '\t' << group << '\n';
  }

  return 0;
}

然后我使用第二个程序 (sum_groups.cc) 来计算每组的总和。

#include <iostream>
#include <chrono>
#include <vector>

// This is the function whose performance I am interested in
void grouped_sum(int* p_x, int *p_g, int n, int* p_out) {
  for (size_t i = 0; i < n; ++i) {
    p_out[p_g[i]] += p_x[i];
  }
}

int main() {
  std::vector<int> values;
  std::vector<int> groups;
  std::vector<int> sums;

  int n_groups = 0;

  // Read in the values and calculate the max number of groups
  while(std::cin) {
    int value, group;
    std::cin >> value >> group;
    values.push_back(value);
    groups.push_back(group);
    if (group > n_groups) {
      n_groups = group;
    }
  }
  sums.resize(n_groups);

  // Time grouped sums
  std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  for (int i = 0; i < 10; ++i) {
    grouped_sum(values.data(), groups.data(), values.size(), sums.data());
  }
  std::chrono::system_clock::time_point end = std::chrono::system_clock::now();

  std::cout << (end - start).count() << std::endl;

  return 0;
}

如果我随后在给定大小的数据集上运行这些程序,然后打乱同一数据集的行的顺序,则打乱后的数据计算总和的速度比有序数据快约 2 倍或更多。

g++ -O3 generate_groups.cc -o generate_groups
g++ -O3 sum_groups.cc -o sum_groups
generate_groups 1000000 100 > groups
shuf groups > groups2
sum_groups < groups
sum_groups < groups2
sum_groups < groups2
sum_groups < groups
20784
8854
8220
21006

我原以为按组排序的原始数据具有更好的数据局部性并且速度更快,但我观察到相反的行为。我想知道是否有人可以假设原因?

【问题讨论】:

  • 我不知道,但是您正在写入 sums 向量的超出范围的元素 - 如果您执行正常操作并传递对向量的引用而不是指向数据元素的指针,然后使用.at() 或调试模式operator[] 进行边界检查。
  • 您是否确认“groups2”文件中包含您的所有数据,并且所有数据都在被读取和处理?中间的某个地方可能有一个 EOF 字符吗?
  • 程序具有未定义的行为,因为您从未调整 sum 的大小。而不是sums.reserve(n_groups);,您必须调用sums.resize(n_groups); - 这就是@Shawn 所暗示的。
  • 请注意(参见例如 herehere),成对的向量,而不是两个向量(值和组)的行为符合预期。
  • 您根据值对数据进行了排序,对吧?但这也会对组进行排序,这会对 xpression p_out[p_g[i]] += p_x[i]; 产生影响。也许在最初的打乱顺序中,这些组实际上在访问p_out 数组方面表现出良好的聚类。对值进行排序可能会导致对p_out 的组索引访问模式不佳。

标签: c++ performance


【解决方案1】:

设置/让它变慢

首先,不管怎样,程序运行的时间都差不多:

sumspeed$ time ./sum_groups < groups_shuffled 
11558358

real    0m0.705s
user    0m0.692s
sys 0m0.013s

sumspeed$ time ./sum_groups < groups_sorted
24986825

real    0m0.722s
user    0m0.711s
sys 0m0.012s

大部分时间都花在输入循环上。但既然我们对grouped_sum() 感兴趣,我们就忽略它吧。

将基准循环从 10 次迭代更改为 1000 次迭代,grouped_sum() 开始主导运行时间:

sumspeed$ time ./sum_groups < groups_shuffled 
1131838420

real    0m1.828s
user    0m1.811s
sys 0m0.016s

sumspeed$ time ./sum_groups < groups_sorted
2494032110

real    0m3.189s
user    0m3.169s
sys 0m0.016s

性能差异

现在我们可以使用perf 来查找我们程序中的最热点。

sumspeed$ perf record ./sum_groups < groups_shuffled
1166805982
[ perf record: Woken up 1 times to write data ]
[kernel.kallsyms] with build id 3a2171019937a2070663f3b6419330223bd64e96 not found, continuing without symbols
Warning:
Processed 4636 samples and lost 6.95% samples!

[ perf record: Captured and wrote 0.176 MB perf.data (4314 samples) ]

sumspeed$ perf record ./sum_groups < groups_sorted
2571547832
[ perf record: Woken up 2 times to write data ]
[kernel.kallsyms] with build id 3a2171019937a2070663f3b6419330223bd64e96 not found, continuing without symbols
[ perf record: Captured and wrote 0.420 MB perf.data (10775 samples) ]

以及它们之间的区别:

sumspeed$ perf diff
[...]
# Event 'cycles:uppp'
#
# Baseline  Delta Abs  Shared Object        Symbol                                                                  
# ........  .........  ...................  ........................................................................
#
    57.99%    +26.33%  sum_groups           [.] main
    12.10%     -7.41%  libc-2.23.so         [.] _IO_getc
     9.82%     -6.40%  libstdc++.so.6.0.21  [.] std::num_get<char, std::istreambuf_iterator<char, std::char_traits<c
     6.45%     -4.00%  libc-2.23.so         [.] _IO_ungetc
     2.40%     -1.32%  libc-2.23.so         [.] _IO_sputbackc
     1.65%     -1.21%  libstdc++.so.6.0.21  [.] 0x00000000000dc4a4
     1.57%     -1.20%  libc-2.23.so         [.] _IO_fflush
     1.71%     -1.07%  libstdc++.so.6.0.21  [.] std::istream::sentry::sentry
     1.22%     -0.77%  libstdc++.so.6.0.21  [.] std::istream::operator>>
     0.79%     -0.47%  libstdc++.so.6.0.21  [.] __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::uflow
[...]

更多时间在main(),其中可能有grouped_sum() 内联。太好了,非常感谢,perf。

perf 注释

main()987654335@main()花费的时间有什么不同吗?

洗牌:

sumspeed$ perf annotate -i perf.data.old
[...]
       │     // This is the function whose performance I am interested in
       │     void grouped_sum(int* p_x, int *p_g, int n, int* p_out) {
       │       for (size_t i = 0; i < n; ++i) {
       │180:   xor    %eax,%eax
       │       test   %rdi,%rdi
       │     ↓ je     1a4
       │       nop
       │         p_out[p_g[i]] += p_x[i];
  6,88 │190:   movslq (%r9,%rax,4),%rdx
 58,54 │       mov    (%r8,%rax,4),%esi
       │     #include <chrono>
       │     #include <vector>
       │
       │     // This is the function whose performance I am interested in
       │     void grouped_sum(int* p_x, int *p_g, int n, int* p_out) {
       │       for (size_t i = 0; i < n; ++i) {
  3,86 │       add    $0x1,%rax
       │         p_out[p_g[i]] += p_x[i];
 29,61 │       add    %esi,(%rcx,%rdx,4)
[...]

排序:

sumspeed$ perf annotate -i perf.data
[...]
       │     // This is the function whose performance I am interested in
       │     void grouped_sum(int* p_x, int *p_g, int n, int* p_out) {
       │       for (size_t i = 0; i < n; ++i) {
       │180:   xor    %eax,%eax
       │       test   %rdi,%rdi
       │     ↓ je     1a4
       │       nop
       │         p_out[p_g[i]] += p_x[i];
  1,00 │190:   movslq (%r9,%rax,4),%rdx
 55,12 │       mov    (%r8,%rax,4),%esi
       │     #include <chrono>
       │     #include <vector>
       │
       │     // This is the function whose performance I am interested in
       │     void grouped_sum(int* p_x, int *p_g, int n, int* p_out) {
       │       for (size_t i = 0; i < n; ++i) {
  0,07 │       add    $0x1,%rax
       │         p_out[p_g[i]] += p_x[i];
 43,28 │       add    %esi,(%rcx,%rdx,4)
[...]

不,同样是两条指令占主导地位。因此在这两种情况下都需要很长时间,但在对数据进行排序时会更糟。

性能统计

好的。但是我们应该运行它们相同的次数,所以每条指令都必须因为某种原因变慢。让我们看看perf stat 怎么说。

sumspeed$ perf stat ./sum_groups < groups_shuffled 
1138880176

 Performance counter stats for './sum_groups':

       1826,232278      task-clock (msec)         #    0,999 CPUs utilized          
                72      context-switches          #    0,039 K/sec                  
                 1      cpu-migrations            #    0,001 K/sec                  
             4 076      page-faults               #    0,002 M/sec                  
     5 403 949 695      cycles                    #    2,959 GHz                    
       930 473 671      stalled-cycles-frontend   #   17,22% frontend cycles idle   
     9 827 685 690      instructions              #    1,82  insn per cycle         
                                                  #    0,09  stalled cycles per insn
     2 086 725 079      branches                  # 1142,639 M/sec                  
         2 069 655      branch-misses             #    0,10% of all branches        

       1,828334373 seconds time elapsed

sumspeed$ perf stat ./sum_groups < groups_sorted
2496546045

 Performance counter stats for './sum_groups':

       3186,100661      task-clock (msec)         #    1,000 CPUs utilized          
                 5      context-switches          #    0,002 K/sec                  
                 0      cpu-migrations            #    0,000 K/sec                  
             4 079      page-faults               #    0,001 M/sec                  
     9 424 565 623      cycles                    #    2,958 GHz                    
     4 955 937 177      stalled-cycles-frontend   #   52,59% frontend cycles idle   
     9 829 009 511      instructions              #    1,04  insn per cycle         
                                                  #    0,50  stalled cycles per insn
     2 086 942 109      branches                  #  655,014 M/sec                  
         2 078 204      branch-misses             #    0,10% of all branches        

       3,186768174 seconds time elapsed

只有一件事很突出:stalled-cycles-frontend

好的,指令流水线正在停止。在前端。确切地说,what that means 可能因微架构而异。

不过,我有一个猜测。如果你很慷慨,你甚至可以称之为假设。

假设

通过对输入进行排序,您可以增加写入的局部性。事实上,它们将是非常本地化的;您所做的几乎所有添加都将写入与前一个相同的位置。

这对缓存很好,但对管道来说不是很好。您正在引入数据依赖关系,阻止下一个添加指令继续执行,直到上一个添加完成(或 otherwise made the result available to succeeding instructions

那是你的问题。

我认为。

修复它

多个和向量

实际上,让我们尝试一下。如果我们使用多个和向量,在每次加法时在它们之间切换,然后在最后将它们相加怎么办?它花费了我们一点地方性,但应该消除数据依赖性。

(代码不漂亮;不要评判我,互联网!!)

#include <iostream>
#include <chrono>
#include <vector>

#ifndef NSUMS
#define NSUMS (4) // must be power of 2 (for masking to work)
#endif

// This is the function whose performance I am interested in
void grouped_sum(int* p_x, int *p_g, int n, int** p_out) {
  for (size_t i = 0; i < n; ++i) {
    p_out[i & (NSUMS-1)][p_g[i]] += p_x[i];
  }
}

int main() {
  std::vector<int> values;
  std::vector<int> groups;
  std::vector<int> sums[NSUMS];

  int n_groups = 0;

  // Read in the values and calculate the max number of groups
  while(std::cin) {
    int value, group;
    std::cin >> value >> group;
    values.push_back(value);
    groups.push_back(group);
    if (group >= n_groups) {
      n_groups = group+1;
    }
  }
  for (int i=0; i<NSUMS; ++i) {
    sums[i].resize(n_groups);
  }

  // Time grouped sums
  std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  int* sumdata[NSUMS];
  for (int i = 0; i < NSUMS; ++i) {
    sumdata[i] = sums[i].data();
  }
  for (int i = 0; i < 1000; ++i) {
    grouped_sum(values.data(), groups.data(), values.size(), sumdata);
  }
  for (int i = 1; i < NSUMS; ++i) {
    for (int j = 0; j < n_groups; ++j) {
      sumdata[0][j] += sumdata[i][j];
    }
  }
  std::chrono::system_clock::time_point end = std::chrono::system_clock::now();

  std::cout << (end - start).count() << " with NSUMS=" << NSUMS << std::endl;

  return 0;
}

(哦,我还修正了 n_groups 的计算;它差了一个。)

结果

在配置我的 makefile 以向编译器提供 -DNSUMS=... 参数后,我可以这样做:

sumspeed$ for n in 1 2 4 8 128; do make -s clean && make -s NSUMS=$n && (perf stat ./sum_groups < groups_shuffled && perf stat ./sum_groups < groups_sorted)  2>&1 | egrep '^[0-9]|frontend'; done
1134557008 with NSUMS=1
       924 611 882      stalled-cycles-frontend   #   17,13% frontend cycles idle   
2513696351 with NSUMS=1
     4 998 203 130      stalled-cycles-frontend   #   52,79% frontend cycles idle   
1116188582 with NSUMS=2
       899 339 154      stalled-cycles-frontend   #   16,83% frontend cycles idle   
1365673326 with NSUMS=2
     1 845 914 269      stalled-cycles-frontend   #   29,97% frontend cycles idle   
1127172852 with NSUMS=4
       902 964 410      stalled-cycles-frontend   #   16,79% frontend cycles idle   
1171849032 with NSUMS=4
     1 007 807 580      stalled-cycles-frontend   #   18,29% frontend cycles idle   
1118732934 with NSUMS=8
       881 371 176      stalled-cycles-frontend   #   16,46% frontend cycles idle   
1129842892 with NSUMS=8
       905 473 182      stalled-cycles-frontend   #   16,80% frontend cycles idle   
1497803734 with NSUMS=128
     1 982 652 954      stalled-cycles-frontend   #   30,63% frontend cycles idle   
1180742299 with NSUMS=128
     1 075 507 514      stalled-cycles-frontend   #   19,39% frontend cycles idle   

和向量的最佳数量可能取决于 CPU 的流水线深度。我使用了 7 年的超极本 CPU 可能会用比新的精美台式机 CPU 所需的更少的向量来最大化管道。

显然,更多不一定更好;当我对 128 个 sum 向量发疯时,我们开始遭受更多缓存未命中的困扰——正如您最初预期的那样,shuffled 输入变得比 sorted 慢就证明了这一点。我们绕了一圈! :)

寄存器中的每组总和

(这是在编辑中添加的)

啊,nerd sniped!如果您知道您的输入将被排序并且正在寻找更高的性能,那么下面的函数重写(没有额外的 sum 数组)会更快,至少在我的计算机上是这样。

// This is the function whose performance I am interested in
void grouped_sum(int* p_x, int *p_g, int n, int* p_out) {
  int i = n-1;
  while (i >= 0) {
    int g = p_g[i];
    int gsum = 0;
    do {
      gsum += p_x[i--];
    } while (i >= 0 && p_g[i] == g);
    p_out[g] += gsum;
  }
}

这个技巧是它允许编译器将gsum 变量(组的总和)保存在寄存器中。我猜测(但可能是非常错误的)这更快,因为管道中的反馈循环在这里可以更短,和/或更少的内存访问。一个好的分支预测器将使对组相等性的额外检查变得便宜。

结果

乱序输入太糟糕了...

sumspeed$ time ./sum_groups < groups_shuffled
2236354315

real    0m2.932s
user    0m2.923s
sys 0m0.009s

...但是比我的排序输入的“多和”解决方案快 40% 左右。

sumspeed$ time ./sum_groups < groups_sorted
809694018

real    0m1.501s
user    0m1.496s
sys 0m0.005s

很多小团体会比一些大团体慢,所以这是否是更快的实施将真的取决于您在这里的数据。而且,一如既往,在您的 CPU 型号上。

多个和向量,使用偏移量而不是位掩码

Sopel 建议使用四个展开的添加来替代我的位屏蔽方法。我已经实现了他们建议的通用版本,可以处理不同的NSUMS。我指望编译器为我们展开内部循环(它确实做到了,至少对于NSUMS=4)。

#include <iostream>
#include <chrono>
#include <vector>

#ifndef NSUMS
#define NSUMS (4) // must be power of 2 (for masking to work)
#endif

#ifndef INNER
#define INNER (0)
#endif
#if INNER
// This is the function whose performance I am interested in
void grouped_sum(int* p_x, int *p_g, int n, int** p_out) {
  size_t i = 0;
  int quadend = n & ~(NSUMS-1);
  for (; i < quadend; i += NSUMS) {
    for (int k=0; k<NSUMS; ++k) {
      p_out[k][p_g[i+k]] += p_x[i+k];
    }
  }
  for (; i < n; ++i) {
    p_out[0][p_g[i]] += p_x[i];
  }
}
#else
// This is the function whose performance I am interested in
void grouped_sum(int* p_x, int *p_g, int n, int** p_out) {
  for (size_t i = 0; i < n; ++i) {
    p_out[i & (NSUMS-1)][p_g[i]] += p_x[i];
  }
}
#endif


int main() {
  std::vector<int> values;
  std::vector<int> groups;
  std::vector<int> sums[NSUMS];

  int n_groups = 0;

  // Read in the values and calculate the max number of groups
  while(std::cin) {
    int value, group;
    std::cin >> value >> group;
    values.push_back(value);
    groups.push_back(group);
    if (group >= n_groups) {
      n_groups = group+1;
    }
  }
  for (int i=0; i<NSUMS; ++i) {
    sums[i].resize(n_groups);
  }

  // Time grouped sums
  std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  int* sumdata[NSUMS];
  for (int i = 0; i < NSUMS; ++i) {
    sumdata[i] = sums[i].data();
  }
  for (int i = 0; i < 1000; ++i) {
    grouped_sum(values.data(), groups.data(), values.size(), sumdata);
  }
  for (int i = 1; i < NSUMS; ++i) {
    for (int j = 0; j < n_groups; ++j) {
      sumdata[0][j] += sumdata[i][j];
    }
  }
  std::chrono::system_clock::time_point end = std::chrono::system_clock::now();

  std::cout << (end - start).count() << " with NSUMS=" << NSUMS << ", INNER=" << INNER << std::endl;

  return 0;
}

结果

测量时间。请注意,由于我昨天在 /tmp 中工作,因此我没有完全相同的输入数据。因此,这些结果无法直接与之前的结果进行比较(但可能足够接近)。

sumspeed$ for n in 2 4 8 16; do for inner in 0 1; do make -s clean && make -s NSUMS=$n INNER=$inner && (perf stat ./sum_groups < groups_shuffled && perf stat ./sum_groups < groups_sorted)  2>&1 | egrep '^[0-9]|frontend'; done; done1130558787 with NSUMS=2, INNER=0
       915 158 411      stalled-cycles-frontend   #   16,96% frontend cycles idle   
1351420957 with NSUMS=2, INNER=0
     1 589 408 901      stalled-cycles-frontend   #   26,21% frontend cycles idle   
840071512 with NSUMS=2, INNER=1
     1 053 982 259      stalled-cycles-frontend   #   23,26% frontend cycles idle   
1391591981 with NSUMS=2, INNER=1
     2 830 348 854      stalled-cycles-frontend   #   45,35% frontend cycles idle   
1110302654 with NSUMS=4, INNER=0
       890 869 892      stalled-cycles-frontend   #   16,68% frontend cycles idle   
1145175062 with NSUMS=4, INNER=0
       948 879 882      stalled-cycles-frontend   #   17,40% frontend cycles idle   
822954895 with NSUMS=4, INNER=1
     1 253 110 503      stalled-cycles-frontend   #   28,01% frontend cycles idle   
929548505 with NSUMS=4, INNER=1
     1 422 753 793      stalled-cycles-frontend   #   30,32% frontend cycles idle   
1128735412 with NSUMS=8, INNER=0
       921 158 397      stalled-cycles-frontend   #   17,13% frontend cycles idle   
1120606464 with NSUMS=8, INNER=0
       891 960 711      stalled-cycles-frontend   #   16,59% frontend cycles idle   
800789776 with NSUMS=8, INNER=1
     1 204 516 303      stalled-cycles-frontend   #   27,25% frontend cycles idle   
805223528 with NSUMS=8, INNER=1
     1 222 383 317      stalled-cycles-frontend   #   27,52% frontend cycles idle   
1121644613 with NSUMS=16, INNER=0
       886 781 824      stalled-cycles-frontend   #   16,54% frontend cycles idle   
1108977946 with NSUMS=16, INNER=0
       860 600 975      stalled-cycles-frontend   #   16,13% frontend cycles idle   
911365998 with NSUMS=16, INNER=1
     1 494 671 476      stalled-cycles-frontend   #   31,54% frontend cycles idle   
898729229 with NSUMS=16, INNER=1
     1 474 745 548      stalled-cycles-frontend   #   31,24% frontend cycles idle   

是的,NSUMS=8 的内部循环是我电脑上最快的。与我的“本地 gsum”方法相比,它还有一个额外的好处,那就是对于 shuffled 输入不会变得糟糕。

有趣的是:NSUMS=16 变得比NSUMS=8 更糟。这可能是因为我们开始看到更多的缓存未命中,或者因为我们没有足够的寄存器来正确展开内部循环。

【讨论】:

  • 这很有趣。 :)
  • 太棒了!不知道perf
  • 我想知道在您的第一种方法中,使用 4 个不同的累加器手动展开 4x 是否会产生更好的性能。类似godbolt.org/z/S-PhFm
  • 感谢您的建议。是的,这提高了性能,我已将其添加到答案中。
  • 谢谢!我曾考虑过这种可能性,但不知道如何确定,感谢您的详细回答!
【解决方案2】:

这就是排序组比未排序组慢的原因;

首先是求和循环的汇编代码:

008512C3  mov         ecx,dword ptr [eax+ebx]
008512C6  lea         eax,[eax+4]
008512C9  lea         edx,[esi+ecx*4] // &sums[groups[i]]
008512CC  mov         ecx,dword ptr [eax-4] // values[i]
008512CF  add         dword ptr [edx],ecx // sums[groups[i]]+=values[i]
008512D1  sub         edi,1
008512D4  jne         main+163h (08512C3h)

让我们看看 add 指令是这个问题的主要原因;

008512CF  add         dword ptr [edx],ecx // sums[groups[i]]+=values[i]

当处理器首先执行该指令时,它会向 edx 中的地址发出内存读取(加载)请求,然后添加 ecx 的值,然后对同一地址发出写入(存储)请求。

处理器调用者内存重新排序中有一个功能

为了优化指令执行的性能,IA-32 架构允许偏离称为强排序模型 Pentium 4、Intel Xeon 和 P6 系列处理器中的处理器订购。 这些处理器排序变化(这里称为内存排序 模型)允许性能增强操作,例如允许读取 继续缓冲写入。任何这些变体的目标是 提高指令执行速度,同时保持内存 连贯性,即使在多处理器系统中也是如此。

还有一个规则

读取可能会随着旧写入到不同位置而重新排序,但 不是旧的写入相同的位置。

因此,如果下一次迭代在写请求完成之前到达 add 指令,如果 edx 地址与之前的值不同,它不会等待并发出读请求,它会重新排序较旧的写请求,并且 add 指令继续。但如果地址相同,则添加指令将等到旧写入完成。

请注意,循环很短,处理器执行它的速度比内存控制器完成写入内存请求的速度要快。

因此,对于已排序的组,您将连续多次从同一地址读取和写入,因此它将失去使用内存重新排序的性能增强; 同时,如果使用随机组,则每次迭代可能具有不同的地址,因此读取不会等待较旧的写入并在其之前重新排序;添加指令不会等待前一个执行。

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 2016-05-03
    相关资源
    最近更新 更多