在代码中引入 SIMD 命令时遇到问题

发布于 2024-11-26 23:53:24 字数 2251 浏览 2 评论 0原文

我有一个基本的计算函数,应用于数组中的每个项目。该函数不仅仅只是对两个向量求和。

我想使用 SIMD 命令并行处理数组中的多个项目。

因为我发现这些示例对于我的情况来说太简单了(它们不包括函数调用): http://www.doc.ic.ac.uk/ ~nloriant/files/scfpsc-pc.pdf

我尝试使用数组表示法,如下所示: http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/cpp/mac/optaps/common/optaps_elem_functions.htm

但这并没有加速我的代码。我不明白我做错了什么,如果我需要使用更类似于汇编的 SIMD 风格,我该如何在那里引入函数调用...

如果有人可以帮助我或向我推荐一个好的资料来源需要我会非常感激。

谢谢你!!!!


代码示例:

这是应用于数组中每个项目的基本函数:

float VarFlow::gauss_seidel_step(IplImage* u, int i, float h, float J11, float J12, float J13, float vi){

int x = i%u->width;
int y = i/u->width;

int start_y, end_y, start_x, end_x;
int N_num = 0;

start_y = y - 1;
end_y = y + 1;
start_x = x - 1;
end_x = x+1;         

float temp_u = 0;

// Sum top neighbor    
if(start_y > -1){              

    temp_u += *((float*)(u->imageData + start_y*u->widthStep) + x);

    N_num++;

}

// Sum bottom neighbor            
if(end_y < u->height){   

    temp_u += *((float*)(u->imageData + end_y*u->widthStep) + x);

    N_num++;

}

// Sum left neighbor
if(start_x > -1){              

    temp_u += *((float*)(u->imageData + y*u->widthStep) + start_x);

    N_num++;

}

// Sum right neighbor
if(end_x < u->width){              

    temp_u += *((float*)(u->imageData + y*u->widthStep) + end_x);

    N_num++;

}

temp_u = temp_u - (h*h/alpha)*(J12*vi + J13);
temp_u = temp_u / (N_num + (h*h/alpha)*J11);

return temp_u;

}

我想用 __declspec (向量)声明它并像这样调用它:

    u_ptr[0:max_i:1] = gauss_seidel_step(imgU, vect[0:max_i:1], h, fxfx_ptr[0:max_i:1], fxfy_ptr[0:max_i:1], fxft_ptr[0:max_i:1], v_ptr[0:max_i:1]);
    v_ptr[0:max_i:1] = gauss_seidel_step(imgV, vect[0:max_i:1], h, fyfy_ptr[0:max_i:1], fxfy_ptr[0:max_i:1], fyft_ptr[0:max_i:1], u_ptr[0:max_i:1]);

而不是 for 循环。

我很乐意获得有关此问题的指导(也许是类似示例的链接),但不是完整的解决方案。

谢谢!

I have a basic calculation function that I apply on each item in an array. This function does more then just summing two vectors.

I wanted to work on multiple items from my array in parallel using SIMD commands.

As I found these kind of examples too simple for my case (they don't include function calls):
http://www.doc.ic.ac.uk/~nloriant/files/scfpsc-pc.pdf

I tried using array notation as in here:
http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/cpp/mac/optaps/common/optaps_elem_functions.htm

But this did not accelerate my code. I don't understand what I am doing wrong and if I need to go to the more assembly-like style of SIMD, how do I introduce function calls there...

If anyone can help me or refer me to a good source for my needs I'll be very thakful.

Thank you!!!!


code example:

This is the basic function applied on each item in the array:

float VarFlow::gauss_seidel_step(IplImage* u, int i, float h, float J11, float J12, float J13, float vi){

int x = i%u->width;
int y = i/u->width;

int start_y, end_y, start_x, end_x;
int N_num = 0;

start_y = y - 1;
end_y = y + 1;
start_x = x - 1;
end_x = x+1;         

float temp_u = 0;

// Sum top neighbor    
if(start_y > -1){              

    temp_u += *((float*)(u->imageData + start_y*u->widthStep) + x);

    N_num++;

}

// Sum bottom neighbor            
if(end_y < u->height){   

    temp_u += *((float*)(u->imageData + end_y*u->widthStep) + x);

    N_num++;

}

// Sum left neighbor
if(start_x > -1){              

    temp_u += *((float*)(u->imageData + y*u->widthStep) + start_x);

    N_num++;

}

// Sum right neighbor
if(end_x < u->width){              

    temp_u += *((float*)(u->imageData + y*u->widthStep) + end_x);

    N_num++;

}

temp_u = temp_u - (h*h/alpha)*(J12*vi + J13);
temp_u = temp_u / (N_num + (h*h/alpha)*J11);

return temp_u;

}

I'd like to declare it with __declspec (vector) and call it like so:

    u_ptr[0:max_i:1] = gauss_seidel_step(imgU, vect[0:max_i:1], h, fxfx_ptr[0:max_i:1], fxfy_ptr[0:max_i:1], fxft_ptr[0:max_i:1], v_ptr[0:max_i:1]);
    v_ptr[0:max_i:1] = gauss_seidel_step(imgV, vect[0:max_i:1], h, fyfy_ptr[0:max_i:1], fxfy_ptr[0:max_i:1], fyft_ptr[0:max_i:1], u_ptr[0:max_i:1]);

Instead of a for loop.

I'll be happy to get a direction with this (maybe a link to a similar example) but not a full solution.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临走之时 2024-12-03 23:53:24

SIMD 和条件分支不能很好地混合。

将条件语句转换为布尔掩码和乘法。这将引导您走上矢量化操作的正确路径。

例如

if(end_x < u->width){                  
    temp_u += value;    
    N_num++;    
}

变成

ltmask = (end_x < u->width); // see _mm_cmplt_ps
temp_u += ltmask*value; // see _mm_add_ps, _mm_and_ps
N_num += ltmask; // use _mm_and_ps with a vector of 1.0f

SIMD and conditional branching do not mix well.

Turn your conditional statements into boolean masks and multiplications. That will send you down the right path for vectorizing the operations.

e.g.

if(end_x < u->width){                  
    temp_u += value;    
    N_num++;    
}

becomes

ltmask = (end_x < u->width); // see _mm_cmplt_ps
temp_u += ltmask*value; // see _mm_add_ps, _mm_and_ps
N_num += ltmask; // use _mm_and_ps with a vector of 1.0f
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文