ExecViaCpu.h 5.4 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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. */

/*
 execViaCpu is used to do operations on GpuMatirx and/or GpuIVector through
 cpu functions. It can automatically make a temporary CPU copy for the
 gpu matrix/vector, and copy back after executing the CPU function.

 Examples:
 1. For a function, functor or lambda:
   r = execViaCpu(&f, mat, vec)

 2. For member function of CpuMatirx, execViaCpu2 should be used:
   execViaCpu2(&CpuMatrix::selectElements, *this, table, ids)
*/

#pragma once

namespace paddle {

template <typename Arg>
class CopyToCpu {
public:
  explicit CopyToCpu(Arg& arg) : arg_(arg) {}
  Arg& copiedArg() const { return arg_; }

private:
  Arg& arg_;
};

template <>
class CopyToCpu<Matrix> {
public:
  explicit CopyToCpu(Matrix& arg) : arg_(arg) {
    if (arg.useGpu()) {
      CHECK(!arg.isTransposed()) << "Not supported";
48 49 50 51
      copied_ = Matrix::create(arg.getHeight(),
                               arg.getWidth(),
                               /* trans= */ false,
                               /* useGpu= */ false);
Z
zhangjinchao01 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      copied_->copyFrom(arg);
    }
  }
  ~CopyToCpu() {
    if (copied_) {
      arg_.copyFrom(*copied_);
    }
  }
  Matrix& copiedArg() const { return copied_ ? *copied_ : arg_; }

private:
  Matrix& arg_;
  MatrixPtr copied_;
};

template <>
class CopyToCpu<const Matrix> {
public:
  explicit CopyToCpu(const Matrix& arg) : arg_(arg) {
    if (arg.useGpu()) {
      CHECK(!arg.isTransposed()) << "Not supported";
73 74 75 76
      copied_ = Matrix::create(arg.getHeight(),
                               arg.getWidth(),
                               /* trans= */ false,
                               /* useGpu= */ false);
Z
zhangjinchao01 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      copied_->copyFrom(arg);
    }
  }
  const Matrix& copiedArg() const { return copied_ ? *copied_ : arg_; }

private:
  const Matrix& arg_;
  MatrixPtr copied_;
};

template <>
class CopyToCpu<IVector> {
public:
  explicit CopyToCpu(IVector& arg) : arg_(arg) {
    if (arg.useGpu()) {
      copied_ = IVector::create(arg.getSize(), /* useGpu= */ false);
      copied_->copyFrom(arg);
    }
  }
  ~CopyToCpu() {
    if (copied_) {
      arg_.copyFrom(*copied_);
    }
  }
  IVector& copiedArg() const { return copied_ ? *copied_ : arg_; }

private:
  IVector& arg_;
  IVectorPtr copied_;
};

template <>
class CopyToCpu<const IVector> {
public:
  explicit CopyToCpu(const IVector& arg) : arg_(arg) {
    if (arg.useGpu()) {
      copied_ = IVector::create(arg.getSize(), /* useGpu= */ false);
      copied_->copyFrom(arg);
    }
  }
  const IVector& copiedArg() const { return copied_ ? *copied_ : arg_; }

private:
  const IVector& arg_;
  IVectorPtr copied_;
};

namespace detail {

template <bool isFunction, bool isFunctionPointer, bool isClass, typename F>
class GpuFuncWrapperImp;

template <typename F, typename R, typename... Args>
class GpuFuncWrapperBase {
public:
  typedef R ResultType;
  R operator()(F&& f, Args... args) {
    return f(CopyToCpu<typename std::remove_reference<Args>::type>(args)
                 .copiedArg()...);
  }
};

// function
template <typename R, typename... Args>
class GpuFuncWrapperImp<true, false, false, R(Args...)>
    : public GpuFuncWrapperBase<R(Args...), R, Args...> {};

// function pointer
template <typename R, typename... Args>
class GpuFuncWrapperImp<false, true, false, R (*)(Args...)>
    : public GpuFuncWrapperBase<R (*)(Args...), R, Args...> {};

template <typename F, typename Op>
class GpuFuncWrapperImp2;

template <typename F, typename C, typename R, typename... Args>
class GpuFuncWrapperImp2<F, R (C::*)(Args...) const>
    : public GpuFuncWrapperBase<F, R, Args...> {};

template <typename F, typename C, typename R, typename... Args>
class GpuFuncWrapperImp2<F, R (C::*)(Args...)>
    : public GpuFuncWrapperBase<F, R, Args...> {};

// functor or lambda
template <typename F>
class GpuFuncWrapperImp<false, false, true, F>
    : public GpuFuncWrapperImp2<F, decltype(&F::operator())> {};

template <typename F>
class GpuFuncWrapper2
    : public GpuFuncWrapperImp<
          std::is_function<F>::value,
          std::is_pointer<F>::value &&
              std::is_function<typename std::remove_pointer<F>::type>::value,
171 172
          std::is_class<F>::value,
          F> {};
Z
zhangjinchao01 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

template <typename F>
class GpuFuncWrapper
    : public GpuFuncWrapper2<typename std::remove_reference<F>::type> {};

}  // namespace detail

template <typename F, typename... Args>
typename detail::GpuFuncWrapper<F>::ResultType execViaCpu(F&& f,
                                                          Args&&... args) {
  return detail::GpuFuncWrapper<F>()(std::move(f), args...);
}

// The second version is for F as member function of CpuMatrix
template <typename R, typename... FArgs, typename... Args>
R execViaCpu2(R (CpuMatrix::*f)(FArgs...), Args&&... args) {
  auto lambda = [](R (CpuMatrix::*f)(FArgs...), Matrix& ths, FArgs... args) {
    return (((CpuMatrix&)ths).*f)(args...);
  };
  return execViaCpu(lambda, f, args...);
}

}  // namespace paddle