Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pvelesko committed Aug 27, 2024
1 parent 883cbe9 commit 724b4f0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src/CHIPBindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2777,8 +2777,10 @@ hipError_t hipEventElapsedTime(float *Ms, hipEvent_t Start, hipEvent_t Stop) {
hipErrorInvalidHandle);
}

ChipEventStart->updateFinishStatus(true);
ChipEventStop->updateFinishStatus(true);
if (!ChipEventStart->getEventStatus() == EVENT_STATUS_RECORDING)

Check warning on line 2780 in src/CHIPBindings.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/CHIPBindings.cc:2780:7 [readability-implicit-bool-conversion]

implicit conversion bool -> 'int'

Check warning on line 2780 in src/CHIPBindings.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/CHIPBindings.cc:2780:8 [readability-implicit-bool-conversion]

implicit conversion 'event_status_e' -> bool

Check warning on line 2780 in src/CHIPBindings.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/CHIPBindings.cc:2780:68 [readability-braces-around-statements]

statement should be inside braces
RETURN(hipErrorNotReady);
if (!ChipEventStop->getEventStatus() == EVENT_STATUS_RECORDING)

Check warning on line 2782 in src/CHIPBindings.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/CHIPBindings.cc:2782:7 [readability-implicit-bool-conversion]

implicit conversion bool -> 'int'

Check warning on line 2782 in src/CHIPBindings.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/CHIPBindings.cc:2782:8 [readability-implicit-bool-conversion]

implicit conversion 'event_status_e' -> bool

Check warning on line 2782 in src/CHIPBindings.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/CHIPBindings.cc:2782:67 [readability-braces-around-statements]

statement should be inside braces
RETURN(hipErrorNotReady);

