移調大小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倍)。其他事情正在發生。 –