lod_tensor_test.cu 2.3 KB
Newer Older
1
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6
//
// 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
//
D
dzhwinter 已提交
7
//     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8 9 10 11 12 13
//
// 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.
14

D
dzhwinter 已提交
15
#include <stdio.h>
16

Y
Yi Wang 已提交
17 18
#include "gtest/gtest.h"
#include "paddle/fluid/framework/lod_tensor.h"
19
#include "paddle/fluid/platform/init.h"
Y
Yi Wang 已提交
20
#include "paddle/fluid/platform/place.h"
21 22

__global__ void test(size_t* a, int size) {
23
  CUDA_KERNEL_LOOP(i, size) { a[i] *= 2; }
24 25
}

D
dzhwinter 已提交
26
TEST(LoD, data) {
27
  paddle::framework::InitDevices();
D
dzhwinter 已提交
28 29 30 31 32 33

  paddle::framework::LoD lod{{0, 1, 2}};
  lod.push_back({0, 2, 4, 5});
  lod.push_back(std::vector<size_t>({0, 1, 6, 8, 10, 11}));

  auto& v = lod[0];
Y
Yu Yang 已提交
34
  paddle::platform::CUDAPlace gpu(0);
35 36 37 38 39
#ifdef PADDLE_WITH_HIP
  hipLaunchKernelGGL(test, dim3(1), dim3(1), 0, 0, v.CUDAMutableData(gpu),
                     v.size());
  hipDeviceSynchronize();
#else
Y
Yu Yang 已提交
40
  test<<<1, 1>>>(v.CUDAMutableData(gpu), v.size());
D
dzhwinter 已提交
41
  cudaDeviceSynchronize();
42
#endif
D
dzhwinter 已提交
43 44 45 46 47
  for (size_t i = 0; i < v.size(); ++i) {
    EXPECT_EQ(v[i], i * 2);
  }
}

48
TEST(LoDTensor, LoDInGPU) {
49
  paddle::framework::InitDevices();
D
dzhwinter 已提交
50

51
  paddle::framework::LoDTensor lod_tensor;
D
dzhwinter 已提交
52
  paddle::platform::CUDAPlace place(0);
53 54 55 56

  paddle::framework::LoD src_lod;
  src_lod.push_back(std::vector<size_t>{0, 2, 4, 6, 8, 10, 12, 14});

57 58
  lod_tensor.Resize({14, 16});
  lod_tensor.mutable_data<float>(place);
59 60

  lod_tensor.set_lod(src_lod);
61 62
  EXPECT_EQ(lod_tensor.lod_element(0, 2).first, 4UL);
  EXPECT_EQ(lod_tensor.lod_element(0, 4).first, 8UL);
63 64 65

  auto lod = lod_tensor.lod();

66 67 68 69 70
#ifdef PADDLE_WITH_HIP
  hipLaunchKernelGGL(test, dim3(1), dim3(8), 0, 0,
                     lod[0].CUDAMutableData(place), lod[0].size());
  hipDeviceSynchronize();
#else
Y
Yu Yang 已提交
71
  test<<<1, 8>>>(lod[0].CUDAMutableData(place), lod[0].size());
72
  cudaDeviceSynchronize();
73
#endif
74 75

  for (size_t i = 0; i < src_lod[0].size(); ++i) {
76
    EXPECT_EQ(lod[0].data()[i], src_lod[0].data()[i] * 2);
77
  }
78
}