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.

64 lines
1.6 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <cuda_runtime.h>
  2. #include <curand.h>
  3. #include <cublas_v2.h>
  4. extern "C" {
  5. #include "avgpool_layer.h"
  6. #include "dn_cuda.h"
  7. }
  8. namespace DN{
  9. __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output)
  10. {
  11. int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  12. if (id >= n) return;
  13. int k = id % c;
  14. id /= c;
  15. int b = id;
  16. int i;
  17. int out_index = (k + c*b);
  18. output[out_index] = 0;
  19. for (i = 0; i < w*h; ++i){
  20. int in_index = i + h*w*(k + b*c);
  21. output[out_index] += input[in_index];
  22. }
  23. output[out_index] /= w*h;
  24. }
  25. __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta)
  26. {
  27. int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  28. if (id >= n) return;
  29. int k = id % c;
  30. id /= c;
  31. int b = id;
  32. int i;
  33. int out_index = (k + c*b);
  34. for (i = 0; i < w*h; ++i){
  35. int in_index = i + h*w*(k + b*c);
  36. in_delta[in_index] += out_delta[out_index] / (w*h);
  37. }
  38. }
  39. extern "C" void forward_avgpool_layer_gpu(avgpool_layer layer, network net)
  40. {
  41. size_t n = layer.c*layer.batch;
  42. forward_avgpool_layer_kernel << <cuda_gridsize(n), BLOCK >> >(n, layer.w, layer.h, layer.c, net.input_gpu, layer.output_gpu);
  43. check_error(cudaPeekAtLastError());
  44. }
  45. extern "C" void backward_avgpool_layer_gpu(avgpool_layer layer, network net)
  46. {
  47. size_t n = layer.c*layer.batch;
  48. backward_avgpool_layer_kernel << <cuda_gridsize(n), BLOCK >> >(n, layer.w, layer.h, layer.c, net.delta_gpu, layer.delta_gpu);
  49. check_error(cudaPeekAtLastError());
  50. }
  51. };