简体   繁体   English

为什么循环顺序在大步预取时很重要?

[英]Why does loop order matter when there's strided prefetching?

In C you're told to iterate through a matrix in a row-major order since that's how the arrays are stored underneath the hood and row-major iteration is utilizes the whole cache-line, which leads to fewer cache misses. 在C中,您被告知以行 - 主要顺序迭代矩阵,因为这是阵列存储在引擎盖下的方式,行主要迭代利用整个缓存行,这导致更少的缓存未命中。 And indeed, I do see a massive performance difference between row-major and column-major iteration on my machine. 事实上,我确实看到我的机器上的行主要和列主要迭代之间存在巨大的性能差异。 Test code: 测试代码:

#include <stdio.h>
#include <stdlib.h>

#include <time.h>
#include <sys/resource.h>

int getTime()
{
  struct timespec tsi;

  clock_gettime(CLOCK_MONOTONIC, &tsi);
  double elaps_s = tsi.tv_sec;
  long elaps_ns = tsi.tv_nsec;
  return (int) ((elaps_s + ((double)elaps_ns) / 1.0e9) * 1.0e3);
}

#define N 1000000
#define M 100

void main()
{
  int *src = malloc(sizeof(int) * N * M);
  int **arr = malloc(sizeof(int*) * N);
  for(int i = 0; i < N; ++i)
    arr[i] = &src[i * M];

  for(int i = 0; i < N; ++i)
    for(int j = 0; j < M; ++j)
      arr[i][j] = 1;

  int total = 0;

  int pre = getTime();


  for(int j = 0; j < M; ++j)
    for(int i = 0; i < N; ++i)
      total += arr[i][j];

  /*
  for(int i = 0; i < N; ++i)
    for(int j = 0; j < M; ++j)
      total += arr[i][j];
  */

  int post = getTime();

  printf("Result: %d, took: %d ms\n", total, post - pre);
}

However, modern memory systems have prefetchers which can predict strided accesses and when you iterate through a column you are following a very regular pattern. 但是,现代存储系统具有可以预测跨步访问的预取程序,当您遍历列时,您遵循非常规则的模式。 Shouldn't this allow column-major iteration to perform similarly to row-major iteration? 这不应该允许列主要迭代执行与行主要迭代类似的操作吗?

A cache line has a certain size (for example 64 bytes) and the processor reads and writes complete cache lines. 高速缓存行具有特定大小(例如64字节),并且处理器读取和写入完整的高速缓存行。 Compare the number of bytes that are processed and the number of bytes that are read and written. 比较处理的字节数和读取和写入的字节数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM