OpenCl:添加大量数组

时间:2015-03-10 00:50:41

标签: arrays opencl addition

在下面的代码中,每个工作项生成一个数组sum_qcos_i 为了添加它们,我首先使用本地数组sum_qcos_tmp进行局部加法 然后我在全局一维矩阵sum_qcos_part中复制每个局部数组 我需要为我的目的添加矩阵列,这是在使用结果之前每个工作项的内容。 这是代码

__kernel __attribute__((vec_type_hint(double4))) void energy_forces( const int atom_number, 
                                         const int nvect,
                                 __global double4 *kvect,__global double *qcos,__global double *qsin,
                                 __global double *cst_ewald ,
                                 __global double4 *positions,
                                 __global double4 *forces_r,               
                                 __global double *sum_qcos_part,__global double *sum_qsin_part)                              
{


  int i = 0 ;
  int gti = 0 , ggi = 0 , lti = 0;

  double kr = (double)0.0 ;
  double ss = (double)0.0 , cc = (double)0.0 ;
  double prod = (double)0.0 ;

  double valqcos = 0. , valqsin = 0. ;  

  double4 zeroes_4 = (double4){ 0.0,0.0,0.0,0.0 };

  double sum_qcos_i[NVECTOR_MAX] ;
  double sum_qsin_i[NVECTOR_MAX] ;    

  #if defined NVECTOR_MAX
  __local double sum_qcos_tmp[NVECTOR_MAX] ;
  __local double sum_qsin_tmp[NVECTOR_MAX] ;         
  #endif

  lti = get_local_id(0);
  ggi = get_group_id(0); 

  for (k=0;k<nvect;k++) {         /*k-vectors*/
 sum_qcos_tmp[k] = .0 ;
 sum_qsin_tmp[k] = .0 ;  
 sum_qcos_i[k] = .0 ;
 sum_qsin_i[k] = .0 ;
            }  

  double fk = (double)0.0 ; 
  double4 fr_i = zeroes_4 ;
  double4 kvec_i = zeroes_4;        

  for (gti = get_global_id(0); gti < atom_number; gti += get_global_size(0))
   {

 pos_i = positions[gti];

 for (k=0;k<nvect;k++) {                           /* sum over k-vectors to compute QCOS and QSIN for Ewald sum*/

   prod = dot((double4)pos_i,(double4)kvect[k]);
   ss = (double)sincos(-prod,&cc);
   valqcos = cc ;
   valqsin = ss ; 
//       valqcos = 1. ;
//       valqsin = 1. ;       
   qcos[gti*NVECTOR_MAX+k] = valqcos ;
   qsin[gti*NVECTOR_MAX+k] = valqsin ;
   sum_qcos_i[k] = valqcos ;                     /*  private variable */
   sum_qsin_i[k] = valqsin ;  

                           }                          /* end sum over k-vectors to compute QCOS and QSIN for Ewald sum*/

}    // end for gti 

  int ii = 0 ;

  for ( ii = 0;ii<get_local_size(0);ii++ )
  { 
if (lti == ii)
  { 
 for (k=0;k<nvect;k++) 
    {                   /* k-vectors */               
       sum_qcos_tmp[k] += sum_qcos_i[k] ;     /* accumulates private data to local variable */
       sum_qsin_tmp[k] += sum_qsin_i[k] ; 
    } 
 barrier(CLK_LOCAL_MEM_FENCE|CLK_GLOBAL_MEM_FENCE) ;    
 }
  }

  if (lti == 0)
   {
     for (k=0;k<nvect;k++) {  
      sum_qcos_part[ggi*NVECTOR_MAX+k] = sum_qcos_tmp[k] ;  /* cp local data to global array */    
      sum_qsin_part[ggi*NVECTOR_MAX+k] = sum_qsin_tmp[k] ;
                           }    
   }

  int iii = 0 ;

  for (gti = get_global_id(0); gti < atom_number; gti += get_global_size(0))
   {

 fr_i = zeroes_4 ;

 barrier(CLK_LOCAL_MEM_FENCE|CLK_GLOBAL_MEM_FENCE) ;

 for (k=0;k<nvect;k++) 
  {   

    sum_qcos_i[k] = .0 ;
    sum_qsin_i[k] = .0 ;

   for (iii=0;iii<get_num_groups(0);iii++)
    {  
      sum_qcos_i[k] += sum_qcos_part[iii*NVECTOR_MAX+k] ;
      sum_qsin_i[k] += sum_qsin_part[iii*NVECTOR_MAX+k] ;     
    }

 } 

 barrier(CLK_LOCAL_MEM_FENCE|CLK_GLOBAL_MEM_FENCE) ;

 for (k=0;k<nvect;k++) 
  {

    fk =  ( sum_qcos_i[k]*qsin[gti*NVECTOR_MAX+k] - sum_qsin_i[k]*qcos[gti*NVECTOR_MAX+k] ) ;               
    fr_i += cst_ewald[k] * fk * kvect[k] ;    

  }

#if defined(SCALAR_KERNELS)
forces_r[gti].x = fr_i.x;
forces_r[gti].y = fr_i.y;
forces_r[gti].z = fr_i.z;     
forces_r[gti].w = .0 ;        
#elif defined(VECTOR_KERNELS)
forces_r[gti] = fr_i;   
#endif

  }    // end for gti  

}

这个内核不起作用,我无法理解为什么 一些提示在这里会非常有用 谢谢。

2 个答案:

答案 0 :(得分:1)

添加障碍成了诀窍:

for ( ii = 0;ii<get_local_size(0);ii++ )
{ 
if (lti == ii)
  { 
    barrier(CLK_LOCAL_MEM_FENCE|CLK_GLOBAL_MEM_FENCE) ;      
    for (k=0;k<nvect;k++) 
    {                   /* k-vectors */               
      sum_qcos_tmp[k] += sum_qcos_i[k] ;      /* accumulates private data to local variable */
      sum_qsin_tmp[k] += sum_qsin_i[k] ; 
    } 

  }
}

矢量forces_r的最后一个仍然是错误的,它现在总是一样。

答案 1 :(得分:0)

实际上问题没有解决 我根据粒子数定义全局工作大小,以便每个工作项处理一个粒子 在我的计算中,我点产品每个粒子与一定数量的向量坐标 我现在的问题是这些点积的结果取决于组的大小。我把矢量放在double4数组中。全局工作大小从1000到10000,而我用于点积的向量数总是大约200。 我想知道是否有关于全局工作大小的数组大小的要求。

for (gti = get_global_id(0); gti < atom_number; gti += get_global_size(0))
 {
   pos_i = positions[gti];

   for (k=0;k<nvect;k++) {                           /* sum over k-vectors to compute QCOS and QSIN for Ewald sum*/

 prod = dot((double4)pos_i,(double4)kvect[k]);
 ss = (double)sincos(-prod,&cc);
 valqcos = cc ;
 valqsin = ss ;   
 qcos[gti*NVECTOR_MAX+k] = valqcos ;
 qsin[gti*NVECTOR_MAX+k] = valqsin ;
 sum_qcos_i[k] = valqcos ;                     /*  private variable */
 sum_qsin_i[k] = valqsin ;  

                         }                          /* end sum over k-vectors to compute QCOS and QSIN for Ewald sum*/
 }    // end for gtiforgot 

这里有任何提示吗?