我想优化这个简单的循环:
unsigned int i;
while(j-- != 0){ //j is an unsigned int with a start value of about N = 36.000.000
float sub = 0;
i=1;
unsigned int c = j+s[1];
while(c < N) {
sub += d[i][j]*x[c];//d[][] and x[] are arrays of float
i++;
c = j+s[i];// s[] is an array of unsigned int with 6 entries.
}
x[j] -= sub; // only one memory-write per j
}
使用 4000 MHz AMD Bulldozer 时,该循环的执行时间约为一秒。我考虑过 SIMD 和 OpenMP(我通常使用它们来获得更快的速度),但这个循环是递归的。
有什么建议么?
认为您可能想要转置矩阵 d -- 意味着以可以交换索引的方式存储它 -- 让 i 成为外部索引:
sub += d[j][i]*x[c];
代替
sub += d[i][j]*x[c];
这应该会带来更好的缓存性能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)