### cuFFT Benchmark Setup (CMake) Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt Configures the build for cuFFT benchmarking, including enabling CUDA, finding cuFFT, and compiling benchmark CUDA source files. Supports various precision modes and CUDA architectures. ```cmake if(build_VkFFT_cuFFT_benchmark) add_definitions(-DUSE_cuFFT) find_package(CUDA 9.0 REQUIRED) enable_language(CUDA) if(build_VkFFT_FFTW_precision) add_library(cuFFT_scripts STATIC benchmark_scripts/cuFFT_scripts/src/user_benchmark_cuFFT.cu benchmark_scripts/cuFFT_scripts/src/sample_0_benchmark_cuFFT_single.cu benchmark_scripts/cuFFT_scripts/src/sample_1_benchmark_cuFFT_double.cu benchmark_scripts/cuFFT_scripts/src/sample_2_benchmark_cuFFT_half.cu benchmark_scripts/cuFFT_scripts/src/sample_3_benchmark_cuFFT_single_3d.cu benchmark_scripts/cuFFT_scripts/src/sample_6_benchmark_cuFFT_single_r2c.cu benchmark_scripts/cuFFT_scripts/src/sample_7_benchmark_cuFFT_single_Bluestein.cu benchmark_scripts/cuFFT_scripts/src/sample_8_benchmark_cuFFT_double_Bluestein.cu benchmark_scripts/cuFFT_scripts/src/sample_1000_benchmark_cuFFT_single_2_4096.cu benchmark_scripts/cuFFT_scripts/src/sample_1001_benchmark_cuFFT_double_2_4096.cu benchmark_scripts/cuFFT_scripts/src/sample_1003_benchmark_cuFFT_single_3d_2_512.cu benchmark_scripts/cuFFT_scripts/src/precision_cuFFT_single.cu benchmark_scripts/cuFFT_scripts/src/precision_cuFFT_r2c.cu benchmark_scripts/cuFFT_scripts/src/precision_cuFFT_double.cu benchmark_scripts/cuFFT_scripts/src/precision_cuFFT_half.cu) else() add_library(cuFFT_scripts STATIC benchmark_scripts/cuFFT_scripts/src/user_benchmark_cuFFT.cu benchmark_scripts/cuFFT_scripts/src/sample_0_benchmark_cuFFT_single.cu benchmark_scripts/cuFFT_scripts/src/sample_1_benchmark_cuFFT_double.cu benchmark_scripts/cuFFT_scripts/src/sample_2_benchmark_cuFFT_half.cu benchmark_scripts/cuFFT_scripts/src/sample_3_benchmark_cuFFT_single_3d.cu benchmark_scripts/cuFFT_scripts/src/sample_6_benchmark_cuFFT_single_r2c.cu benchmark_scripts/cuFFT_scripts/src/sample_7_benchmark_cuFFT_single_Bluestein.cu benchmark_scripts/cuFFT_scripts/src/sample_8_benchmark_cuFFT_double_Bluestein.cu benchmark_scripts/cuFFT_scripts/src/sample_1000_benchmark_cuFFT_single_2_4096.cu benchmark_scripts/cuFFT_scripts/src/sample_1001_benchmark_cuFFT_double_2_4096.cu benchmark_scripts/cuFFT_scripts/src/sample_1003_benchmark_cuFFT_single_3d_2_512.cu) endif() set_property(TARGET cuFFT_scripts PROPERTY CUDA_ARCHITECTURES 60 70 75 80 86) CUDA_ADD_CUFFT_TO_TARGET(cuFFT_scripts) target_compile_options(cuFFT_scripts PRIVATE "$<$:SHELL: -std=c++11 -gencode arch=compute_60,code=compute_60 -gencode arch=compute_70,code=compute_70 -gencode arch=compute_75,code=compute_75 -gencode arch=compute_80,code=compute_80 -gencode arch=compute_86,code=compute_86>") target_include_directories(cuFFT_scripts PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/benchmark_scripts/cuFFT_scripts/include) target_include_directories(cuFFT_scripts PUBLIC ${CUDA_INCLUDE_DIRS}) set_target_properties(cuFFT_scripts PROPERTIES CUDA_SEPARABLE_COMPILATION ON) set_target_properties(cuFFT_scripts PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON) target_link_libraries(${PROJECT_NAME} PUBLIC cuFFT_scripts) endif() ``` -------------------------------- ### Configure sampler state Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Shows the setup of a sampler descriptor and state creation, comparing Objective-C and C++ syntax. ```objective-c MTLSamplerDescriptor* samplerDescriptor = [[MTLSamplerDescriptor alloc] init]; [samplerDescriptor setSAddressMode: MTLSamplerAddressModeRepeat]; [samplerDescriptor setSupportArgumentBuffers: YES]; id< MTLSamplerState > samplerState = [device newSamplerStateWithDescriptor:samplerDescriptor]; ``` ```objective-c MTLSamplerDescriptor* samplerDescriptor = [[MTLSamplerDescriptor alloc] init]; [samplerDescriptor setSAddressMode: MTLSamplerAddressModeRepeat]; [samplerDescriptor setSupportArgumentBuffers: YES]; id< MTLSamplerState > samplerState = [device newSamplerStateWithDescriptor:samplerDescriptor]; [samplerDescriptor release]; [samplerState release]; ``` ```cpp MTL::SamplerDescriptor* pSamplerDescriptor = MTL::SamplerDescriptor::alloc()->init(); pSamplerDescriptor->setSAddressMode( MTL::SamplerAddressModeRepeat ); pSamplerDescriptor->setSupportArgumentBuffers( true ); MTL::SamplerState* pSamplerState = pDevice->newSamplerState( pSamplerDescriptor ); pSamplerDescriptor->release(); pSamplerState->release(); ``` -------------------------------- ### rocFFT Benchmark Setup (CMake) Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt Configures the build for rocFFT benchmarking, including finding HIP and hipfft, and compiling benchmark C++ source files. Supports different precision modes. ```cmake if(build_VkFFT_rocFFT_benchmark) add_definitions(-DUSE_rocFFT) list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hip /opt/rocm) find_package(hip) find_package(hipfft) if(build_VkFFT_FFTW_precision) add_library(rocFFT_scripts STATIC benchmark_scripts/rocFFT_scripts/src/user_benchmark_rocFFT.cpp benchmark_scripts/rocFFT_scripts/src/sample_0_benchmark_rocFFT_single.cpp benchmark_scripts/rocFFT_scripts/src/sample_1_benchmark_rocFFT_double.cpp ) ``` -------------------------------- ### Save and Load Compiled Kernels in VkFFT (C) Source: https://context7.com/dtolm/vkfft/llms.txt Provides a method for saving and loading compiled GPU kernels in VkFFT to persistent storage. This avoids the overhead of recompiling kernels on subsequent runs, significantly speeding up application startup. The example shows saving the application string on the first run and loading it on subsequent runs. ```c VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; configuration.FFTdim = 2; configuration.size[0] = 1024; configuration.size[1] = 1024; configuration.device = &device; // First run: save compiled kernels configuration.saveApplicationToString = 1; uint64_t bufferSize = sizeof(float) * 2 * configuration.size[0] * configuration.size[1]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; initializeVkFFT(&app, configuration); // Save to file FILE* cache = fopen("vkfft_cache.bin", "wb"); fwrite(app.saveApplicationString, app.applicationStringSize, 1, cache); fclose(cache); deleteVkFFT(&app); // Subsequent runs: load from cache configuration.saveApplicationToString = 0; configuration.loadApplicationFromString = 1; FILE* cacheRead = fopen("vkfft_cache.bin", "rb"); fseek(cacheRead, 0, SEEK_END); uint64_t cacheSize = ftell(cacheRead); fseek(cacheRead, 0, SEEK_SET); configuration.loadApplicationString = malloc(cacheSize); fread(configuration.loadApplicationString, cacheSize, 1, cacheRead); fclose(cacheRead); initializeVkFFT(&app, configuration); free(configuration.loadApplicationString); // Use the pre-compiled plan VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app, -1, &launchParams); deleteVkFFT(&app); cudaFree(buffer); ``` -------------------------------- ### Discrete Cosine Transform (DCT) with VkFFT Source: https://context7.com/dtolm/vkfft/llms.txt Illustrates how to configure and perform Discrete Cosine Transforms (DCT) using VkFFT. This function supports various DCT types (I, II, III, IV) and is suitable for applications like image and signal compression. The example shows a DCT-II forward transform and a DCT-III inverse transform, with an option to normalize the inverse transform. ```c VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; // DCT-II configuration (most common, used in JPEG) configuration.FFTdim = 2; configuration.size[0] = 512; configuration.size[1] = 512; configuration.performDCT = 2; // DCT type (1, 2, 3, or 4) configuration.normalize = 1; // Normalize inverse transform configuration.device = &device; // DCT uses real values only uint64_t bufferSize = sizeof(float) * configuration.size[0] * configuration.size[1]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; initializeVkFFT(&app, configuration); VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app, -1, &launchParams); // Forward DCT-II VkFFTAppend(&app, 1, &launchParams); // Inverse DCT-III deleteVkFFT(&app); cudaFree(buffer); ``` -------------------------------- ### VkFFT Error Handling with VkFFTResult (C) Source: https://context7.com/dtolm/vkfft/llms.txt Demonstrates how to handle errors returned by VkFFT functions using the `VkFFTResult` enumeration. The example shows a switch statement to check for common error codes and provides a fallback to a generic error string function for unhandled errors. ```c #include "vkFFT.h" VkFFTResult result = initializeVkFFT(&app, configuration); switch (result) { case VKFFT_SUCCESS: printf("Success\n"); break; case VKFFT_ERROR_MALLOC_FAILED: printf("Memory allocation failed\n"); break; case VKFFT_ERROR_INVALID_DEVICE: printf("Invalid GPU device\n"); break; case VKFFT_ERROR_UNSUPPORTED_FFT_LENGTH: printf("FFT length not supported\n"); break; case VKFFT_ERROR_UNSUPPORTED_RADIX: printf("Unsupported radix in FFT decomposition\n"); break; case VKFFT_ERROR_FAILED_TO_COMPILE_PROGRAM: printf("Shader compilation failed\n"); break; default: printf("Error: %s\n", getVkFFTErrorString(result)); break; } ``` -------------------------------- ### Initialize VkFFT Application Source: https://context7.com/dtolm/vkfft/llms.txt Configures and initializes an FFT plan. This process involves setting dimensions, device context, and buffer pointers before compiling shaders. ```c #include "vkFFT.h" VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; configuration.FFTdim = 1; configuration.size[0] = 1024; configuration.numberBatches = 64; configuration.device = &cudaDevice; uint64_t bufferSize = sizeof(float) * 2 * configuration.size[0] * configuration.numberBatches; cuFloatComplex* buffer = NULL; cudaMalloc((void**)&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; VkFFTResult result = initializeVkFFT(&app, configuration); if (result != VKFFT_SUCCESS) { printf("Error: %s\n", getVkFFTErrorString(result)); return -1; } ``` -------------------------------- ### initializeVkFFT Source: https://context7.com/dtolm/vkfft/llms.txt Initializes and configures an FFT plan, compiling shaders and setting up execution strategies. ```APIDOC ## initializeVkFFT ### Description Creates and configures an FFT plan based on the provided configuration. This function compiles GPU shaders, creates pipelines, and optimizes the FFT execution strategy. ### Method Function Call ### Parameters #### Request Body - **configuration** (VkFFTConfiguration) - Required - Struct containing FFT dimensions, device pointers, and buffer information. ### Response #### Success Response (VKFFT_SUCCESS) - **result** (VkFFTResult) - Returns VKFFT_SUCCESS on successful initialization. ### Request Example VkFFTConfiguration configuration = {}; configuration.FFTdim = 1; configuration.size[0] = 1024; configuration.device = &cudaDevice; VkFFTResult result = initializeVkFFT(&app, configuration); ``` -------------------------------- ### Create Metal device Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Demonstrates how to initialize the system default Metal device across different environments and memory management models. ```objective-c id< MTLDevice > device = MTLCreateSystemDefaultDevice(); ``` ```objective-c id< MTLDevice > device = MTLCreateSystemDefaultDevice(); [device release]; ``` ```cpp MTL::Device* pDevice = MTL::CreateSystemDefaultDevice(); pDevice->release(); ``` -------------------------------- ### Enable Zero Padding for Open Systems in VkFFT (C) Source: https://context7.com/dtolm/vkfft/llms.txt Demonstrates how to enable and configure native zero padding for modeling open boundary conditions in VkFFT. This feature can significantly improve performance compared to manual padding. It involves setting configuration parameters for zero padding and defining the padded regions. ```c VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; configuration.FFTdim = 3; configuration.size[0] = 256; configuration.size[1] = 256; configuration.size[2] = 256; configuration.device = &device; // Enable zero padding on specific axes configuration.performZeropadding[0] = 1; configuration.performZeropadding[1] = 1; configuration.performZeropadding[2] = 1; // Define zero-padded regions (values outside this range are treated as zero) configuration.fft_zeropad_left[0] = 0; configuration.fft_zeropad_right[0] = 128; // Only first 128 elements are non-zero configuration.fft_zeropad_left[1] = 0; configuration.fft_zeropad_right[1] = 128; configuration.fft_zeropad_left[2] = 0; configuration.fft_zeropad_right[2] = 128; uint64_t bufferSize = sizeof(float) * 2 * configuration.size[0] * configuration.size[1] * configuration.size[2]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; initializeVkFFT(&app, configuration); VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app, -1, &launchParams); deleteVkFFT(&app); cudaFree(buffer); ``` -------------------------------- ### Basic String Operations in Objective-C and C++ Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Demonstrates how to create and print strings using Objective-C and C++ with NSAutoreleasePool. It shows basic string manipulation and output. ```Objective-C NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSString* string = [NSString stringWithCString: "Hello World" encoding: NSASCIIStringEncoding]; printf( "string = \"%s\"\n", [string cStringUsingEncoding: NSASCIIStringEncoding] ); [pool release]; ``` ```C++ NS::AutoreleasePool* pPool = NS::AutoreleasePool::alloc()->init(); NS::String* pString = NS::String::string( "Hello World", NS::ASCIIStringEncoding ); printf( "pString = \"%s\"\n", pString->cString( NS::ASCIIStringEncoding ) ); pPool->release(); ``` -------------------------------- ### Configure metal-cpp project implementation Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Defines the necessary macros to include and implement Metal and QuartzCore headers in a C++ project. ```cpp #define NS_PRIVATE_IMPLEMENTATION #define MTL_PRIVATE_IMPLEMENTATION #include "Metal/Metal.hpp" #define CA_PRIVATE_IMPLEMENTATION #include "QuartzCore/QuartzCore.hpp" ``` -------------------------------- ### Zero Padding Configuration Source: https://context7.com/dtolm/vkfft/llms.txt Configure VkFFT to perform zero padding on specific axes to model open boundary conditions efficiently. ```APIDOC ## Zero Padding Configuration ### Description Enables native zero padding support for modeling open boundary conditions, allowing users to define non-zero regions within the FFT buffer. ### Parameters #### Configuration Fields - **performZeropadding** (int[3]) - Required - Array to enable/disable padding on each axis. - **fft_zeropad_left** (uint64_t[3]) - Required - Starting index for non-zero data per axis. - **fft_zeropad_right** (uint64_t[3]) - Required - Ending index for non-zero data per axis. ### Request Example ```c configuration.performZeropadding[0] = 1; configuration.fft_zeropad_left[0] = 0; configuration.fft_zeropad_right[0] = 128; ``` ``` -------------------------------- ### VkFFT Backend Linking Configuration (CMake) Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt Configures the libraries to link against based on the selected VkFFT backend. Supports HIP, OpenCL, Zeon, and Metal backends, along with the 'half' precision library. ```cmake target_link_libraries(${PROJECT_NAME} PUBLIC hip::host VkFFT half) elseif(${VKFFT_BACKEND} EQUAL 3) target_link_libraries(${PROJECT_NAME} PUBLIC OpenCL::OpenCL VkFFT half) elseif(${VKFFT_BACKEND} EQUAL 4) target_link_libraries(${PROJECT_NAME} PUBLIC ze_loader VkFFT half) elseif(${VKFFT_BACKEND} EQUAL 5) target_link_libraries(${PROJECT_NAME} PUBLIC ${FOUNDATION_LIB} ${QUARTZ_CORE_LIB} ${METAL_LIB} VkFFT half) endif() ``` -------------------------------- ### Perform Double Precision FFT in VkFFT (C) Source: https://context7.com/dtolm/vkfft/llms.txt Illustrates how to configure and execute a Fast Fourier Transform (FFT) using double precision in VkFFT. This is useful for applications requiring higher accuracy. The code snippet shows enabling double precision and allocating the appropriate buffer size. ```c VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; configuration.FFTdim = 1; configuration.size[0] = 4096; configuration.doublePrecision = 1; // Enable double precision configuration.device = &device; // Double precision uses 16 bytes per complex value uint64_t bufferSize = sizeof(double) * 2 * configuration.size[0]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; initializeVkFFT(&app, configuration); VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app, -1, &launchParams); deleteVkFFT(&app); cudaFree(buffer); ``` -------------------------------- ### Configure VkFFT Backend and Dependencies Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt This CMake logic detects the selected backend (Vulkan, CUDA, HIP, OpenCL, Level Zero, or Metal) and sets the appropriate compiler flags, include directories, and linked libraries for the project. ```cmake add_definitions(-DVKFFT_BACKEND=${VKFFT_BACKEND}) if(${VKFFT_BACKEND} EQUAL 0) find_package(Vulkan REQUIRED) elseif(${VKFFT_BACKEND} EQUAL 1) find_package(CUDA 9.0 REQUIRED) enable_language(CUDA) elseif(${VKFFT_BACKEND} EQUAL 3) find_package(OpenCL REQUIRED) elseif(${VKFFT_BACKEND} EQUAL 5) find_library(METAL_LIB Metal REQUIRED) target_include_directories(${PROJECT_NAME} PUBLIC "metal-cpp/") endif() ``` -------------------------------- ### FFTW Integration for VkFFT (CMake) Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt Enables and configures FFTW integration for VkFFT, including finding FFTW libraries and include directories. Supports different FFTW precision modes (single, double, quad). ```cmake if(build_VkFFT_FFTW_precision OR VkFFT_use_FP128_Bluestein_RaderFFT) add_definitions(-DUSE_FFTW) set(FFTW3_LIB_DIR "/usr/lib/x86_64-linux-gnu/") set(FFTW3_INCLUDE_DIR "/usr/include/") find_library( FFTW_LIB NAMES "libfftw3-3" "fftw3" PATHS ${FFTW3_LIB_DIR} PATH_SUFFIXES "lib" "lib64" NO_DEFAULT_PATH ) find_path( FFTW_INCLUDES NAMES "fftw3.h" PATHS ${FFTW3_INCLUDE_DIR} PATH_SUFFIXES "include" NO_DEFAULT_PATH ) target_include_directories(${PROJECT_NAME} PUBLIC ${FFTW_INCLUDES}) if(VkFFT_use_FP128_double_double) find_library( FFTWQ_LIB NAMES "libfftw3q" "fftw3q" PATHS ${FFTW3_LIB_DIR} PATH_SUFFIXES "lib" "lib64" NO_DEFAULT_PATH ) target_link_libraries (${PROJECT_NAME} PUBLIC ${FFTWQ_LIB}) endif() if(VkFFT_use_FP128_Bluestein_RaderFFT) find_library( FFTWL_LIB NAMES "libfftw3l" "fftw3l" PATHS ${FFTW3_LIB_DIR} PATH_SUFFIXES "lib" "lib64" NO_DEFAULT_PATH ) target_link_libraries (${PROJECT_NAME} PUBLIC ${FFTWL_LIB}) endif() target_link_libraries (${PROJECT_NAME} PUBLIC ${FFTW_LIB}) endif() ``` -------------------------------- ### Complex-to-Complex (C2C) Transform Source: https://context7.com/dtolm/vkfft/llms.txt Demonstrates a 2D C2C FFT configuration, initialization, and execution cycle. ```c VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; configuration.FFTdim = 2; configuration.size[0] = 512; configuration.size[1] = 512; configuration.device = &device; uint64_t bufferSize = sizeof(float) * 2 * configuration.size[0] * configuration.size[1]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; initializeVkFFT(&app, configuration); VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app, -1, &launchParams); VkFFTAppend(&app, 1, &launchParams); deleteVkFFT(&app); cudaFree(buffer); ``` -------------------------------- ### Convolution with Matrix Kernel using VkFFT Source: https://context7.com/dtolm/vkfft/llms.txt This code snippet demonstrates how to perform FFT-based convolution with matrix kernels (1x1, 2x2, 3x3) using VkFFT. It involves configuring a kernel FFT, then configuring the convolution itself, specifying matrix dimensions and kernel properties. The process includes initializing both the kernel and the convolution applications, executing the convolution (which internally performs FFT, multiplication, and inverse FFT), and finally cleaning up resources. ```c VkFFTConfiguration kernel_config = {}; VkFFTConfiguration conv_config = {}; VkFFTApplication app_kernel = {}; VkFFTApplication app_conv = {}; // Step 1: Configure kernel FFT kernel_config.FFTdim = 1; kernel_config.size[0] = 8 * 1024 * 1024; kernel_config.kernelConvolution = 1; // This is a kernel kernel_config.coordinateFeatures = 9; // 3x3 matrix = 9 components kernel_config.normalize = 1; kernel_config.device = &device; uint64_t kernelSize = kernel_config.coordinateFeatures * sizeof(float) * 2 * kernel_config.size[0]; void* kernel = NULL; cudaMalloc(&kernel, kernelSize); kernel_config.buffer = (void**)&kernel; kernel_config.bufferSize = &kernelSize; // Initialize kernel and optionally transform it initializeVkFFT(&app_kernel, kernel_config); // Step 2: Configure convolution conv_config = kernel_config; conv_config.kernelConvolution = 0; conv_config.performConvolution = 1; // Enable convolution conv_config.matrixConvolution = 3; // 3x3 matrix convolution conv_config.coordinateFeatures = 3; // 3D vector input conv_config.symmetricKernel = 0; // Non-symmetric kernel conv_config.kernel = (void**)&kernel; conv_config.kernelSize = &kernelSize; uint64_t bufferSize = conv_config.coordinateFeatures * sizeof(float) * 2 * conv_config.size[0]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); conv_config.buffer = (void**)&buffer; conv_config.bufferSize = &bufferSize; initializeVkFFT(&app_conv, conv_config); // Execute convolution (FFT -> multiply -> iFFT in one step) VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app_conv, -1, &launchParams); deleteVkFFT(&app_kernel); deleteVkFFT(&app_conv); cudaFree(kernel); cudaFree(buffer); ``` -------------------------------- ### Configure rocFFT Benchmark Library in CMake Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt This snippet demonstrates how to define a static library in CMake, include necessary headers, and link against HIP and rocFFT libraries. It ensures that benchmark source files are compiled and linked correctly into the project build process. ```cmake add_library(rocFFT_scripts STATIC benchmark_scripts/rocFFT_scripts/src/user_benchmark_rocFFT.cpp benchmark_scripts/rocFFT_scripts/src/sample_0_benchmark_rocFFT_single.cpp benchmark_scripts/rocFFT_scripts/src/sample_1_benchmark_rocFFT_double.cpp ) target_include_directories(rocFFT_scripts PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/benchmark_scripts/rocFFT_scripts/include) target_link_libraries(rocFFT_scripts PRIVATE hip::host hip::hipfft) target_link_libraries(${PROJECT_NAME} PUBLIC rocFFT_scripts) ``` -------------------------------- ### Execute FFT Transform Source: https://context7.com/dtolm/vkfft/llms.txt Appends FFT or inverse FFT operations to the command buffer using the initialized application instance. ```c #include "vkFFT.h" VkFFTLaunchParams launchParams = {}; VkFFTResult result = VkFFTAppend(&app, -1, &launchParams); if (result != VKFFT_SUCCESS) { printf("FFT execution failed: %s\n", getVkFFTErrorString(result)); } result = VkFFTAppend(&app, 1, &launchParams); if (result != VKFFT_SUCCESS) { printf("iFFT execution failed: %s\n", getVkFFTErrorString(result)); } ``` -------------------------------- ### Real-to-Complex (R2C) FFT Transform with VkFFT Source: https://context7.com/dtolm/vkfft/llms.txt Demonstrates the configuration and execution of a Real-to-Complex (R2C) Fast Fourier Transform using VkFFT. This function optimizes for real-valued input data, producing a half-spectrum complex output, resulting in reduced memory usage and improved performance compared to complex-to-complex transforms. It also shows the inverse Complex-to-Real (C2R) transform. ```c VkFFTConfiguration configuration = {}; VkFFTApplication app = {}; // R2C configuration configuration.FFTdim = 2; configuration.size[0] = 1920; // Real input width configuration.size[1] = 1080; // Height configuration.performR2C = 1; // Enable R2C transform configuration.device = &device; // R2C output size: (N/2 + 1) complex values per row uint64_t bufferSize = sizeof(float) * 2 * (configuration.size[0] / 2 + 1) * configuration.size[1]; void* buffer = NULL; cudaMalloc(&buffer, bufferSize); configuration.buffer = (void**)&buffer; configuration.bufferSize = &bufferSize; initializeVkFFT(&app, configuration); VkFFTLaunchParams launchParams = {}; VkFFTAppend(&app, -1, &launchParams); // Forward R2C VkFFTAppend(&app, 1, &launchParams); // Inverse C2R deleteVkFFT(&app); cudaFree(buffer); ``` -------------------------------- ### CMake Build Configuration for VkFFT Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt This CMake script configures the VkFFT project. It sets the minimum CMake version, project name, build type, and handles fetching external dependencies like glslang. It also defines options for selecting the backend (Vulkan, CUDA, HIP, etc.) and enabling various benchmarks and precision tests. ```cmake cmake_minimum_required(VERSION 3.11) project(VkFFT_TestSuite) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "" FORCE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE) endif() if (NOT DEFINED GLSLANG_GIT_TAG) set(GLSLANG_GIT_TAG "12.3.1") endif() include(FetchContent) set(VKFFT_BACKEND 0 CACHE STRING "0 - Vulkan, 1 - CUDA, 2 - HIP, 3 - OpenCL, 4 - Level Zero, 5 - Metal") if(${VKFFT_BACKEND} EQUAL 1) option(build_VkFFT_cuFFT_benchmark "Build VkFFT cuFFT benchmark" ON) else() option(build_VkFFT_cuFFT_benchmark "Build VkFFT cuFFT benchmark" OFF) endif() if(${VKFFT_BACKEND} EQUAL 2) option(build_VkFFT_rocFFT_benchmark "Build VkFFT rocFFT benchmark" ON) else() option(build_VkFFT_rocFFT_benchmark "Build VkFFT rocFFT benchmark" OFF) endif() option(build_VkFFT_FFTW_precision "Build VkFFT FFTW precision comparison" OFF) option(VkFFT_use_FP128_Bluestein_RaderFFT "Use LD for Bluestein and Rader FFT kernel calculations. Currently requires LD FFT library, like FFTWl, will be reworked" OFF) option(VkFFT_use_FP128_double_double "Build VkFFT quad double-double" OFF) if (MSVC) set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME}) add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() if(build_VkFFT_FFTW_precision) add_executable(${PROJECT_NAME} VkFFT_TestSuite.cpp benchmark_scripts/vkFFT_scripts/src/utils_VkFFT.cpp benchmark_scripts/vkFFT_scripts/src/user_benchmark_VkFFT.cpp benchmark_scripts/vkFFT_scripts/src/sample_0_benchmark_VkFFT_single.cpp benchmark_scripts/vkFFT_scripts/src/sample_1_benchmark_VkFFT_double.cpp benchmark_scripts/vkFFT_scripts/src/sample_2_benchmark_VkFFT_half.cpp benchmark_scripts/vkFFT_scripts/src/sample_3_benchmark_VkFFT_single_3d.cpp benchmark_scripts/vkFFT_scripts/src/sample_4_benchmark_VkFFT_single_3d_zeropadding.cpp benchmark_scripts/vkFFT_scripts/src/sample_5_benchmark_VkFFT_single_disableReorderFourStep.cpp benchmark_scripts/vkFFT_scripts/src/sample_6_benchmark_VkFFT_single_r2c.cpp benchmark_scripts/vkFFT_scripts/src/sample_7_benchmark_VkFFT_single_Bluestein.cpp benchmark_scripts/vkFFT_scripts/src/sample_8_benchmark_VkFFT_double_Bluestein.cpp benchmark_scripts/vkFFT_scripts/src/sample_9_benchmark_VkFFT_quadDoubleDouble.cpp benchmark_scripts/vkFFT_scripts/src/sample_10_benchmark_VkFFT_single_multipleBuffers.cpp benchmark_scripts/vkFFT_scripts/src/sample_11_precision_VkFFT_single.cpp benchmark_scripts/vkFFT_scripts/src/sample_12_precision_VkFFT_double.cpp benchmark_scripts/vkFFT_scripts/src/sample_13_precision_VkFFT_half.cpp benchmark_scripts/vkFFT_scripts/src/sample_14_precision_VkFFT_single_nonPow2.cpp benchmark_scripts/vkFFT_scripts/src/sample_15_precision_VkFFT_single_r2c.cpp benchmark_scripts/vkFFT_scripts/src/sample_16_precision_VkFFT_single_dct.cpp benchmark_scripts/vkFFT_scripts/src/sample_17_precision_VkFFT_double_dct.cpp benchmark_scripts/vkFFT_scripts/src/sample_18_precision_VkFFT_double_nonPow2.cpp benchmark_scripts/vkFFT_scripts/src/sample_19_precision_VkFFT_quadDoubleDouble_nonPow2.cpp benchmark_scripts/vkFFT_scripts/src/sample_50_convolution_VkFFT_single_1d_matrix.cpp benchmark_scripts/vkFFT_scripts/src/sample_51_convolution_VkFFT_single_3d_matrix_zeropadding_r2c.cpp benchmark_scripts/vkFFT_scripts/src/sample_52_convolution_VkFFT_single_2d_batched_r2c.cpp benchmark_scripts/vkFFT_scripts/src/sample_100_benchmark_VkFFT_single_nd_dct.cpp benchmark_scripts/vkFFT_scripts/src/sample_101_benchmark_VkFFT_double_nd_dct.cpp benchmark_scripts/vkFFT_scripts/src/sample_1000_benchmark_VkFFT_single_2_4096.cpp benchmark_scripts/vkFFT_scripts/src/sample_1001_benchmark_VkFFT_double_2_4096.cpp benchmark_scripts/vkFFT_scripts/src/sample_1002_benchmark_VkFFT_half_2_4096.cpp benchmark_scripts/vkFFT_scripts/src/sample_1003_benchmark_VkFFT_single_3d_2_512.cpp benchmark_scripts/vkFFT_scripts/src/sample_1004_benchmark_VkFFT_quadDoubleDouble_2_4096.cpp) else() add_executable(${PROJECT_NAME} VkFFT_TestSuite.cpp benchmark_scripts/vkFFT_scripts/src/utils_VkFFT.cpp benchmark_scripts/vkFFT_scripts/src/user_benchmark_VkFFT.cpp benchmark_scripts/vkFFT_scripts/src/sample_0_benchmark_VkFFT_single.cpp benchmark_scripts/vkFFT_scripts/src/sample_1_benchmark_VkFFT_double.cpp benchmark_scripts/vkFFT_scripts/src/sample_2_benchmark_VkFFT_half.cpp benchmark_scripts/vkFFT_scripts/src/sample_3_benchmark_VkFFT_single_3d.cpp benchmark_scripts/vkFFT_scripts/src/sample_4_benchmark_VkFFT_single_3d_zeropadding.cpp benchmark_scripts/vkFFT_scripts/src/sample_5_benchmark_VkFFT_single_disableReorderFourStep.cpp benchmark_scripts/vkFFT_scripts/src/sample_6_benchmark_VkFFT_single_r2c.cpp benchmark_scripts/vkFFT_scripts/src/sample_7_benchmark_VkFFT_single_Bluestein.cpp benchmark_scripts/vkFFT_scripts/src/sample_8_benchmark_VkFFT_double_Bluestein.cpp benchmark_scripts/vkFFT_scripts/src/sample_9_benchmark_VkFFT_quadDoubleDouble.cpp) endif() ``` -------------------------------- ### Interoperability with CoreFoundation in C++ Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Shows how to create a C++ acceleration structure descriptor and convert it into a CoreFoundation array. This is useful for integrating C++ objects with CoreFoundation APIs. ```C++ MTL::AccelerationStructureTriangleGeometryDescriptor* pGeoDescriptor = MTL::AccelerationStructureTriangleGeometryDescriptor::alloc()->init(); CFTypeRef descriptors[] = { ( CFTypeRef )( pGeoDescriptor ) }; NS::Array* pGeoDescriptors = ( NS::Array* )( CFArrayCreate( kCFAllocatorDefault, descriptors, SIZEOF_ARRAY( descriptors), &kCFTypeArrayCallBacks ) ); // ... pGeoDescriptors->release(); ``` -------------------------------- ### Retrieve Library Version Source: https://context7.com/dtolm/vkfft/llms.txt Returns the current library version as an integer, which can be parsed into major, minor, and patch components. ```c #include "vkFFT.h" int version = VkFFTGetVersion(); int major = version / 10000; int minor = (version - major * 10000) / 100; int patch = version - major * 10000 - minor * 100; printf("VkFFT v%d.%d.%d\n", major, minor, patch); ``` -------------------------------- ### Generate single header file for metal-cpp Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Uses the provided Python script to consolidate all metal-cpp headers into a single file. ```shell ./SingleHeader/MakeSingleHeader.py Foundation/Foundation.hpp QuartzCore/QuartzCore.hpp Metal/Metal.hpp ``` -------------------------------- ### Kernel Caching Source: https://context7.com/dtolm/vkfft/llms.txt Save and load compiled GPU kernels to optimize performance and reduce initialization time on subsequent runs. ```APIDOC ## Kernel Caching ### Description Allows caching of compiled GPU kernels to disk, preventing the need for expensive recompilation during application startup. ### Parameters #### Configuration Fields - **saveApplicationToString** (int) - Required - Set to 1 to export the compiled kernel string. - **loadApplicationFromString** (int) - Required - Set to 1 to import a previously saved kernel string. ### Request Example ```c configuration.saveApplicationToString = 1; // ... initialize and save string ... configuration.loadApplicationFromString = 1; configuration.loadApplicationString = buffer_from_file; ``` ``` -------------------------------- ### Configure CUDA Compilation Settings Source: https://github.com/dtolm/vkfft/blob/master/CMakeLists.txt Sets specific CUDA architectures and compilation flags for the project when the CUDA backend is selected. ```cmake set_property(TARGET ${PROJECT_NAME} PROPERTY CUDA_ARCHITECTURES 60 70 75 80 86) target_compile_options(${PROJECT_NAME} PUBLIC "$<$:SHELL: -std=c++11 -DVKFFT_BACKEND=${VKFFT_BACKEND} -gencode arch=compute_60,code=compute_60 -gencode arch=compute_70,code=compute_70 -gencode arch=compute_75,code=compute_75 -gencode arch=compute_80,code=compute_80 -gencode arch=compute_86,code=compute_86>") ``` -------------------------------- ### VkFFTAppend Source: https://context7.com/dtolm/vkfft/llms.txt Appends FFT or inverse FFT operations to the command buffer for execution. ```APIDOC ## VkFFTAppend ### Description Appends FFT or inverse FFT operations to the command buffer. The direction is controlled by the inverse parameter. ### Method Function Call ### Parameters - **app** (VkFFTApplication*) - Required - Pointer to the initialized VkFFT application. - **inverse** (int) - Required - Direction of the transform (-1 for forward, 1 for inverse). - **launchParams** (VkFFTLaunchParams*) - Required - Parameters for the execution, including command buffers for Vulkan. ### Response #### Success Response (VKFFT_SUCCESS) - **result** (VkFFTResult) - Returns VKFFT_SUCCESS on successful execution. ``` -------------------------------- ### Cleanup VkFFT Resources Source: https://context7.com/dtolm/vkfft/llms.txt Releases internal VkFFT resources such as pipelines and shaders. Users are responsible for freeing their own allocated GPU buffers. ```c #include "vkFFT.h" deleteVkFFT(&app); #if (VKFFT_BACKEND == 1) cudaFree(buffer); #elif (VKFFT_BACKEND == 2) hipFree(buffer); #elif (VKFFT_BACKEND == 3) clReleaseMemObject(buffer); #endif ``` -------------------------------- ### VkFFTGetVersion Source: https://context7.com/dtolm/vkfft/llms.txt Retrieves the current version of the VkFFT library. ```APIDOC ## VkFFTGetVersion ### Description Returns the VkFFT library version in X.XX.XX format encoded as an integer. ### Method Function Call ### Response - **version** (int) - The version integer where major, minor, and patch are encoded. ``` -------------------------------- ### Discrete Cosine Transform (DCT) Source: https://context7.com/dtolm/vkfft/llms.txt Configures and executes DCT transforms (types I-IV) for signal and image processing applications. ```APIDOC ## Discrete Cosine Transform (DCT) ### Description Supports real-to-real transforms including DCT types I, II, III, and IV, commonly used in compression algorithms like JPEG. ### Method C/C++ API (VkFFTConfiguration) ### Parameters - **performDCT** (int) - Required - Specifies the DCT type (1, 2, 3, or 4). - **normalize** (int) - Optional - Set to 1 to enable normalization for inverse transforms. ### Request Example ```c configuration.performDCT = 2; configuration.normalize = 1; ``` ``` -------------------------------- ### Accessing a CAMetalDrawable in Objective-C Source: https://github.com/dtolm/vkfft/blob/master/metal-cpp/README.md Illustrates how to obtain a CAMetalDrawable from a CAMetalLayer and cast it to a C++ MetalDrawable object. This is essential for Metal rendering. ```Objective-C #import #import // ... CAMetalLayer* metalLayer = /* get your layer from your view */; id< CAMetalDrawable > metalDrawable = [metalLayer nextDrawable]; CA::MetalDrawable* pMetalCppDrawable = ( __bridge CA::MetalDrawable* ) metalDrawable; // ... ``` -------------------------------- ### Real-to-Complex (R2C) Transform Source: https://context7.com/dtolm/vkfft/llms.txt Configures and executes an optimized FFT for real-valued input data, utilizing half-spectrum output for memory efficiency. ```APIDOC ## R2C Transform ### Description Performs an optimized FFT on real-valued input data, producing a half-spectrum complex output. This method reduces memory usage by 50% and improves performance compared to standard C2C transforms. ### Method C/C++ API (VkFFTConfiguration) ### Parameters - **FFTdim** (int) - Required - Number of dimensions (e.g., 2). - **size** (uint64_t[]) - Required - Dimensions of the input data. - **performR2C** (int) - Required - Set to 1 to enable R2C transform. ### Request Example ```c configuration.FFTdim = 2; configuration.size[0] = 1920; configuration.size[1] = 1080; configuration.performR2C = 1; ``` ### Response - **buffer** (void*) - Output buffer containing the half-spectrum complex data. ``` -------------------------------- ### Double Precision FFT Source: https://context7.com/dtolm/vkfft/llms.txt Enable high-precision FFT calculations using double-precision floating point values. ```APIDOC ## Double Precision FFT ### Description Configures the VkFFT application to utilize double precision for calculations, ensuring maximum accuracy for sensitive scientific computations. ### Parameters #### Configuration Fields - **doublePrecision** (int) - Required - Set to 1 to enable double precision mode. ### Request Example ```c configuration.doublePrecision = 1; ``` ``` -------------------------------- ### Error Handling Source: https://context7.com/dtolm/vkfft/llms.txt Standard error reporting mechanism using the VkFFTResult enumeration. ```APIDOC ## Error Handling ### Description VkFFT returns a `VkFFTResult` enum for every initialization call. Use this to handle common errors like memory allocation failures or unsupported FFT lengths. ### Response #### Success Response (VKFFT_SUCCESS) - **result** (enum) - Operation completed successfully. #### Error Codes - **VKFFT_ERROR_MALLOC_FAILED** - Memory allocation failed. - **VKFFT_ERROR_INVALID_DEVICE** - GPU device is invalid or inaccessible. - **VKFFT_ERROR_FAILED_TO_COMPILE_PROGRAM** - Shader compilation failed. ``` -------------------------------- ### Convolution with Matrix Kernel Source: https://context7.com/dtolm/vkfft/llms.txt Performs FFT-based convolution supporting 1x1, 2x2, and 3x3 matrix kernels for vector field processing. ```APIDOC ## Convolution with Matrix Kernel ### Description Executes high-performance convolution by performing FFT, multiplication, and iFFT in a single pipeline. Supports multi-component vector inputs and matrix kernels. ### Method C/C++ API (VkFFTConfiguration) ### Parameters - **kernelConvolution** (int) - Required - Set to 1 for kernel configuration. - **performConvolution** (int) - Required - Set to 1 to enable convolution. - **matrixConvolution** (int) - Required - Defines kernel size (e.g., 3 for 3x3). - **coordinateFeatures** (int) - Required - Number of components in the input vector. ### Request Example ```c conv_config.performConvolution = 1; conv_config.matrixConvolution = 3; conv_config.coordinateFeatures = 3; ``` ``` -------------------------------- ### deleteVkFFT Source: https://context7.com/dtolm/vkfft/llms.txt Releases all resources associated with a VkFFT application. ```APIDOC ## deleteVkFFT ### Description Releases all resources associated with a VkFFT application, including compiled shaders, pipelines, and internal buffers. ### Method Function Call ### Parameters - **app** (VkFFTApplication*) - Required - Pointer to the application to be destroyed. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.