码迷,mamicode.com
首页 > 其他好文 > 详细

cuda float atomic操作

时间:2017-06-06 18:27:53      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:res   fat   sum   nbsp   dev   atom   tmp   val   mic   

atomic add.用第二个,暂时还没弄明白

#ifdef FLOAT
  #define T float
#else
  #define T int
#endif

#ifdef FORUM
__device__ inline void atomicAdd(float *address, float val){
      int i_val = __float_as_int(val);
      int tmp0 = 0;
      int tmp1;
      while( (tmp1 = atomicCAS((int *)address, tmp0, i_val)) != tmp0)  {
              tmp0 = tmp1;
              i_val = __float_as_int(val + __int_as_float(tmp1));
      }
}
#else
__device__ inline float atomicAdd(float* address, float value){
  float old = value;  
  float ret=atomicExch(address, 0.0f);
  float new_old=ret+old;
  while ((old = atomicExch(address, new_old))!=0.0f){
    new_old = atomicExch(address, 0.0f);
    new_old += old;
  }
  return ret;
};
#endif

atomic min

__device__ float fatomicMin(float *addr,float value){
          float old = *addr, assumed;
          if(old<=value) return old;
          do {
                     assumed = old;
                     old = atomicCAS((int*)addr, __float_as_int(assumed), __float_as_int(MIN(value, assumed)));
          }while(old!=assumed);
          return old;
};

 

cuda float atomic操作

标签:res   fat   sum   nbsp   dev   atom   tmp   val   mic   

原文地址:http://www.cnblogs.com/redips-l/p/6952414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!