yum-mirror/VkFFT
git clone https://git.yummers.dev/yum-mirror/VkFFT
ba7001c
v1.2.30
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#elif ( VKFFT_BACKEND == 5 ) 43#include "Foundation/Foundation.hpp" 44#include "QuartzCore/QuartzCore.hpp" 45#include "Metal/Metal.hpp" 46#endif 47#include "vkFFT.h" 48#include "utils_VkFFT.h" 49#if ( VKFFT_BACKEND == 0 ) 50 51VkResult CreateDebugUtilsMessengerEXT ( VkGPU * vkGPU , const VkDebugUtilsMessengerCreateInfoEXT * pCreateInfo , const VkAllocationCallbacks * pAllocator , VkDebugUtilsMessengerEXT * pDebugMessenger ) { 52//pointer to the function, as it is not part of the core. Function creates debugging messenger 53PFN_vkCreateDebugUtilsMessengerEXT func = ( PFN_vkCreateDebugUtilsMessengerEXT ) vkGetInstanceProcAddr ( vkGPU -> instance , "vkCreateDebugUtilsMessengerEXT" ) ; 54if ( func != NULL ) { 55return func ( vkGPU -> instance , pCreateInfo , pAllocator , pDebugMessenger ) ; 56} 57else { 58return VK_ERROR_EXTENSION_NOT_PRESENT ; 59} 60} 61void DestroyDebugUtilsMessengerEXT ( VkGPU * vkGPU , const VkAllocationCallbacks * pAllocator ) { 62//pointer to the function, as it is not part of the core. Function destroys debugging messenger 63PFN_vkDestroyDebugUtilsMessengerEXT func = ( PFN_vkDestroyDebugUtilsMessengerEXT ) vkGetInstanceProcAddr ( vkGPU -> instance , "vkDestroyDebugUtilsMessengerEXT" ) ; 64if ( func != NULL ) { 65func ( vkGPU -> instance , vkGPU -> debugMessenger , pAllocator ) ; 66} 67} 68static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback ( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity , VkDebugUtilsMessageTypeFlagsEXT messageType , const VkDebugUtilsMessengerCallbackDataEXT * pCallbackData , void * pUserData ) { 69printf ( "validation layer: %s \n " , pCallbackData -> pMessage ) ; 70return VK_FALSE ; 71} 72 73 74VkResult setupDebugMessenger ( VkGPU * vkGPU ) { 75//function that sets up the debugging messenger 76if ( vkGPU -> enableValidationLayers == 0 ) return VK_SUCCESS ; 77 78VkDebugUtilsMessengerCreateInfoEXT createInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT } ; 79createInfo . 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 ; 80createInfo . 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 ; 81createInfo . pfnUserCallback = debugCallback ; 82 83if ( CreateDebugUtilsMessengerEXT ( vkGPU , & createInfo , NULL , & vkGPU -> debugMessenger ) != VK_SUCCESS ) { 84return VK_ERROR_INITIALIZATION_FAILED ; 85} 86return VK_SUCCESS ; 87} 88VkResult checkValidationLayerSupport () { 89//check if validation layers are supported when an instance is created 90uint32_t layerCount ; 91vkEnumerateInstanceLayerProperties ( & layerCount , NULL ) ; 92 93VkLayerProperties * availableLayers = ( VkLayerProperties * ) malloc ( sizeof ( VkLayerProperties ) * layerCount ) ; 94if ( ! availableLayers ) return VK_INCOMPLETE ; 95vkEnumerateInstanceLayerProperties ( & layerCount , availableLayers ) ; 96if ( availableLayers ) { 97for ( uint64_t i = 0 ; i < layerCount ; i ++ ) { 98if ( strcmp ( "VK_LAYER_KHRONOS_validation" , availableLayers [ i ] . layerName ) == 0 ) { 99free ( availableLayers ) ; 100return VK_SUCCESS ; 101} 102} 103free ( availableLayers ) ; 104} 105else { 106return VK_INCOMPLETE ; 107} 108return VK_ERROR_LAYER_NOT_PRESENT ; 109} 110 111std :: vector < const char *> getRequiredExtensions ( VkGPU * vkGPU , uint64_t sample_id ) { 112std :: vector < const char *> extensions ; 113 114if ( vkGPU -> enableValidationLayers ) { 115extensions . push_back ( VK_EXT_DEBUG_UTILS_EXTENSION_NAME ) ; 116} 117switch ( sample_id ) { 118#if ( VK_API_VERSION > 10 ) 119case 2 : case 102 : 120extensions . push_back ( "VK_KHR_get_physical_device_properties2" ) ; 121break ; 122#endif 123default : 124break ; 125} 126 127 128return extensions ; 129} 130 131VkResult createInstance ( VkGPU * vkGPU , uint64_t sample_id ) { 132//create instance - a connection between the application and the Vulkan library 133VkResult res = VK_SUCCESS ; 134//check if validation layers are supported 135if ( vkGPU -> enableValidationLayers == 1 ) { 136res = checkValidationLayerSupport () ; 137if ( res != VK_SUCCESS ) return res ; 138} 139 140VkApplicationInfo applicationInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO } ; 141applicationInfo . pApplicationName = "VkFFT" ; 142applicationInfo . applicationVersion = ( uint32_t ) VkFFTGetVersion () ; 143applicationInfo . pEngineName = "VkFFT" ; 144applicationInfo . engineVersion = 1 ; 145#if ( VK_API_VERSION >= 12 ) 146applicationInfo . apiVersion = VK_API_VERSION_1_2 ; 147#elif ( VK_API_VERSION == 11 ) 148applicationInfo . apiVersion = VK_API_VERSION_1_1 ; 149#else 150applicationInfo . apiVersion = VK_API_VERSION_1_0 ; 151#endif 152 153VkInstanceCreateInfo createInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO } ; 154createInfo . flags = 0 ; 155createInfo . pApplicationInfo = & applicationInfo ; 156 157auto extensions = getRequiredExtensions ( vkGPU , sample_id ) ; 158createInfo . enabledExtensionCount = ( uint32_t )( extensions . size ()) ; 159createInfo . ppEnabledExtensionNames = extensions . data () ; 160 161VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT } ; 162if ( vkGPU -> enableValidationLayers ) { 163//query for the validation layer support in the instance 164createInfo . enabledLayerCount = 1 ; 165const char * validationLayers = "VK_LAYER_KHRONOS_validation" ; 166createInfo . ppEnabledLayerNames = & validationLayers ; 167debugCreateInfo . 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 ; 168debugCreateInfo . 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 ; 169debugCreateInfo . pfnUserCallback = debugCallback ; 170createInfo . pNext = ( VkDebugUtilsMessengerCreateInfoEXT * ) & debugCreateInfo ; 171} 172else { 173createInfo . enabledLayerCount = 0 ; 174 175createInfo . pNext = nullptr ; 176} 177 178res = vkCreateInstance ( & createInfo , NULL , & vkGPU -> instance ) ; 179if ( res != VK_SUCCESS ) return res ; 180 181return res ; 182} 183 184VkResult findPhysicalDevice ( VkGPU * vkGPU ) { 185//check if there are GPUs that support Vulkan and select one 186VkResult res = VK_SUCCESS ; 187uint32_t deviceCount ; 188res = vkEnumeratePhysicalDevices ( vkGPU -> instance , & deviceCount , NULL ) ; 189if ( res != VK_SUCCESS ) return res ; 190if ( deviceCount == 0 ) { 191return VK_ERROR_DEVICE_LOST ; 192} 193 194VkPhysicalDevice * devices = ( VkPhysicalDevice * ) malloc ( sizeof ( VkPhysicalDevice ) * deviceCount ) ; 195if ( ! devices ) return VK_INCOMPLETE ; 196res = vkEnumeratePhysicalDevices ( vkGPU -> instance , & deviceCount , devices ) ; 197if ( res != VK_SUCCESS ) return res ; 198if ( devices ) { 199vkGPU -> physicalDevice = devices [ vkGPU -> device_id ] ; 200free ( devices ) ; 201return VK_SUCCESS ; 202} 203else 204return VK_INCOMPLETE ; 205} 206VkResult getComputeQueueFamilyIndex ( VkGPU * vkGPU ) { 207//find a queue family for a selected GPU, select the first available for use 208uint32_t queueFamilyCount ; 209vkGetPhysicalDeviceQueueFamilyProperties ( vkGPU -> physicalDevice , & queueFamilyCount , NULL ) ; 210 211VkQueueFamilyProperties * queueFamilies = ( VkQueueFamilyProperties * ) malloc ( sizeof ( VkQueueFamilyProperties ) * queueFamilyCount ) ; 212if ( ! queueFamilies ) return VK_INCOMPLETE ; 213if ( queueFamilies ) { 214vkGetPhysicalDeviceQueueFamilyProperties ( vkGPU -> physicalDevice , & queueFamilyCount , queueFamilies ) ; 215uint64_t i = 0 ; 216for ( ; i < queueFamilyCount ; i ++ ) { 217VkQueueFamilyProperties props = queueFamilies [ i ] ; 218 219if ( props . queueCount > 0 && ( props . queueFlags & VK_QUEUE_COMPUTE_BIT )) { 220break ; 221} 222} 223free ( queueFamilies ) ; 224if ( i == queueFamilyCount ) { 225return VK_ERROR_INITIALIZATION_FAILED ; 226} 227vkGPU -> queueFamilyIndex = i ; 228return VK_SUCCESS ; 229} 230else 231return VK_INCOMPLETE ; 232} 233 234VkResult createDevice ( VkGPU * vkGPU , uint64_t sample_id ) { 235//create logical device representation 236VkResult res = VK_SUCCESS ; 237VkDeviceQueueCreateInfo queueCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO } ; 238res = getComputeQueueFamilyIndex ( vkGPU ) ; 239if ( res != VK_SUCCESS ) return res ; 240queueCreateInfo . queueFamilyIndex = ( uint32_t ) vkGPU -> queueFamilyIndex ; 241queueCreateInfo . queueCount = 1 ; 242float queuePriorities = 1.0 ; 243queueCreateInfo . pQueuePriorities = & queuePriorities ; 244VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO } ; 245VkPhysicalDeviceFeatures deviceFeatures = {} ; 246switch ( sample_id ) { 247case 1 : case 12 : case 17 : case 18 : case 101 : case 201 : case 1001 : { 248deviceFeatures . shaderFloat64 = true ; 249deviceCreateInfo . enabledExtensionCount = ( uint32_t ) vkGPU -> enabledDeviceExtensions . size () ; 250deviceCreateInfo . ppEnabledExtensionNames = vkGPU -> enabledDeviceExtensions . data () ; 251deviceCreateInfo . pQueueCreateInfos = & queueCreateInfo ; 252deviceCreateInfo . queueCreateInfoCount = 1 ; 253deviceCreateInfo . pEnabledFeatures = & deviceFeatures ; 254res = vkCreateDevice ( vkGPU -> physicalDevice , & deviceCreateInfo , NULL , & vkGPU -> device ) ; 255if ( res != VK_SUCCESS ) return res ; 256vkGetDeviceQueue ( vkGPU -> device , ( uint32_t ) vkGPU -> queueFamilyIndex , 0 , & vkGPU -> queue ) ; 257break ; 258} 259#if ( VK_API_VERSION > 10 ) 260case 2 : case 102 : { 261VkPhysicalDeviceFeatures2 deviceFeatures2 = {} ; 262VkPhysicalDevice16BitStorageFeatures shaderFloat16 = {} ; 263shaderFloat16 . sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES ; 264shaderFloat16 . storageBuffer16BitAccess = true ; 265/*VkPhysicalDeviceShaderFloat16Int8Features shaderFloat16 = {}; 266shaderFloat16.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES; 267shaderFloat16.shaderFloat16 = true; 268shaderFloat16.shaderInt8 = true;*/ 269deviceFeatures2 . sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 ; 270deviceFeatures2 . pNext = & shaderFloat16 ; 271deviceFeatures2 . features = deviceFeatures ; 272vkGetPhysicalDeviceFeatures2 ( vkGPU -> physicalDevice , & deviceFeatures2 ) ; 273deviceCreateInfo . pNext = & deviceFeatures2 ; 274vkGPU -> enabledDeviceExtensions . push_back ( "VK_KHR_16bit_storage" ) ; 275deviceCreateInfo . enabledExtensionCount = ( uint32_t ) vkGPU -> enabledDeviceExtensions . size () ; 276deviceCreateInfo . ppEnabledExtensionNames = vkGPU -> enabledDeviceExtensions . data () ; 277deviceCreateInfo . pQueueCreateInfos = & queueCreateInfo ; 278deviceCreateInfo . queueCreateInfoCount = 1 ; 279deviceCreateInfo . pEnabledFeatures = NULL ; 280res = vkCreateDevice ( vkGPU -> physicalDevice , & deviceCreateInfo , NULL , & vkGPU -> device ) ; 281if ( res != VK_SUCCESS ) return res ; 282vkGetDeviceQueue ( vkGPU -> device , ( uint32_t ) vkGPU -> queueFamilyIndex , 0 , & vkGPU -> queue ) ; 283break ; 284} 285#endif 286default : { 287deviceCreateInfo . enabledExtensionCount = ( uint32_t ) vkGPU -> enabledDeviceExtensions . size () ; 288deviceCreateInfo . ppEnabledExtensionNames = vkGPU -> enabledDeviceExtensions . data () ; 289deviceCreateInfo . pQueueCreateInfos = & queueCreateInfo ; 290deviceCreateInfo . queueCreateInfoCount = 1 ; 291deviceCreateInfo . pEnabledFeatures = NULL ; 292deviceCreateInfo . pEnabledFeatures = & deviceFeatures ; 293res = vkCreateDevice ( vkGPU -> physicalDevice , & deviceCreateInfo , NULL , & vkGPU -> device ) ; 294if ( res != VK_SUCCESS ) return res ; 295vkGetDeviceQueue ( vkGPU -> device , ( uint32_t ) vkGPU -> queueFamilyIndex , 0 , & vkGPU -> queue ) ; 296break ; 297} 298} 299return res ; 300} 301VkResult createFence ( VkGPU * vkGPU ) { 302//create fence for synchronization 303VkResult res = VK_SUCCESS ; 304VkFenceCreateInfo fenceCreateInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO } ; 305fenceCreateInfo . flags = 0 ; 306res = vkCreateFence ( vkGPU -> device , & fenceCreateInfo , NULL , & vkGPU -> fence ) ; 307return res ; 308} 309VkResult createCommandPool ( VkGPU * vkGPU ) { 310//create a place, command buffer memory is allocated from 311VkResult res = VK_SUCCESS ; 312VkCommandPoolCreateInfo commandPoolCreateInfo = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO } ; 313commandPoolCreateInfo . flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT ; 314commandPoolCreateInfo . queueFamilyIndex = ( uint32_t ) vkGPU -> queueFamilyIndex ; 315res = vkCreateCommandPool ( vkGPU -> device , & commandPoolCreateInfo , NULL , & vkGPU -> commandPool ) ; 316return res ; 317} 318 319VkFFTResult findMemoryType ( VkGPU * vkGPU , uint64_t memoryTypeBits , uint64_t memorySize , VkMemoryPropertyFlags properties , uint32_t * memoryTypeIndex ) { 320VkPhysicalDeviceMemoryProperties memoryProperties = { 0 } ; 321 322vkGetPhysicalDeviceMemoryProperties ( vkGPU -> physicalDevice , & memoryProperties ) ; 323 324for ( uint64_t i = 0 ; i < memoryProperties . memoryTypeCount ; ++ i ) { 325if (( memoryTypeBits & (( uint64_t ) 1 << i )) && (( memoryProperties . memoryTypes [ i ] . propertyFlags & properties ) == properties ) && ( memoryProperties . memoryHeaps [ memoryProperties . memoryTypes [ i ] . heapIndex ] . size >= memorySize )) 326{ 327memoryTypeIndex [ 0 ] = ( uint32_t ) i ; 328return VKFFT_SUCCESS ; 329} 330} 331return VKFFT_ERROR_FAILED_TO_FIND_MEMORY ; 332} 333 334VkFFTResult allocateBuffer ( VkGPU * vkGPU , VkBuffer * buffer , VkDeviceMemory * deviceMemory , VkBufferUsageFlags usageFlags , VkMemoryPropertyFlags propertyFlags , uint64_t size ) { 335//allocate the buffer used by the GPU with specified properties 336VkFFTResult resFFT = VKFFT_SUCCESS ; 337VkResult res = VK_SUCCESS ; 338uint32_t queueFamilyIndices ; 339VkBufferCreateInfo bufferCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO } ; 340bufferCreateInfo . sharingMode = VK_SHARING_MODE_EXCLUSIVE ; 341bufferCreateInfo . queueFamilyIndexCount = 1 ; 342bufferCreateInfo . pQueueFamilyIndices = & queueFamilyIndices ; 343bufferCreateInfo . size = size ; 344bufferCreateInfo . usage = usageFlags ; 345res = vkCreateBuffer ( vkGPU -> device , & bufferCreateInfo , NULL , buffer ) ; 346if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_BUFFER ; 347VkMemoryRequirements memoryRequirements = { 0 } ; 348vkGetBufferMemoryRequirements ( vkGPU -> device , buffer [ 0 ] , & memoryRequirements ) ; 349VkMemoryAllocateInfo memoryAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO } ; 350memoryAllocateInfo . allocationSize = memoryRequirements . size ; 351resFFT = findMemoryType ( vkGPU , memoryRequirements . memoryTypeBits , memoryRequirements . size , propertyFlags , & memoryAllocateInfo . memoryTypeIndex ) ; 352if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 353res = vkAllocateMemory ( vkGPU -> device , & memoryAllocateInfo , NULL , deviceMemory ) ; 354if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_MEMORY ; 355res = vkBindBufferMemory ( vkGPU -> device , buffer [ 0 ] , deviceMemory [ 0 ] , 0 ) ; 356if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_BIND_BUFFER_MEMORY ; 357return resFFT ; 358} 359#endif 360VkFFTResult transferDataToCPU ( VkGPU * vkGPU , void * cpu_arr , void * output_buffer , uint64_t transferSize ) { 361//a function that transfers data from the GPU to the CPU using staging buffer, because the GPU memory is not host-coherent 362VkFFTResult resFFT = VKFFT_SUCCESS ; 363#if ( VKFFT_BACKEND == 0 ) 364VkResult res = VK_SUCCESS ; 365VkBuffer * buffer = ( VkBuffer * ) output_buffer ; 366uint64_t stagingBufferSize = transferSize ; 367VkBuffer stagingBuffer = { 0 } ; 368VkDeviceMemory stagingBufferMemory = { 0 } ; 369resFFT = allocateBuffer ( vkGPU , & stagingBuffer , & stagingBufferMemory , VK_BUFFER_USAGE_TRANSFER_DST_BIT , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT , stagingBufferSize ) ; 370if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 371VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 372commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 373commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 374commandBufferAllocateInfo . commandBufferCount = 1 ; 375VkCommandBuffer commandBuffer = { 0 } ; 376res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 377if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 378VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 379commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 380res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 381if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 382VkBufferCopy copyRegion = { 0 } ; 383copyRegion . srcOffset = 0 ; 384copyRegion . dstOffset = 0 ; 385copyRegion . size = stagingBufferSize ; 386vkCmdCopyBuffer ( commandBuffer , buffer [ 0 ] , stagingBuffer , 1 , & copyRegion ) ; 387res = vkEndCommandBuffer ( commandBuffer ) ; 388if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 389VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 390submitInfo . commandBufferCount = 1 ; 391submitInfo . pCommandBuffers = & commandBuffer ; 392res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 393if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 394res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 395if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 396res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 397if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 398vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 399void * data ; 400res = vkMapMemory ( vkGPU -> device , stagingBufferMemory , 0 , stagingBufferSize , 0 , & data ) ; 401if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 402memcpy ( cpu_arr , data , stagingBufferSize ) ; 403vkUnmapMemory ( vkGPU -> device , stagingBufferMemory ) ; 404vkDestroyBuffer ( vkGPU -> device , stagingBuffer , NULL ) ; 405vkFreeMemory ( vkGPU -> device , stagingBufferMemory , NULL ) ; 406#elif ( VKFFT_BACKEND == 1 ) 407cudaError_t res = cudaSuccess ; 408void * buffer = (( void ** ) output_buffer )[ 0 ] ; 409res = cudaMemcpy ( cpu_arr , buffer , transferSize , cudaMemcpyDeviceToHost ) ; 410if ( res != cudaSuccess ) { 411return VKFFT_ERROR_FAILED_TO_COPY ; 412} 413#elif ( VKFFT_BACKEND == 2 ) 414hipError_t res = hipSuccess ; 415void * buffer = (( void ** ) output_buffer )[ 0 ] ; 416res = hipMemcpy ( cpu_arr , buffer , transferSize , hipMemcpyDeviceToHost ) ; 417if ( res != hipSuccess ) { 418return VKFFT_ERROR_FAILED_TO_COPY ; 419} 420#elif ( VKFFT_BACKEND == 3 ) 421cl_int res = CL_SUCCESS ; 422cl_mem * buffer = ( cl_mem * ) output_buffer ; 423cl_command_queue commandQueue = clCreateCommandQueue ( vkGPU -> context , vkGPU -> device , 0 , & res ) ; 424if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QUEUE ; 425res = clEnqueueReadBuffer ( commandQueue , buffer [ 0 ] , CL_TRUE , 0 , transferSize , cpu_arr , 0 , NULL , NULL ) ; 426if ( res != CL_SUCCESS ) { 427return VKFFT_ERROR_FAILED_TO_COPY ; 428} 429res = clReleaseCommandQueue ( commandQueue ) ; 430if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_RELEASE_COMMAND_QUEUE ; 431#elif ( VKFFT_BACKEND == 4 ) 432ze_result_t res = ZE_RESULT_SUCCESS ; 433void * buffer = (( void ** ) output_buffer )[ 0 ] ; 434ze_command_queue_desc_t commandQueueCopyDesc = { 435ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC , 4360 , 437vkGPU -> commandQueueID , 4380 , // index 4390 , // flags 440ZE_COMMAND_QUEUE_MODE_DEFAULT , 441ZE_COMMAND_QUEUE_PRIORITY_NORMAL 442} ; 443ze_command_list_handle_t copyCommandList ; 444res = zeCommandListCreateImmediate ( vkGPU -> context , vkGPU -> device , & commandQueueCopyDesc , & copyCommandList ) ; 445if ( res != ZE_RESULT_SUCCESS ) { 446return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 447} 448res = zeCommandListAppendMemoryCopy ( copyCommandList , cpu_arr , buffer , transferSize , 0 , 0 , 0 ) ; 449if ( res != ZE_RESULT_SUCCESS ) { 450return VKFFT_ERROR_FAILED_TO_COPY ; 451} 452res = zeCommandQueueSynchronize ( vkGPU -> commandQueue , UINT32_MAX ) ; 453if ( res != ZE_RESULT_SUCCESS ) { 454return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 455} 456#elif ( VKFFT_BACKEND == 5 ) 457MTL :: Buffer * stagingBuffer = vkGPU -> device -> newBuffer ( transferSize , MTL :: ResourceStorageModeShared ) ; 458MTL :: CommandBuffer * copyCommandBuffer = vkGPU -> queue -> commandBuffer () ; 459if ( copyCommandBuffer == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 460MTL :: BlitCommandEncoder * blitCommandEncoder = copyCommandBuffer -> blitCommandEncoder () ; 461if ( blitCommandEncoder == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 462MTL :: Buffer * buffer = (( MTL :: Buffer ** ) output_buffer )[ 0 ] ; 463blitCommandEncoder -> copyFromBuffer (( MTL :: Buffer * ) buffer , 0 , ( MTL :: Buffer * ) stagingBuffer , 0 , transferSize ) ; 464blitCommandEncoder -> endEncoding () ; 465copyCommandBuffer -> commit () ; 466copyCommandBuffer -> waitUntilCompleted () ; 467blitCommandEncoder -> release () ; 468copyCommandBuffer -> release () ; 469memcpy ( cpu_arr , stagingBuffer -> contents () , transferSize ) ; 470stagingBuffer -> release () ; 471#endif 472return resFFT ; 473} 474VkFFTResult transferDataFromCPU ( VkGPU * vkGPU , void * cpu_arr , void * input_buffer , uint64_t transferSize ) { 475VkFFTResult resFFT = VKFFT_SUCCESS ; 476#if ( VKFFT_BACKEND == 0 ) 477VkResult res = VK_SUCCESS ; 478VkBuffer * buffer = ( VkBuffer * ) input_buffer ; 479uint64_t stagingBufferSize = transferSize ; 480VkBuffer stagingBuffer = { 0 } ; 481VkDeviceMemory stagingBufferMemory = { 0 } ; 482resFFT = allocateBuffer ( vkGPU , & stagingBuffer , & stagingBufferMemory , VK_BUFFER_USAGE_TRANSFER_SRC_BIT , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT , stagingBufferSize ) ; 483if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 484void * data ; 485res = vkMapMemory ( vkGPU -> device , stagingBufferMemory , 0 , stagingBufferSize , 0 , & data ) ; 486if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 487memcpy ( data , cpu_arr , stagingBufferSize ) ; 488vkUnmapMemory ( vkGPU -> device , stagingBufferMemory ) ; 489VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 490commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 491commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 492commandBufferAllocateInfo . commandBufferCount = 1 ; 493VkCommandBuffer commandBuffer = { 0 } ; 494res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 495if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 496VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 497commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 498res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 499if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 500VkBufferCopy copyRegion = { 0 } ; 501copyRegion . srcOffset = 0 ; 502copyRegion . dstOffset = 0 ; 503copyRegion . size = stagingBufferSize ; 504vkCmdCopyBuffer ( commandBuffer , stagingBuffer , buffer [ 0 ] , 1 , & copyRegion ) ; 505res = vkEndCommandBuffer ( commandBuffer ) ; 506if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 507VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 508submitInfo . commandBufferCount = 1 ; 509submitInfo . pCommandBuffers = & commandBuffer ; 510res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 511if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 512res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 513if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 514res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 515if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 516vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 517vkDestroyBuffer ( vkGPU -> device , stagingBuffer , NULL ) ; 518vkFreeMemory ( vkGPU -> device , stagingBufferMemory , NULL ) ; 519return resFFT ; 520#elif ( VKFFT_BACKEND == 1 ) 521cudaError_t res = cudaSuccess ; 522void * buffer = (( void ** ) input_buffer )[ 0 ] ; 523res = cudaMemcpy ( buffer , cpu_arr , transferSize , cudaMemcpyHostToDevice ) ; 524if ( res != cudaSuccess ) { 525return VKFFT_ERROR_FAILED_TO_COPY ; 526} 527#elif ( VKFFT_BACKEND == 2 ) 528hipError_t res = hipSuccess ; 529void * buffer = (( void ** ) input_buffer )[ 0 ] ; 530res = hipMemcpy ( buffer , cpu_arr , transferSize , hipMemcpyHostToDevice ) ; 531if ( res != hipSuccess ) { 532return VKFFT_ERROR_FAILED_TO_COPY ; 533} 534#elif ( VKFFT_BACKEND == 3 ) 535cl_int res = CL_SUCCESS ; 536cl_mem * buffer = ( cl_mem * ) input_buffer ; 537cl_command_queue commandQueue = clCreateCommandQueue ( vkGPU -> context , vkGPU -> device , 0 , & res ) ; 538if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QUEUE ; 539res = clEnqueueWriteBuffer ( commandQueue , buffer [ 0 ] , CL_TRUE , 0 , transferSize , cpu_arr , 0 , NULL , NULL ) ; 540if ( res != CL_SUCCESS ) { 541return VKFFT_ERROR_FAILED_TO_COPY ; 542} 543res = clReleaseCommandQueue ( commandQueue ) ; 544if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_RELEASE_COMMAND_QUEUE ; 545#elif ( VKFFT_BACKEND == 4 ) 546ze_result_t res = ZE_RESULT_SUCCESS ; 547void * buffer = (( void ** ) input_buffer )[ 0 ] ; 548ze_command_queue_desc_t commandQueueCopyDesc = { 549ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC , 5500 , 551vkGPU -> commandQueueID , 5520 , // index 5530 , // flags 554ZE_COMMAND_QUEUE_MODE_DEFAULT , 555ZE_COMMAND_QUEUE_PRIORITY_NORMAL 556} ; 557ze_command_list_handle_t copyCommandList ; 558res = zeCommandListCreateImmediate ( vkGPU -> context , vkGPU -> device , & commandQueueCopyDesc , & copyCommandList ) ; 559if ( res != ZE_RESULT_SUCCESS ) { 560return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 561} 562res = zeCommandListAppendMemoryCopy ( copyCommandList , buffer , cpu_arr , transferSize , 0 , 0 , 0 ) ; 563if ( res != ZE_RESULT_SUCCESS ) { 564return VKFFT_ERROR_FAILED_TO_COPY ; 565} 566res = zeCommandQueueSynchronize ( vkGPU -> commandQueue , UINT32_MAX ) ; 567if ( res != ZE_RESULT_SUCCESS ) { 568return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 569} 570#elif ( VKFFT_BACKEND == 5 ) 571MTL :: Buffer * stagingBuffer = vkGPU -> device -> newBuffer ( cpu_arr , transferSize , MTL :: ResourceStorageModeShared ) ; 572MTL :: CommandBuffer * copyCommandBuffer = vkGPU -> queue -> commandBuffer () ; 573if ( copyCommandBuffer == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 574MTL :: BlitCommandEncoder * blitCommandEncoder = copyCommandBuffer -> blitCommandEncoder () ; 575if ( blitCommandEncoder == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 576MTL :: Buffer * buffer = (( MTL :: Buffer ** ) input_buffer )[ 0 ] ; 577blitCommandEncoder -> copyFromBuffer (( MTL :: Buffer * ) stagingBuffer , 0 , ( MTL :: Buffer * ) buffer , 0 , transferSize ) ; 578blitCommandEncoder -> endEncoding () ; 579copyCommandBuffer -> commit () ; 580copyCommandBuffer -> waitUntilCompleted () ; 581blitCommandEncoder -> release () ; 582copyCommandBuffer -> release () ; 583stagingBuffer -> release () ; 584#endif 585return resFFT ; 586} 587VkFFTResult devices_list () { 588//this function creates an instance and prints the list of available devices 589#if ( VKFFT_BACKEND == 0 ) 590VkResult res = VK_SUCCESS ; 591VkInstance local_instance = { 0 } ; 592VkInstanceCreateInfo createInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO } ; 593createInfo . flags = 0 ; 594createInfo . pApplicationInfo = NULL ; 595VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT } ; 596createInfo . enabledLayerCount = 0 ; 597createInfo . enabledExtensionCount = 0 ; 598createInfo . pNext = NULL ; 599res = vkCreateInstance ( & createInfo , NULL , & local_instance ) ; 600if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_INSTANCE ; 601 602uint32_t deviceCount ; 603res = vkEnumeratePhysicalDevices ( local_instance , & deviceCount , NULL ) ; 604if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 605 606VkPhysicalDevice * devices = ( VkPhysicalDevice * ) malloc ( sizeof ( VkPhysicalDevice ) * deviceCount ) ; 607if ( ! devices ) return VKFFT_ERROR_MALLOC_FAILED ; 608if ( devices ) { 609res = vkEnumeratePhysicalDevices ( local_instance , & deviceCount , devices ) ; 610if ( res != VK_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 611for ( uint64_t i = 0 ; i < deviceCount ; i ++ ) { 612VkPhysicalDeviceProperties device_properties ; 613vkGetPhysicalDeviceProperties ( devices [ i ] , & device_properties ) ; 614printf ( "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 )) ; 615} 616free ( devices ) ; 617} 618else 619return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 620vkDestroyInstance ( local_instance , NULL ) ; 621#elif ( VKFFT_BACKEND == 1 ) 622CUresult res = CUDA_SUCCESS ; 623res = cuInit ( 0 ) ; 624if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 625int numDevices ; 626res = cuDeviceGetCount ( & numDevices ) ; 627if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID ; 628for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 629char deviceName [ 256 ] ; 630CUdevice device = {} ; 631res = cuDeviceGet ( & device , ( int ) i ) ; 632if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 633res = cuDeviceGetName ( deviceName , 256 , device ) ; 634if ( res != CUDA_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 635printf ( "Device id: %" PRIu64 " name: %s \n " , i , deviceName ) ; 636} 637#elif ( VKFFT_BACKEND == 2 ) 638hipError_t res = hipSuccess ; 639res = hipInit ( 0 ) ; 640if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 641int numDevices ; 642res = hipGetDeviceCount ( & numDevices ) ; 643if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID ; 644for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 645char deviceName [ 256 ] ; 646hipDevice_t device = {} ; 647res = hipDeviceGet ( & device , i ) ; 648if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 649res = hipDeviceGetName ( deviceName , 256 , device ) ; 650if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 651printf ( "Device id: %" PRIu64 " name: %s \n " , i , deviceName ) ; 652} 653#elif ( VKFFT_BACKEND == 3 ) 654cl_int res = CL_SUCCESS ; 655cl_uint numPlatforms ; 656res = clGetPlatformIDs ( 0 , 0 , & numPlatforms ) ; 657if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 658cl_platform_id * platforms = ( cl_platform_id * ) malloc ( sizeof ( cl_platform_id ) * numPlatforms ) ; 659if ( ! platforms ) return VKFFT_ERROR_MALLOC_FAILED ; 660res = clGetPlatformIDs ( numPlatforms , platforms , 0 ) ; 661if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 662uint64_t k = 0 ; 663for ( uint64_t j = 0 ; j < numPlatforms ; j ++ ) { 664cl_uint numDevices ; 665res = clGetDeviceIDs ( platforms [ j ] , CL_DEVICE_TYPE_ALL , 0 , 0 , & numDevices ) ; 666cl_device_id * deviceList = ( cl_device_id * ) malloc ( sizeof ( cl_device_id ) * numDevices ) ; 667if ( ! deviceList ) return VKFFT_ERROR_MALLOC_FAILED ; 668res = clGetDeviceIDs ( platforms [ j ] , CL_DEVICE_TYPE_ALL , numDevices , deviceList , 0 ) ; 669if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 670for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 671char deviceName [ 256 ] ; 672char apiVersion [ 256 ] ; 673res = clGetDeviceInfo ( deviceList [ i ] , CL_DEVICE_NAME , 256 * sizeof ( char ) , deviceName , 0 ) ; 674if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 675res = clGetDeviceInfo ( deviceList [ i ] , CL_DEVICE_VERSION , 256 * sizeof ( char ) , apiVersion , 0 ) ; 676if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 677printf ( "Platform id: %" PRIu64 " Device id: %" PRIu64 " name: %s API:%s \n " , j , k , deviceName , apiVersion ) ; 678k ++ ; 679} 680free ( deviceList ) ; 681} 682free ( platforms ) ; 683#elif ( VKFFT_BACKEND == 4 ) 684ze_result_t res = ZE_RESULT_SUCCESS ; 685res = zeInit ( 0 ) ; 686if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 687uint32_t numDrivers = 0 ; 688res = zeDriverGet ( & numDrivers , 0 ) ; 689if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 690ze_driver_handle_t * drivers = ( ze_driver_handle_t * ) malloc ( numDrivers * sizeof ( ze_driver_handle_t )) ; 691if ( ! drivers ) return VKFFT_ERROR_MALLOC_FAILED ; 692res = zeDriverGet ( & numDrivers , drivers ) ; 693if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_INITIALIZE ; 694uint64_t k = 0 ; 695for ( uint64_t j = 0 ; j < numDrivers ; j ++ ) { 696uint32_t numDevices = 0 ; 697res = zeDeviceGet ( drivers [ j ] , & numDevices , nullptr ) ; 698if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 699ze_device_handle_t * deviceList = ( ze_device_handle_t * ) malloc ( numDevices * sizeof ( ze_device_handle_t )) ; 700if ( ! deviceList ) return VKFFT_ERROR_MALLOC_FAILED ; 701res = zeDeviceGet ( drivers [ j ] , & numDevices , deviceList ) ; 702if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_GET_DEVICE ; 703for ( uint64_t i = 0 ; i < numDevices ; i ++ ) { 704ze_device_properties_t device_properties ; 705res = zeDeviceGetProperties ( deviceList [ i ] , & device_properties ) ; 706if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES ; 707printf ( "Driver id: %" PRIu64 " Device id: %" PRIu64 " name: %s \n " , j , k , device_properties . name ) ; 708k ++ ; 709} 710 711free ( deviceList ) ; 712} 713free ( drivers ) ; 714#elif ( VKFFT_BACKEND == 5 ) 715NS :: Array * devices = MTL :: CopyAllDevices () ; 716for ( uint64_t i = 0 ; i < devices -> count () ; i ++ ) { 717MTL :: Device * loc_device = ( MTL :: Device * ) devices -> object ( i ) ; 718printf ( "Device id: %" PRIu64 " name: %s \n " , i , loc_device -> name () -> cString ( NS :: UTF8StringEncoding )) ; 719} 720#endif 721return VKFFT_SUCCESS ; 722} 723VkFFTResult performVulkanFFT ( VkGPU * vkGPU , VkFFTApplication * app , VkFFTLaunchParams * launchParams , int inverse , uint64_t num_iter ) { 724VkFFTResult resFFT = VKFFT_SUCCESS ; 725#if ( VKFFT_BACKEND == 0 ) 726VkResult res = VK_SUCCESS ; 727VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 728commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 729commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 730commandBufferAllocateInfo . commandBufferCount = 1 ; 731VkCommandBuffer commandBuffer = {} ; 732res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 733if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 734VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 735commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 736res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 737if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 738launchParams -> commandBuffer = & commandBuffer ; 739//Record commands num_iter times. Allows to perform multiple convolutions/transforms in one submit. 740for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 741resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 742if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 743} 744res = vkEndCommandBuffer ( commandBuffer ) ; 745if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 746VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 747submitInfo . commandBufferCount = 1 ; 748submitInfo . pCommandBuffers = & commandBuffer ; 749std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 750res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 751if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 752res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 753if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 754std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 755double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 756//printf("Pure submit execution time per num_iter: %.3f ms\n", totTime / num_iter); 757res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 758if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 759vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 760#elif ( VKFFT_BACKEND == 1 ) 761cudaError_t res = cudaSuccess ; 762std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 763for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 764resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 765if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 766} 767res = cudaDeviceSynchronize () ; 768if ( res != cudaSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 769std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 770double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 771#elif ( VKFFT_BACKEND == 2 ) 772hipError_t res = hipSuccess ; 773std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 774for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 775resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 776if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 777} 778res = hipDeviceSynchronize () ; 779if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 780std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 781double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 782#elif ( VKFFT_BACKEND == 3 ) 783cl_int res = CL_SUCCESS ; 784launchParams -> commandQueue = & vkGPU -> commandQueue ; 785std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 786for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 787resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 788if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 789} 790res = clFinish ( vkGPU -> commandQueue ) ; 791if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 792std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 793double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 794#elif ( VKFFT_BACKEND == 4 ) 795ze_result_t res = ZE_RESULT_SUCCESS ; 796ze_command_list_desc_t commandListDescription = {} ; 797commandListDescription . stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC ; 798ze_command_list_handle_t commandList = {} ; 799res = zeCommandListCreate ( vkGPU -> context , vkGPU -> device , & commandListDescription , & commandList ) ; 800if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 801 802launchParams -> commandList = & commandList ; 803//Record commands num_iter times. Allows to perform multiple convolutions/transforms in one submit. 804for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 805resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 806if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 807} 808res = zeCommandListClose ( commandList ) ; 809if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 810 811std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 812res = zeCommandQueueExecuteCommandLists ( vkGPU -> commandQueue , 1 , & commandList , 0 ) ; 813if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 814res = zeCommandQueueSynchronize ( vkGPU -> commandQueue , UINT32_MAX ) ; 815if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 816 817std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 818double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 819//printf("Pure submit execution time per num_iter: %.3f ms\n", totTime / num_iter); 820res = zeCommandListDestroy ( commandList ) ; 821if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_DESTROY_COMMAND_LIST ; 822#elif ( VKFFT_BACKEND == 5 ) 823MTL :: CommandBuffer * commandBuffer = vkGPU -> queue -> commandBuffer () ; 824if ( commandBuffer == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 825launchParams -> commandBuffer = commandBuffer ; 826MTL :: ComputeCommandEncoder * commandEncoder = commandBuffer -> computeCommandEncoder () ; 827if ( commandEncoder == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 828launchParams -> commandEncoder = commandEncoder ; 829for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 830resFFT = VkFFTAppend ( app , inverse , launchParams ) ; 831if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 832} 833commandEncoder -> endEncoding () ; 834 835std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 836commandBuffer -> commit () ; 837commandBuffer -> waitUntilCompleted () ; 838std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 839double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 840 841commandEncoder -> release () ; 842commandBuffer -> release () ; 843#endif 844return resFFT ; 845} 846VkFFTResult performVulkanFFTiFFT ( VkGPU * vkGPU , VkFFTApplication * app , VkFFTLaunchParams * launchParams , uint64_t num_iter , double * time_result ) { 847VkFFTResult resFFT = VKFFT_SUCCESS ; 848#if ( VKFFT_BACKEND == 0 ) 849VkResult res = VK_SUCCESS ; 850VkCommandBufferAllocateInfo commandBufferAllocateInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO } ; 851commandBufferAllocateInfo . commandPool = vkGPU -> commandPool ; 852commandBufferAllocateInfo . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ; 853commandBufferAllocateInfo . commandBufferCount = 1 ; 854VkCommandBuffer commandBuffer = {} ; 855res = vkAllocateCommandBuffers ( vkGPU -> device , & commandBufferAllocateInfo , & commandBuffer ) ; 856if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS ; 857VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO } ; 858commandBufferBeginInfo . flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT ; 859res = vkBeginCommandBuffer ( commandBuffer , & commandBufferBeginInfo ) ; 860if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER ; 861launchParams -> commandBuffer = & commandBuffer ; 862for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 863resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 864if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 865resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 866if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 867} 868res = vkEndCommandBuffer ( commandBuffer ) ; 869if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 870VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO } ; 871submitInfo . commandBufferCount = 1 ; 872submitInfo . pCommandBuffers = & commandBuffer ; 873std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 874res = vkQueueSubmit ( vkGPU -> queue , 1 , & submitInfo , vkGPU -> fence ) ; 875if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 876res = vkWaitForFences ( vkGPU -> device , 1 , & vkGPU -> fence , VK_TRUE , 100000000000 ) ; 877if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 878std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 879double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 880time_result [ 0 ] = totTime / num_iter ; 881res = vkResetFences ( vkGPU -> device , 1 , & vkGPU -> fence ) ; 882if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_RESET_FENCES ; 883vkFreeCommandBuffers ( vkGPU -> device , vkGPU -> commandPool , 1 , & commandBuffer ) ; 884#elif ( VKFFT_BACKEND == 1 ) 885cudaError_t res = cudaSuccess ; 886std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 887for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 888resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 889if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 890resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 891if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 892} 893res = cudaDeviceSynchronize () ; 894if ( res != cudaSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 895std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 896double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 897time_result [ 0 ] = totTime / num_iter ; 898#elif ( VKFFT_BACKEND == 2 ) 899hipError_t res = hipSuccess ; 900std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 901for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 902resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 903if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 904resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 905if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 906} 907res = hipDeviceSynchronize () ; 908if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 909std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 910double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 911time_result [ 0 ] = totTime / num_iter ; 912#elif ( VKFFT_BACKEND == 3 ) 913cl_int res = CL_SUCCESS ; 914launchParams -> commandQueue = & vkGPU -> commandQueue ; 915std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 916for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 917resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 918if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 919resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 920if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 921} 922res = clFinish ( vkGPU -> commandQueue ) ; 923if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_SYNCHRONIZE ; 924std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 925double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 926time_result [ 0 ] = totTime / num_iter ; 927#elif ( VKFFT_BACKEND == 4 ) 928ze_result_t res = ZE_RESULT_SUCCESS ; 929ze_command_list_desc_t commandListDescription = {} ; 930commandListDescription . stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC ; 931ze_command_list_handle_t commandList = {} ; 932res = zeCommandListCreate ( vkGPU -> context , vkGPU -> device , & commandListDescription , & commandList ) ; 933if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 934 935launchParams -> commandList = & commandList ; 936for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 937resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 938if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 939resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 940if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 941} 942res = zeCommandListClose ( commandList ) ; 943if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER ; 944 945std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 946res = zeCommandQueueExecuteCommandLists ( vkGPU -> commandQueue , 1 , & commandList , 0 ) ; 947if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE ; 948res = zeCommandQueueSynchronize ( vkGPU -> commandQueue , UINT32_MAX ) ; 949if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES ; 950 951std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 952double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 953time_result [ 0 ] = totTime / num_iter ; 954res = zeCommandListDestroy ( commandList ) ; 955if ( res != 0 ) return VKFFT_ERROR_FAILED_TO_DESTROY_COMMAND_LIST ; 956#elif ( VKFFT_BACKEND == 5 ) 957MTL :: CommandBuffer * commandBuffer = vkGPU -> queue -> commandBuffer () ; 958if ( commandBuffer == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 959launchParams -> commandBuffer = commandBuffer ; 960MTL :: ComputeCommandEncoder * commandEncoder = commandBuffer -> computeCommandEncoder () ; 961if ( commandEncoder == 0 ) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST ; 962launchParams -> commandEncoder = commandEncoder ; 963for ( uint64_t i = 0 ; i < num_iter ; i ++ ) { 964resFFT = VkFFTAppend ( app , - 1 , launchParams ) ; 965if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 966resFFT = VkFFTAppend ( app , 1 , launchParams ) ; 967if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 968} 969commandEncoder -> endEncoding () ; 970 971std :: chrono :: steady_clock :: time_point timeSubmit = std :: chrono :: steady_clock :: now () ; 972commandBuffer -> commit () ; 973commandBuffer -> waitUntilCompleted () ; 974std :: chrono :: steady_clock :: time_point timeEnd = std :: chrono :: steady_clock :: now () ; 975double totTime = std :: chrono :: duration_cast < std :: chrono :: microseconds > ( timeEnd - timeSubmit ) . count () * 0.001 ; 976time_result [ 0 ] = totTime / num_iter ; 977commandEncoder -> release () ; 978commandBuffer -> release () ; 979#endif 980return resFFT ; 981}