math_function_test.cc 1.1 KB
Newer Older
Q
qijun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "paddle/operators/math/math_function.h"
#include "gtest/gtest.h"

#ifndef PADDLE_ONLY_CPU
TEST(math_function, GPU) {
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input1_gpu;
  paddle::framework::Tensor input2_gpu;
  paddle::framework::Tensor out_gpu;
  paddle::framework::Tensor out;

  auto* cpu_place = new paddle::platform::CPUPlace();
  float* input1_ptr = input1.mutable_data<float>({2, 2}, *cpu_place);
  float arr[4] = {0, 1, 2, 3};
Q
qijun 已提交
15
  memcpy(input1_ptr, arr, 4 * sizeof(int));
Q
qijun 已提交
16 17

  auto* gpu_place = new paddle::platform::GPUPlace(0);
Q
qijun 已提交
18
  paddle::platform::DeviceContext* context =
Q
qijun 已提交
19
      new paddle::platform::CUDADeviceContext(*gpu_place);
Q
qijun 已提交
20 21 22 23 24

  input1_gpu.CopyFrom<float>(input1, *gpu_place);
  input2_gpu.CopyFrom<float>(input1, *gpu_place);
  out_gpu.CopyFrom<float>(input1, *gpu_place);

Q
qijun 已提交
25
  paddle::operators::math::matmul<paddle::platform::GPUPlace, float>(
Q
qijun 已提交
26
      input1_gpu, false, input2_gpu, false, 1, &out_gpu, 0, context);
Q
qijun 已提交
27 28 29 30 31 32 33 34 35

  out.CopyFrom<float>(out_gpu, *cpu_place);

  float* out_ptr = out.data<float>();
  EXPECT_EQ(out_ptr[0], 2);
  EXPECT_EQ(out_ptr[1], 3);
  EXPECT_EQ(out_ptr[2], 6);
  EXPECT_EQ(out_ptr[3], 11);
}
Q
qijun 已提交
36
#endif