support for timestamps

This commit is contained in:
alexander-g 2021-03-06 11:45:29 +01:00
parent 9edbac4b94
commit 6f5a8f8968
6 changed files with 96 additions and 8 deletions

View file

@ -129,6 +129,7 @@ PYBIND11_MODULE(kp, m) {
.def("is_recording", &kp::Sequence::isRecording)
.def("is_running", &kp::Sequence::isRunning)
.def("is_init", &kp::Sequence::isInit)
.def("get_timestamps", &kp::Sequence::getTimestamps)
.def("clear", &kp::Sequence::clear)
.def("destroy", &kp::Sequence::destroy);
@ -136,7 +137,7 @@ PYBIND11_MODULE(kp, m) {
.def(py::init())
.def(py::init<uint32_t>())
.def(py::init<uint32_t,const std::vector<uint32_t>&>())
.def("sequence", &kp::Manager::sequence, py::arg("queueIndex") = 0)
.def("sequence", &kp::Manager::sequence, py::arg("queueIndex") = 0, py::arg("nrOfTimestamps") = 0)
.def("tensor", [np](kp::Manager& self,
const py::array_t<float> data,
kp::Tensor::TensorTypes tensor_type) {

View file

@ -1527,11 +1527,13 @@ class Sequence : public std::enable_shared_from_this<Sequence>
* @param device Vulkan logical device
* @param computeQueue Vulkan compute queue
* @param queueIndex Vulkan compute queue index in device
* @param nrOfTimestamps Maximum number of timestamps to allocate
*/
Sequence(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::Queue> computeQueue,
uint32_t queueIndex);
uint32_t queueIndex,
uint32_t nrOfTimestamps = 0);
/**
* Destructor for sequence which is responsible for cleaning all subsequent
* owned operations.
@ -1649,6 +1651,12 @@ class Sequence : public std::enable_shared_from_this<Sequence>
*/
void clear();
/**
* Return the timestamps that were latched at the beginning and
* after each operation during the last eval() call.
*/
std::vector<std::uint64_t> getTimestamps();
/**
* Begins recording commands for commands to be submitted into the command
* buffer.
@ -1706,6 +1714,7 @@ class Sequence : public std::enable_shared_from_this<Sequence>
// -------------- ALWAYS OWNED RESOURCES
vk::Fence mFence;
std::vector<std::shared_ptr<OpBase>> mOperations;
std::shared_ptr<vk::QueryPool> timestampQueryPool = nullptr;
// State
bool mRecording = false;
@ -1714,6 +1723,7 @@ class Sequence : public std::enable_shared_from_this<Sequence>
// Create functions
void createCommandPool();
void createCommandBuffer();
void createTimestampQueryPool(uint32_t);
};
} // End namespace kp
@ -1778,9 +1788,11 @@ class Manager
* @param sequenceName The name for the named sequence to be retrieved or
* created
* @param queueIndex The queue to use from the available queues
* @param nrOfTimestamps The maximum number of timestamps to allocate.
* If zero (default), disables latching of timestamps.
* @return Shared pointer to the manager owned sequence resource
*/
std::shared_ptr<Sequence> sequence(uint32_t queueIndex = 0);
std::shared_ptr<Sequence> sequence(uint32_t queueIndex = 0, uint32_t nrOfTimestamps = 0);
/**
* Function that simplifies the common workflow of tensor creation and

View file

@ -377,7 +377,7 @@ Manager::algorithm(const std::vector<std::shared_ptr<Tensor>>& tensors,
}
std::shared_ptr<Sequence>
Manager::sequence(uint32_t queueIndex)
Manager::sequence(uint32_t queueIndex, uint32_t nrOfTimestamps)
{
KP_LOG_DEBUG("Kompute Manager sequence() with queueIndex: {}", queueIndex);
@ -385,7 +385,8 @@ Manager::sequence(uint32_t queueIndex)
this->mPhysicalDevice,
this->mDevice,
this->mComputeQueues[queueIndex],
this->mComputeQueueFamilyIndices[queueIndex]) };
this->mComputeQueueFamilyIndices[queueIndex],
nrOfTimestamps) };
if (this->mManageResources) {
this->mManagedSequences.push_back(sq);

View file

@ -6,7 +6,8 @@ namespace kp {
Sequence::Sequence(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::Queue> computeQueue,
uint32_t queueIndex)
uint32_t queueIndex,
uint32_t nrOfTimestamps)
{
KP_LOG_DEBUG("Kompute Sequence Constructor with existing device & queue");
@ -17,6 +18,8 @@ Sequence::Sequence(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
this->createCommandPool();
this->createCommandBuffer();
if(nrOfTimestamps>0)
this->createTimestampQueryPool(nrOfTimestamps+1); //+1 for the first one
}
Sequence::~Sequence()
@ -44,6 +47,13 @@ Sequence::begin()
KP_LOG_INFO("Kompute Sequence command now started recording");
this->mCommandBuffer->begin(vk::CommandBufferBeginInfo());
this->mRecording = true;
//latch the first timestamp before any commands are submitted
if(this->timestampQueryPool)
this->mCommandBuffer->writeTimestamp(
vk::PipelineStageFlagBits::eAllCommands,
*this->timestampQueryPool, 0
);
}
void
@ -261,6 +271,12 @@ Sequence::record(std::shared_ptr<OpBase> op)
this->mOperations.push_back(op);
if(this->timestampQueryPool)
this->mCommandBuffer->writeTimestamp(
vk::PipelineStageFlagBits::eAllCommands,
*this->timestampQueryPool, this->mOperations.size()
);
return shared_from_this();
}
@ -308,4 +324,49 @@ Sequence::createCommandBuffer()
KP_LOG_DEBUG("Kompute Sequence Command Buffer Created");
}
void
Sequence::createTimestampQueryPool(uint32_t query_size)
{
KP_LOG_DEBUG("Kompute Sequence creating query pool");
if (!this->mDevice) {
throw std::runtime_error("Kompute Sequence device is null");
}
if (!this->mPhysicalDevice) {
throw std::runtime_error("Kompute Sequence physical device is null");
}
vk::PhysicalDeviceProperties physicalDeviceProperties =
this->mPhysicalDevice->getProperties();
if(physicalDeviceProperties.limits.timestampComputeAndGraphics){
vk::QueryPoolCreateInfo queryPoolInfo;
queryPoolInfo.setQueryCount(query_size);
queryPoolInfo.setQueryType(vk::QueryType::eTimestamp);
this->timestampQueryPool = std::make_shared<vk::QueryPool>(this->mDevice->createQueryPool(queryPoolInfo));
KP_LOG_DEBUG("Query pool for timestamps created");
}
else{
KP_LOG_DEBUG("Device does not support timestamps");
}
}
std::vector<std::uint64_t>
Sequence::getTimestamps(){
if(!this->timestampQueryPool)
throw std::runtime_error("Timestamp latching not enabled");
const auto n = this->mOperations.size()+1;
std::vector<std::uint64_t> timestamps(n, 0);
//XXX: the C++ method this->mDevice->getQueryPoolResults does not compile for me
const VkResult result =
vkGetQueryPoolResults(*this->mDevice, *this->timestampQueryPool,
0, n, timestamps.size()*sizeof(std::uint64_t), timestamps.data(),
sizeof(uint64_t), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
if(result!=VK_SUCCESS)
throw std::runtime_error("vkGetQueryPoolResults failed");
return timestamps;
}
}

View file

@ -64,9 +64,11 @@ class Manager
* @param sequenceName The name for the named sequence to be retrieved or
* created
* @param queueIndex The queue to use from the available queues
* @param nrOfTimestamps The maximum number of timestamps to allocate.
* If zero (default), disables latching of timestamps.
* @return Shared pointer to the manager owned sequence resource
*/
std::shared_ptr<Sequence> sequence(uint32_t queueIndex = 0);
std::shared_ptr<Sequence> sequence(uint32_t queueIndex = 0, uint32_t nrOfTimestamps = 0);
/**
* Function that simplifies the common workflow of tensor creation and

View file

@ -3,6 +3,7 @@
#include "kompute/Core.hpp"
#include "kompute/operations/OpBase.hpp"
#include "kompute/operations/OpAlgoDispatch.hpp"
namespace kp {
@ -20,11 +21,13 @@ class Sequence : public std::enable_shared_from_this<Sequence>
* @param device Vulkan logical device
* @param computeQueue Vulkan compute queue
* @param queueIndex Vulkan compute queue index in device
* @param nrOfTimestamps Maximum number of timestamps to allocate
*/
Sequence(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::Queue> computeQueue,
uint32_t queueIndex);
uint32_t queueIndex,
uint32_t nrOfTimestamps = 0);
/**
* Destructor for sequence which is responsible for cleaning all subsequent
* owned operations.
@ -142,6 +145,12 @@ class Sequence : public std::enable_shared_from_this<Sequence>
*/
void clear();
/**
* Return the timestamps that were latched at the beginning and
* after each operation during the last eval() call.
*/
std::vector<std::uint64_t> getTimestamps();
/**
* Begins recording commands for commands to be submitted into the command
* buffer.
@ -199,6 +208,7 @@ class Sequence : public std::enable_shared_from_this<Sequence>
// -------------- ALWAYS OWNED RESOURCES
vk::Fence mFence;
std::vector<std::shared_ptr<OpBase>> mOperations;
std::shared_ptr<vk::QueryPool> timestampQueryPool = nullptr;
// State
bool mRecording = false;
@ -207,6 +217,7 @@ class Sequence : public std::enable_shared_from_this<Sequence>
// Create functions
void createCommandPool();
void createCommandBuffer();
void createTimestampQueryPool(uint32_t);
};
} // End namespace kp