darknet  v3
col2im_kernels.cu
Go to the documentation of this file.
1 #include "cuda_runtime.h"
2 #include "curand.h"
3 #include "cublas_v2.h"
4 
5 extern "C" {
6 #include "col2im.h"
7 #include "cuda.h"
8 }
9 
10 // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu
11 // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE
12 
13 __global__ void col2im_gpu_kernel(const int n, const float* data_col,
14  const int height, const int width, const int ksize,
15  const int pad,
16  const int stride,
17  const int height_col, const int width_col,
18  float *data_im) {
19  int index = blockIdx.x*blockDim.x+threadIdx.x;
20  for(; index < n; index += blockDim.x*gridDim.x){
21  float val = 0;
22  int w = index % width + pad;
23  int h = (index / width) % height + pad;
24  int c = index / (width * height);
25  // compute the start and end of the output
26  int w_col_start = (w < ksize) ? 0 : (w - ksize) / stride + 1;
27  int w_col_end = min(w / stride + 1, width_col);
28  int h_col_start = (h < ksize) ? 0 : (h - ksize) / stride + 1;
29  int h_col_end = min(h / stride + 1, height_col);
30  // equivalent implementation
31  int offset =
32  (c * ksize * ksize + h * ksize + w) * height_col * width_col;
33  int coeff_h_col = (1 - stride * ksize * height_col) * width_col;
34  int coeff_w_col = (1 - stride * height_col * width_col);
35  for (int h_col = h_col_start; h_col < h_col_end; ++h_col) {
36  for (int w_col = w_col_start; w_col < w_col_end; ++w_col) {
37  val += data_col[offset + h_col * coeff_h_col + w_col * coeff_w_col];
38  }
39  }
40  data_im[index] += val;
41  }
42 }
43 
44 void col2im_gpu(float *data_col,
45  int channels, int height, int width,
46  int ksize, int stride, int pad, float *data_im){
47  // We are going to launch channels * height_col * width_col kernels, each
48  // kernel responsible for copying a single-channel grid.
49  int height_col = (height + 2 * pad - ksize) / stride + 1;
50  int width_col = (width + 2 * pad - ksize) / stride + 1;
51  int num_kernels = channels * height * width;
52  col2im_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK,
53  BLOCK>>>(
54  num_kernels, data_col, height, width, ksize, pad,
55  stride, height_col,
56  width_col, data_im);
57 }
58 
void col2im_gpu(float *data_col, int channels, int height, int width, int ksize, int stride, int pad, float *data_im)
__global__ void col2im_gpu_kernel(const int n, const float *data_col, const int height, const int width, const int ksize, const int pad, const int stride, const int height_col, const int width_col, float *data_im)