*Ms = ChipEventStart->getElapsedTime(ChipEventStop);
RETURN(hipSuccess);
Expand Down
8 changes: 5 additions & 3 deletions src/backend/Level0/zeHipErrorConversion.hh
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,16 @@ inline hipError_t hip_convert_error(ze_result_t zeStatus, FuncPtr func) {
#undef CHIPERR_CHECK_LOG_AND_THROW_TABLE
#define CHIPERR_CHECK_LOG_AND_THROW_TABLE(func, ...) \
do { \
if (zeStatus != ZE_RESULT_SUCCESS) { \
hipError_t err = hip_convert_error(zeStatus, func); \
if (zeStatus != ZE_RESULT_SUCCESS) { \
hipError_t err = hip_convert_error(zeStatus, func); \
if (err == hipErrorTbd) { \
std::cerr << "Error: Unmapped API or API Error Code encountered at " \
<< __FILE__ << ":" << __LINE__ << std::endl; \
std::cerr << "API call: " << #func << std::endl; \
std::cerr << "Error code: " << resultToString(zeStatus) << std::endl; \
std::abort(); \
} \
std::string error_msg = std::string(resultToString(zeStatus)); \
std::string error_msg = std::string(resultToString(zeStatus)); \
std::string custom_msg = std::string(__VA_ARGS__); \
std::string msg_ = error_msg + " " + custom_msg; \
CHIPERR_LOG_AND_THROW(msg_, err); \
Expand Down
44 changes: 20 additions & 24 deletions src/backend/OpenCL/CHIPBackendOpenCL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ CHIPEventOpenCL::CHIPEventOpenCL(CHIPContextOpenCL *ChipContext,
uint64_t CHIPEventOpenCL::getFinishTime() {
int clStatus;
uint64_t Ret;
clStatus = clGetEventProfilingInfo(ClEvent, CL_PROFILING_COMMAND_END,
clStatus = clGetEventProfilingInfo(ClEvent, CL_PROFILING_COMMAND_COMPLETE,
sizeof(Ret), &Ret, NULL);

Check warning on line 670 in src/backend/OpenCL/CHIPBackendOpenCL.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.cc:670:57 [modernize-use-nullptr]

use nullptr

if (clStatus != CL_SUCCESS) {
Expand Down Expand Up @@ -730,7 +730,8 @@ void CHIPEventOpenCL::recordEventCopy(
this->ClEvent = Other->ClEvent;
this->RecordedEvent = Other;
this->Msg = "recordEventCopy: " + Other->Msg;
this->HostTimeStamp = std::chrono::high_resolution_clock::now().time_since_epoch().count();
this->HostTimeStamp =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
}

bool CHIPEventOpenCL::wait() {
Expand All @@ -749,8 +750,6 @@ bool CHIPEventOpenCL::wait() {

bool CHIPEventOpenCL::updateFinishStatus(bool ThrowErrorIfNotReady) {
logTrace("CHIPEventOpenCL::updateFinishStatus()");


if (ThrowErrorIfNotReady && this->ClEvent == nullptr)
CHIPERR_LOG_AND_THROW("OpenCL has not been initialized cl_event is null",
hipErrorNotReady);
Expand Down Expand Up @@ -801,14 +800,20 @@ float CHIPEventOpenCL::getElapsedTime(chipstar::Event *OtherIn) {
logTrace("EventElapsedTime: STARTED {} / {} FINISHED {} / {} \n",
(void *)this, Started, (void *)Other, Finished);

// apparently fails for Intel NEO, god knows why
// assert(Finished >= Started);
int64_t Elapsed;
const int64_t NANOSECS = 1000000000;
if (Finished < Started && Other->HostTimeStamp > this->HostTimeStamp) {
std::swap(Started, Finished);
logWarn("Events swapped based on host timestamps\n");
if (Finished < Started) {
logWarn("Finished < Started\n");
std::cout << "delta t: "
<< std::setprecision(std::numeric_limits<int64_t>::digits10 + 1)
<< std::fixed << (Finished - Started) << "\n";
}

// if (Finished < Started && Other->HostTimeStamp > this->HostTimeStamp) {
// std::swap(Started, Finished);
// logWarn("Events swapped based on host timestamps\n");
// }

Elapsed = Finished - Started;
int64_t MS = (Elapsed / NANOSECS) * 1000;
int64_t NS = Elapsed % NANOSECS;
Expand Down Expand Up @@ -1089,8 +1094,9 @@ void CL_CALLBACK pfn_notify(cl_event Event, cl_int CommandExecStatus,
std::static_pointer_cast<CHIPEventOpenCL>(Cbo->CallbackFinishEvent)
->ClEvent,
CL_COMPLETE);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetUserEventStatus);
}
delete Cbo;
// delete Cbo;
}

void CHIPQueueOpenCL::MemMap(const chipstar::AllocationInfo *AllocInfo,
Expand Down Expand Up @@ -1181,21 +1187,12 @@ void CHIPQueueOpenCL::addCallback(hipStreamCallback_t Callback,
cl::Context *ClContext_ = ((CHIPContextOpenCL *)ChipContext_)->get();
cl_int Err;

std::shared_ptr<chipstar::Event> HoldBackEvent =
static_cast<CHIPBackendOpenCL *>(Backend)->createEventShared(
ChipContext_);

std::static_pointer_cast<CHIPEventOpenCL>(HoldBackEvent)->ClEvent =
clCreateUserEvent(ClContext_->get(), &Err);

std::vector<std::shared_ptr<chipstar::Event>> WaitForEvents{HoldBackEvent};

// Enqueue a barrier used to ensure the callback is not called too early,
// otherwise it would be (at worst) executed in this host thread when
// setting it, blocking the execution, while the clients might expect
// parallel execution.
std::shared_ptr<chipstar::Event> HoldbackBarrierCompletedEv =
enqueueBarrier(WaitForEvents);
enqueueBarrier(std::vector<std::shared_ptr<chipstar::Event>>{});

// OpenCL event callbacks have undefined execution ordering/finishing
// guarantees. We need to enforce CUDA ordering using user events.
Expand Down Expand Up @@ -1227,13 +1224,12 @@ void CHIPQueueOpenCL::addCallback(hipStreamCallback_t Callback,
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetEventCallback);

updateLastEvent(CallbackCompleted);
get()->flush();

// Now the CB can start executing in the background:
clSetUserEventStatus(
std::static_pointer_cast<CHIPEventOpenCL>(HoldBackEvent)->ClEvent,
std::static_pointer_cast<CHIPEventOpenCL>(HoldbackBarrierCompletedEv)->ClEvent,
CL_COMPLETE);
// HoldBackEvent->decreaseRefCount("Notified finished.");
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetUserEventStatus);

return;
};
Expand Down Expand Up @@ -1517,7 +1513,7 @@ void CHIPQueueOpenCL::finish() {
LOCK(Backend->DubiousLockOpenCL)
#endif
clStatus = get()->finish();
// CHIPERR_CHECK_LOG_AND_ABORT(clStatus, CL_SUCCESS, hipErrorTbd);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clFinish);
}

std::shared_ptr<chipstar::Event>
Expand Down
3 changes: 2 additions & 1 deletion src/backend/OpenCL/clHipErrorConversion.hh
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ const std::unordered_map<void *, cl_hip_error_map_t> CL_HIP_ERROR_MAPS = {
{(void *)&clFinish,
{{CL_SUCCESS, hipSuccess},
{CL_INVALID_COMMAND_QUEUE, hipErrorInvalidResourceHandle},
{CL_OUT_OF_HOST_MEMORY, hipErrorOutOfMemory}}},
{CL_OUT_OF_HOST_MEMORY, hipErrorOutOfMemory},
{CL_OUT_OF_RESOURCES, hipErrorOutOfMemory}}},

{(void *)&clFlush,
{{CL_SUCCESS, hipSuccess},
Expand Down

0 comments on commit 724b4f0

Please sign in to comment.