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
10 changes: 8 additions & 2 deletions backends/vulkan/runtime/api/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,14 @@ void Context::submit_cmd_to_gpu(VkFence fence_handle, const bool final_use) {
}
}

void Context::flush() {
void Context::wait_for_queue() {
VK_CHECK(vkQueueWaitIdle(queue().handle));
}

void Context::clear_resources() {
command_pool_.flush();
descriptor_pool_.flush();

// If there is an existing command buffer, invalidate it
if (cmd_) {
cmd_.invalidate();
}
Expand All @@ -243,6 +244,11 @@ void Context::flush() {
images_to_clear_.clear();
}

void Context::flush() {
wait_for_queue();
clear_resources();
}

bool available() {
return context();
}
Expand Down
4 changes: 4 additions & 0 deletions backends/vulkan/runtime/api/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ class Context final {
return cmd_;
}

void wait_for_queue();

void clear_resources();

void flush();

#if defined(VK_KHR_pipeline_executable_properties) && \
Expand Down
19 changes: 14 additions & 5 deletions backends/vulkan/runtime/graph/ComputeGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,22 @@ ComputeGraph::ComputeGraph(GraphConfig config)
}

ComputeGraph::~ComputeGraph() {
values_.clear();
// Wait for all currently executing commands to complete before cleaning up.
// If wait_for_queue() throws an exception, still proceed with cleanup.
try {
context_->wait_for_queue();
} catch (...) {
}

prepack_nodes_.clear();
execute_nodes_.clear();
clear_deferred_cmds();
// Wrap in try/catch to ensure that destructor does not throw
try {
values_.clear();

context_->flush();
prepack_nodes_.clear();
execute_nodes_.clear();
clear_deferred_cmds();
} catch (...) {
}
}

std::vector<int64_t> ComputeGraph::extract_int_or_symint_list(
Expand Down
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/vk_api/Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Adapter::Adapter(
const uint32_t num_queues,
const std::string& cache_data_path)
: queue_usage_mutex_{},
physical_device_(physical_device),
physical_device_(instance, physical_device),
queues_{},
queue_usage_{},
queue_mutexes_{},
Expand Down
219 changes: 169 additions & 50 deletions backends/vulkan/runtime/vk_api/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,40 @@
namespace vkcompute {
namespace vkapi {

PhysicalDevice::PhysicalDevice(VkPhysicalDevice physical_device_handle)
: handle(physical_device_handle),
PhysicalDevice::PhysicalDevice(
VkInstance instance_handle,
VkPhysicalDevice physical_device_handle)
: instance(instance_handle),
handle(physical_device_handle),
properties{},
memory_properties{},
#ifdef VK_KHR_16bit_storage
shader_16bit_storage{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES},
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES,
nullptr},
#endif /* VK_KHR_16bit_storage */
#ifdef VK_KHR_8bit_storage
shader_8bit_storage{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES},
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES,
nullptr},
#endif /* VK_KHR_8bit_storage */
#ifdef VK_KHR_shader_float16_int8
shader_float16_int8_types{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR},
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR,
nullptr},
#endif /* VK_KHR_shader_float16_int8 */
#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_features{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR},
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_FEATURES_KHR,
nullptr},
shader_int_dot_product_properties{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_PROPERTIES_KHR},
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_DOT_PRODUCT_PROPERTIES_KHR,
nullptr},
#endif
queue_families{},
num_compute_queues(0),
api_version_major(0),
api_version_minor(0),
supports_int16_shader_types(false),
supports_int64_shader_types(false),
supports_float64_shader_types(false),
Expand All @@ -57,62 +67,28 @@ PhysicalDevice::PhysicalDevice(VkPhysicalDevice physical_device_handle)
// Extract physical device properties
vkGetPhysicalDeviceProperties(handle, &properties);

api_version_major = VK_VERSION_MAJOR(properties.apiVersion);
api_version_minor = VK_VERSION_MINOR(properties.apiVersion);

// Extract fields of interest
has_timestamps = properties.limits.timestampComputeAndGraphics;
timestamp_period = properties.limits.timestampPeriod;
min_ubo_alignment = properties.limits.minUniformBufferOffsetAlignment;

vkGetPhysicalDeviceMemoryProperties(handle, &memory_properties);

VkPhysicalDeviceFeatures2 features2{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};

// Create linked list to query availability of extensions

