blob: 76aa1e443ac923b5b7aa1ff6cd3d07d4a86dc1b6 [file] [log] [blame]
#include <cmath>
#include "caffe2/core/context_gpu.h"
#include "caffe2/operators/elementwise_op.h"
namespace caffe2 {
template <typename T>
__global__ void CosKernel(const int N, const T* X, T* Y) {
CUDA_1D_KERNEL_LOOP(i, N) {
Y[i] = cos(X[i]);
}
}
template <typename T>
__global__ void CosGradientKernel(const int N, const T* X, const T* dY, T* dX) {
CUDA_1D_KERNEL_LOOP(i, N) {
dX[i] = -dY[i] * sin(X[i]);
}
}
struct CosCUDAFunctor {
template <typename T>
inline void
operator()(const int n, const T* x, T* y, CUDAContext* device_context) {
CosKernel<T>
<<<CAFFE_GET_BLOCKS(n),
CAFFE_CUDA_NUM_THREADS,
0,
device_context->cuda_stream()>>>(n, x, y);
return;
}
};
struct CosGradientCUDAFunctor {
template <typename T>
inline void Run(
const int n,
const T* x,
const T* dy,
T* dx,
CUDAContext* device_context) {
CosGradientKernel<T>
<<<CAFFE_GET_BLOCKS(n),
CAFFE_CUDA_NUM_THREADS,
0,
device_context->cuda_stream()>>>(n, x, dy, dx);
return;
}
};
REGISTER_CUDA_OPERATOR(
Cos,
UnaryElementwiseOp<TensorTypes<float>, CUDAContext, CosCUDAFunctor>);
REGISTER_CUDA_OPERATOR(
CosGradient,
BinaryElementwiseOp<
TensorTypes<float>,
CUDAContext,
WithoutBroadcast<CosGradientCUDAFunctor>>);
} // namespace caffe2