diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 4178daad4677..75583a71d630 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -5012,9 +5012,9 @@ pi_result piEventGetProfilingInfo(pi_event Event, pi_profiling_info ParamName, case PI_PROFILING_INFO_COMMAND_QUEUED: case PI_PROFILING_INFO_COMMAND_SUBMIT: // Note: No users for this case - // TODO: Implement commmand submission time when needed, - // by recording device timestamp (using zeDeviceGetGlobalTimestamps) - // before submitting command to device + // The "command_submit" time is implemented by recording submission + // timestamp with a call to piGetDeviceAndHostTimer before command enqueue. + // return ReturnValue(uint64_t{0}); default: urPrint("piEventGetProfilingInfo: not supported ParamName\n"); @@ -8677,7 +8677,6 @@ pi_result piGetDeviceAndHostTimer(pi_device Device, uint64_t *DeviceTime, &DeviceClockCount)); if (DeviceTime != nullptr) { - *DeviceTime = (DeviceClockCount & TimestampMaxCount) * ZeTimerResolution; } return PI_SUCCESS; diff --git a/sycl/source/detail/device_impl.cpp b/sycl/source/detail/device_impl.cpp index 7314fcf4e377..fa4d68453397 100644 --- a/sycl/source/detail/device_impl.cpp +++ b/sycl/source/detail/device_impl.cpp @@ -441,53 +441,54 @@ std::string device_impl::getDeviceName() const { return MDeviceName; } -/* On first call this function queries for device timestamp - along with host synchronized timestamp - and stores it in memeber varaible deviceTimePair. - Subsequent calls to this function would just retrieve the host timestamp , - compute difference against the host timestamp in deviceTimePair - and calculate the device timestamp based on the difference. - deviceTimePair is refreshed with new device and host timestamp after a - certain interval (determined by timeTillRefresh) to account for clock drift - between host and device. -*/ - +// On first call this function queries for device timestamp +// along with host synchronized timestamp and stores it in memeber varaible +// MDeviceHostBaseTime. Subsequent calls to this function would just retrieve +// the host timestamp, compute difference against the host timestamp in +// MDeviceHostBaseTime and calculate the device timestamp based on the +// difference. +// +// The MDeviceHostBaseTime is refreshed with new device and host timestamp +// after a certain interval (determined by TimeTillRefresh) to account for +// clock drift between host and device. +// uint64_t device_impl::getCurrentDeviceTime() { - // To account for potential clock drift between host clock and device clock. - // The value set is arbitrary: 200 seconds - constexpr uint64_t timeTillRefresh = 200e9; - - uint64_t hostTime; + using namespace std::chrono; + uint64_t HostTime = + duration_cast(steady_clock::now().time_since_epoch()) + .count(); if (MIsHostDevice) { - using namespace std::chrono; - return duration_cast(steady_clock::now().time_since_epoch()) - .count(); + return HostTime; } - auto plugin = getPlugin(); - RT::PiResult result = - plugin.call_nocheck( - MDevice, nullptr, &hostTime); - plugin.checkPiResult(result == PI_ERROR_INVALID_OPERATION ? PI_SUCCESS - : result); - - if (result == PI_ERROR_INVALID_OPERATION) { - char *p = nullptr; - plugin.call_nocheck(&p); - std::string errorMsg(p ? p : ""); - throw sycl::feature_not_supported( - "Device and/or backend does not support querying timestamp: " + - errorMsg, - result); - } - uint64_t diff = hostTime - MDeviceHostBaseTime.second; - if (diff > timeTillRefresh || diff <= 0) { - plugin.call( - MDevice, &MDeviceHostBaseTime.first, &MDeviceHostBaseTime.second); - diff = 0; + // To account for potential clock drift between host clock and device clock. + // The value set is arbitrary: 200 seconds + constexpr uint64_t TimeTillRefresh = 200e9; + uint64_t Diff = HostTime - MDeviceHostBaseTime.second; + + if (Diff > TimeTillRefresh || Diff <= 0) { + auto Plugin = getPlugin(); + auto Result = + Plugin.call_nocheck( + MDevice, &MDeviceHostBaseTime.first, &MDeviceHostBaseTime.second); + + if (Result == PI_ERROR_INVALID_OPERATION) { + char *p = nullptr; + Plugin.call_nocheck(&p); + std::string errorMsg(p ? p : ""); + throw sycl::feature_not_supported( + "Device and/or backend does not support querying timestamp: " + + errorMsg, + Result); + } else { + Plugin.checkPiResult(Result); + } + // Until next sync we will compute device time based on the host time + // returned in HostTime, so make this our base host time. + MDeviceHostBaseTime.second = HostTime; + Diff = 0; } - - return MDeviceHostBaseTime.first + diff; + return MDeviceHostBaseTime.first + Diff; } } // namespace detail