非常基础的 SSE
我有一个非常简单的程序,我正在尝试提高性能。我知道有帮助的一种方法是利用 SSE3(因为我正在工作的机器支持这一点),但我完全不知道如何做到这一点。这是一个代码片段(c++):
int sum1, sum2, sum3, sum4;
for (int i=0; i<length; i+=4) {
for (int j=0; j<length; j+=4) {
sum1 = sum1 + input->value[i][j];
sum2 = sum2 + input->value[i+1][j+1];
sum3 = sum3 + input->value[i+2][j+3];
sum4 = sum4 + input->value[i+3][j+4];
{
}
我已经阅读了一些相关内容,并理解了这个想法,但我完全不知道如何实现它。有人可以帮我吗?我认为这相当简单,特别是对于我的简单程序来说,但有时入门是最困难的部分。
谢谢!
I have a very simple program that I am trying to improve performance. One way that I know will help is to utilize SSE3 (since the machine that I am working supports this), but I have absolutely no idea how to to do this. Here is a code snippet (c++):
int sum1, sum2, sum3, sum4;
for (int i=0; i<length; i+=4) {
for (int j=0; j<length; j+=4) {
sum1 = sum1 + input->value[i][j];
sum2 = sum2 + input->value[i+1][j+1];
sum3 = sum3 + input->value[i+2][j+3];
sum4 = sum4 + input->value[i+3][j+4];
{
}
I've read a little about this, and understand the idea, but I have absolutely no idea how to implement this. Can somebody help me please? I think that this is fairly simple, particularly for my simple program, but sometimes getting started is the hardest part.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,就你的情况而言,事情并没有那么简单。就目前而言,您的代码不能可矢量化。 (至少在没有显着的循环转换的情况下)
这样做的原因是您也在内部循环内更改了索引
i
。这会破坏对 j 迭代进行向量化的任何机会,因为内存位置不再相邻并且位于矩阵的不同行中。 (因为你似乎沿着矩阵对角线运行)但是,我感觉你正在尝试总结矩阵中的所有元素,并且你实际上希望你的循环是这样的(并且你有很多拼写错误太):
如果这是你想要的,那么它是非常可矢量化的。
在使用内在函数的 C/C++ 中,可以仅使用 SSE2 按如下方式完成此操作:
请注意,将应用对齐限制。通过进一步展开循环可以获得更多的加速。
Actually, in your case, it is not that simple. As it stands right now, your code is NOT vectorizable. (at least not without significant loop transformations)
The reason for this is that you are changing the index
i
as well inside the inner loop. The breaks any chance of being able to vectorize thej
iteration because the memory locations are no longer adjacent and are in different rows of the matrix. (as you seem to be running down the matrix diagonally)However, I get the feeling that you are trying to sum up all the elements in your matrix, and you actually intended your loop to be like this (and you had a number of typos too):
If this is what you wanted, then it is very vectorizable.
In C/C++ using intrinsics, this can be done as follows using just SSE2:
Note that alignment restrictions will apply. And a lot more speedup can be gained by further unrolling the loop.