OpenCL优化-解除data dependence

  1. 删除loop-carrried依赖
  1. __kernel void unoptimized(__global int * restrict A,  
  2.                           __global int * restrict B,  
  3.                           __global int * restrict result)  
  4. {  
  5.     int sum = 0;  
  6.   
  7.     for(unsigned i=0;i<N;i++){  
  8.         for(unsigned j=0;j<N;j++){  
  9.             sum +=A[i*N+j];  
  10.         }  
  11.         sum += B[i];  
  12.     }  
  13.     *result = sum;  
  14. }  

使用局部变量能够解除依赖。

  1. __kernel void optimized(__global int * restrict A,  
  2.                           __global int * restrict B,  
  3.                           __global int * restrict result)  
  4. {  
  5.     int sum = 0;  
  6.   
  7.     for(unsigned i=0;i<N;i++){  
  8.         int sum2 = 0;  
  9.         for(unsigned j=0;j<N;j++){  
  10.             sum2 +=A[i*N+j];  
  11.         }  
  12.         sum += sum2;  
  13.         sum += B[i];  
  14.     }  
  15.   
  16.     *result = sum;  
  17. }  

6.2

 

  1. #define N 128  
  2.   
  3. __kernel void unoptimized(__global float * restrict A,  
  4.                           __global float * restrict result)  
  5. {  
  6.     float mul = 1.0f;  
  7.       
  8.     for(unsigned i=0;i<N;i++)  
  9.         mul *= A[i];  
  10.   
  11.     *result = mul;  
  12. }  

原因在于在未进行优化之前float类型的乘法的II为3,进行优化之后II为1.思想是不使用单个变量来存储乘法结果,而是对变量的M个副本进行操作。相当于将乘法得到的数据存储到长度为M的数组中,并对数组里的数据进行移位赋值,这样的话长为M的数组中就各自存储了一部分的乘法数据,最后将这些数据进行相乘即为最终结果。

  1. #define N 128  
  2. #define M 8  
  3.   
  4. __kernel void optimized(__global float * restrict A,  
  5.                           __global float * restrict result)  
  6. {  
  7.     float mul = 1.0f;  
  8.   
  9.     float mul_copies[M];  
  10.   
  11.     for(unsigned i = 0;i < M;i++)  
  12.         mul_copies[i] = 1.0f;  
  13.   
  14.       
  15.     for(unsigned i=0;i<N;i++){  
  16.         float cur = mul_copies[M-1] * A[i];  
  17.   
  18.         #pragma unroll  
  19.         for(unsigned j = M-1;j >0;j--){  
  20.             mul_copies[j] = mul_copies[j-1];  
  21.             mul_copies[0] = cur;  
  22.         }  
  23.     }  
  24.       
  25.     #pragma unroll  
  26.     for(unsigned i =0;i < M;i++)  
  27.         mul *= mul_copies[i];  
  28.   
  29.     *result = mul;  
  30. }  

 

 

 

对于无法删除的循环依赖,通过将循环携带依赖项的数组从全局内存移动到本地内存来改进II

  1. #define N 128  
  2.   
  3. __kernel void unoptimized(__global float * restrict A)  
  4. {  
  5.     for(unsigned i =0;i< N;i++){  
  6.         A[N-i] = A[i];  
  7.     }  
  8.   
  9. }  
  10.   
  11. #define N 128  
  12.   
  13. __kernel void optimized(__global float * restrict A)  
  14. {  
  15.     float B[N];  
  16.     for(unsigned i =0;i< N;i++){  
  17.         B[i] = A[i];  
  18.     }  
  19.     for(unsigned i =0;i< N;i++){  
  20.         B[N-i] = B[i];  
  21.     }  
  22.     for(unsigned i =0;i< N;i++){  
  23.         A[i] = B[i];  
  24.     }  
  25.   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值