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