移调大小1 GB与平铺方法(高速缓存感知)的全球2D方阵/阵列大2D矩阵的转置没有性能增益具有在单线程执行在普通转置方法没有性能增益。未讨论的转置加速使用AVX,SSE(SIMD)或任何其它高速缓存不经意转置算法(http://supertech.csail.mit.edu/papers/FrigoLePr12.pdf)使用环路平铺
#include <stdio.h>
#include <sys/time.h>
#define SIZE 16384
float a[SIZE][SIZE], b[SIZE][SIZE];
void testNormalTranspose() {
int i, j, k, l;
b[0][9999] = 1.0;
for (i=0; i<SIZE; i++)
for (j=0; j<SIZE; j++)
a[i][j] = b[j][i];
}
void testTiledTranspose(){
int i, j, k, l;
b[0][9999] = 1.0;
int blocksize = 16;
for (i=0; i<SIZE; i+= blocksize) {
for (j=0; j<SIZE; j+=blocksize) {
for (int ii = i;ii <i + blocksize; ++ii) {
for (int jj = j; jj < j + blocksize; ++jj) {
a[ii][jj] = b[jj][ii];
}
}
}
}
}
int main()
{
struct timeval t1, t2;
/*
gettimeofday(&t1, NULL);
testNormalTranspose();
gettimeofday(&t2, NULL);
printf("Time for the Normal transpose is %ld milliseconds\n",
(t2.tv_sec - t1.tv_sec)*1000 +
(t2.tv_usec - t1.tv_usec)/1000);
*/
gettimeofday(&t1, NULL);
testTiledTranspose();
gettimeofday(&t2, NULL);
printf("Time for the Tiled transpose is %ld milliseconds\n",
(t2.tv_sec - t1.tv_sec)*1000 +
(t2.tv_usec - t1.tv_usec)/1000);
printf("%f\n", a[9999][0]);
}
如果你不喜欢谈论缓存cohercency,北京时间什么你的问题,为什么shold一种方法要快呃比另一个。 – schorsch312
平铺提供了空间局部性。它如何帮助提高上述方法的性能:testTiledTranspose – Dhiraj
无法重现失败。我所做的所有测试都可以显着提高性能(2.5..3.2倍)。其他事情正在发生。 –