event.cc 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2022 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.

15
#include "paddle/phi/backends/event.h"
16

17
#include "paddle/fluid/platform/device/device_wrapper.h"
18 19
#include "paddle/phi/backends/device_guard.h"
#include "paddle/phi/backends/stream.h"
20

21
namespace phi {
22 23 24 25 26 27 28 29
namespace event {

event_t Event::raw_event() const { return event_; }

void Event::set_event(event_t event) { event_ = event; }

Event::Event(const Place& place, event_t event)
    : place_(place),
30
      device_(phi::DeviceManager::GetDeviceWithPlace(place)),
31 32 33 34 35 36 37
      event_(event),
      own_data_(false) {}

Event::~Event() { Destroy(); }

bool Event::Init(const Place& place, Flag flags) {
  place_ = place;
38 39 40 41 42
  device_ = phi::DeviceManager::GetDeviceWithPlace(place);

  // note(wangran16): bind device to the current thread. fix npu plugin null
  // context bug.
  phi::DeviceManager::SetDevice(place_);
43 44 45 46 47 48 49 50 51
  device_->CreateEvent(this, flags);
  VLOG(3) << "Init Event: " << event_ << ", place: " << place_
          << ", flag:" << static_cast<int>(flags);
  own_data_ = true;
  return true;
}

void Event::Destroy() {
  if (own_data_) {
52
    phi::DeviceManager::SetDevice(place_);
53 54 55 56 57 58 59 60 61
    device_->DestroyEvent(this);
    own_data_ = false;
  }
}

void Event::Record(const stream::Stream* stream) { stream->RecordEvent(this); }

bool Event::Query() const { return device_->QueryEvent(this); }

62
void Event::Synchronize() const { device_->SynchronizeEvent(this); }
63 64 65 66

const Place& Event::GetPlace() const { return place_; }

}  // namespace event
67
}  // namespace phi