You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1 KiB

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