|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "vulkan_timer.h" |
| 21 | + |
| 22 | +#include <tvm/runtime/logging.h> |
| 23 | + |
| 24 | +#include "vulkan_device_api.h" |
| 25 | + |
| 26 | +namespace tvm { |
| 27 | +namespace runtime { |
| 28 | +namespace vulkan { |
| 29 | + |
| 30 | +VulkanTimerNode::VulkanTimerNode(Device dev) : dev_(dev) { |
| 31 | + // Get the Vulkan device and stream |
| 32 | + auto& vk_dev = VulkanDeviceAPI::Global()->device(dev_.device_id); |
| 33 | + stream_ = &vk_dev.ThreadLocalStream(); |
| 34 | + device_ = vk_dev; |
| 35 | + |
| 36 | + // Retrieve the timestamp period from device properties |
| 37 | + timestamp_period_ = vk_dev.device_properties.timestamp_period; |
| 38 | + TVM_FFI_ICHECK_GT(timestamp_period_, 0) << "Vulkan device does not support timestamp queries."; |
| 39 | + |
| 40 | + CreateQueryPool(); |
| 41 | +} |
| 42 | + |
| 43 | +VulkanTimerNode::~VulkanTimerNode() { Cleanup(); } |
| 44 | + |
| 45 | +void VulkanTimerNode::CreateQueryPool() { |
| 46 | + VkQueryPoolCreateInfo query_pool_info{}; |
| 47 | + query_pool_info.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; |
| 48 | + query_pool_info.queryType = VK_QUERY_TYPE_TIMESTAMP; |
| 49 | + query_pool_info.queryCount = 2; |
| 50 | + |
| 51 | + VkResult res = vkCreateQueryPool(device_, &query_pool_info, nullptr, &query_pool_); |
| 52 | + TVM_FFI_ICHECK(res == VK_SUCCESS) << "Failed to create Vulkan query pool."; |
| 53 | +} |
| 54 | + |
| 55 | +void VulkanTimerNode::Start() { |
| 56 | + stream_->Launch([this](VulkanStreamState* state) { |
| 57 | + // Reset the query pool before writing timestamps |
| 58 | + vkCmdResetQueryPool(state->cmd_buffer_, query_pool_, start_query_, 2); |
| 59 | + vkCmdWriteTimestamp(state->cmd_buffer_, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, query_pool_, |
| 60 | + start_query_); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +void VulkanTimerNode::Stop() { |
| 65 | + stream_->Launch([this](VulkanStreamState* state) { |
| 66 | + vkCmdWriteTimestamp(state->cmd_buffer_, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, query_pool_, |
| 67 | + end_query_); |
| 68 | + }); |
| 69 | + |
| 70 | + // Ensure GPU has finished writing timestamps before collecting them |
| 71 | + stream_->Synchronize(); |
| 72 | + CollectTimestamps(); |
| 73 | +} |
| 74 | + |
| 75 | +int64_t VulkanTimerNode::SyncAndGetElapsedNanos() { return duration_; } |
| 76 | + |
| 77 | +void VulkanTimerNode::CollectTimestamps() { |
| 78 | + uint64_t timestamps[2] = {0}; |
| 79 | + |
| 80 | + VkResult result = |
| 81 | + vkGetQueryPoolResults(device_, query_pool_, 0, 2, sizeof(timestamps), timestamps, |
| 82 | + sizeof(uint64_t), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 83 | + |
| 84 | + TVM_FFI_ICHECK(result == VK_SUCCESS) << "Failed to get Vulkan query pool results."; |
| 85 | + |
| 86 | + // Calculate the duration in nanoseconds |
| 87 | + uint64_t diff = timestamps[1] - timestamps[0]; |
| 88 | + duration_ = static_cast<int64_t>(static_cast<double>(diff) * timestamp_period_); |
| 89 | +} |
| 90 | + |
| 91 | +void VulkanTimerNode::Cleanup() { |
| 92 | + if (query_pool_ != VK_NULL_HANDLE) { |
| 93 | + vkDestroyQueryPool(device_, query_pool_, nullptr); |
| 94 | + query_pool_ = VK_NULL_HANDLE; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace vulkan |
| 99 | +} // namespace runtime |
| 100 | +} // namespace tvm |
0 commit comments