void* extension_list_top = nullptr;

#ifdef VK_KHR_16bit_storage
shader_16bit_storage.pNext = extension_list_top;
extension_list_top = &shader_16bit_storage;
#endif /* VK_KHR_16bit_storage */

#ifdef VK_KHR_8bit_storage
shader_8bit_storage.pNext = extension_list_top;
extension_list_top = &shader_8bit_storage;
#endif /* VK_KHR_8bit_storage */

#ifdef VK_KHR_shader_float16_int8
shader_float16_int8_types.pNext = extension_list_top;
extension_list_top = &shader_float16_int8_types;
#endif /* VK_KHR_shader_float16_int8 */

#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_features.pNext = extension_list_top;
extension_list_top = &shader_int_dot_product_features;
shader_int_dot_product_properties.pNext = extension_list_top;
extension_list_top = &shader_int_dot_product_properties;
#endif /* VK_KHR_shader_integer_dot_product */

features2.pNext = extension_list_top;

vkGetPhysicalDeviceFeatures2(handle, &features2);

if (features2.features.shaderInt16 == VK_TRUE) {
supports_int16_shader_types = true;
}
if (features2.features.shaderInt64 == VK_TRUE) {
supports_int64_shader_types = true;
}
if (features2.features.shaderFloat64 == VK_TRUE) {
supports_float64_shader_types = true;
if (properties.apiVersion >= VK_API_VERSION_1_1) {
query_extensions_vk_1_1();
} else {
query_extensions_vk_1_0();
}

// Check if there are any memory types have both the HOST_VISIBLE and the
// DEVICE_LOCAL property flags
const VkMemoryPropertyFlags unified_memory_flags =
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
for (size_t i = 0; i < memory_properties.memoryTypeCount; ++i) {
if (memory_properties.memoryTypes[i].propertyFlags | unified_memory_flags) {
if (memory_properties.memoryTypes[i].propertyFlags & unified_memory_flags) {
has_unified_memory = true;
break;
}
Expand Down Expand Up @@ -153,6 +129,149 @@ PhysicalDevice::PhysicalDevice(VkPhysicalDevice physical_device_handle)
}
}

void PhysicalDevice::query_extensions_vk_1_0() {
VkPhysicalDeviceFeatures features{};
vkGetPhysicalDeviceFeatures(handle, &features);

if (features.shaderInt16 == VK_TRUE) {
supports_int16_shader_types = true;
}
if (features.shaderInt64 == VK_TRUE) {
supports_int64_shader_types = true;
}
if (features.shaderFloat64 == VK_TRUE) {
supports_float64_shader_types = true;
}

// Try to query extension features using
// VK_KHR_get_physical_device_properties2
auto vkGetPhysicalDeviceFeatures2KHR =
(PFN_vkGetPhysicalDeviceFeatures2KHR)vkGetInstanceProcAddr(
instance, "vkGetPhysicalDeviceFeatures2KHR");

if (vkGetPhysicalDeviceFeatures2KHR == nullptr) {
// Manually set extension features to false if the function is not available
#ifdef VK_KHR_16bit_storage
shader_16bit_storage.storageBuffer16BitAccess = VK_FALSE;
shader_16bit_storage.uniformAndStorageBuffer16BitAccess = VK_FALSE;
shader_16bit_storage.storagePushConstant16 = VK_FALSE;
shader_16bit_storage.storageInputOutput16 = VK_FALSE;
#endif /* VK_KHR_16bit_storage */
#ifdef VK_KHR_8bit_storage
shader_8bit_storage.storageBuffer8BitAccess = VK_FALSE;
shader_8bit_storage.uniformAndStorageBuffer8BitAccess = VK_FALSE;
shader_8bit_storage.storagePushConstant8 = VK_FALSE;
#endif /* VK_KHR_8bit_storage */
#ifdef VK_KHR_shader_float16_int8
shader_float16_int8_types.shaderFloat16 = VK_FALSE;
shader_float16_int8_types.shaderInt8 = VK_FALSE;
#endif /* VK_KHR_shader_float16_int8 */
#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_features.shaderIntegerDotProduct = VK_FALSE;
#endif /* VK_KHR_shader_integer_dot_product */
return;
}

VkPhysicalDeviceFeatures2KHR features2{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR};

void* extension_list_top = nullptr;

#ifdef VK_KHR_16bit_storage
shader_16bit_storage.pNext = extension_list_top;
extension_list_top = &shader_16bit_storage;
#endif /* VK_KHR_16bit_storage */

#ifdef VK_KHR_8bit_storage
shader_8bit_storage.pNext = extension_list_top;
extension_list_top = &shader_8bit_storage;
#endif /* VK_KHR_8bit_storage */

#ifdef VK_KHR_shader_float16_int8
shader_float16_int8_types.pNext = extension_list_top;
extension_list_top = &shader_float16_int8_types;
#endif /* VK_KHR_shader_float16_int8 */

#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_features.pNext = extension_list_top;
extension_list_top = &shader_int_dot_product_features;
#endif /* VK_KHR_shader_integer_dot_product */

features2.pNext = extension_list_top;

vkGetPhysicalDeviceFeatures2KHR(handle, &features2);

// Query properties separately from features
auto vkGetPhysicalDeviceProperties2KHR =
(PFN_vkGetPhysicalDeviceProperties2KHR)vkGetInstanceProcAddr(
instance, "vkGetPhysicalDeviceProperties2KHR");

if (vkGetPhysicalDeviceProperties2KHR != nullptr) {
VkPhysicalDeviceProperties2KHR properties2{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR};

#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_properties.pNext = nullptr;
properties2.pNext = &shader_int_dot_product_properties;
#endif /* VK_KHR_shader_integer_dot_product */

vkGetPhysicalDeviceProperties2KHR(handle, &properties2);
}
}

void PhysicalDevice::query_extensions_vk_1_1() {
VkPhysicalDeviceFeatures2 features2{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};

// Create linked list to query availability of extensions
void* extension_list_top = nullptr;

#ifdef VK_KHR_16bit_storage
shader_16bit_storage.pNext = extension_list_top;
extension_list_top = &shader_16bit_storage;
#endif /* VK_KHR_16bit_storage */

#ifdef VK_KHR_8bit_storage
shader_8bit_storage.pNext = extension_list_top;
extension_list_top = &shader_8bit_storage;
#endif /* VK_KHR_8bit_storage */

#ifdef VK_KHR_shader_float16_int8
shader_float16_int8_types.pNext = extension_list_top;
extension_list_top = &shader_float16_int8_types;
#endif /* VK_KHR_shader_float16_int8 */

#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_features.pNext = extension_list_top;
extension_list_top = &shader_int_dot_product_features;
#endif /* VK_KHR_shader_integer_dot_product */

features2.pNext = extension_list_top;

vkGetPhysicalDeviceFeatures2(handle, &features2);

if (features2.features.shaderInt16 == VK_TRUE) {
supports_int16_shader_types = true;
}
if (features2.features.shaderInt64 == VK_TRUE) {
supports_int64_shader_types = true;
}
if (features2.features.shaderFloat64 == VK_TRUE) {
supports_float64_shader_types = true;
}

// Query properties separately from features
VkPhysicalDeviceProperties2 properties2{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2};

#ifdef VK_KHR_shader_integer_dot_product
shader_int_dot_product_properties.pNext = nullptr;
properties2.pNext = &shader_int_dot_product_properties;
#endif /* VK_KHR_shader_integer_dot_product */

vkGetPhysicalDeviceProperties2(handle, &properties2);
}

//
// DeviceHandle
//
Expand Down
11 changes: 9 additions & 2 deletions backends/vulkan/runtime/vk_api/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ enum class DeviceType : uint32_t {
};

struct PhysicalDevice final {
// Handle
// Handles
VkInstance instance;
VkPhysicalDevice handle;

// Properties obtained from Vulkan
Expand Down Expand Up @@ -56,6 +57,8 @@ struct PhysicalDevice final {

// Metadata
uint32_t num_compute_queues;
uint32_t api_version_major;
uint32_t api_version_minor;
bool supports_int16_shader_types;
bool supports_int64_shader_types;
bool supports_float64_shader_types;
Expand All @@ -68,7 +71,11 @@ struct PhysicalDevice final {
std::string device_name;
DeviceType device_type;

explicit PhysicalDevice(VkPhysicalDevice);
explicit PhysicalDevice(VkInstance instance, VkPhysicalDevice);

private:
void query_extensions_vk_1_0();
void query_extensions_vk_1_1();
};

struct DeviceHandle final {
Expand Down
Loading
Loading