gpu对任意长度的矢量求和

 

blockDim.x*gridDim.x 跳过一个grid

 int <<<参数1,参数2>>>(int *a,int * b,int * c);

如果是一维的,参数1表示一个grid里面有多少个block块,参数2表示一个block块里面有多少个thread线程

 

 

namespace caffe {

template <typename Dtype>

__global__ void LogwxlForward(const float scale_,const int n,const Dtype * in,Dtype * out){

  CUDA_KERNEL_LOOP(index,n){

    out[index]=scale_*in[index];  

  }

}

template <typename Dtype>

__global__ void LogwxlBackward(const float scale_,const int n,const Dtype *in_diff,Dtype* out_diff){

    CUDA_KERNEL_LOOP(index,n) {

     out_diff[index]=in_diff[index]*scale_;

   }

}

 

template <typename Dtype>

void LogwxlLayer<Dtype>::Forward_gpu(

    const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {

    //this->Forward_cpu(bottom, top);

    const Dtype* bottom_data=bottom[0]->gpu_data();

    Dtype * top_data=top[0]->mutable_gpu_data();

    const int count=bottom[0]->count();

    LogwxlForward<Dtype><<<CAFFE_GET_BLOCKS(count),CAFFE_CUDA_NUM_THREADS>>>(scale_,count,bottom_data,top_data);

    CUDA_POST_KERNEL_CHECK;

}

 

template <typename Dtype>

void LogwxlLayer<Dtype>::Backward_gpu(

    const vector<Blob<Dtype>*>& top,

    const vector<bool>& propagate_down,

    const vector<Blob<Dtype>*>& bottom) {

    //this->Backward_cpu(top, propagate_down, bottom);

    if(propagate_down[0]){

   const Dtype *top_diff=top[0]->gpu_diff();

   Dtype * bottom_diff=bottom[0]->mutable_gpu_diff();

   const int count=bottom[0]->count();

   LogwxlBackward<Dtype><<<CAFFE_GET_BLOCKS(count),CAFFE_CUDA_NUM_THREADS>>>(scale_,count,top_diff,bottom_diff);

   CUDA_POST_KERNEL_CHECK;

}

}

INSTANTIATE_LAYER_GPU_FUNCS(LogwxlLayer);

} // namespace caffe

转载于:https://www.cnblogs.com/wuxiangli/p/6201437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值