【发布时间】:2015-10-28 17:41:43
【问题描述】:
我需要使用 SSE 执行高斯消除,但我不确定如何从 128 位寄存器(每个寄存器存储 4 个元素)访问每个元素(32 位)。这是原始代码(不使用 SSE):
unsigned int i, j, k;
for (i = 0; i < num_elements; i ++) /* Copy the contents of the A matrix into the U matrix. */
for(j = 0; j < num_elements; j++)
U[num_elements * i + j] = A[num_elements*i + j];
for (k = 0; k < num_elements; k++){ /* Perform Gaussian elimination in place on the U matrix. */
for (j = (k + 1); j < num_elements; j++){ /* Reduce the current row. */
if (U[num_elements*k + k] == 0){
printf("Numerical instability detected. The principal diagonal element is zero. \n");
return 0;
}
/* Division step. */
U[num_elements * k + j] = (float)(U[num_elements * k + j] / U[num_elements * k + k]);
}
U[num_elements * k + k] = 1; /* Set the principal diagonal entry in U to be 1. */
for (i = (k+1); i < num_elements; i++){
for (j = (k+1); j < num_elements; j++)
/* Elimnation step. */
U[num_elements * i + j] = U[num_elements * i + j] -\
(U[num_elements * i + k] * U[num_elements * k + j]);
U[num_elements * i + k] = 0;
}
}
好的,我在这段代码中遇到了分段错误[核心转储]。我是 SSE 的新手。有人可以帮忙吗?谢谢。
int i,j,k;
__m128 a_i,b_i,c_i,d_i;
for (i = 0; i < num_rows; i++)
{
for (j = 0; j < num_rows; j += 4)
{
int index = num_rows * i + j;
__m128 v = _mm_loadu_ps(&A[index]); // load 4 x floats
_mm_storeu_ps(&U[index], v); // store 4 x floats
}
}
for (k = 0; k < num_rows; k++){
a_i= _mm_load_ss(&U[num_rows*k+k]);
for (j = (4*k + 1); j < num_rows; j+=4){
b_i= _mm_loadu_ps(&U[num_rows*k+j]);// Reduce the currentrow.
if (U[num_rows*k+k] == 0){
printf("Numerical instability detected.);
}
/* Division step. */
b_i = _mm_div_ps(b_i, a_i);
}
a_i = _mm_set_ss(1);
for (i = (k+1); i < num_rows; i++){
d_i= _mm_load_ss(&U[num_rows*i+k]);
for (j = (4*k+1); j < num_rows; j+=4){
c_i= _mm_loadu_ps(&U[num_rows*i+j]); /* Elimnation step. */
b_i= _mm_loadu_ps(&U[num_rows*k+j]);
c_i = _mm_sub_ps(c_i, _mm_mul_ss(b_i,d_i));
}
d_i= _mm_set_ss(0);
}
}
【问题讨论】:
-
在您最初的尝试中犯了很多基本错误 - 也许先尝试一些更简单的东西,以便学习 SIMD 编码的基础知识?
-
x86 标签 wiki (stackoverflow.com/tags/x86/info) 有链接。如果您找到一个好的介绍/概述/入门指南,请编辑 wiki 中的链接,或在此处发表评论,我会查看它。大多数链接都是参考资料,当您了解基础知识时很有用,但必须查看哪些指令的确切作用以及可用的内容。
-
是的,我看到了错误。感谢您的链接。
-
哦,对不起。是的,矩阵是大小为 num_elements 的正方形。我应该将它用作扁平的二维数组,就像保罗说的那样,目标是学习 SIMD。谢谢。我会尝试以此为基础
-
@PeterCordes,x86 标签的链接和描述太棒了!这是一个很好的资源。