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#include "half.hpp" 46 47VkFFTResult user_benchmark_VkFFT ( VkGPU * vkGPU , uint64_t file_output , FILE * output , uint64_t isCompilerInitialized , VkFFTUserSystemParameters * userParams ) 48{ 49VkFFTResult resFFT = VKFFT_SUCCESS ; 50#if ( VKFFT_BACKEND == 0 ) 51VkResult res = VK_SUCCESS ; 52#elif ( VKFFT_BACKEND == 1 ) 53cudaError_t res = cudaSuccess ; 54#elif ( VKFFT_BACKEND == 2 ) 55hipError_t res = hipSuccess ; 56#elif ( VKFFT_BACKEND == 3 ) 57cl_int res = CL_SUCCESS ; 58#elif ( VKFFT_BACKEND == 4 ) 59ze_result_t res = ZE_RESULT_SUCCESS ; 60#endif 61const int num_runs = 3 ; 62double benchmark_result = 0 ; //averaged result = sum(system_size/iteration_time)/num_benchmark_samples 63//memory allocated on the CPU once, makes benchmark completion faster + avoids performance issues connected to frequent allocation/deallocation. 64uint64_t storageComplexSize = 8 ; 65switch ( userParams -> P ) { 66case 0 : 67storageComplexSize = ( 2 * sizeof ( float )) ; 68break ; 69case 1 : 70storageComplexSize = ( 2 * sizeof ( double )) ; 71break ; 72case 2 : 73storageComplexSize = ( 2 * 2 ) ; 74break ; 75default : 76storageComplexSize = ( 2 * sizeof ( float )) ; 77break ; 78} 79for ( uint64_t n = 0 ; n < 2 ; n ++ ) { 80double run_time [ num_runs ] ; 81for ( uint64_t r = 0 ; r < num_runs ; r ++ ) { 82//Configuration + FFT application . 83VkFFTConfiguration configuration = {} ; 84VkFFTApplication app = {} ; 85//FFT + iFFT sample code. 86//Setting up FFT configuration for forward and inverse FFT. 87configuration . FFTdim = 1 ; //FFT dimension, 1D, 2D or 3D (default 1). 88//if (n == 1) configuration.keepShaderCode = 1; 89 90configuration . size [ 0 ] = userParams -> X ; 91configuration . size [ 1 ] = userParams -> Y ; 92configuration . size [ 2 ] = userParams -> Z ; 93if ( userParams -> Y > 1 ) configuration . FFTdim ++ ; 94if ( userParams -> Z > 1 ) configuration . FFTdim ++ ; 95configuration . numberBatches = userParams -> B ; 96configuration . performR2C = userParams -> R2C ; 97configuration . performDCT = userParams -> DCT ; 98if ( userParams -> P == 1 ) configuration . doublePrecision = 1 ; 99if ( userParams -> P == 2 ) configuration . halfPrecision = 1 ; 100if ( userParams -> saveApplicationToString && ( n == 0 ) && ( r == 0 )) configuration . saveApplicationToString = 1 ; 101if ( userParams -> loadApplicationFromString || ( userParams -> saveApplicationToString && (( n != 0 ) || ( r != 0 )))) configuration . loadApplicationFromString = 1 ; 102//After this, configuration file contains pointers to Vulkan objects needed to work with the GPU: VkDevice* device - created device, [uint64_t *bufferSize, VkBuffer *buffer, VkDeviceMemory* bufferDeviceMemory] - allocated GPU memory FFT is performed on. [uint64_t *kernelSize, VkBuffer *kernel, VkDeviceMemory* kernelDeviceMemory] - allocated GPU memory, where kernel for convolution is stored. 103configuration . device = & vkGPU -> device ; 104#if ( VKFFT_BACKEND == 0 ) 105configuration . queue = & vkGPU -> queue ; //to allocate memory for LUT, we have to pass a queue, vkGPU->fence, commandPool and physicalDevice pointers 106configuration . fence = & vkGPU -> fence ; 107configuration . commandPool = & vkGPU -> commandPool ; 108configuration . physicalDevice = & vkGPU -> physicalDevice ; 109configuration . isCompilerInitialized = isCompilerInitialized ; //compiler can be initialized before VkFFT plan creation. if not, VkFFT will create and destroy one after initialization 110#elif ( VKFFT_BACKEND == 3 ) 111configuration . context = & vkGPU -> context ; 112#elif ( VKFFT_BACKEND == 4 ) 113configuration . context = & vkGPU -> context ; 114configuration . commandQueue = & vkGPU -> commandQueue ; 115configuration . commandQueueID = vkGPU -> commandQueueID ; 116#endif 117//Allocate buffer for the input data. 118uint64_t bufferSize = 0 ; 119if ( userParams -> R2C ) { 120bufferSize = ( uint64_t )( storageComplexSize / 2 ) * ( configuration . size [ 0 ] + 2 ) * configuration . size [ 1 ] * configuration . size [ 2 ] * configuration . numberBatches ; 121} 122else { 123if ( userParams -> DCT ) { 124bufferSize = ( uint64_t )( storageComplexSize / 2 ) * configuration . size [ 0 ] * configuration . size [ 1 ] * configuration . size [ 2 ] * configuration . numberBatches ; 125} 126else { 127bufferSize = ( uint64_t ) storageComplexSize * configuration . size [ 0 ] * configuration . size [ 1 ] * configuration . size [ 2 ] * configuration . numberBatches ; 128} 129} 130#if ( VKFFT_BACKEND == 0 ) 131VkBuffer buffer = {} ; 132VkDeviceMemory bufferDeviceMemory = {} ; 133resFFT = allocateBuffer ( vkGPU , & buffer , & bufferDeviceMemory , VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT , VK_MEMORY_HEAP_DEVICE_LOCAL_BIT , bufferSize ) ; 134if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 135configuration . buffer = & buffer ; 136#elif ( VKFFT_BACKEND == 1 ) 137cuFloatComplex * buffer = 0 ; 138res = cudaMalloc (( void ** ) & buffer , bufferSize ) ; 139if ( res != cudaSuccess ) return VKFFT_ERROR_FAILED_TO_ALLOCATE ; 140configuration . buffer = ( void ** ) & buffer ; 141#elif ( VKFFT_BACKEND == 2 ) 142hipFloatComplex * buffer = 0 ; 143res = hipMalloc (( void ** ) & buffer , bufferSize ) ; 144if ( res != hipSuccess ) return VKFFT_ERROR_FAILED_TO_ALLOCATE ; 145configuration . buffer = ( void ** ) & buffer ; 146#elif ( VKFFT_BACKEND == 3 ) 147cl_mem buffer = 0 ; 148buffer = clCreateBuffer ( vkGPU -> context , CL_MEM_READ_WRITE , bufferSize , 0 , & res ) ; 149if ( res != CL_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE ; 150configuration . buffer = & buffer ; 151#elif ( VKFFT_BACKEND == 4 ) 152void * buffer = 0 ; 153ze_device_mem_alloc_desc_t device_desc = {} ; 154device_desc . stype = ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC ; 155res = zeMemAllocDevice ( vkGPU -> context , & device_desc , bufferSize , sizeof ( float ) , vkGPU -> device , & buffer ) ; 156if ( res != ZE_RESULT_SUCCESS ) return VKFFT_ERROR_FAILED_TO_ALLOCATE ; 157configuration . buffer = & buffer ; 158#endif 159 160configuration . bufferSize = & bufferSize ; 161if ( configuration . loadApplicationFromString ) { 162FILE * kernelCache ; 163uint64_t str_len ; 164char fname [ 500 ] ; 165int VkFFT_version = VkFFTGetVersion () ; 166sprintf ( fname , "VkFFT_binary_X%" PRIu64 "_Y%" PRIu64 "_Z%" PRIu64 "_P%" PRIu64 "_B%" PRIu64 "_N%" PRIu64 "_R2C%" PRIu64 "_DCT%" PRIu64 "_ver%d" , userParams -> X , userParams -> Y , userParams -> Z , userParams -> P , userParams -> B , userParams -> N , userParams -> R2C , userParams -> DCT , VkFFT_version ) ; 167#if (( VKFFT_BACKEND == 0 ) || ( VKFFT_BACKEND == 2 ) || ( VKFFT_BACKEND == 4 )) 168kernelCache = fopen ( fname , "rb" ) ; //Vulkan and HIP backends load data as a uint32_t sequence 169#else 170kernelCache = fopen ( fname , "r" ) ; 171#endif 172if ( ! kernelCache ) return VKFFT_ERROR_EMPTY_FILE ; 173fseek ( kernelCache , 0 , SEEK_END ) ; 174str_len = ftell ( kernelCache ) ; 175fseek ( kernelCache , 0 , SEEK_SET ) ; 176configuration . loadApplicationString = malloc ( str_len ) ; 177fread ( configuration . loadApplicationString , str_len , 1 , kernelCache ) ; 178fclose ( kernelCache ) ; 179} 180//Initialize applications. This function loads shaders, creates pipeline and configures FFT based on configuration file. No buffer allocations inside VkFFT library. 181resFFT = initializeVkFFT ( & app , configuration ) ; 182if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 183 184if ( configuration . loadApplicationFromString ) 185free ( configuration . loadApplicationString ) ; 186 187if ( configuration . saveApplicationToString ) { 188FILE * kernelCache ; 189char fname [ 500 ] ; 190int VkFFT_version = VkFFTGetVersion () ; 191sprintf ( fname , "VkFFT_binary_X%" PRIu64 "_Y%" PRIu64 "_Z%" PRIu64 "_P%" PRIu64 "_B%" PRIu64 "_N%" PRIu64 "_R2C%" PRIu64 "_DCT%" PRIu64 "_ver%d" , userParams -> X , userParams -> Y , userParams -> Z , userParams -> P , userParams -> B , userParams -> N , userParams -> R2C , userParams -> DCT , VkFFT_version ) ; 192#if (( VKFFT_BACKEND == 0 ) || ( VKFFT_BACKEND == 2 ) || ( VKFFT_BACKEND == 4 )) 193kernelCache = fopen ( fname , "wb" ) ; //Vulkan and HIP backends save data as a uint32_t sequence 194#else 195kernelCache = fopen ( fname , "w" ) ; 196#endif 197fwrite ( app . saveApplicationString , app . applicationStringSize , 1 , kernelCache ) ; 198fclose ( kernelCache ) ; 199} 200 201//Submit FFT+iFFT. 202 203double totTime = 0 ; 204 205VkFFTLaunchParams launchParams = {} ; 206resFFT = performVulkanFFTiFFT ( vkGPU , & app , & launchParams , userParams -> N , & totTime ) ; 207if ( resFFT != VKFFT_SUCCESS ) return resFFT ; 208run_time [ r ] = totTime ; 209if ( n > 0 ) { 210if ( r == num_runs - 1 ) { 211double std_error = 0 ; 212double avg_time = 0 ; 213for ( uint64_t t = 0 ; t < num_runs ; t ++ ) { 214avg_time += run_time [ t ] ; 215} 216avg_time /= num_runs ; 217for ( uint64_t t = 0 ; t < num_runs ; t ++ ) { 218std_error += ( run_time [ t ] - avg_time ) * ( run_time [ t ] - avg_time ) ; 219} 220std_error = sqrt ( std_error / num_runs ) ; 221uint64_t num_tot_transfers = 0 ; 222for ( uint64_t i = 0 ; i < configuration . FFTdim ; i ++ ) 223num_tot_transfers += app . localFFTPlan -> numAxisUploads [ i ] ; 224num_tot_transfers *= 4 ; 225if ( file_output ) 226fprintf ( output , "VkFFT System: %" PRIu64 "x%" PRIu64 "x%" PRIu64 " Batch: %" PRIu64 " Buffer: %" PRIu64 " MB avg_time_per_step: %0.3f ms std_error: %0.3f num_iter: %" PRIu64 " benchmark: %" PRIu64 " scaled bandwidth: %0.1f bandwidth: %0.1f \n " , configuration . size [ 0 ] , configuration . size [ 1 ] , configuration . size [ 2 ] , userParams -> B , bufferSize / 1024 / 1024 , avg_time , std_error , userParams -> N , ( uint64_t )((( double ) bufferSize / 1024 ) / avg_time ) , bufferSize / 1024.0 / 1024.0 / 1.024 * 4 * configuration . FFTdim / avg_time , bufferSize / 1024.0 / 1024.0 / 1.024 * num_tot_transfers / avg_time ) ; 227 228printf ( "VkFFT System: %" PRIu64 "x%" PRIu64 "x%" PRIu64 " Batch: %" PRIu64 " Buffer: %" PRIu64 " MB avg_time_per_step: %0.3f ms std_error: %0.3f num_iter: %" PRIu64 " benchmark: %" PRIu64 " scaled bandwidth: %0.1f real bandwidth: %0.1f \n " , configuration . size [ 0 ] , configuration . size [ 1 ] , configuration . size [ 2 ] , userParams -> B , bufferSize / 1024 / 1024 , avg_time , std_error , userParams -> N , ( uint64_t )((( double ) bufferSize / 1024 ) / avg_time ) , bufferSize / 1024.0 / 1024.0 / 1.024 * 4 * configuration . FFTdim / avg_time , bufferSize / 1024.0 / 1024.0 / 1.024 * num_tot_transfers / avg_time ) ; 229benchmark_result += (( double ) bufferSize / 1024 ) / avg_time ; 230} 231 232 233} 234 235#if ( VKFFT_BACKEND == 0 ) 236vkDestroyBuffer ( vkGPU -> device , buffer , NULL ) ; 237vkFreeMemory ( vkGPU -> device , bufferDeviceMemory , NULL ) ; 238#elif ( VKFFT_BACKEND == 1 ) 239cudaFree ( buffer ) ; 240#elif ( VKFFT_BACKEND == 2 ) 241hipFree ( buffer ) ; 242#elif ( VKFFT_BACKEND == 3 ) 243clReleaseMemObject ( buffer ) ; 244#elif ( VKFFT_BACKEND == 4 ) 245zeMemFree ( vkGPU -> context , buffer ) ; 246#endif 247 248deleteVkFFT ( & app ) ; 249 250} 251} 252return resFFT ; 253}