TensorEvaluate.h 3.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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. */
H
hedaoyuan 已提交
14 15 16 17 18

#pragma once

#include <algorithm>
#include "hl_base.h"
Y
Yu Yang 已提交
19
#include "paddle/utils/Logging.h"
H
hedaoyuan 已提交
20 21 22 23 24 25

namespace paddle {

/**
 * \brief The tensor cpu evaluate api.
 */
H
hedaoyuan 已提交
26
template <class T, typename LeftType, typename RightType>
H
hedaoyuan 已提交
27 28 29 30 31 32 33
inline void TensorCpuApply(LeftType& lhs, const RightType& rhs) {
  TensorApply<LeftType, T> lhs_(lhs);
  TensorApply<const RightType, T> rhs_(rhs);
  CHECK_EQ(lhs_.getWidth(), rhs_.getWidth());
  CHECK_EQ(lhs_.getHeight(), rhs_.getHeight());
  CHECK_EQ(lhs_.useGpu(), rhs_.useGpu());

H
hedaoyuan 已提交
34 35
  int height = lhs_.getHeight();
  int width = lhs_.getWidth();
H
hedaoyuan 已提交
36
  if (lhs_.isContiguous() && rhs_.isContiguous()) {
H
hedaoyuan 已提交
37
    int size = height * width;
H
hedaoyuan 已提交
38 39 40 41
    for (int index = 0; index < size; index++) {
      lhs_.applyRef(index) = rhs_.apply(index);
    }
  } else {
H
hedaoyuan 已提交
42 43
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
H
hedaoyuan 已提交
44 45 46 47 48 49 50
        lhs_.applyRef(i, j) = rhs_.apply(i, j);
      }
    }
  }
}

#ifdef __NVCC__
H
hedaoyuan 已提交
51 52 53 54
template <typename LeftType, typename RightType>
__global__ void TensorElementWiseOp(LeftType lhs,
                                    RightType rhs,
                                    const int border) {
H
hedaoyuan 已提交
55 56 57 58 59 60
  const int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < border) {
    lhs.applyRef(idx) = rhs.apply(idx);
  }
}

H
hedaoyuan 已提交
61
template <typename LeftType, typename RightType>
H
hedaoyuan 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
__global__ void TensorElementWiseOp(LeftType lhs, RightType rhs) {
  const int colIdx = blockIdx.x * blockDim.x + threadIdx.x;
  const int rowIdx = blockIdx.y * blockDim.y + threadIdx.y;
  for (int i = rowIdx; i < lhs.getHeight(); i += gridDim.y * blockDim.y) {
    for (int j = colIdx; j < lhs.getWidth(); j += gridDim.x * blockDim.x) {
      lhs.applyRef(i, j) = rhs.apply(i, j);
    }
  }
}

/**
 * \brief The tensor gpu evaluate api.
 */
H
hedaoyuan 已提交
75
template <class T, typename LeftType, typename RightType>
H
hedaoyuan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89
inline void TensorGpuApply(LeftType& lhs, const RightType& rhs) {
  TensorApply<LeftType, T> lhs_(lhs);
  TensorApply<const RightType, T> rhs_(rhs);
  CHECK_EQ(lhs_.getWidth(), rhs_.getWidth());
  CHECK_EQ(lhs_.getHeight(), rhs_.getHeight());
  CHECK_EQ(lhs_.useGpu(), rhs_.useGpu());

  int dimM = lhs_.getHeight();
  int dimN = lhs_.getWidth();

  if (lhs_.isContiguous() && rhs_.isContiguous()) {
    int size = dimM * dimN;
    int blockSize = size <= 1024 ? size : 1024;
    int gridSize = (size + 1024 - 1) / 1024;
H
hedaoyuan 已提交
90 91
    TensorElementWiseOp<<<gridSize, blockSize, 0, STREAM_DEFAULT>>>(
        lhs_, rhs_, size);
H
hedaoyuan 已提交
92 93 94 95 96 97 98
  } else {
    int blockSizeY = std::min(32, dimM);
    int blockSizeX = (32 / blockSizeY) * 32;
    int gridSizeX = std::min(32, (dimN + blockSizeX - 1) / blockSizeX);
    int gridSizeY = std::min(32, (dimM + blockSizeY - 1) / blockSizeY);
    dim3 threads(blockSizeX, blockSizeY);
    dim3 grid(gridSizeX, gridSizeY);
H
hedaoyuan 已提交
99
    TensorElementWiseOp<<<grid, threads, 0, STREAM_DEFAULT>>>(lhs_, rhs_);
H
hedaoyuan 已提交
100 101 102 103 104
  }

  CHECK_SYNC("TensorGpuApply failed");
}
#else
H
hedaoyuan 已提交
105
template <class T, typename LeftType, typename RightType>
H
hedaoyuan 已提交
106 107 108 109
inline void TensorGpuApply(LeftType& lhs, RightType& rhs) {
  LOG(FATAL) << "Since it is gcc compiled, "
                "this calculation does not support GPU implementation.";
}
H
hedaoyuan 已提交
110 111 112
#endif

}  // namespace paddle