SwitchOpGpu.cu 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 Paddle

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "SwitchOp.h"
W
wanghaoshuang 已提交
16
#include "hl_base.h"
17 18 19

namespace paddle {

W
wanghaoshuang 已提交
20 21 22 23 24 25 26
__global__ void KeNCHW2NHWC(real* outputs,
                            const real* inputs,
                            int inC,
                            int inH,
                            int inW,
                            int nthreads,
                            int argType) {
27 28 29 30 31 32 33
  const int idx = threadIdx.x + blockIdx.x * blockDim.x;
  if (idx < nthreads) {
    const int w = idx % inW;
    const int h = (idx / inW) % inH;
    const int c = (idx / inW / inH) % inC;
    const int n = idx / inW / inH / inC;

W
wanghaoshuang 已提交
34
    const int off = ((n * inH + h) * inW + w) * inC + c;
35 36 37 38 39
    if (argType == ADD_TO) {
      outputs[off] += inputs[idx];
    } else {
      outputs[off] = inputs[idx];
    }
40 41 42 43 44
  }
}

template <>
void NCHW2NHWC<DEVICE_TYPE_GPU>(real* outputs,
W
wanghaoshuang 已提交
45 46 47 48 49 50
                                const real* inputs,
                                const int num,
                                const int inC,
                                const int inH,
                                const int inW,
                                const int argType) {
51 52 53
  size_t nth = num * inC * inH * inW;
  int blockSize = 1024;
  int gridSize = (nth + 1024 - 1) / 1024;
W
wanghaoshuang 已提交
54 55
  KeNCHW2NHWC<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>(
      outputs, inputs, inC, inH, inW, nth, argType);
56 57 58
  CHECK_SYNC("NCHW2NHWC");
}

W
wanghaoshuang 已提交
59 60 61 62 63 64 65
__global__ void KeNHWC2NCHW(real* outputs,
                            const real* inputs,
                            int inH,
                            int inW,
                            int inC,
                            int nthreads,
                            int argType) {
66 67 68 69 70 71 72 73
  const int idx = threadIdx.x + blockIdx.x * blockDim.x;
  if (idx < nthreads) {
    const int c = idx % inC;
    const int w = (idx / inC) % inW;
    const int h = (idx / inC / inW) % inH;
    const int n = idx / inW / inH / inC;

    const int off = ((n * inC + c) * inH + h) * inW + w;
74 75 76 77 78
    if (argType == ADD_TO) {
      outputs[off] += inputs[idx];
    } else {
      outputs[off] = inputs[idx];
    }
79 80 81 82 83
  }
}

template <>
void NHWC2NCHW<DEVICE_TYPE_GPU>(real* outputs,
W
wanghaoshuang 已提交
84 85 86 87 88 89
                                const real* inputs,
                                const int num,
                                const int inH,
                                const int inW,
                                const int inC,
                                const int argType) {
90 91 92
  int nth = num * inC * inH * inW;
  int blockSize = 1024;
  int gridSize = (nth + 1024 - 1) / 1024;
W
wanghaoshuang 已提交
93 94
  KeNHWC2NCHW<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>(
      outputs, inputs, inH, inW, inC, nth, argType);
95 96 97 98
  CHECK_SYNC("NHWC2NCHW");
}

}  // namespace paddle