Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -8677,7 +8677,6 @@ pi_result piGetDeviceAndHostTimer(pi_device Device, uint64_t *DeviceTime,
&DeviceClockCount));

if (DeviceTime != nullptr) {

*DeviceTime = (DeviceClockCount & TimestampMaxCount) * ZeTimerResolution;
}
return PI_SUCCESS;
Expand Down
85 changes: 43 additions & 42 deletions sycl/source/detail/device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nanoseconds>(steady_clock::now().time_since_epoch())
.count();
if (MIsHostDevice) {
using namespace std::chrono;
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch())
.count();
return HostTime;
}
auto plugin = getPlugin();
RT::PiResult result =
plugin.call_nocheck<detail::PiApiKind::piGetDeviceAndHostTimer>(
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<detail::PiApiKind::piPluginGetLastError>(&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<detail::PiApiKind::piGetDeviceAndHostTimer>(
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<detail::PiApiKind::piGetDeviceAndHostTimer>(
MDevice, &MDeviceHostBaseTime.first, &MDeviceHostBaseTime.second);

if (Result == PI_ERROR_INVALID_OPERATION) {
char *p = nullptr;
Plugin.call_nocheck<detail::PiApiKind::piPluginGetLastError>(&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
Expand Down