2021-07-02 16:46:49 -07:00
|
|
|
/* Copyright (c) 2015 - 2021 Advanced Micro Devices, Inc.
|
2018-05-01 18:10:09 -04:00
|
|
|
|
2020-02-04 08:45:01 -08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
2018-05-01 18:10:09 -04:00
|
|
|
|
2020-02-04 08:45:01 -08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
2018-05-01 18:10:09 -04:00
|
|
|
|
2020-02-04 08:45:01 -08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE. */
|
2018-05-01 18:10:09 -04:00
|
|
|
|
|
|
|
|
#ifndef HIP_EVENT_H
|
|
|
|
|
#define HIP_EVENT_H
|
|
|
|
|
|
|
|
|
|
#include "hip_internal.hpp"
|
2019-05-06 17:43:06 -04:00
|
|
|
#include "thread/monitor.hpp"
|
2018-05-01 18:10:09 -04:00
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
|
2020-07-10 06:24:35 +00:00
|
|
|
// Internal structure for stream callback handler
|
|
|
|
|
class StreamCallback {
|
2021-12-01 17:40:49 -08:00
|
|
|
public:
|
|
|
|
|
StreamCallback(hipStream_t stream, hipStreamCallback_t callback, void* userData,
|
|
|
|
|
amd::Command* command)
|
|
|
|
|
: stream_(stream), callBack_(callback), userData_(userData), command_(command){};
|
|
|
|
|
hipStream_t stream_;
|
|
|
|
|
hipStreamCallback_t callBack_;
|
|
|
|
|
void* userData_;
|
|
|
|
|
amd::Command* command_;
|
2020-07-10 06:24:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void CL_CALLBACK ihipStreamCallback(cl_event event, cl_int command_exec_status, void* user_data);
|
|
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
namespace hip {
|
|
|
|
|
|
2020-07-10 06:24:35 +00:00
|
|
|
#define IPC_SIGNALS_PER_EVENT 32
|
|
|
|
|
typedef struct ihipIpcEventShmem_s {
|
|
|
|
|
std::atomic<int> owners;
|
2021-03-09 12:00:16 -05:00
|
|
|
std::atomic<int> owners_device_id;
|
2021-07-28 00:03:29 -04:00
|
|
|
std::atomic<int> owners_process_id;
|
2020-07-10 06:24:35 +00:00
|
|
|
std::atomic<int> read_index;
|
|
|
|
|
std::atomic<int> write_index;
|
|
|
|
|
std::atomic<int> signal[IPC_SIGNALS_PER_EVENT];
|
|
|
|
|
} ihipIpcEventShmem_t;
|
|
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
class EventMarker : public amd::Marker {
|
|
|
|
|
public:
|
|
|
|
|
EventMarker(amd::HostQueue& queue, bool disableFlush, bool markerTs = false)
|
|
|
|
|
: amd::Marker(queue, disableFlush) {
|
|
|
|
|
profilingInfo_.enabled_ = true;
|
|
|
|
|
profilingInfo_.callback_ = nullptr;
|
|
|
|
|
profilingInfo_.marker_ts_ = markerTs;
|
|
|
|
|
profilingInfo_.clear();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-01 18:10:09 -04:00
|
|
|
class Event {
|
2021-03-02 16:23:47 -05:00
|
|
|
/// event recorded on stream where capture is active
|
|
|
|
|
bool onCapture_;
|
|
|
|
|
/// capture stream where event is recorded
|
|
|
|
|
hipStream_t captureStream_;
|
|
|
|
|
/// Previous captured nodes before event record
|
|
|
|
|
std::vector<hipGraphNode_t> nodesPrevToRecorded_;
|
|
|
|
|
|
|
|
|
|
public:
|
2021-11-29 17:53:36 +00:00
|
|
|
Event(unsigned int flags) : flags(flags), lock_("hipEvent_t", true),
|
|
|
|
|
event_(nullptr), recorded_(false), stream_(nullptr) {
|
2020-03-26 15:07:41 -07:00
|
|
|
// No need to init event_ here as addMarker does that
|
2021-03-02 16:23:47 -05:00
|
|
|
onCapture_ = false;
|
2021-12-01 17:40:49 -08:00
|
|
|
device_id_ = hip::getCurrentDevice()->deviceId(); // Created in current device ctx
|
2020-03-26 15:07:41 -07:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
virtual ~Event() {
|
2018-05-15 16:26:16 -04:00
|
|
|
if (event_ != nullptr) {
|
|
|
|
|
event_->release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-01 18:10:09 -04:00
|
|
|
unsigned int flags;
|
2018-05-15 16:26:16 -04:00
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
virtual hipError_t query();
|
|
|
|
|
virtual hipError_t synchronize();
|
2021-11-29 17:53:36 +00:00
|
|
|
hipError_t elapsedTime(Event& eStop, float& ms);
|
2019-05-06 17:43:06 -04:00
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
virtual hipError_t streamWaitCommand(amd::Command*& command, amd::HostQueue* queue);
|
|
|
|
|
virtual hipError_t enqueueStreamWaitCommand(hipStream_t stream, amd::Command* command);
|
|
|
|
|
virtual hipError_t streamWait(hipStream_t stream, uint flags);
|
2021-10-05 10:06:40 -07:00
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
virtual hipError_t recordCommand(amd::Command*& command, amd::HostQueue* queue);
|
|
|
|
|
virtual hipError_t enqueueRecordCommand(hipStream_t stream, amd::Command* command, bool record);
|
2021-10-05 10:06:40 -07:00
|
|
|
hipError_t addMarker(hipStream_t stream, amd::Command* command, bool record);
|
2021-12-01 17:40:49 -08:00
|
|
|
|
2021-10-25 21:46:46 -07:00
|
|
|
void BindCommand(amd::Command& command, bool record) {
|
2021-10-19 00:01:41 -04:00
|
|
|
amd::ScopedLock lock(lock_);
|
|
|
|
|
if (event_ != nullptr) {
|
|
|
|
|
event_->release();
|
|
|
|
|
}
|
|
|
|
|
event_ = &command.event();
|
2021-10-25 21:46:46 -07:00
|
|
|
recorded_ = record;
|
2021-10-19 00:01:41 -04:00
|
|
|
command.retain();
|
|
|
|
|
}
|
2021-10-05 10:06:40 -07:00
|
|
|
|
2021-11-29 17:53:36 +00:00
|
|
|
bool isRecorded() const { return recorded_; }
|
2020-07-15 15:11:59 -04:00
|
|
|
amd::Monitor& lock() { return lock_; }
|
2021-11-29 17:53:36 +00:00
|
|
|
const int deviceId() const { return device_id_; }
|
2021-03-09 12:00:16 -05:00
|
|
|
void setDeviceId(int id) { device_id_ = id; }
|
2020-07-15 15:11:59 -04:00
|
|
|
|
2021-03-02 16:23:47 -05:00
|
|
|
/// End capture on this event
|
|
|
|
|
void EndCapture() {
|
|
|
|
|
onCapture_ = false;
|
|
|
|
|
captureStream_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
/// Start capture when waited on this event
|
|
|
|
|
void StartCapture(hipStream_t stream) {
|
|
|
|
|
onCapture_ = true;
|
|
|
|
|
captureStream_ = stream;
|
|
|
|
|
}
|
|
|
|
|
/// Get capture status of the graph
|
2021-11-29 17:53:36 +00:00
|
|
|
bool GetCaptureStatus() const { return onCapture_; }
|
2021-03-02 16:23:47 -05:00
|
|
|
/// Get capture stream where event is recorded
|
2021-11-29 17:53:36 +00:00
|
|
|
hipStream_t GetCaptureStream() const { return captureStream_; }
|
2021-03-02 16:23:47 -05:00
|
|
|
/// Set capture stream where event is recorded
|
|
|
|
|
void SetCaptureStream(hipStream_t stream) { captureStream_ = stream; }
|
|
|
|
|
/// Returns previous captured nodes before event record
|
|
|
|
|
std::vector<hipGraphNode_t> GetNodesPrevToRecorded() const { return nodesPrevToRecorded_; }
|
|
|
|
|
/// Set last captured graph node before event record
|
|
|
|
|
void SetNodesPrevToRecorded(std::vector<hipGraphNode_t>& graphNode) {
|
|
|
|
|
nodesPrevToRecorded_ = graphNode;
|
|
|
|
|
}
|
2021-12-01 17:40:49 -08:00
|
|
|
virtual hipError_t GetHandle(ihipIpcEventHandle_t* handle) {
|
|
|
|
|
return hipErrorInvalidConfiguration;
|
|
|
|
|
}
|
|
|
|
|
virtual hipError_t OpenHandle(ihipIpcEventHandle_t* handle) {
|
|
|
|
|
return hipErrorInvalidConfiguration;
|
|
|
|
|
}
|
2021-03-02 16:23:47 -05:00
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
protected:
|
2019-05-06 17:43:06 -04:00
|
|
|
amd::Monitor lock_;
|
2018-05-15 16:26:16 -04:00
|
|
|
amd::HostQueue* stream_;
|
|
|
|
|
amd::Event* event_;
|
2021-03-09 12:00:16 -05:00
|
|
|
int device_id_;
|
2020-05-27 00:36:27 -07:00
|
|
|
//! Flag to indicate hipEventRecord has been called. This is needed except for
|
|
|
|
|
//! hip*ModuleLaunchKernel API which takes start and stop events so no
|
|
|
|
|
//! hipEventRecord is called. Cleanup needed once those APIs are deprecated.
|
|
|
|
|
bool recorded_;
|
|
|
|
|
|
2019-05-06 17:43:06 -04:00
|
|
|
bool ready();
|
2021-05-03 12:55:37 -07:00
|
|
|
int64_t time() const;
|
2018-05-01 18:10:09 -04:00
|
|
|
};
|
|
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
class IPCEvent : public Event {
|
|
|
|
|
// IPC Events
|
|
|
|
|
struct ihipIpcEvent_t {
|
|
|
|
|
std::string ipc_name_;
|
|
|
|
|
int ipc_fd_;
|
|
|
|
|
ihipIpcEventShmem_t* ipc_shmem_;
|
|
|
|
|
ihipIpcEvent_t() : ipc_name_("dummy"), ipc_fd_(0), ipc_shmem_(nullptr) {}
|
|
|
|
|
void setipcname(const char* name) { ipc_name_ = std::string(name); }
|
|
|
|
|
};
|
|
|
|
|
ihipIpcEvent_t ipc_evt_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
~IPCEvent() {
|
|
|
|
|
if (ipc_evt_.ipc_shmem_) {
|
|
|
|
|
int owners = --ipc_evt_.ipc_shmem_->owners;
|
|
|
|
|
// Make sure event is synchronized
|
|
|
|
|
hipError_t status = synchronize();
|
|
|
|
|
if (!amd::Os::MemoryUnmapFile(ipc_evt_.ipc_shmem_, sizeof(hip::ihipIpcEventShmem_t))) {
|
|
|
|
|
// print hipErrorInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IPCEvent() : Event(hipEventInterprocess) {}
|
|
|
|
|
bool createIpcEventShmemIfNeeded();
|
|
|
|
|
hipError_t GetHandle(ihipIpcEventHandle_t* handle);
|
|
|
|
|
hipError_t OpenHandle(ihipIpcEventHandle_t* handle);
|
|
|
|
|
hipError_t synchronize();
|
|
|
|
|
hipError_t query();
|
|
|
|
|
|
|
|
|
|
hipError_t streamWaitCommand(amd::Command*& command, amd::HostQueue* queue);
|
|
|
|
|
hipError_t enqueueStreamWaitCommand(hipStream_t stream, amd::Command* command);
|
|
|
|
|
hipError_t streamWait(hipStream_t stream, uint flags);
|
|
|
|
|
|
|
|
|
|
hipError_t recordCommand(amd::Command*& command, amd::HostQueue* queue);
|
|
|
|
|
hipError_t enqueueRecordCommand(hipStream_t stream, amd::Command* command, bool record);
|
2018-05-01 18:10:09 -04:00
|
|
|
};
|
|
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
}; // namespace hip
|
|
|
|
|
|
2021-10-05 10:06:40 -07:00
|
|
|
struct CallbackData {
|
|
|
|
|
int previous_read_index;
|
|
|
|
|
hip::ihipIpcEventShmem_t* shmem;
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-01 17:40:49 -08:00
|
|
|
#endif // HIP_EVEMT_H
|