yum-mirror/VkFFT
git clone https://git.yummers.dev/yum-mirror/VkFFT
b5de819
v1.2.26
1//general parts 2#include <stdio.h> 3#include <vector> 4#include <memory> 5#include <string.h> 6#include <chrono> 7#include <thread> 8#include <iostream> 9#ifndef __STDC_FORMAT_MACROS 10#define __STDC_FORMAT_MACROS 11#endif 12#include <inttypes.h> 13 14#if ( VKFFT_BACKEND == 0 ) 15#include "vulkan/vulkan.h" 16#include "glslang_c_interface.h" 17#elif ( VKFFT_BACKEND == 1 ) 18#include < cuda . h > 19#include <cuda_runtime.h> 20#include <nvrtc.h> 21#include <cuda_runtime_api.h> 22#include <cuComplex.h> 23#elif ( VKFFT_BACKEND == 2 ) 24#ifndef __HIP_PLATFORM_HCC__ 25#define __HIP_PLATFORM_HCC__ 26#endif 27#include <hip/hip_runtime.h> 28#include <hip/hiprtc.h> 29#include <hip/hip_runtime_api.h> 30#include <hip/hip_complex.h> 31#elif ( VKFFT_BACKEND == 3 ) 32#ifndef CL_USE_DEPRECATED_OPENCL_1_2_APIS 33#define CL_USE_DEPRECATED_OPENCL_1_2_APIS 34#endif 35#ifdef __APPLE__ 36#include <OpenCL/opencl.h> 37#else 38#include <CL/cl.h> 39#endif 40#elif ( VKFFT_BACKEND == 4 ) 41#include < ze_api . h > 42#endif 43#include "vkFFT.h" 44#include "utils_VkFFT.h" 45#if ( VKFFT_BACKEND == 0 ) 46 47VkResult CreateDebugUtilsMessengerEXT ( VkGPU * vkGPU , const VkDebugUtilsMessengerCreateInfoEXT * pCreateInfo , const VkAllocationCallbacks * pAllocator , VkDebugUtilsMessengerEXT * pDebugMessenger ) { 48//pointer to the function, as it is not part of the core. Function creates debugging messenger 49PFN_vkCreateDebugUtilsMessengerEXT func = ( PFN_vkCreateDebugUtilsMessengerEXT ) vkGetInstanceProcAddr ( vkGPU -> instance , "vkCreateDebugUtilsMessengerEXT" ) ; 50if ( func != NULL ) { 51return func ( vkGPU -> instance , pCreateInfo , pAllocator , pDebugMessenger ) ; 52} 53else { 54return VK_ERROR_EXTENSION_NOT_PRESENT ; 55} 56} 57void DestroyDebugUtilsMessengerEXT ( VkGPU * vkGPU , const VkAllocationCallbacks * pAllocator ) { 58//pointer to the function, as it is not part of the core. Function destroys debugging messenger 59PFN_vkDestroyDebugUtilsMessengerEXT func = ( PFN_vkDestroyDebugUtilsMessengerEXT ) vkGetInstanceProcAddr ( vkGPU -> instance , "vkDestroyDebugUtilsMessengerEXT" ) ; 60if ( func != NULL ) { 61func ( vkGPU -> instance , vkGPU -> debugMessenger , pAllocator ) ; 62} 63} 64static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback ( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity , VkDebugUtilsMessageTypeFlagsEXT messageType , const VkDebugUtilsMessengerCallbackDataEXT * pCallbackData , void * pUserData ) { 65printf ( "validation layer: %s \n " , pCallbackData -> pMessage ) ; 66return VK_FALSE ; 67} 68 69 70VkResult setupDebugMessenger ( VkGPU * vkGPU ) { 71//function that sets up the debugging messenger 72if ( vkGPU -> enableValidationLayers == 0 ) return VK_SUCCESS ; 73 74VkDebugUtilsMessengerCreateInfoEXT createInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT } ; 75createInfo . messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT ; 76createInfo . messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT ; 77createInfo . pfnUserCallback = debugCallback ; 78 79if ( CreateDebugUtilsMessengerEXT ( vkGPU , & createInfo , NULL , & vkGPU -> debugMessenger ) != VK_SUCCESS ) { 80return VK_ERROR_INITIALIZATION_FAILED ; 81} 82return VK_SUCCESS ; 83} 84VkResult checkValidationLayerSupport () { 85//check if validation layers are supported when an instance is created 86uint32_t layerCount ; 87vkEnumerateInstanceLayerProperties ( & layerCount , NULL ) ; 88 89VkLayerProperties * availableLayers = ( VkLayerProperties * ) malloc ( sizeof ( VkLayerProperties ) * layerCount ) ; 90if ( ! availableLayers ) return VK_INCOMPLETE ; 91vkEnumerateInstanceLayerProperties ( & layerCount , availableLayers ) ; 92if ( availableLayers ) { 93for ( uint64_t i = 0 ; i < layerCount ; i ++ ) { 94if ( strcmp ( "VK_LAYER_KHRONOS_validation" , availableLayers [ i ] . layerName ) == 0 ) { 95free ( availableLayers ) ; 96return VK_SUCCESS ; 97} 98} 99free ( availableLayers ) ; 100} 101else { 102return VK_INCOMPLETE ; 103} 104return VK_ERROR_LAYER_NOT_PRESENT ; 105} 106 107std :: vector < const char *> getRequiredExtensions ( VkGPU * vkGPU , uint64_t sample_id ) { 108std :: vector < const char *> extensions ; 109 110if ( vkGPU -> enableValidationLayers ) { 111extensions . push_back ( VK_EXT_DEBUG_UTILS_EXTENSION_NAME ) ; 112} 113switch ( sample_id ) { 114#if ( VK_API_VERSION > 10 ) 115case 2 : case 102 : 116extensions . push_back ( "VK_KHR_get_physical_device_properties2" ) ; 117break ; 118#endif 119default : 120break ; 121} 122 123 124return extensions ; 125} 126 127VkResult createInstance ( VkGPU * vkGPU , uint64_t sample_id ) { 128//create instance - a connection between the application and the Vulkan library 129VkResult res = VK_SUCCESS ; 130//check if validation layers are supported 131if ( vkGPU -> enableValidationLayers == 1 ) { 132res = checkValidationLayerSupport () ; 133if ( res != VK_SUCCESS ) return res ; 134} 135 136VkApplicationInfo applicationInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO } ; 137applicationInfo . pApplicationName = "VkFFT" ; 138applicationInfo . applicationVersion = ( uint32_t ) VkFFTGetVersion () ; 139applicationInfo . pEngineName = "VkFFT" ; 140applicationInfo . engineVersion = 1 ; 141#if ( VK_API_VERSION >= 12 ) 142applicationInfo . apiVersion = VK_API_VERSION_1_2 ; 143#elif ( VK_API_VERSION == 11 ) 144applicationInfo . apiVersion = VK_API_VERSION_1_1 ; 145#else 146applicationInfo . apiVersion = VK_API_VERSION_1_0 ; 147#endif 148 149VkInstanceCreateInfo createInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO } ; 150createInfo . flags = 0 ; 151createInfo . pApplicationInfo = & applicationInfo ; 152 153auto extensions = getRequiredExtensions ( vkGPU , sample_id ) ; 154createInfo . enabledExtensionCount = ( uint32_t )( extensions . size ()) ; 155createInfo . ppEnabledExtensionNames = extensions . data () ; 156 157VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT } ; 158if ( vkGPU -> enableValidationLayers ) { 159//query for the validation layer support in the instance 160createInfo . enabledLayerCount = 1 ; 161const char * validationLayers = "VK_LAYER_KHRONOS_validation" ; 162createInfo . ppEnabledLayerNames = & validationLayers ; 163debugCreateInfo . messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT ; 164debugCreateInfo . messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT ; 165debugCreateInfo . pfnUserCallback = debugCallback ; 166createInfo . pNext = ( VkDebugUtilsMessengerCreateInfoEXT * ) & debugCreateInfo ; 167} 168else { 169createInfo . enabledLayerCount = 0 ; 170 171createInfo . pNext = nullptr ; 172} 173 174res = vkCreateInstance ( & createInfo , NULL , & vkGPU -> instance ) ; 175if ( res != VK_SUCCESS ) return res ; 176 177return res ; 178} 179 180VkResult findPhysicalDevice ( VkGPU * vkGPU ) { 181//check if there are GPUs that support Vulkan and select one 182VkResult res = VK_SUCCESS ; 183uint32_t deviceCount ; 184res = vkEnumeratePhysicalDevices ( vkGPU -> instance , & deviceCount , NULL ) ; 185if ( res != VK_SUCCESS ) return res ; 186if ( deviceCount == 0 ) { 187return VK_ERROR_DEVICE_LOST ; 188} 189 190VkPhysicalDevice * devices = ( VkPhysicalDevice * ) malloc ( sizeof ( VkPhysicalDevice ) * deviceCount ) ; 191if ( ! devices ) return VK_INCOMPLETE ; 192res = vkEnumeratePhysicalDevices ( vkGPU -> instance , & deviceCount , devices ) ; 193if ( res != VK_SUCCESS ) return res ; 194if ( devices ) { 195vkGPU -> physicalDevice = devices [ vkGPU -> device_id ] ; 196free ( devices ) ; 197return VK_SUCCESS ; 198} 199else 200return VK_INCOMPLETE ; 201} 202VkResult getComputeQueueFamilyIndex ( VkGPU * vkGPU ) { 203//find a queue family for a selected GPU, select the first available for use 204uint32_t queueFamilyCount ; 205vkGetPhysicalDeviceQueueFamilyProperties ( vkGPU -> physicalDevice , & queueFamilyCount , NULL ) ; 206 207VkQueueFamilyProperties * queueFamilies = ( VkQueueFamilyProperties * ) malloc ( sizeof ( VkQueueFamilyProperties ) * queueFamilyCount ) ; 208if ( ! queueFamilies ) return VK_INCOMPLETE ; 209if ( queueFamilies ) { 210vkGetPhysicalDeviceQueueFamilyProperties ( vkGPU -> physicalDevice , & queueFamilyCount , queueFamilies ) ; 211uint64_t i = 0 ; 212for ( ; i < queueFamilyCount ; i ++ ) { 213VkQueueFamilyProperties props = queueFamilies [ i ] ; 214 215if ( props . queueCount > 0 && ( props . queueFlags & VK_QUEUE_COMPUTE_BIT )) { 216break ; 217} 218} 219free ( queueFamilies ) ; 220if ( i == queueFamilyCount ) { 221return VK_ERROR_INITIALIZATION_FAILED ; 222} 223vkGPU -> queueFamilyIndex = i ; 224return VK_SUCCESS ; 225} 226else 227return VK_INCOMPLETE ; 228} 229 230VkResult createDevice ( VkGPU * vkGPU , uint64_t sample_id ) { 231//create logical device representation 232VkResult res = VK_SUCCESS ; 233VkDeviceQueueCreateInfo queueCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO } ; 234res = getComputeQueueFamilyIndex ( vkGPU ) ; 235if ( res != VK_SUCCESS ) return res ; 236queueCreateInfo . queueFamilyIndex = ( uint32_t ) vkGPU -> queueFamilyIndex ; 237queueCreateInfo . queueCount = 1 ; 238float queuePriorities = 1.0 ; 239queueCreateInfo . pQueuePriorities = & queuePriorities ; 240VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO } ; 241VkPhysicalDeviceFeatures deviceFeatures = {} ; 242switch ( sample_id ) { 243case 1 : case 12 : case 17 : case 18 : case 101 : case 201 : case 1001 : { 244deviceFeatures . shaderFloat64 = true ; 245deviceCreateInfo . enabledExtensionCount = ( uint32_t ) vkGPU -> enabledDeviceExtensions . size () ; 246deviceCreateInfo . ppEnabledExtensionNames = vkGPU -> enabledDeviceExtensions . data () ; 247deviceCreateInfo . pQueueCreateInfos = & queueCreateInfo ; 248deviceCreateInfo . queueCreateInfoCount = 1 ; 249deviceCreateInfo . pEnabledFeatures = & deviceFeatures ; 250res = vkCreateDevice ( vkGPU -> physicalDevice , & deviceCreateInfo , NULL , & vkGPU -> device ) ; 251if ( res != VK_SUCCESS ) return res ; 252vkGetDeviceQueue ( vkGPU -> device , ( uint32_t ) vkGPU -> queueFamilyIndex , 0 , & vkGPU -> queue ) ; 253break ; 254} 255#if ( VK_API_VERSION > 10 ) 256case 2 : case 102 : { 257VkPhysicalDeviceFeatures2 deviceFeatures2 = {} ; 258VkPhysicalDevice16BitStorageFeatures shaderFloat16 = {} ; 259shaderFloat16 . sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES ; 260shaderFloat16 . storageBuffer16BitAccess = true ; 261/*VkPhysicalDeviceShaderFloat16Int8Features shaderFloat16 = {}; 262shaderFloat16.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES; 263shaderFloat16.shaderFloat16 = true; 264shaderFloat16.shaderInt8 = true;*/ 265deviceFeatures2 . sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 ; 266deviceFeatures2 . pNext = & shaderFloat16 ; 267deviceFeatures2 . features = deviceFeatures ; 268vkGetPhysicalDeviceFeatures2 ( vkGPU -> physicalDevice , & deviceFeatures2 ) ; 269deviceCreateInfo . pNext = & deviceFeatures2 ; 270vkGPU -> enabledDeviceExtensions . push_back ( "VK_KHR_16bit_storage" ) ; 271deviceCreateInfo . enabledExtensionCount = ( uint32_t ) vkGPU -> enabledDeviceExtensions . size () ; 272deviceCreateInfo . ppEnabledExtensionNames = vkGPU -> enabledDeviceExtensions . data () ; 273deviceCreateInfo . pQueueCreateInfos = & queueCreateInfo ; 274deviceCreateInfo . queueCreateInfoCount = 1 ; 275deviceCreateInfo . pEnabledFeatures = NULL ; 276res = vkCreateDevice ( vkGPU -> physicalDevice , & deviceCreateInfo , NULL , & vkGPU -> device ) ; 277if ( res != VK_SUCCESS ) return res ; 278vkGetDeviceQueue ( vkGPU -> device , ( uint32_t ) vkGPU -> queueFamilyIndex , 0 , & vkGPU -> queue ) ; 279break ; 280} 281#endif 282default : { 283deviceCreateInfo . enabledExtensionCount = ( uint32_t ) vkGPU -> enabledDeviceExtensions . size () ; 284deviceCreateInfo . ppEnabledExtensionNames = vkGPU -> enabledDeviceExtensions . data () ; 285deviceCreateInfo . pQueueCreateInfos = & queueCreateInfo ; 286deviceCreateInfo . queueCreateInfoCount = 1 ; 287deviceCreateInfo . pEnabledFeatures = NULL ; 288deviceCreateInfo . pEnabledFeatures = & deviceFeatures ; 289res = vkCreateDevice ( vkGPU -> physicalDevice , & deviceCreateInfo , NULL , & vkGPU -> device ) ; 290if ( res != VK_SUCCESS ) return res ; 291vkGetDeviceQueue ( vkGPU -> device , ( uint32_t ) vkGPU -> queueFamilyIndex , 0 , & vkGPU -> queue ) ; 292break ; 293} 294} 295return res ; 296} 297VkResult createFence ( VkGPU * vkGPU ) { 298//create fence for synchronization 299VkResult res = VK_SUCCESS ; 300VkFenceCreateInfo fenceCreateInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO } ; 301fenceCreateInfo . flags = 0 ; 302res = vkCreateFence ( vkGPU -> device , & fenceCreateInfo , NULL , & vkGPU -> fence ) ; 303return res ; 304} 305VkResult createCommandPool ( VkGPU * vkGPU ) { 306//create a place, command buffer memory is allocated from 307VkResult res = VK_SUCCESS ; 308VkCommandPoolCreateInfo commandPoolCreateInfo = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO } ; 309commandPoolCreateInfo . flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT ; 310commandPoolCreateInfo . queueFamilyIndex = ( uint32_t ) vkGPU -> queueFamilyIndex ; 311res = vkCreateCommandPool ( vkGPU -> device , & commandPoolCreateInfo , NULL , & vkGPU -> commandPool ) ; 312return res ; 313} 314 315VkFFTResult findMemoryType ( VkGPU * vkGPU , uint64_t memoryTypeBits , uint64_t memorySize , VkMemoryPropertyFlags properties , uint32_t * memoryTypeIndex ) { 316VkPhysicalDeviceMemoryProperties memoryProperties = { 0 } ; 317 318vkGetPhysicalDeviceMemoryProperties ( vkGPU -> physicalDevice , & memoryProperties ) ; 319 320for ( uint64_t i = 0 ; i < memoryProperties . memoryTypeCount ; ++ i ) { 321if (( memoryTypeBits & (( uint64_t ) 1 << i )) && (( memoryProperties . memoryTypes [ i ] . propertyFlags & properties ) == properties ) && ( memoryProperties . memoryHeaps [ memoryProperties . memoryTypes [ i ] . heapIndex ] . size >= memorySize )) 322{ 323memoryTypeIndex [ 0 ] = ( uint32_t ) i ; 324return VKFFT_SUCCESS ; 325} 326} 327return VKFFT_ERROR_FAILED_TO_FIND_MEMORY ; 328} 329 330VkFFTResult allocateBuffer ( VkGPU * vkGPU , VkBuffer * buffer , VkDeviceMemory * deviceMemory , VkBufferUsageFlags usageFlags , VkMemoryPropertyFlags propertyFlags , uint64_t size ) { 331//allocate the buffer used by the GPU with specified properties 332VkFFTResult resFFT = VKFFT_SUCCESS ; 333VkResult res = VK_SUCCESS ; 334uint32_t queueFamilyIndices ; 335VkBufferCreateInfo bufferCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO } ; 336bufferCreateInfo . sharingMode = VK_SHARING_MODE_EXCLUSIVE ; 337bufferCreateInfo . queueFamilyIndexCount = 1 ; 338bufferCreateInfo . pQueueFamilyIndices = & queueFamilyIndices ; 339bufferCreateInfo . size = size ; 340bufferCreateInfo . usage = usageFlags ; 341res = vkCreateBuffer ( vkGPU -> device , & bufferCreateInfo , NULL , buffer ) ; 342if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_BUFFER ; 343VkMemoryRequirements memoryRequirements = { 0 } ; 344vkGetBufferMemoryRequirements ( vkGPU -> device , buffer [ 0 ] , & memoryRequirements ) ; 345VkMemoryAllocateInfo memoryAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO } ; 346memoryAllocateInfo . allocationSize = memoryRequirements . size ; 347resFFT = findMemoryType ( vkGPU , memoryRequirements . memoryTypeBits , memoryRequirements . size , propertyFlags , & memoryAllocateInfo . memoryTypeIndex ) ; 348if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 349res = vkAllocateMemory ( vkGPU -> device , & memoryAllocateInfo , NULL , deviceMemory ) ; 350if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_MEMORY ; 351res = vkBindBufferMemory ( vkGPU -> device , buffer [ 0 ] , deviceMemory [ 0 ] , 0 ) ; 352if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_BIND_BUFFER_MEMORY ; 353return resFFT ; 354} 355VkFFTResult transferDataFromCPU ( VkGPU * vkGPU , void * arr , VkBuffer * buffer , uint64_t bufferSize ) { 356//a function that transfers data from the CPU to the GPU using staging buffer, because the GPU memory is not host-coherent 357VkFFTResult resFFT = VKFFT_SUCCESS ; 358VkResult res = VK_SUCCESS ; 359uint64_t stagingBufferSize = bufferSize ; 360VkBuffer stagingBuffer = { 0 } ; 361VkDeviceMemory stagingBufferMemory = { 0 } ; 362resFFT = allocateBuffer ( vkGPU , & stagingBuffer , & stagingBufferMemory , VK_BUFFER_USAGE_TRANSFER_SRC_BIT , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT , stagingBufferSize ) ; 363if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 364void * data ; 365res = vkMapMemory ( vkGPU -> device , stagingBufferMemory , 0 , stagingBufferSize , 0 , & data ) ; 366if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 367memcpy ( data , arr , stagingBufferSize ) ; 368vkUnmapMemory ( vkGPU -> device , stagingBufferMemory ) ; 369VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 370commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 371commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 372commandBufferAllocateInfo . commandBufferCount = 1 ; 373VkCommandBuffer commandBuffer = { 0 } ; 374res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 375if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 376VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 377commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 378res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 379if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 380VkBufferCopy copyRegion = { 0 } ; 381copyRegion . srcOffset = 0 ; 382copyRegion . dstOffset = 0 ; 383copyRegion . size = stagingBufferSize ; 384vkCmdCopyBuffer ( commandBuffer , stagingBuffer , buffer [ 0 ] , 1 , & copyRegion ) ; 385res = vkEndCommandBuffer ( commandBuffer ) ; 386if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 387VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 388submitInfo . commandBufferCount = 1 ; 389submitInfo . pCommandBuffers = & commandBuffer ; 390res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 391if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 392res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 393if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 394res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 395if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 396vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 397vkDestroyBuffer ( vkGPU -> device , stagingBuffer , NULL ) ; 398vkFreeMemory ( vkGPU -> device , stagingBufferMemory , NULL ) ; 399return resFFT ; 400} 401VkFFTResult transferDataToCPU ( VkGPU * vkGPU , void * arr , VkBuffer * buffer , uint64_t bufferSize ) { 402//a function that transfers data from the GPU to the CPU using staging buffer, because the GPU memory is not host-coherent 403VkFFTResult resFFT = VKFFT_SUCCESS ; 404VkResult res = VK_SUCCESS ; 405uint64_t stagingBufferSize = bufferSize ; 406VkBuffer stagingBuffer = { 0 } ; 407VkDeviceMemory stagingBufferMemory = { 0 } ; 408resFFT = allocateBuffer ( vkGPU , & stagingBuffer , & stagingBufferMemory , VK_BUFFER_USAGE_TRANSFER_DST_BIT , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT , stagingBufferSize ) ; 409if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 410VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 411commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 412commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 413commandBufferAllocateInfo . commandBufferCount = 1 ; 414VkCommandBuffer commandBuffer = { 0 } ; 415res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 416if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 417VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 418commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 419res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 420if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 421VkBufferCopy copyRegion = { 0 } ; 422copyRegion . srcOffset = 0 ; 423copyRegion . dstOffset = 0 ; 424copyRegion . size = stagingBufferSize ; 425vkCmdCopyBuffer ( commandBuffer , buffer [ 0 ] , stagingBuffer , 1 , & copyRegion ) ; 426res = vkEndCommandBuffer ( commandBuffer ) ; 427if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 428VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 429submitInfo . commandBufferCount = 1 ; 430submitInfo . pCommandBuffers = & commandBuffer ; 431res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 432if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 433res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 434if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 435res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 436if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 437vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 438void * data ; 439res = vkMapMemory ( vkGPU -> device , stagingBufferMemory , 0 , stagingBufferSize , 0 , & data ) ; 440if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 441memcpy ( arr , data , stagingBufferSize ) ; 442vkUnmapMemory ( vkGPU -> device , stagingBufferMemory ) ; 443vkDestroyBuffer ( vkGPU -> device , stagingBuffer , NULL ) ; 444vkFreeMemory ( vkGPU -> device , stagingBufferMemory , NULL ) ; 445return resFFT ; 446} 447#endif 448VkFFTResult devices_list () { 449//this function creates an instance and prints the list of available devices 450#if ( VKFFT_BACKEND == 0 ) 451VkResult res = VK_SUCCESS ; 452VkInstance local_instance = { 0 } ; 453VkInstanceCreateInfo createInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO } ; 454createInfo . flags = 0 ; 455createInfo . pApplicationInfo = NULL ; 456VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT } ; 457createInfo . enabledLayerCount = 0 ; 458createInfo . enabledExtensionCount = 0 ; 459createInfo . pNext = NULL ; 460res = vkCreateInstance ( & createInfo , NULL , & local_instance ) ; 461if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_INSTANCE ; 462 463uint32_t deviceCount ; 464res = vkEnumeratePhysicalDevices ( local_instance , & deviceCount , NULL ) ; 465if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 466 467VkPhysicalDevice * devices = ( VkPhysicalDevice * ) malloc ( sizeof ( VkPhysicalDevice ) * deviceCount ) ; 468if ( ! devices ) return VKFFT_ERROR_MALLOC_FAILED ; 469if ( devices ) { 470res = vkEnumeratePhysicalDevices ( local_instance , & deviceCount , devices ) ; 471if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 472for ( uint64_t i = 0 ; i < deviceCount ; i ++ ) { 473VkPhysicalDeviceProperties device_properties ; 474vkGetPhysicalDeviceProperties ( devices [ i ] , & device_properties ) ; 475printf ( "Device id: %" PRIu64 " name: %s API:%d.%d.%d \n " , i , device_properties . deviceName , ( device_properties . apiVersion >> 22 ) , (( device_properties . apiVersion >> 12 ) & 0x3ff ) , ( device_properties . apiVersion & 0xfff )) ; 476} 477free ( devices ) ; 478} 479else 480return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 481vkDestroyInstance ( local_instance , NULL ) ; 482#elif ( VKFFT_BACKEND == 1 ) 483CUresult res = CUDA_SUCCESS ; 484res = cuInit ( 0 ) ; 485if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 486int numDevices ; 487res = cuDeviceGetCount ( & numDevices ) ; 488if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID ; 489for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 490char deviceName [ 256 ] ; 491CUdevice device = {} ; 492res = cuDeviceGet ( & device , ( int ) i ) ; 493if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 494res = cuDeviceGetName ( deviceName , 256 , device ) ; 495if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 496printf ( "Device id: %" PRIu64 " name: %s \n " , i , deviceName ) ; 497} 498#elif ( VKFFT_BACKEND == 2 ) 499hipError_t res = hipSuccess ; 500res = hipInit ( 0 ) ; 501if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 502int numDevices ; 503res = hipGetDeviceCount ( & numDevices ) ; 504if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID ; 505for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 506char deviceName [ 256 ] ; 507hipDevice_t device = {} ; 508res = hipDeviceGet ( & device , i ) ; 509if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 510res = hipDeviceGetName ( deviceName , 256 , device ) ; 511if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 512printf ( "Device id: %" PRIu64 " name: %s \n " , i , deviceName ) ; 513} 514#elif ( VKFFT_BACKEND == 3 ) 515cl_int res = CL_SUCCESS ; 516cl_uint numPlatforms ; 517res = clGetPlatformIDs ( 0 , 0 , & numPlatforms ) ; 518if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 519cl_platform_id * platforms = ( cl_platform_id * ) malloc ( sizeof ( cl_platform_id ) * numPlatforms ) ; 520if ( ! platforms ) return VKFFT_ERROR_MALLOC_FAILED ; 521res = clGetPlatformIDs ( numPlatforms , platforms , 0 ) ; 522if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 523uint64_t k = 0 ; 524for ( uint64_t j = 0 ; j < numPlatforms ; j ++ ) { 525cl_uint numDevices ; 526res = clGetDeviceIDs ( platforms [ j ] , CL_DEVICE_TYPE_ALL , 0 , 0 , & numDevices ) ; 527cl_device_id * deviceList = ( cl_device_id * ) malloc ( sizeof ( cl_device_id ) * numDevices ) ; 528if ( ! deviceList ) return VKFFT_ERROR_MALLOC_FAILED ; 529res = clGetDeviceIDs ( platforms [ j ] , CL_DEVICE_TYPE_ALL , numDevices , deviceList , 0 ) ; 530if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 531for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 532char deviceName [ 256 ] ; 533char apiVersion [ 256 ] ; 534res = clGetDeviceInfo ( deviceList [ i ] , CL_DEVICE_NAME , 256 * sizeof ( char ) , deviceName , 0 ) ; 535if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 536res = clGetDeviceInfo ( deviceList [ i ] , CL_DEVICE_VERSION , 256 * sizeof ( char ) , apiVersion , 0 ) ; 537if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 538printf ( "Platform id: %" PRIu64 " Device id: %" PRIu64 " name: %s API:%s \n " , j , k , deviceName , apiVersion ) ; 539k ++ ; 540} 541free ( deviceList ) ; 542} 543free ( platforms ) ; 544#elif ( VKFFT_BACKEND == 4 ) 545ze_result_t res = ZE_RESULT_SUCCESS ; 546res = zeInit ( 0 ) ; 547if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 548uint32_t numDrivers = 0 ; 549res = zeDriverGet ( & numDrivers , 0 ) ; 550if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 551ze_driver_handle_t * drivers = ( ze_driver_handle_t * ) malloc ( numDrivers * sizeof ( ze_driver_handle_t )) ; 552if ( ! drivers ) return VKFFT_ERROR_MALLOC_FAILED ; 553res = zeDriverGet ( & numDrivers , drivers ) ; 554if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 555uint64_t k = 0 ; 556for ( uint64_t j = 0 ; j < numDrivers ; j ++ ) { 557uint32_t numDevices = 0 ; 558res = zeDeviceGet ( drivers [ j ] , & numDevices , nullptr ) ; 559if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 560ze_device_handle_t * deviceList = ( ze_device_handle_t * ) malloc ( numDevices * sizeof ( ze_device_handle_t )) ; 561if ( ! deviceList ) return VKFFT_ERROR_MALLOC_FAILED ; 562res = zeDeviceGet ( drivers [ j ] , & numDevices , deviceList ) ; 563if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 564for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 565ze_device_properties_t device_properties ; 566res = zeDeviceGetProperties ( deviceList [ i ] , & device_properties ) ; 567if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 568printf ( "Driver id: %" PRIu64 " Device id: %" PRIu64 " name: %s \n " , j , k , device_properties . name ) ; 569k ++ ; 570} 571 572free ( deviceList ) ; 573} 574free ( drivers ) ; 575#endif 576return VKFFT_SUCCESS ; 577} 578VkFFTResult performVulkanFFT ( VkGPU * vkGPU , VkFFTApplication * app , VkFFTLaunchParams * launchParams , int inverse , uint64_t num_iter ) { 579VkFFTResult resFFT = VKFFT_SUCCESS ; 580#if ( VKFFT_BACKEND == 0 ) 581VkResult res = VK_SUCCESS ; 582VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 583commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 584commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 585commandBufferAllocateInfo . commandBufferCount = 1 ; 586VkCommandBuffer commandBuffer = {} ; 587res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 588if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 589VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 590commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 591res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 592if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 593launchParams -> commandBuffer = & commandBuffer ; 594//Record commands num_iter times. Allows to perform multiple convolutions/transforms in one submit. 595for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 596resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 597if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 598} 599res = vkEndCommandBuffer ( commandBuffer ) ; 600if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 601VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 602submitInfo . commandBufferCount = 1 ; 603submitInfo . pCommandBuffers = & commandBuffer ; 604std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 605res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 606if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 607res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 608if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 609std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 610double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 611//printf("Pure submit execution time per num_iter: %.3f ms\n", totTime / num_iter); 612res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 613if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 614vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 615#elif ( VKFFT_BACKEND == 1 ) 616cudaError_t res = cudaSuccess ; 617std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 618for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 619resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 620if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 621} 622res = cudaDeviceSynchronize () ; 623if ( res != cudaSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 624std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 625double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 626#elif ( VKFFT_BACKEND == 2 ) 627hipError_t res = hipSuccess ; 628std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 629for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 630resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 631if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 632} 633res = hipDeviceSynchronize () ; 634if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 635std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 636double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 637#elif ( VKFFT_BACKEND == 3 ) 638cl_int res = CL_SUCCESS ; 639launchParams -> commandQueue = & vkGPU -> commandQueue ; 640std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 641for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 642resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 643if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 644} 645res = clFinish ( vkGPU -> commandQueue ) ; 646if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 647std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 648double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 649#elif ( VKFFT_BACKEND == 4 ) 650ze_result_t res = ZE_RESULT_SUCCESS ; 651ze_command_list_desc_t commandListDescription = {} ; 652commandListDescription . stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC ; 653ze_command_list_handle_t commandList = {} ; 654res = zeCommandListCreate ( vkGPU -> context , vkGPU -> device , & commandListDescription , & commandList ) ; 655if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 656 657launchParams -> commandList = & commandList ; 658//Record commands num_iter times. Allows to perform multiple convolutions/transforms in one submit. 659for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 660resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 661if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 662} 663res = zeCommandListClose ( commandList ) ; 664if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 665 666std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 667res = zeCommandQueueExecuteCommandLists ( vkGPU -> commandQueue , 1 , & commandList , 0 ) ; 668if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 669res = zeCommandQueueSynchronize ( vkGPU -> commandQueue , UINT32_MAX ) ; 670if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 671 672std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 673double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 674//printf("Pure submit execution time per num_iter: %.3f ms\n", totTime / num_iter); 675res = zeCommandListDestroy ( commandList ) ; 676if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_DESTROY_COMMAND_LIST ; 677#endif 678return resFFT ; 679} 680VkFFTResult performVulkanFFTiFFT ( VkGPU * vkGPU , VkFFTApplication * app , VkFFTLaunchParams * launchParams , uint64_t num_iter , double * time_result ) { 681VkFFTResult resFFT = VKFFT_SUCCESS ; 682#if ( VKFFT_BACKEND == 0 ) 683VkResult res = VK_SUCCESS ; 684VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 685commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 686commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 687commandBufferAllocateInfo . commandBufferCount = 1 ; 688VkCommandBuffer commandBuffer = {} ; 689res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 690if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 691VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 692commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 693res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 694if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 695launchParams -> commandBuffer = & commandBuffer ; 696for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 697resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 698if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 699resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 700if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 701} 702res = vkEndCommandBuffer ( commandBuffer ) ; 703if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 704VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 705submitInfo . commandBufferCount = 1 ; 706submitInfo . pCommandBuffers = & commandBuffer ; 707std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 708res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 709if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 710res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 711if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 712std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 713double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 714time_result [ 0 ] = totTime / num_iter ; 715res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 716if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 717vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 718#elif ( VKFFT_BACKEND == 1 ) 719cudaError_t res = cudaSuccess ; 720std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 721for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 722resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 723if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 724resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 725if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 726} 727res = cudaDeviceSynchronize () ; 728if ( res != cudaSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 729std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 730double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 731time_result [ 0 ] = totTime / num_iter ; 732#elif ( VKFFT_BACKEND == 2 ) 733hipError_t res = hipSuccess ; 734std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 735for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 736resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 737if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 738resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 739if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 740} 741res = hipDeviceSynchronize () ; 742if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 743std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 744double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 745time_result [ 0 ] = totTime / num_iter ; 746#elif ( VKFFT_BACKEND == 3 ) 747cl_int res = CL_SUCCESS ; 748launchParams -> commandQueue = & vkGPU -> commandQueue ; 749std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 750for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 751resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 752if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 753resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 754if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 755} 756res = clFinish ( vkGPU -> commandQueue ) ; 757if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 758std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 759double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 760time_result [ 0 ] = totTime / num_iter ; 761#elif ( VKFFT_BACKEND == 4 ) 762ze_result_t res = ZE_RESULT_SUCCESS ; 763ze_command_list_desc_t commandListDescription = {} ; 764commandListDescription . stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC ; 765ze_command_list_handle_t commandList = {} ; 766res = zeCommandListCreate ( vkGPU -> context , vkGPU -> device , & commandListDescription , & commandList ) ; 767if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 768 769launchParams -> commandList = & commandList ; 770for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 771resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 772if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 773resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 774if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 775} 776res = zeCommandListClose ( commandList ) ; 777if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 778 779std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 780res = zeCommandQueueExecuteCommandLists ( vkGPU -> commandQueue , 1 , & commandList , 0 ) ; 781if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 782res = zeCommandQueueSynchronize ( vkGPU -> commandQueue , UINT32_MAX ) ; 783if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 784 785std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 786double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 787time_result [ 0 ] = totTime / num_iter ; 788res = zeCommandListDestroy ( commandList ) ; 789if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_DESTROY_COMMAND_LIST ; 790#endif 791return resFFT ; 792}