device_event_test.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#include "paddle/fluid/platform/device_event.h"
16

17 18
#include "glog/logging.h"
#include "gtest/gtest.h"
W
Wilber 已提交
19
#include "paddle/fluid/platform/place.h"
20 21

using ::paddle::platform::kCPU;
22
using ::paddle::platform::kCUDA;
23 24

using paddle::platform::DeviceContextPool;
25
using paddle::platform::DeviceEvent;
26 27 28

#ifdef PADDLE_WITH_CUDA
#include <cuda_runtime.h>
29 30 31 32 33 34 35 36 37 38 39 40

TEST(DeviceEvent, CUDA) {
  VLOG(1) << "In Test";
  using paddle::platform::CUDAPlace;

  auto& pool = DeviceContextPool::Instance();
  auto place = CUDAPlace(0);
  auto* context =
      static_cast<paddle::platform::CUDADeviceContext*>(pool.Get(place));

  ASSERT_NE(context, nullptr);
  // case 1. test for event_creator
41
  DeviceEvent event(place);
42
  ASSERT_NE(event.GetEvent().get(), nullptr);
W
Wilber 已提交
43 44
  bool status = event.Query();
  ASSERT_EQ(status, true);
45
  // case 2. test for event_recorder
46
  event.Record(context);
47 48 49 50 51 52 53 54 55 56 57 58
  // case 3. test for event_finisher
  event.Finish();
  status = event.Query();
  ASSERT_EQ(status, true);

  // case 4. test for event_waiter
  float *src_fp32, *dst_fp32;
  int size = 1000000 * sizeof(float);
  cudaMallocHost(reinterpret_cast<void**>(&src_fp32), size);
  cudaMalloc(reinterpret_cast<void**>(&dst_fp32), size);
  cudaMemcpyAsync(dst_fp32, src_fp32, size, cudaMemcpyHostToDevice,
                  context->stream());
59
  event.Record(context);  // step 1. record it
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  status = event.Query();
  ASSERT_EQ(status, false);

  event.Wait(kCUDA, context);  // step 2. add streamWaitEvent
  status = event.Query();
  ASSERT_EQ(status, false);  // async

  event.Wait(kCPU, context);  // step 3. EventSynchornize
  status = event.Query();
  ASSERT_EQ(status, true);  // sync

  // release resource
  cudaFree(dst_fp32);
  cudaFreeHost(src_fp32);
}
#endif
76

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
#ifdef PADDLE_WITH_HIP
#include <hip/hip_runtime.h>

TEST(DeviceEvent, CUDA) {
  VLOG(1) << "In Test";
  using paddle::platform::CUDAPlace;

  auto& pool = DeviceContextPool::Instance();
  auto place = CUDAPlace(0);
  auto* context =
      static_cast<paddle::platform::CUDADeviceContext*>(pool.Get(place));

  ASSERT_NE(context, nullptr);
  // case 1. test for event_creator
  DeviceEvent event(place);
  ASSERT_NE(event.GetEvent().get(), nullptr);
  bool status = event.Query();
  ASSERT_EQ(status, true);
  // case 2. test for event_recorder
  event.Record(context);
  status = event.Query();
  ASSERT_EQ(status, false);
  // case 3. test for event_finisher
  event.Finish();
  status = event.Query();
  ASSERT_EQ(status, true);

  // case 4. test for event_waiter
  float *src_fp32, *dst_fp32;
  int size = 1000000 * sizeof(float);
  hipMallocHost(reinterpret_cast<void**>(&src_fp32), size);
  hipMalloc(reinterpret_cast<void**>(&dst_fp32), size);
  hipMemcpyAsync(dst_fp32, src_fp32, size, hipMemcpyHostToDevice,
                 context->stream());
  event.Record(context);  // step 1. record it
  status = event.Query();
  ASSERT_EQ(status, false);

  event.Wait(kCUDA, context);  // step 2. add streamWaitEvent
  status = event.Query();
  ASSERT_EQ(status, false);  // async

  event.Wait(kCPU, context);  // step 3. EventSynchornize
  status = event.Query();
  ASSERT_EQ(status, true);  // sync

  // release resource
  hipFree(dst_fp32);
  hipFreeHost(src_fp32);
}
#endif

129 130 131 132 133 134 135 136 137 138 139 140
TEST(DeviceEvent, CPU) {
  using paddle::platform::CPUPlace;
  auto place = CPUPlace();
  DeviceEvent event(place);
  auto& pool = DeviceContextPool::Instance();
  auto* context = pool.Get(place);

  // TODO(Aurelius84): All DeviceContext should has Record/Wait
  event.Record(context);
  event.SetFininshed();
  bool status = event.Query();
  ASSERT_EQ(status, true);
141 142 143 144 145 146 147 148

  // test for Record again
  event.Record(context);
  status = event.Query();
  ASSERT_EQ(status, false);  // SCHEDULED

  event.Reset();
  ASSERT_EQ(status, false);  // INITIALIZED
149
}