yum-mirror/VkFFT
git clone https://git.yummers.dev/yum-mirror/VkFFT
ba7001c
v1.2.30
1#LyX 2.3 created this file. For more info see http://www.lyx.org/ 2\lyxformat 544 3\begin_document 4\begin_header 5\save_transient_properties true 6\origin unavailable 7\textclass article 8\begin_preamble 9\usepackage{sourcecodepro} 10\usepackage[parfill]{parskip} 11\usepackage{enumitem} 12\setlist[itemize]{leftmargin=*} 13\usepackage{minted} 14\usepackage{mdframed} 15\definecolor{bg}{rgb}{0.95,0.95,0.95} 16\usepackage{hyperref} 17\hypersetup{ 18 colorlinks, 19 citecolor=black, 20 filecolor=black, 21 linkcolor=black, 22 urlcolor=black 23} 24\end_preamble 25\use_default_options true 26\maintain_unincluded_children false 27\language english 28\language_package default 29\inputencoding auto 30\fontencoding global 31\font_roman "default" "default" 32\font_sans "default" "default" 33\font_typewriter "default" "default" 34\font_math "auto" "auto" 35\font_default_family default 36\use_non_tex_fonts true 37\font_sc false 38\font_osf false 39\font_sf_scale 100 100 40\font_tt_scale 100 100 41\use_microtype false 42\use_dash_ligatures true 43\graphics default 44\default_output_format default 45\output_sync 0 46\bibtex_command default 47\index_command default 48\paperfontsize 12 49\spacing single 50\use_hyperref false 51\papersize default 52\use_geometry true 53\use_package amsmath 1 54\use_package amssymb 1 55\use_package cancel 1 56\use_package esint 1 57\use_package mathdots 1 58\use_package mathtools 1 59\use_package mhchem 1 60\use_package stackrel 1 61\use_package stmaryrd 1 62\use_package undertilde 1 63\cite_engine basic 64\cite_engine_type default 65\biblio_style plain 66\use_bibtopic false 67\use_indices false 68\paperorientation portrait 69\suppress_date false 70\justification true 71\use_refstyle 1 72\use_minted 0 73\index Index 74\shortcut idx 75\color #008000 76\end_index 77\leftmargin 2.5cm 78\topmargin 2.5cm 79\rightmargin 2.5cm 80\bottommargin 2.5cm 81\secnumdepth 3 82\tocdepth 3 83\paragraph_separation indent 84\paragraph_indentation default 85\is_math_indent 0 86\math_numbering_side default 87\quotes_style english 88\dynamic_quotes 0 89\papercolumns 1 90\papersides 1 91\paperpagestyle default 92\tracking_changes false 93\output_changes false 94\html_math_output 0 95\html_css_as_file 0 96\html_be_strict false 97\end_header 98 99\begin_body 100 101\begin_layout Standard 102\begin_inset ERT 103status open 104 105\begin_layout Plain Layout 106 107 108\backslash 109UseRawInputEncoding 110\end_layout 111 112\begin_layout Plain Layout 113 114 115\backslash 116begin{titlepage} 117\end_layout 118 119\begin_layout Plain Layout 120 121 122\backslash 123centering 124\end_layout 125 126\begin_layout Plain Layout 127 128 129\backslash 130vspace{1cm} 131\end_layout 132 133\begin_layout Plain Layout 134 135{ 136\backslash 137scshape 138\backslash 139LARGE VkFFT - Vulkan/CUDA/HIP/OpenCL/Level Zero/Metal Fast Fourier Transform 140 library 141\backslash 142par} 143\end_layout 144 145\begin_layout Plain Layout 146 147 148\backslash 149vspace{1.5cm} 150\end_layout 151 152\begin_layout Plain Layout 153 154{ 155\backslash 156huge 157\backslash 158bfseries API guide with examples 159\backslash 160par} 161\end_layout 162 163\begin_layout Plain Layout 164 165 166\backslash 167vspace{2cm} 168\end_layout 169 170\begin_layout Plain Layout 171 172{ 173\backslash 174Large Dmitrii Tolmachev 175\backslash 176par} 177\end_layout 178 179\begin_layout Plain Layout 180 181 182\end_layout 183 184\begin_layout Plain Layout 185 186 187\backslash 188vspace{1cm} 189\end_layout 190 191\begin_layout Plain Layout 192 193{ 194\backslash 195large October 2022, version 1.2.30 196\backslash 197par} 198\end_layout 199 200\begin_layout Plain Layout 201 202 203\backslash 204end{titlepage} 205\end_layout 206 207\end_inset 208 209 210\end_layout 211 212\begin_layout Standard 213\begin_inset Newpage newpage 214\end_inset 215 216 217\end_layout 218 219\begin_layout Standard 220\begin_inset CommandInset toc 221LatexCommand tableofcontents 222 223\end_inset 224 225 226\end_layout 227 228\begin_layout Standard 229\begin_inset Newpage newpage 230\end_inset 231 232 233\end_layout 234 235\begin_layout Section 236Introduction 237\end_layout 238 239\begin_layout Standard 240This document describes VkFFT - Vulkan/CUDA/HIP/OpenCL/Level Zero/Metal 241 Fast Fourier Transform library. 242 It describes the features and current limitations of VkFFT, explains the 243 API and compares it to other FFT libraries (like FFTW and cuFFT) on the 244 set of examples. 245 It is by no means the final version, so if there is something unclear - 246 feel free to contact me (dtolm96@gmail.com), so I can update it. 247 248\end_layout 249 250\begin_layout Standard 251\begin_inset Newpage newpage 252\end_inset 253 254 255\end_layout 256 257\begin_layout Section 258Using the VkFFT API 259\end_layout 260 261\begin_layout Standard 262This chapter will cover the basics of VkFFT. 263 Fourier transform of a sequence is called Discrete Fourier Transform (DFT). 264 It is defined by the following formula: 265\begin_inset Formula 266\begin{equation} 267X_{k}=\stackrel[n=0]{N-1}{\sum}x_{n}e^{-\frac{2\pi i}{N}nk}=\mathrm{DFT}{}_{N}(x_{n},k),\label{eq:dft} 268\end{equation} 269 270\end_inset 271 272 273\end_layout 274 275\begin_layout Standard 276where 277\begin_inset Formula $x_{n}$ 278\end_inset 279 280is the input sequence, 281\begin_inset Formula $N$ 282\end_inset 283 284 is the length of the input sequence and 285\begin_inset Formula $k\in[0,N-1],k\in\mathbb{Z}$ 286\end_inset 287 288is the output index, corresponding to frequency in Fourier space. 289 Corresponding to that, inverse DFT is defined as following: 290\begin_inset Formula 291\begin{equation} 292x_{n}=\stackrel[k=0]{\mathrm{N-1}}{\sum}X_{k}e^{\frac{2\pi i}{N}nk}=\mathrm{iDFT}{}_{N}(X_{k},n) 293\end{equation} 294 295\end_inset 296 297 298\end_layout 299 300\begin_layout Standard 301VkFFT follows the same definitions as FFTW and cuFFT - forward FFT has the 302 exponent sign 303\begin_inset Formula $-1$ 304\end_inset 305 306, while the inverse has the exponent sign 307\begin_inset Formula $1$ 308\end_inset 309 310. 311 Note, that inverse transform by default is unnormalized, so to get the 312 input sequence after FFT + iFFT, the user has to divide the result by 313\begin_inset Formula $N$ 314\end_inset 315 316. 317\end_layout 318 319\begin_layout Subsection 320Installing VkFFT 321\end_layout 322 323\begin_layout Standard 324VkFFT is distributed as a header-only library. 325 The installation process consists of the following steps: 326\end_layout 327 328\begin_layout Enumerate 329\noindent 330\align left 331Copy vkFFT.h file into one of the directories included in the user's project. 332\end_layout 333 334\begin_layout Enumerate 335\noindent 336Define VKFFT_BACKEND as a number corresponding to the API used in the user's 337 project: 0 - Vulkan, 1 - CUDA, 2 - HIP, 3 - OpenCL, 4 - Level Zero, 5 - 338 Metal. 339 Definition is done like: 340\begin_inset ERT 341status open 342 343\begin_layout Plain Layout 344 345 346\backslash 347begin{mdframed}[backgroundcolor=bg] 348\end_layout 349 350\begin_layout Plain Layout 351 352 353\backslash 354begin{minted}[tabsize=4,obeytabs,breaklines]{make} 355\end_layout 356 357\begin_layout Plain Layout 358 359-DVKFFT_BACKEND=X 360\end_layout 361 362\begin_layout Plain Layout 363 364 365\backslash 366end{minted} 367\end_layout 368 369\begin_layout Plain Layout 370 371 372\backslash 373end{mdframed} 374\end_layout 375 376\end_inset 377 378 in GCC or as 379\begin_inset ERT 380status open 381 382\begin_layout Plain Layout 383 384 385\backslash 386begin{mdframed}[backgroundcolor=bg] 387\end_layout 388 389\begin_layout Plain Layout 390 391 392\backslash 393begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 394\end_layout 395 396\begin_layout Plain Layout 397 398set(VKFFT_BACKEND 1 CACHE STRING "0 - Vulkan, 1 - CUDA, 2 - HIP, 3 - OpenCL, 399 4 - Level Zero, 5 - Metal") 400\end_layout 401 402\begin_layout Plain Layout 403 404 405\backslash 406end{minted} 407\end_layout 408 409\begin_layout Plain Layout 410 411 412\backslash 413end{mdframed} 414\end_layout 415 416\end_inset 417 418in CMake. 419\end_layout 420 421\begin_layout Enumerate 422Depending on the API backend, the project must use additional libraries 423 for run-time compilation: 424\end_layout 425 426\begin_deeper 427\begin_layout Enumerate 428Vulkan API: SPIRV, glslang and Vulkan. 429 Define VK_API_VERSION to the available Vulkan version. 430 Sample CMakeLists can look like this: 431\begin_inset ERT 432status open 433 434\begin_layout Plain Layout 435 436 437\backslash 438begin{mdframed}[backgroundcolor=bg] 439\end_layout 440 441\begin_layout Plain Layout 442 443 444\backslash 445begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 446\end_layout 447 448\begin_layout Plain Layout 449 450find_package(Vulkan REQUIRED) 451\end_layout 452 453\begin_layout Plain Layout 454 455target_compile_definitions(${PROJECT_NAME} PUBLIC -DVK_API_VERSION=11)#10 456 - Vulkan 1.0, 11 - Vulkan 1.1, 12 - Vulkan 1.2 457\end_layout 458 459\begin_layout Plain Layout 460 461target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/gl 462slang-master/glslang/Include/) 463\end_layout 464 465\begin_layout Plain Layout 466 467add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glslang-master) 468\end_layout 469 470\begin_layout Plain Layout 471 472\end_layout 473 474\begin_layout Plain Layout 475 476target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vk 477FFT/) 478\end_layout 479 480\begin_layout Plain Layout 481 482add_library(VkFFT INTERFACE) 483\end_layout 484 485\begin_layout Plain Layout 486 487target_compile_definitions(VkFFT INTERFACE -DVKFFT_BACKEND=0) 488\end_layout 489 490\begin_layout Plain Layout 491 492\end_layout 493 494\begin_layout Plain Layout 495 496target_link_libraries(${PROJECT_NAME} PUBLIC SPIRV glslang Vulkan::Vulkan 497 VkFFT) 498\end_layout 499 500\begin_layout Plain Layout 501 502 503\backslash 504end{minted} 505\end_layout 506 507\begin_layout Plain Layout 508 509 510\backslash 511end{mdframed} 512\end_layout 513 514\end_inset 515 516 517\end_layout 518 519\begin_layout Enumerate 520CUDA API: CUDA and NVRTC. 521 Sample CMakeLists can look like this: 522\begin_inset ERT 523status open 524 525\begin_layout Plain Layout 526 527 528\backslash 529begin{mdframed}[backgroundcolor=bg] 530\end_layout 531 532\begin_layout Plain Layout 533 534 535\backslash 536begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 537\end_layout 538 539\begin_layout Plain Layout 540 541find_package(CUDA 9.0 REQUIRED) 542\end_layout 543 544\begin_layout Plain Layout 545 546enable_language(CUDA) 547\end_layout 548 549\begin_layout Plain Layout 550 551set_property(TARGET ${PROJECT_NAME} PROPERTY CUDA_ARCHITECTURES 35 60 70 552 75 80 86) 553\end_layout 554 555\begin_layout Plain Layout 556 557target_compile_options(${PROJECT_NAME} PUBLIC "$<$<COMPILE_LANGUAGE:CUDA>:SHELL: 558 559\end_layout 560 561\begin_layout Plain Layout 562 563 -DVKFFT_BACKEND=${VKFFT_BACKEND} 564\end_layout 565 566\begin_layout Plain Layout 567 568 -gencode arch=compute_35,code=compute_35 569\end_layout 570 571\begin_layout Plain Layout 572 573 -gencode arch=compute_60,code=compute_60 574\end_layout 575 576\begin_layout Plain Layout 577 578 -gencode arch=compute_70,code=compute_70 579\end_layout 580 581\begin_layout Plain Layout 582 583 -gencode arch=compute_75,code=compute_75 584\end_layout 585 586\begin_layout Plain Layout 587 588 -gencode arch=compute_80,code=compute_80 589\end_layout 590 591\begin_layout Plain Layout 592 593 -gencode arch=compute_86,code=compute_86>") 594\end_layout 595 596\begin_layout Plain Layout 597 598set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION 599 ON) 600\end_layout 601 602\begin_layout Plain Layout 603 604set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS 605 ON) 606\end_layout 607 608\begin_layout Plain Layout 609 610\end_layout 611 612\begin_layout Plain Layout 613 614find_library(CUDA_NVRTC_LIB libnvrtc nvrtc HINTS "${CUDA_TOOLKIT_ROOT_DIR}/lib64 615" "${LIBNVRTC_LIBRARY_DIR}" "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64" /usr/lib64 616 /usr/local/cuda/lib64) 617\end_layout 618 619\begin_layout Plain Layout 620 621\end_layout 622 623\begin_layout Plain Layout 624 625target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vk 626FFT/) 627\end_layout 628 629\begin_layout Plain Layout 630 631add_library(VkFFT INTERFACE) 632\end_layout 633 634\begin_layout Plain Layout 635 636target_compile_definitions(VkFFT INTERFACE -DVKFFT_BACKEND=1) 637\end_layout 638 639\begin_layout Plain Layout 640 641\end_layout 642 643\begin_layout Plain Layout 644 645target_link_libraries(${PROJECT_NAME} PUBLIC ${CUDA_LIBRARIES} cuda ${CUDA_NVRTC 646_LIB} VkFFT) 647\end_layout 648 649\begin_layout Plain Layout 650 651 652\backslash 653end{minted} 654\end_layout 655 656\begin_layout Plain Layout 657 658 659\backslash 660end{mdframed} 661\end_layout 662 663\end_inset 664 665 666\end_layout 667 668\begin_layout Enumerate 669HIP API: HIP and HIPRTC. 670 Sample CMakeLists can look like this: 671\begin_inset ERT 672status open 673 674\begin_layout Plain Layout 675 676 677\backslash 678begin{mdframed}[backgroundcolor=bg] 679\end_layout 680 681\begin_layout Plain Layout 682 683 684\backslash 685begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 686\end_layout 687 688\begin_layout Plain Layout 689 690list(APPEND CMAKE_PREFIX_PATH /opt/rocm/hip /opt/rocm) 691\end_layout 692 693\begin_layout Plain Layout 694 695find_package(hip) 696\end_layout 697 698\begin_layout Plain Layout 699 700\end_layout 701 702\begin_layout Plain Layout 703 704target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vk 705FFT/) 706\end_layout 707 708\begin_layout Plain Layout 709 710add_library(VkFFT INTERFACE) 711\end_layout 712 713\begin_layout Plain Layout 714 715target_compile_definitions(VkFFT INTERFACE -DVKFFT_BACKEND=2) 716\end_layout 717 718\begin_layout Plain Layout 719 720#target_compile_definitions(${PROJECT_NAME} PUBLIC -DVKFFT_OLD_ROCM) #ROCm 721 versions before 4.5 needed kernel include of hiprtc 722\end_layout 723 724\begin_layout Plain Layout 725 726\end_layout 727 728\begin_layout Plain Layout 729 730target_link_libraries(${PROJECT_NAME} PUBLIC hip::host VkFFT) 731\end_layout 732 733\begin_layout Plain Layout 734 735 736\backslash 737end{minted} 738\end_layout 739 740\begin_layout Plain Layout 741 742 743\backslash 744end{mdframed} 745\end_layout 746 747\end_inset 748 749 750\end_layout 751 752\begin_layout Enumerate 753OpenCL API: OpenCL. 754 Sample CMakeLists can look like this: 755\begin_inset ERT 756status open 757 758\begin_layout Plain Layout 759 760 761\backslash 762begin{mdframed}[backgroundcolor=bg] 763\end_layout 764 765\begin_layout Plain Layout 766 767 768\backslash 769begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 770\end_layout 771 772\begin_layout Plain Layout 773 774find_package(OpenCL REQUIRED) 775\end_layout 776 777\begin_layout Plain Layout 778 779\end_layout 780 781\begin_layout Plain Layout 782 783target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vk 784FFT/) 785\end_layout 786 787\begin_layout Plain Layout 788 789add_library(VkFFT INTERFACE) 790\end_layout 791 792\begin_layout Plain Layout 793 794target_compile_definitions(VkFFT INTERFACE -DVKFFT_BACKEND=3) 795\end_layout 796 797\begin_layout Plain Layout 798 799\end_layout 800 801\begin_layout Plain Layout 802 803target_link_libraries(${PROJECT_NAME} PUBLIC OpenCL::OpenCL VkFFT) 804\end_layout 805 806\begin_layout Plain Layout 807 808 809\backslash 810end{minted} 811\end_layout 812 813\begin_layout Plain Layout 814 815 816\backslash 817end{mdframed} 818\end_layout 819 820\end_inset 821 822 823\end_layout 824 825\begin_layout Enumerate 826Level Zero API: Level Zero; Clang and llvm-spirv must be in the system path 827 (for kernel compilation). 828 Sample CMakeLists can look like this: 829\begin_inset ERT 830status open 831 832\begin_layout Plain Layout 833 834 835\backslash 836begin{mdframed}[backgroundcolor=bg] 837\end_layout 838 839\begin_layout Plain Layout 840 841 842\backslash 843begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 844\end_layout 845 846\begin_layout Plain Layout 847 848set(LevelZero_LIBRARY "/usr/lib/x86_64-linux-gnu/") 849\end_layout 850 851\begin_layout Plain Layout 852 853set(LevelZero_INCLUDE_DIR "/usr/include/") 854\end_layout 855 856\begin_layout Plain Layout 857 858find_library( 859\end_layout 860 861\begin_layout Plain Layout 862 863 LevelZero_LIB 864\end_layout 865 866\begin_layout Plain Layout 867 868 NAMES "ze_loader" 869\end_layout 870 871\begin_layout Plain Layout 872 873 PATHS ${LevelZero_LIBRARY} 874\end_layout 875 876\begin_layout Plain Layout 877 878 PATH_SUFFIXES "lib" "lib64" 879\end_layout 880 881\begin_layout Plain Layout 882 883 NO_DEFAULT_PATH 884\end_layout 885 886\begin_layout Plain Layout 887 888 ) 889\end_layout 890 891\begin_layout Plain Layout 892 893find_path( 894\end_layout 895 896\begin_layout Plain Layout 897 898 LevelZero_INCLUDES 899\end_layout 900 901\begin_layout Plain Layout 902 903 NAMES "ze_api.h" 904\end_layout 905 906\begin_layout Plain Layout 907 908 PATHS ${LevelZero_INCLUDE_DIR} 909\end_layout 910 911\begin_layout Plain Layout 912 913 PATH_SUFFIXES "include" 914\end_layout 915 916\begin_layout Plain Layout 917 918 NO_DEFAULT_PATH 919\end_layout 920 921\begin_layout Plain Layout 922 923 ) 924\end_layout 925 926\begin_layout Plain Layout 927 928target_include_directories(${PROJECT_NAME} PUBLIC ${LevelZero_INCLUDES}) 929\end_layout 930 931\begin_layout Plain Layout 932 933add_library(VkFFT INTERFACE) 934\end_layout 935 936\begin_layout Plain Layout 937 938target_compile_definitions(VkFFT INTERFACE -DVKFFT_BACKEND=4) 939\end_layout 940 941\begin_layout Plain Layout 942 943\end_layout 944 945\begin_layout Plain Layout 946 947target_link_libraries(${PROJECT_NAME} PUBLIC LevelZero VkFFT) 948\end_layout 949 950\begin_layout Plain Layout 951 952 953\backslash 954end{minted} 955\end_layout 956 957\begin_layout Plain Layout 958 959 960\backslash 961end{mdframed} 962\end_layout 963 964\end_inset 965 966 967\end_layout 968 969\begin_layout Enumerate 970Metal API: Metal. 971 Sample CMakeLists can look like this: 972\begin_inset ERT 973status open 974 975\begin_layout Plain Layout 976 977 978\backslash 979begin{mdframed}[backgroundcolor=bg] 980\end_layout 981 982\begin_layout Plain Layout 983 984 985\backslash 986begin{minted}[tabsize=4,obeytabs,breaklines]{cmake} 987\end_layout 988 989\begin_layout Plain Layout 990 991add_compile_options(-WMTL_IGNORE_WARNINGS) 992\end_layout 993 994\begin_layout Plain Layout 995 996find_library(FOUNDATION_LIB Foundation REQUIRED) 997\end_layout 998 999\begin_layout Plain Layout 1000 1001find_library(QUARTZ_CORE_LIB QuartzCore REQUIRED) 1002\end_layout 1003 1004\begin_layout Plain Layout 1005 1006find_library(METAL_LIB Metal REQUIRED) 1007\end_layout 1008 1009\begin_layout Plain Layout 1010 1011target_include_directories(${PROJECT_NAME} PUBLIC "metal-cpp/") 1012\end_layout 1013 1014\begin_layout Plain Layout 1015 1016\end_layout 1017 1018\begin_layout Plain Layout 1019 1020target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vk 1021FFT/) 1022\end_layout 1023 1024\begin_layout Plain Layout 1025 1026add_library(VkFFT INTERFACE) 1027\end_layout 1028 1029\begin_layout Plain Layout 1030 1031target_compile_definitions(VkFFT INTERFACE -DVKFFT_BACKEND=5) 1032\end_layout 1033 1034\begin_layout Plain Layout 1035 1036\end_layout 1037 1038\begin_layout Plain Layout 1039 1040target_link_libraries(${PROJECT_NAME} PUBLIC ${FOUNDATION_LIB} ${QUARTZ_CORE_LIB 1041} ${METAL_LIB} VkFFT) 1042\end_layout 1043 1044\begin_layout Plain Layout 1045 1046 1047\backslash 1048end{minted} 1049\end_layout 1050 1051\begin_layout Plain Layout 1052 1053 1054\backslash 1055end{mdframed} 1056\end_layout 1057 1058\end_inset 1059 1060 1061\end_layout 1062 1063\end_deeper 1064\begin_layout Subsection 1065Fourier Transform Setup 1066\end_layout 1067 1068\begin_layout Standard 1069VkFFT follows a plan structure like FFTW/cuFFT with a notable difference 1070 - there is a unified interface to all transforms. 1071 This means that there are no separate functions like fftPlan1D/fftPlan2D/fftPla 1072nMany/etc. 1073 The initialization is done through a single configuration struct - VkFFTConfigu 1074ration. 1075 Each parameter of it will be covered in detail in this document. 1076 Plans in VkFFT are called VkFFTApplication and they are created with a 1077 unified initializeVkFFT call. 1078 1079\end_layout 1080 1081\begin_layout Standard 1082As the code is written in C, don't forget to zero-initialize used structs! 1083\end_layout 1084 1085\begin_layout Standard 1086During the initializeVkFFT(VkFFTApplication* app, VkFFTConfiguration inputLaunch 1087Configuration) call VkFFT performs kernel generation and compilation from 1088 scratch (kernel reuse may be added later). 1089 The overall process of initialization looks like this: 1090\end_layout 1091 1092\begin_layout Enumerate 1093Get device parameters, perform default initialization of internal copy of 1094 configuration struct inside the VkFFTApplication, then fill in user-defined 1095 parameters from inputLaunchConfiguration. 1096\begin_inset space ~ 1097\end_inset 1098 1099VkFFTApplication is passed as a pointer, so initializeVkFFT modifies the 1100 user-provided application. 1101\end_layout 1102 1103\begin_layout Enumerate 1104By default, there are two internal FFT plans created - inverse and forward. 1105 Multidimensional FFT is done as a combination of 1D FFTs in each axis direction. 1106 For each axis, the VkFFTPlanAxis function is called. 1107\end_layout 1108 1109\begin_layout Enumerate 1110VkFFTPlanAxis configures parameters for each axis. 1111 It may perform additional memory allocations (see: memory allocated by 1112 VkFFT). 1113\end_layout 1114 1115\begin_layout Enumerate 1116shaderGenVkFFT generates corresponding to the axis code in a char buffer 1117 (each axis may require more than one kernel: see Four-step FFT, Bluestein's 1118 algorithm for FFT). 1119 1120\end_layout 1121 1122\begin_layout Enumerate 1123Code is then compiled with the run-time compiler of the specified backend. 1124\end_layout 1125 1126\begin_layout Standard 1127Once the plan is no longer need, a call to the deleteVkFFT function frees 1128 all the allocated resources. 1129 There are no processes launched that continue to work outside of the VkFFT 1130 related function calls. 1131\end_layout 1132 1133\begin_layout Subsection 1134Fourier Transform types and their definitions 1135\end_layout 1136 1137\begin_layout Standard 1138VkFFT supports commonly used Complex to complex (C2C), real to complex (R2C), 1139 complex to real (C2R) transformations and real to real (R2R) Discrete Cosine 1140 Transformations of types II, III and IV. 1141 VkFFT uses the same definitions as FFTW, except for the multidimensional 1142 FFT axis ordering: in FFTW dimensions are ordered with the decrease in 1143 consecutive elements stride, while VkFFT does the opposite - the first 1144 axis is the non-strided axis (the one that has elements located consecutively 1145 in memory with no gaps, usually named as the X-axis). 1146 So, in FFTW dimensions are specified as ZYX and in VkFFT as XYZ. 1147 This felt more logical to me - no matter if there are 1, 2 or 3 dimensions, 1148 the user can always find the axis with the same stride at the same position. 1149 This choice doesn't require any modification in the user's data management 1150 - just provide the FFT dimensions in the reverse order to VkFFT. 1151 1152\end_layout 1153 1154\begin_layout Standard 1155In addition to up to the 3 dimensions of FFT, VkFFT supports two forms of 1156 batching: the number of coordinates and the number of systems. 1157 The choice of two distinct batching ways is made to support matrix-vector 1158 convolutions, where the kernel is presented as a matrix. 1159 Overall, the layout of VkFFT can be described as WHDCN - width, height, 1160 depth, coordinate and number of systems (in order of increasing strides, 1161 starting with 1 for width). 1162 Coordinate and number of systems can be 1, if the user has 1 as one of 1163 the FFT dimensions, the user can omit it from setup altogether as FFT of 1164 size 1 produces the same number as the input. 1165 Often, the coordinate part of the layout is not used, so the main batching 1166 is done by specifying N. 1167\end_layout 1168 1169\begin_layout Standard 1170VkFFT assumes that complex numbers are stored consecutively in memory: RIRIRI... 1171 where R denotes the real part of the complex number and I denotes the imaginary 1172 part. 1173 There is no difference between using a float2/double2/half2 container or 1174 access memory as float/double/half as long as the byte order remains the 1175 same. 1176\end_layout 1177 1178\begin_layout Standard 1179This section and the next one will cover the basics of VkFFT data layouts 1180 and memory management. 1181 1182\end_layout 1183 1184\begin_layout Subsubsection 1185C2C transforms 1186\end_layout 1187 1188\begin_layout Standard 1189The base FFT algorithm - C2C in VkFFT has the same definition as FFTW. 1190 Forward FFT has the exponent sign 1191\begin_inset Formula $-1$ 1192\end_inset 1193 1194, while the inverse has the exponent sign 1195\begin_inset Formula $1$ 1196\end_inset 1197 1198. 1199 By default, the inverse transform is unnormalized. 1200 1201\begin_inset Formula $N_{x}N_{y}N_{z}$ 1202\end_inset 1203 1204 complex numbers map to 1205\begin_inset Formula $N_{x}N_{y}N_{z}$ 1206\end_inset 1207 1208 complex numbers and no additional padding is required. 1209 The resulting data order will be the same as in FFTW/cuFFT, unless special 1210 parameters are provided in configuration (see: advanced memory management) 1211\end_layout 1212 1213\begin_layout Subsubsection 1214R2C/C2R transforms 1215\end_layout 1216 1217\begin_layout Standard 1218R2C/C2R transforms can be explained as C2C transforms with imaginary part 1219 set to zero. 1220 They exploit Hermitian symmetry of the result: 1221\begin_inset Formula $X_{k}=X_{N-k}^{*}$ 1222\end_inset 1223 1224 on the non-strided axis (the one that has elements located consecutively 1225 in memory with no gaps). 1226 This results in s reduction of required memory to store the complex result 1227 - we may only store 1228\begin_inset Formula $floor(\frac{N_{x}}{2})+1$ 1229\end_inset 1230 1231 complex numbers instead of 1232\begin_inset Formula $N_{x}$ 1233\end_inset 1234 1235. 1236 However, this results in memory requirements mismatch between input and 1237 output in R2C: 1238\begin_inset Formula $floor(\frac{N_{x}}{2})+1$ 1239\end_inset 1240 1241 complex elements will require 1242\begin_inset Formula $N_{x}+2$ 1243\end_inset 1244 1245 real numbers worth of memory for even 1246\begin_inset Formula $N_{x}$ 1247\end_inset 1248 1249 and 1250\begin_inset Formula $N_{x}+1$ 1251\end_inset 1252 1253 real numbers worth of memory for odd 1254\begin_inset Formula $N_{x}$ 1255\end_inset 1256 1257. 1258 For C2R the situation is reversed. 1259 There are two approaches to this problem: pad each sequence of the non-strided 1260 axis with zeros to the required length or use out-of-place mode. 1261 More information on how to do this will be given in the next section. 1262\end_layout 1263 1264\begin_layout Subsubsection 1265R2R (DCT) transforms 1266\end_layout 1267 1268\begin_layout Standard 1269R2R transforms in VkFFT are implemented in the form of Discrete cosine transform 1270s of types I, II, III and IV. 1271 Their definitions and transforms results match FFTW: 1272\end_layout 1273 1274\begin_layout Enumerate 1275DCT-I: 1276\begin_inset Formula $X_{k}=x_{0}+(-1)^{k}x_{N-1}+2\stackrel[n=1]{N-2}{\sum}x_{n}cos(\frac{\pi}{N-1}nk)$ 1277\end_inset 1278 1279, inverse of DCT-I (itself) 1280\end_layout 1281 1282\begin_layout Enumerate 1283DCT-II: 1284\begin_inset Formula $X_{k}=2\stackrel[n=1]{N-1}{\sum}x_{n}cos(\frac{\pi}{N}(n+\frac{1}{2})k)$ 1285\end_inset 1286 1287, inverse of DCT-III 1288\end_layout 1289 1290\begin_layout Enumerate 1291DCT-III: 1292\begin_inset Formula $X_{k}=x_{0}+2\stackrel[n=1]{N-1}{\sum}x_{n}cos(\frac{\pi}{N}n(k+\frac{1}{2}))$ 1293\end_inset 1294 1295, inverse of DCT-II 1296\end_layout 1297 1298\begin_layout Enumerate 1299DCT-IV: 1300\begin_inset Formula $X_{k}=2\stackrel[n=0]{N-1}{\sum}x_{n}cos(\frac{\pi}{N}(n+\frac{1}{2})(k+\frac{1}{2}))$ 1301\end_inset 1302 1303, inverse of DCT-IV (itself) 1304\end_layout 1305 1306\begin_layout Standard 1307R2R transforms are performed by redefinition of them to the C2C transforms 1308 (internal C2C sequence length can be different from the input R2R sequence 1309 length). 1310 R2R transform performs a one-to-one mapping between real numbers, so they 1311 don't require stride management, unlike R2C/C2R. 1312\end_layout 1313 1314\begin_layout Subsection 1315Memory management, data layouts for different transforms 1316\end_layout 1317 1318\begin_layout Subsubsection 1319VkFFT buffers 1320\end_layout 1321 1322\begin_layout Standard 1323VkFFT allows for explicit control over the data flow, which makes both in-place 1324 and out-of-place transforms possible. 1325 Buffers are passed to VkFFT as VkBuffer pointer in Vulkan, as double void 1326 pointers in CUDA/HIP/Level Zero, as cl_mem pointer in OpenCL and as MTL::Buffer 1327 pointer in Metal. 1328 This is done to maintain a uniform data pattern because some of the buffers 1329 can be allocated automatically. 1330 1331\end_layout 1332 1333\begin_layout Standard 1334The main buffer is called buffer and it always has to be provided, either 1335 during the plan creation or when the plan is executed. 1336 All calculations are performed in this buffer and it is always overwritten. 1337 To do calculations out-of-place, VkFFT provides an option to specify inputBuffe 1338r/outputBuffer buffer. 1339 The logic behind their usage is fairly simple - the user specifies inputBuffer 1340 if the input data has to be read from a buffer, different from the main 1341 buffer. 1342 As the data is read only once and nothing is written back to the inputBuffer, 1343 this allows doing truly out-of-place transformations. 1344 The same logic applies to outputBuffer with the difference that it is responsib 1345le for the absolute last write of the VkFFT. 1346 It is possible to use all three buffers to create complex data management 1347 paths. 1348\end_layout 1349 1350\begin_layout Standard 1351It must be noted, that sometimes FFT can not be done inside one buffer (see: 1352 Four-Step FFT algorithm, Bluestein's algorithm). 1353 To compute FFT in these cases, there exists tempBuffer buffer and data 1354 is transferred between the main buffer and tempBuffer during the FFT execution. 1355 The ordering of transfers between the main buffer and tempBuffer is done 1356 in such a way, so the initial data read and final data write are obeying 1357 the configuration from the previous paragraph. 1358 Users can allocate tempBuffer themselves of some memory that does not have 1359 any useful information at the time of FFT execution (the tempBuffer size 1360 can depend on the configuration, so this is a rather advanced operation 1361 - read more in the advanced memory management section) or allow VkFFT to 1362 manage tempBuffer allocation itself (tempBuffer will be freed at the deleteVkFF 1363T call). 1364\end_layout 1365 1366\begin_layout Standard 1367To compute convolutions and cross-correlations, a kernel buffer has to be 1368 specified. 1369 It must have the same layout as the result of the FFT transform. 1370\end_layout 1371 1372\begin_layout Subsubsection 1373VkFFT buffers strides. 1374 A special case of R2C/C2R transforms 1375\end_layout 1376 1377\begin_layout Standard 1378To have better control of memory, the user can specify the strides between 1379 consecutive elements of different axis for H (height), D (depth) and C 1380 (coordinate) parts of the WHDCN layout (W (width) stride is fixed to be 1381 1, N (number of systems) stride will be consecutive of C in memory if C 1382 is used, otherwise N will propagate the previous non-uniform stride multiplied 1383 by the corresponding axis length). 1384 Strides are specified not in bytes, but in the element type used - similar 1385 to the way how the user would access the corresponding element in the array. 1386 If all elements are consecutive in C2C, stride for H will be equal to the 1387 FFT length of W axis, stride for D will be multiplication of first two 1388 FFT axis lengths, stride for C will be multiplication of first three FFT 1389 axis lengths, etc. 1390 These are the default values of C2C and R2R strides if they are not explicitly 1391 specified. 1392\end_layout 1393 1394\begin_layout Standard 1395One of the main use-cases of strides comes to solve the R2C/C2R Hermitian 1396 symmetry H stride mismatch - for real space, it is equal to 1397\begin_inset Formula $N_{x}$ 1398\end_inset 1399 1400 real elements and for the frequency space it is equal to 1401\begin_inset Formula $floor(\frac{N_{x}}{2})+1$ 1402\end_inset 1403 1404 complex numbers. 1405 So, with strides it is possible to use a buffer, padded to 1406\begin_inset Formula $2\cdot(floor(\frac{N_{x}}{2})+1)$ 1407\end_inset 1408 1409 real elements in H stride (all elements between 1410\begin_inset Formula $N_{x}$ 1411\end_inset 1412 1413 and 1414\begin_inset Formula $2\cdot(floor(\frac{N_{x}}{2})+1)$ 1415\end_inset 1416 1417 will not be read so it does not matter what data is there before the write 1418 stage). 1419 All other strides are done as a multiplication between the previous stride 1420 and the number of elements in the previous axis. 1421 These are the default values of R2C/C2R strides if they are not explicitly 1422 specified. 1423\end_layout 1424 1425\begin_layout Standard 1426It is possible to specify separate sets of strides for all user-defined 1427 buffers: bufferStride for the main buffer, inputStride for input buffer, 1428 outputStride for output buffer (kernel stride is assumed to be the same 1429 as bufferStride, tempBuffer strides are configured automatically). 1430 1431\end_layout 1432 1433\begin_layout Standard 1434For an out-of-place R2C FFT, there is no need to pad buffer with real numbers, 1435 but user must specify H stride there (as it differs to default one) - 1436\begin_inset Formula $N_{x}$ 1437\end_inset 1438 1439 real elements for real space and 1440\begin_inset Formula $floor(\frac{N_{x}}{2})+1$ 1441\end_inset 1442 1443 complex numbers for the frequency space. 1444\end_layout 1445 1446\begin_layout Standard 1447An out-of-place C2R FFT is a more tricky transform. 1448 In the multidimensional case, the main buffer will be written to and read 1449 from multiple times. 1450 The intermediate stores have a complex layout, which requires more space 1451 than the output real layout, so in order not to modify the input data, 1452 there exist two options. 1453 First, pad the real data layout has to 1454\begin_inset Formula $2\cdot(floor(\frac{N_{x}}{2})+1)$ 1455\end_inset 1456 1457 real elements in H stride (complex buffer will be used as inputBuffer, 1458 real buffer as buffer). 1459 Second, use the third buffer, so both input and output buffers have their 1460 original layouts (complex buffer will be used as inputBuffer, the main 1461 buffer for calculations is buffer and output real buffer as outputBuffer). 1462 If you use inverseReturnToInputBuffer option, where R2C is configured to 1463 read from input buffer and C2R is configured to write to the input buffer; 1464 C2R will modify the buffer it reads from in some cases (see issue 1465\begin_inset CommandInset href 1466LatexCommand href 1467name "#58" 1468target "https://github.com/DTolm/VkFFT/issues/58#issuecomment-1007205682" 1469literal "false" 1470 1471\end_inset 1472 1473) 1474\end_layout 1475 1476\begin_layout Subsection 1477VkFFT algorithms 1478\end_layout 1479 1480\begin_layout Standard 1481VkFFT implements a wide range of algorithms to compute different types of 1482 FFTs but all of them can be reduced to a mixed-radix Cooley-Tukey FFT algorithm 1483 in the Stockham autosort form. 1484 The main idea behind it is to decompose the sequence as a set of primes, 1485 for each of which FFT can be written down exactly. 1486 As of now, VkFFT has radix implementations for primes up to 13, so all 1487 C2C sequences decomposable into a multiplication of such primes will be 1488 done purely with the Stockham algorithm. 1489 Below additional algorithms and their use-cases are described. 1490\end_layout 1491 1492\begin_layout Subsubsection 1493Bluestein's algorithm 1494\end_layout 1495 1496\begin_layout Standard 1497A complex algorithm that is used in cases where the sequence is not decomposable 1498 with implemented radix butterflies (currently - primes up to 13). 1499 It is derived by replacing 1500\begin_inset Formula $nk=\left(n^{2}+k^{2}-(n-k)^{2}\right)/2$ 1501\end_inset 1502 1503 in 1504\begin_inset CommandInset ref 1505LatexCommand ref 1506reference "eq:dft" 1507plural "false" 1508caps "false" 1509noprefix "false" 1510 1511\end_inset 1512 1513: 1514\end_layout 1515 1516\begin_layout Standard 1517\begin_inset Formula 1518\begin{align} 1519X_{k} & =\left(e^{-\pi i\frac{k^{2}}{N}}\right)\stackrel[n=0]{N-1}{\sum}\left(x_{n}e^{-\pi i\frac{n^{2}}{N}}\right)\left(e^{\pi i\frac{\left(k-n\right)^{2}}{N}}\right)=b_{k}^{\ast}\stackrel[n=0]{N-1}{\sum}a_{n}b_{k-n}\label{eq:dft-1}\\ 1520a_{n} & =x_{n}b_{n}^{\ast}\\ 1521b_{n} & =e^{\pi i\frac{n^{2}}{N}} 1522\end{align} 1523 1524\end_inset 1525 1526 1527\end_layout 1528 1529\begin_layout Standard 1530Here FFT is represented as a convolution between two sequences: 1531\begin_inset Formula $a_{n}$ 1532\end_inset 1533 1534 and 1535\begin_inset Formula $b_{n}$ 1536\end_inset 1537 1538, which can be performed by the means of convolution theorem: 1539\end_layout 1540 1541\begin_layout Standard 1542\begin_inset Formula 1543\begin{equation} 1544F\{a\ast b\}=F\{a\}\cdot F\{b\} 1545\end{equation} 1546 1547\end_inset 1548 1549 1550\end_layout 1551 1552\begin_layout Standard 1553By padding 1554\begin_inset Formula $a_{n}$ 1555\end_inset 1556 1557 and 1558\begin_inset Formula $b_{n}$ 1559\end_inset 1560 1561 to a sequence length decomposable with implemented radix butterflies with 1562 a size of at least 1563\begin_inset Formula $2N-1$ 1564\end_inset 1565 1566 (because the length of 1567\begin_inset Formula $b_{n}$ 1568\end_inset 1569 1570 is 1571\begin_inset Formula $2N-1$ 1572\end_inset 1573 1574), we can perform FFT of any length. 1575 FFT of 1576\begin_inset Formula $b_{n}$ 1577\end_inset 1578 1579 can be precomputed, so overall this algorithm requires at least 4x the 1580 computations and more memory transfers. 1581 This algorithm can be combined with all other algorithms implemented in 1582 VkFFT. 1583 If an FFT can not be done in a single upload, a tempBuffer has to be allocated 1584 (because the logical FFT buffer size is bigger than the original system). 1585\end_layout 1586 1587\begin_layout Subsubsection 1588The Four-Step FFT algorithm 1589\end_layout 1590 1591\begin_layout Standard 1592GPUs and CPUs have a hierarchical memory model - the closer memory to the 1593 unit that performs the computations, the faster its speed and the lower 1594 the size. 1595 So it is advantageous to split FFTs, not to the lowest primes, but to some 1596 bigger multiplication of those primes, then upload this subsequence to 1597 the closest cache level to the cores and do the final prime split there. 1598 The absolute lowest level is the register file, however, it does not allow 1599 for thread communications outside the warp. 1600 For this purpose, modern GPUs employ shared memory - a fast memory with 1601 a bank structure that is visible to all threads in a thread block. 1602 The usual sizes of it change on a scale from 16KB to 192KB and it is often 1603 beneficial to use it fully. 1604 However, if the full sequence can not fit inside the shared memory, FFT 1605 has to be done in multiple uploads - with the Four Step FFT algorithm. 1606 The main idea behind it is to represent a big 1D sequence as a 2D (or 3D 1607 for the three-upload scheme) FFT - we first do FFT along the columns, then 1608 the rows, then transpose the result and multiply by a special set of phase 1609 vectors. 1610 Similar decomposition idea as the main Cooley-Tukey algorithm. 1611 However, performing transpositions in-place is a complicated task - especially 1612 for a non-trivial ratio between dimensions. 1613 It will also require an additional read/write stage, as it can not be merged 1614 with the last write of the FFT algorithm. 1615 The easiest and the most performant solution is to use a tempBuffer (it 1616 is the main reason for having this functionality, actually) and store intermedi 1617ate FFT results out-of-place. 1618 This way the last transposition step can be merged with the write step, 1619 as we can overwrite the output buffer without losing data. 1620\end_layout 1621 1622\begin_layout Standard 1623To estimate if your sequence size is single upload or not, divide the amount 1624 of available shared memory (48KB - Nvidia GPUs with Vulkan/OpenCL API, 1625 64KB - AMD GPUs, 100KB - Nvidia GPUs in CUDA API) by the complex size used 1626 for calculations (8 byte - single precision, 16 byte - double precision). 1627 For 64KB of shared memory, we get 8192 as max single upload single-precision 1628 non-strided FFT, 4096 for double precision. 1629 For strided axes (H and D parts of the layout) these numbers have to be 1630 divided by 4 and 2 respectively to achieve coalescing, resulting in 2048 1631 length for single upload in both precisions. 1632 For more information on coalescing see: coalescing API reference. 1633\end_layout 1634 1635\begin_layout Standard 1636In the case of the Four-Step FFT algorithm, tempBuffer size has to be at 1637 least the same as the default main buffer size. 1638 It does not matter how many uploads are in the Four Step FFT algorithm 1639 - only a single tempBuffer is required. 1640 In this document, all systems that can fit in the shared memory entirely 1641 and be done without the Four Step FFT algorithm (and multiple uploads) 1642 are called single upload systems. 1643\end_layout 1644 1645\begin_layout Standard 1646If the last transposition is not required (the output data is allowed to 1647 be in not unshuffled form) it can be disabled during the configuration 1648 phase. 1649 This way tempBuffer will not be needed and all computations will be done 1650 in-place (unless Bluestein's algorithm is used). 1651 An example use-case of this is convolutions - if the kernel is computed 1652 with the same operation ordering, point-wise multiplication in the frequency 1653 domain is not dependent on the correct data ordering and the inverse FFT 1654 will restore the original layout. 1655\end_layout 1656 1657\begin_layout Subsubsection 1658R2C/C2R FFTs 1659\end_layout 1660 1661\begin_layout Standard 1662A typical approach to a single upload R2C/C2R system is to just set the 1663 imaginary part to zero inside the shared memory and do a simple C2C transform. 1664 This doesn't affect the amount of memory transferred from VRAM and is not 1665 a bad approach as FFT is a memory-bound algorithm, however, this can be 1666 improved in multidimensional (in HDCN part of the layout) case by the compositi 1667on of a single C2C sequence from two real sequences and some write for R2C/read 1668 for C2R post-processing. 1669 Both of these algorithms are implemented in VkFFT. 1670 Note, that R2C/C2R only affects the non-strided axis (W). 1671 All strided axes are still done as C2C. 1672\end_layout 1673 1674\begin_layout Subsubsection 1675R2C/C2R multi-upload FFT algorithm 1676\end_layout 1677 1678\begin_layout Standard 1679For even sequences there exists an easy mapping between R2C/C2R FFTs and 1680 the C2C of half the size. 1681 In this case, all even indices (starting from 0) are read as the real values 1682 of a complex number and all odd indices are read as the imaginary values. 1683 This C2C sequence can be done with the help of the Four-Step FFT algorithm. 1684 When FFT is done, separate post-processing for R2C/pre-processing for C2R 1685 is applied. 1686\end_layout 1687 1688\begin_layout Subsubsection 1689R2R Discrete Cosine Transforms 1690\end_layout 1691 1692\begin_layout Standard 1693There exist many different mappings between DCT and FFT. 1694 As of now, VkFFT has the following algorithms implemented (all single-upload 1695 for now): 1696\end_layout 1697 1698\begin_layout Itemize 1699DCT-I - mapping between R2R and C2C of the 1700\begin_inset Formula $2N-2$ 1701\end_inset 1702 1703 length. 1704 For non-strided axis can use an optimization similar to the R2C/C2R multidimens 1705ional case (setting the imaginary part to the next FFT sequence). 1706\end_layout 1707 1708\begin_layout Itemize 1709DCT-II/DCT-III - mapping between R2R and C2C of the same length. 1710 For non-strided axis can use an optimization similar to the R2C/C2R multidimens 1711ional case (setting the imaginary part to the next FFT sequence). 1712\end_layout 1713 1714\begin_layout Itemize 1715DCT-IV - for even sizes, mapping between R2R and C2C sequence of half-length. 1716 For odd sizes mapping to the FFT of the same length (for non-strided axis 1717 can use an optimization similar to the R2C/C2R multidimensional case (setting 1718 the imaginary part to the next FFT sequence)). 1719\end_layout 1720 1721\begin_layout Subsubsection 1722Register overutilization 1723\end_layout 1724 1725\begin_layout Standard 1726Not an FFT algorithm by itself, but an optimization to do bigger sequences 1727 in a single upload instead of switching to the Four Step FFT algorithm. 1728 The main idea behind it is to use a register file (which is often bigger 1729 than the amount of shared memory) to store the sequence and use shared 1730 memory only as a communication buffer. 1731 This is useful in Vulkan and OpenCL APIs on Nvidia GPU, as they are only 1732 allowed to allocate 48KB of shared memory with a register file having the 1733 size of 256KB. 1734\end_layout 1735 1736\begin_layout Subsubsection 1737Zero padding 1738\end_layout 1739 1740\begin_layout Standard 1741Not an FFT algorithm by itself, but a memory management optimization. 1742 If the user's system has parts that are known to be zero - for example, 1743 when an open system is modeled, to avoid a circular part of the FFT system 1744 has to be padded with zeros up to 2x in each direction. 1745 VkFFT can omit sequences full of zeros and don't perform the corresponding 1746 memory transfers and computations, as the output result will be zero. 1747 This way it is possible to get up to two times speed increase in the 2D 1748 case and up to 3x increase in the 3D case. 1749 1750\end_layout 1751 1752\begin_layout Subsubsection 1753Convolution and cross-correlation support 1754\end_layout 1755 1756\begin_layout Standard 1757With the help of the Convolution theorem, which states that the Fourier 1758 transform of a convolution is the pointwise product of signals Fourier 1759 transforms, it is possible to perform convolution with 1760\begin_inset Formula $NlogN$ 1761\end_inset 1762 1763 complexity, compared to 1764\begin_inset Formula $N^{2}$ 1765\end_inset 1766 1767 complexity of the simple multiplication approach. 1768 This is extremely useful for kernels spanning more than 50 elements in 1769 size. 1770 VkFFT can merge the last step FFT, kernel multiplication in the Fourier 1771 domain and the first step of inverse FFT to provide substantial memory 1772 transfer savings. 1773 Moreover, FFTs of big sequences can be performed without data reordering, 1774 which results in a better locality. 1775 1776\end_layout 1777 1778\begin_layout Subsection 1779VkFFT accuracy 1780\end_layout 1781 1782\begin_layout Standard 1783To measure how VkFFT (single/double/half precision) results compare to cuFFT/roc 1784FFT (single/double/half precision) and FFTW (double precision), multiple 1785 sets of systems covering full supported C2C/R2C+C2R/R2R FFT range are filled 1786 with random complex data on the scale of [-1,1] and one transform was performed 1787 on each system. 1788 Samples 11(single), 12(double), 13(half), 14(non-power of 2 C2C, single), 1789 15(R2C+C2R, single), 16(DCT-I/II/III/IV, single), 17(DCT-I/II/III/IV, double), 1790 18(non-power of 2 C2C, double) are available in VkFFT Benchmark Suite to 1791 perform VkFFT verification on any of the target platforms. 1792 Overall, the Cooley-Tukey algorithm (Stockham autosort) exhibits logarithmic 1793 relative error scaling, similar to those of other GPU FFT libraries. 1794 Typically, the more computationally expensive algorithm is - the worse 1795 its precision is. 1796 So, Bluestein's algorithm has lower accuracy than Stockham autosort algorithm. 1797\end_layout 1798 1799\begin_layout Standard 1800Single precision in VkFFT supports two modes of calculation - by using the 1801 on-chip Special Function Units that can compute sines and cosines on the 1802 go or by using the precomputed on CPU look-up tables. 1803 For Nvidia and AMD GPUs, SFU provide great precision, while Intel iGPUs 1804 and mobile GPUs must use LUT to perform FFTs correctly. 1805\end_layout 1806 1807\begin_layout Standard 1808Double precision in VkFFT also supports two modes of calculation - by using 1809 polynomial sincos approximation and computing them on-chip or by using 1810 precomputed LUT as well. 1811 The second option is the better one, as polynomial sincos approximation 1812 is too compute-heavy for modern GPUs. 1813 It is selected by default on all devices. 1814\end_layout 1815 1816\begin_layout Standard 1817Half precision is currently only supported in the Vulkan backend and is 1818 often experiencing precision problems with the first number of the resulting 1819 FFT sequence, which is the sum of all input numbers. 1820 Half precision is implemented only as a memory trick - all on-chip computations 1821 are done in single precision, but this doesn't help with the first number 1822 problem. 1823 Half precision can use SFU or LUT as well. 1824\end_layout 1825 1826\begin_layout Standard 1827VkFFT also supports mixed-precision operations, where memory storing is 1828 done at lower precision, compared to the on-chip calculations. 1829 For example, it is possible to read data in single precision, do calculations 1830 in double and store data back in single precision. 1831 1832\end_layout 1833 1834\begin_layout Subsection 1835VkFFT additional memory allocations 1836\end_layout 1837 1838\begin_layout Standard 1839In this section, all GPU memory allocations that are done by VkFFT are described. 1840 There are up to three situations when VkFFT allocates memory. 1841 All of the VkFFT allocated memory is freed at the deleteVkFFT call. 1842\end_layout 1843 1844\begin_layout Subsubsection 1845LUT allocations 1846\end_layout 1847 1848\begin_layout Standard 1849This memory is used to store precomputed twiddle factors and phase vectors 1850 used during the computation. 1851 This buffer can have: 1852\end_layout 1853 1854\begin_layout Itemize 1855twiddle factors for each radix stage of Stockham FFT calculation 1856\end_layout 1857 1858\begin_layout Itemize 1859phase vectors used in the Four Step FFT algorithm between stages 1860\end_layout 1861 1862\begin_layout Itemize 1863phase vectors used in DCT-II/III/IV to perform a mapping between R2R and 1864 C2C 1865\end_layout 1866 1867\begin_layout Itemize 1868phase vectors used in post-processing for R2C/pre-processing for C2R for 1869 even length sequences as C2C of half size 1870\end_layout 1871 1872\begin_layout Standard 1873VkFFT manages LUT allocations by itself and they are performed during the 1874 initializeVkFFT call. 1875 LUT are allocated per axis, though some of them can be reused if the axes 1876 have the same LUT. 1877 Inverse and forward FFT plans share the same LUT (conjugation is performed 1878 on-chip). 1879\end_layout 1880 1881\begin_layout Subsubsection 1882The Four-Step FFT algorithm - tempBuffer allocation 1883\end_layout 1884 1885\begin_layout Standard 1886To perform the merging of the transposition with the last upload of an axis, 1887 VkFFT requires additional memory to mimic an out-of-place execution. 1888 This memory is located in tempBuffer and has to be of at least the same 1889 size as the main buffer. 1890 It is possible for the users to allocate it themselves, though if this 1891 is not done, VkFFT can do the allocation automatically (the size of the 1892 tempBuffer will be the same as the main buffer, unless the logical dimensions 1893 of FFT are bigger than user-defined - then, it will allocate the system 1894 with the minimal size, that can cover maximal logical system size used 1895 in any of the axes - see next subsection). 1896\end_layout 1897 1898\begin_layout Subsubsection 1899Bluestein's buffers allocation 1900\end_layout 1901 1902\begin_layout Standard 1903To do Bluestein's FFT algorithm, precomputed sequences 1904\begin_inset Formula $b_{n}=e^{\pi i\frac{n^{2}}{N}}$ 1905\end_inset 1906 1907, 1908\begin_inset Formula $FFT(b_{n})$ 1909\end_inset 1910 1911 and 1912\begin_inset Formula $iFFT(b_{n})$ 1913\end_inset 1914 1915 are required. 1916 For each axis, they can be different and are computed separately (unless 1917 VkFFT can determine that they match, then the buffers are allocated only 1918 once). 1919 Notably, as Bluestein's algorithm pads the sequence length to at least 1920 1921\begin_inset Formula $2N-1$ 1922\end_inset 1923 1924, if it can not be done in a single upload and the Four Step algorithm has 1925 to be used, the intermediate storage required will be bigger than the main 1926 buffer size. 1927 In this case, tempBuffer must always be allocated. 1928 As the padded sequence can be different for each of the dimensions, the 1929 required size of the tempBuffer will also vary. 1930 VkFFT determines the biggest size needed among axes and allocated tempBuffer 1931 of this size. 1932\end_layout 1933 1934\begin_layout Standard 1935\begin_inset Newpage newpage 1936\end_inset 1937 1938 1939\end_layout 1940 1941\begin_layout Section 1942VkFFT API Reference 1943\end_layout 1944 1945\begin_layout Standard 1946This section covers error codes, API functions that can be used by the user 1947 and configuration parameters. 1948\end_layout 1949 1950\begin_layout Subsection 1951Return value VkFFTResult 1952\end_layout 1953 1954\begin_layout Standard 1955All VkFFT Library return values except for VKFFT_SUCCESS are used in case 1956 of a failure and provide information on what has gone wrong. 1957 VkFFTResult is unified among different backends, though some of its values 1958 may not be used in specific backends. 1959 Possible return values of VkFFTResult are defined as following: 1960\end_layout 1961 1962\begin_layout Standard 1963\begin_inset ERT 1964status open 1965 1966\begin_layout Plain Layout 1967 1968 1969\backslash 1970begin{mdframed}[backgroundcolor=bg] 1971\end_layout 1972 1973\begin_layout Plain Layout 1974 1975 1976\backslash 1977begin{minted}[tabsize=4,obeytabs,breaklines]{C} 1978\end_layout 1979 1980\begin_layout Plain Layout 1981 1982typedef enum VkFFTResult { 1983\end_layout 1984 1985\begin_layout Plain Layout 1986 1987VKFFT_SUCCESS = 0, // The VkFFT operation was successful 1988\end_layout 1989 1990\begin_layout Plain Layout 1991 1992VKFFT_ERROR_MALLOC_FAILED = 1, // Some malloc call inside VkFFT has failed. 1993 Report this to the GitHub repo 1994\end_layout 1995 1996\begin_layout Plain Layout 1997 1998VKFFT_ERROR_INSUFFICIENT_CODE_BUFFER = 2, // Generated kernel is bigger 1999 than default kernel array. 2000 Increase it with maxCodeLength parameter of configuration. 2001\end_layout 2002 2003\begin_layout Plain Layout 2004 2005VKFFT_ERROR_INSUFFICIENT_TEMP_BUFFER = 3, // Temporary string used in kernel 2006 generation is bigger than default temporary string array. 2007 Increase it with maxTempLength parameter of configuration. 2008\end_layout 2009 2010\begin_layout Plain Layout 2011 2012VKFFT_ERROR_PLAN_NOT_INITIALIZED = 4, // Code attempts to use uninitialized 2013 plan (it is zero inside VkFFTApplication) 2014\end_layout 2015 2016\begin_layout Plain Layout 2017 2018VKFFT_ERROR_NULL_TEMP_PASSED = 5, // Internal kernel generation error 2019\end_layout 2020 2021\begin_layout Plain Layout 2022 2023VKFFT_ERROR_INVALID_PHYSICAL_DEVICE = 1001, // No physical device is provided 2024 (Vulkan API) 2025\end_layout 2026 2027\begin_layout Plain Layout 2028 2029VKFFT_ERROR_INVALID_DEVICE = 1002, // No device is provided (All APIs) 2030\end_layout 2031 2032\begin_layout Plain Layout 2033 2034VKFFT_ERROR_INVALID_QUEUE = 1003, // No queue is provided (Vulkan API) 2035\end_layout 2036 2037\begin_layout Plain Layout 2038 2039VKFFT_ERROR_INVALID_COMMAND_POOL = 1004, // No command pool is provided 2040 (Vulkan API) 2041\end_layout 2042 2043\begin_layout Plain Layout 2044 2045VKFFT_ERROR_INVALID_FENCE = 1005, // No fence is provided (Vulkan API) 2046\end_layout 2047 2048\begin_layout Plain Layout 2049 2050VKFFT_ERROR_ONLY_FORWARD_FFT_INITIALIZED = 1006, // VkFFT tries to access 2051 inverse FFT plan, when appliction is created with makeForwardPlanOnly flag 2052\end_layout 2053 2054\begin_layout Plain Layout 2055 2056VKFFT_ERROR_ONLY_INVERSE_FFT_INITIALIZED = 1007, // VkFFT tries to access 2057 forward FFT plan, when appliction is created with makeInversePlanOnly flag 2058\end_layout 2059 2060\begin_layout Plain Layout 2061 2062VKFFT_ERROR_INVALID_CONTEXT = 1008, // No context is provided (OpenCL API) 2063\end_layout 2064 2065\begin_layout Plain Layout 2066 2067VKFFT_ERROR_INVALID_PLATFORM = 1009, // No platform is provided (OpenCL 2068 API) 2069\end_layout 2070 2071\begin_layout Plain Layout 2072 2073VKFFT_ERROR_EMPTY_FILE = 1011, 2074\end_layout 2075 2076\begin_layout Plain Layout 2077 2078VKFFT_ERROR_EMPTY_FFTdim = 2001, // Number of dimensions is not provided 2079 in the configuration 2080\end_layout 2081 2082\begin_layout Plain Layout 2083 2084VKFFT_ERROR_EMPTY_size = 2002, // Array of dimensions is not provided in 2085 the configuration 2086\end_layout 2087 2088\begin_layout Plain Layout 2089 2090VKFFT_ERROR_EMPTY_bufferSize = 2003, // Buffer size has to be provided during 2091 the application creation 2092\end_layout 2093 2094\begin_layout Plain Layout 2095 2096VKFFT_ERROR_EMPTY_buffer = 2004, // Buffer has te be specified either at 2097 the application creation stage or during launch through VkFFTLaunchParams 2098 struct 2099\end_layout 2100 2101\begin_layout Plain Layout 2102 2103VKFFT_ERROR_EMPTY_tempBufferSize = 2005, // Same error as VKFFT_ERROR_EMPTY_buff 2104erSize if userTempBuffer is enabled 2105\end_layout 2106 2107\begin_layout Plain Layout 2108 2109VKFFT_ERROR_EMPTY_tempBuffer = 2006, // Same error as VKFFT_ERROR_EMPTY_buffer 2110 if userTempBuffer is enabled 2111\end_layout 2112 2113\begin_layout Plain Layout 2114 2115VKFFT_ERROR_EMPTY_inputBufferSize = 2007, // Same error as VKFFT_ERROR_EMPTY_buf 2116ferSize if isInputFormatted is enabled 2117\end_layout 2118 2119\begin_layout Plain Layout 2120 2121VKFFT_ERROR_EMPTY_inputBuffer = 2008, // Same error as VKFFT_ERROR_EMPTY_buffer 2122 if isInputFormatted is enabled 2123\end_layout 2124 2125\begin_layout Plain Layout 2126 2127VKFFT_ERROR_EMPTY_outputBufferSize = 2009, // Same error as VKFFT_ERROR_EMPTY_bu 2128fferSize if isOutputFormatted is enabled 2129\end_layout 2130 2131\begin_layout Plain Layout 2132 2133VKFFT_ERROR_EMPTY_outputBuffer = 2010, // Same error as VKFFT_ERROR_EMPTY_buffer 2134 if isOutputFormatted is enabled 2135\end_layout 2136 2137\begin_layout Plain Layout 2138 2139VKFFT_ERROR_EMPTY_kernelSize = 2011, // Same error as VKFFT_ERROR_EMPTY_bufferSi 2140ze if performConvolution is enabled 2141\end_layout 2142 2143\begin_layout Plain Layout 2144 2145VKFFT_ERROR_EMPTY_kernel = 2012, // Same error as VKFFT_ERROR_EMPTY_buffer 2146 if performConvolution is enabled 2147\end_layout 2148 2149\begin_layout Plain Layout 2150 2151VKFFT_ERROR_EMPRY_useCustomBluesteinPaddingPattern_arrays = 2014, // pointers 2152 to primeSizes or paddedSizes arrays are zero when useCustomBluesteinPaddingPatt 2153ern is enabled 2154\end_layout 2155 2156\begin_layout Plain Layout 2157 2158VKFFT_ERROR_UNSUPPORTED_RADIX = 3001, // VkFFT has encountered unsupported 2159 radix (more than 13) during decomposition and Bluestein's FFT fallback 2160 did not work 2161\end_layout 2162 2163\begin_layout Plain Layout 2164 2165VKFFT_ERROR_UNSUPPORTED_FFT_LENGTH = 3002, // VkFFT can not do this sequence 2166 length currently - it requires mor than three-upload Four step FFT 2167\end_layout 2168 2169\begin_layout Plain Layout 2170 2171VKFFT_ERROR_UNSUPPORTED_FFT_LENGTH_R2C = 3003, // VkFFT can not do this 2172 sequence length currently - odd multi-upload R2C/C2R FFTs 2173\end_layout 2174 2175\begin_layout Plain Layout 2176 2177VKFFT_ERROR_UNSUPPORTED_FFT_LENGTH_DCT = 3004, // VkFFT can not do this 2178 sequence length currently - multi-upload R2R transforms, odd DCT-IV transforms 2179\end_layout 2180 2181\begin_layout Plain Layout 2182 2183VKFFT_ERROR_UNSUPPORTED_FFT_OMIT = 3005, // VkFFT can not omit sequences 2184 in convolution calculations and R2C/C2R case 2185\end_layout 2186 2187\begin_layout Plain Layout 2188 2189VKFFT_ERROR_FAILED_TO_ALLOCATE = 4001, // VkFFT failed to allocate GPU memory 2190\end_layout 2191 2192\begin_layout Plain Layout 2193 2194VKFFT_ERROR_FAILED_TO_MAP_MEMORY = 4002, // 4002-4052 are handlers for errors 2195 of used backend APIs. 2196 They may indicate a driver failure. 2197 If they are thrown - report to the GitHub repo 2198\end_layout 2199 2200\begin_layout Plain Layout 2201 2202VKFFT_ERROR_FAILED_TO_ALLOCATE_COMMAND_BUFFERS = 4003, 2203\end_layout 2204 2205\begin_layout Plain Layout 2206 2207VKFFT_ERROR_FAILED_TO_BEGIN_COMMAND_BUFFER = 4004, 2208\end_layout 2209 2210\begin_layout Plain Layout 2211 2212VKFFT_ERROR_FAILED_TO_END_COMMAND_BUFFER = 4005, 2213\end_layout 2214 2215\begin_layout Plain Layout 2216 2217VKFFT_ERROR_FAILED_TO_SUBMIT_QUEUE = 4006, 2218\end_layout 2219 2220\begin_layout Plain Layout 2221 2222VKFFT_ERROR_FAILED_TO_WAIT_FOR_FENCES = 4007, 2223\end_layout 2224 2225\begin_layout Plain Layout 2226 2227VKFFT_ERROR_FAILED_TO_RESET_FENCES = 4008, 2228\end_layout 2229 2230\begin_layout Plain Layout 2231 2232VKFFT_ERROR_FAILED_TO_CREATE_DESCRIPTOR_POOL = 4009, 2233\end_layout 2234 2235\begin_layout Plain Layout 2236 2237VKFFT_ERROR_FAILED_TO_CREATE_DESCRIPTOR_SET_LAYOUT = 4010, 2238\end_layout 2239 2240\begin_layout Plain Layout 2241 2242VKFFT_ERROR_FAILED_TO_ALLOCATE_DESCRIPTOR_SETS = 4011, 2243\end_layout 2244 2245\begin_layout Plain Layout 2246 2247VKFFT_ERROR_FAILED_TO_CREATE_PIPELINE_LAYOUT = 4012, 2248\end_layout 2249 2250\begin_layout Plain Layout 2251 2252VKFFT_ERROR_FAILED_SHADER_PREPROCESS = 4013, 2253\end_layout 2254 2255\begin_layout Plain Layout 2256 2257VKFFT_ERROR_FAILED_SHADER_PARSE = 4014, 2258\end_layout 2259 2260\begin_layout Plain Layout 2261 2262VKFFT_ERROR_FAILED_SHADER_LINK = 4015, 2263\end_layout 2264 2265\begin_layout Plain Layout 2266 2267VKFFT_ERROR_FAILED_SPIRV_GENERATE = 4016, 2268\end_layout 2269 2270\begin_layout Plain Layout 2271 2272VKFFT_ERROR_FAILED_TO_CREATE_SHADER_MODULE = 4017, 2273\end_layout 2274 2275\begin_layout Plain Layout 2276 2277VKFFT_ERROR_FAILED_TO_CREATE_INSTANCE = 4018, 2278\end_layout 2279 2280\begin_layout Plain Layout 2281 2282VKFFT_ERROR_FAILED_TO_SETUP_DEBUG_MESSENGER = 4019, 2283\end_layout 2284 2285\begin_layout Plain Layout 2286 2287VKFFT_ERROR_FAILED_TO_FIND_PHYSICAL_DEVICE = 4020, 2288\end_layout 2289 2290\begin_layout Plain Layout 2291 2292VKFFT_ERROR_FAILED_TO_CREATE_DEVICE = 4021, 2293\end_layout 2294 2295\begin_layout Plain Layout 2296 2297VKFFT_ERROR_FAILED_TO_CREATE_FENCE = 4022, 2298\end_layout 2299 2300\begin_layout Plain Layout 2301 2302VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_POOL = 4023, 2303\end_layout 2304 2305\begin_layout Plain Layout 2306 2307VKFFT_ERROR_FAILED_TO_CREATE_BUFFER = 4024, 2308\end_layout 2309 2310\begin_layout Plain Layout 2311 2312VKFFT_ERROR_FAILED_TO_ALLOCATE_MEMORY = 4025, 2313\end_layout 2314 2315\begin_layout Plain Layout 2316 2317VKFFT_ERROR_FAILED_TO_BIND_BUFFER_MEMORY = 4026, 2318\end_layout 2319 2320\begin_layout Plain Layout 2321 2322VKFFT_ERROR_FAILED_TO_FIND_MEMORY = 4027, 2323\end_layout 2324 2325\begin_layout Plain Layout 2326 2327VKFFT_ERROR_FAILED_TO_SYNCHRONIZE = 4028, 2328\end_layout 2329 2330\begin_layout Plain Layout 2331 2332VKFFT_ERROR_FAILED_TO_COPY = 4029, 2333\end_layout 2334 2335\begin_layout Plain Layout 2336 2337VKFFT_ERROR_FAILED_TO_CREATE_PROGRAM = 4030, 2338\end_layout 2339 2340\begin_layout Plain Layout 2341 2342VKFFT_ERROR_FAILED_TO_COMPILE_PROGRAM = 4031, 2343\end_layout 2344 2345\begin_layout Plain Layout 2346 2347VKFFT_ERROR_FAILED_TO_GET_CODE_SIZE = 4032, 2348\end_layout 2349 2350\begin_layout Plain Layout 2351 2352VKFFT_ERROR_FAILED_TO_GET_CODE = 4033, 2353\end_layout 2354 2355\begin_layout Plain Layout 2356 2357VKFFT_ERROR_FAILED_TO_DESTROY_PROGRAM = 4034, 2358\end_layout 2359 2360\begin_layout Plain Layout 2361 2362VKFFT_ERROR_FAILED_TO_LOAD_MODULE = 4035, 2363\end_layout 2364 2365\begin_layout Plain Layout 2366 2367VKFFT_ERROR_FAILED_TO_GET_FUNCTION = 4036, 2368\end_layout 2369 2370\begin_layout Plain Layout 2371 2372VKFFT_ERROR_FAILED_TO_SET_DYNAMIC_SHARED_MEMORY = 4037, 2373\end_layout 2374 2375\begin_layout Plain Layout 2376 2377VKFFT_ERROR_FAILED_TO_MODULE_GET_GLOBAL = 4038, 2378\end_layout 2379 2380\begin_layout Plain Layout 2381 2382VKFFT_ERROR_FAILED_TO_LAUNCH_KERNEL = 4039, 2383\end_layout 2384 2385\begin_layout Plain Layout 2386 2387VKFFT_ERROR_FAILED_TO_EVENT_RECORD = 4040, 2388\end_layout 2389 2390\begin_layout Plain Layout 2391 2392VKFFT_ERROR_FAILED_TO_ADD_NAME_EXPRESSION = 4041, 2393\end_layout 2394 2395\begin_layout Plain Layout 2396 2397VKFFT_ERROR_FAILED_TO_INITIALIZE = 4042, 2398\end_layout 2399 2400\begin_layout Plain Layout 2401 2402VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID = 4043, 2403\end_layout 2404 2405\begin_layout Plain Layout 2406 2407VKFFT_ERROR_FAILED_TO_GET_DEVICE = 4044, 2408\end_layout 2409 2410\begin_layout Plain Layout 2411 2412VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT = 4045, 2413\end_layout 2414 2415\begin_layout Plain Layout 2416 2417VKFFT_ERROR_FAILED_TO_CREATE_PIPELINE = 4046, 2418\end_layout 2419 2420\begin_layout Plain Layout 2421 2422VKFFT_ERROR_FAILED_TO_SET_KERNEL_ARG = 4047, 2423\end_layout 2424 2425\begin_layout Plain Layout 2426 2427VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QUEUE = 4048, 2428\end_layout 2429 2430\begin_layout Plain Layout 2431 2432VKFFT_ERROR_FAILED_TO_RELEASE_COMMAND_QUEUE = 4049, 2433\end_layout 2434 2435\begin_layout Plain Layout 2436 2437VKFFT_ERROR_FAILED_TO_ENUMERATE_DEVICES = 4050, 2438\end_layout 2439 2440\begin_layout Plain Layout 2441 2442VKFFT_ERROR_FAILED_TO_GET_ATTRIBUTE = 4051, 2443\end_layout 2444 2445\begin_layout Plain Layout 2446 2447VKFFT_ERROR_FAILED_TO_CREATE_EVENT = 4052, 2448\end_layout 2449 2450\begin_layout Plain Layout 2451 2452VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_LIST = 4053, 2453\end_layout 2454 2455\begin_layout Plain Layout 2456 2457VKFFT_ERROR_FAILED_TO_DESTROY_COMMAND_LIST = 4054, 2458\end_layout 2459 2460\begin_layout Plain Layout 2461 2462VKFFT_ERROR_FAILED_TO_SUBMIT_BARRIER = 4055 2463\end_layout 2464 2465\begin_layout Plain Layout 2466 2467} VkFFTResult; 2468\end_layout 2469 2470\begin_layout Plain Layout 2471 2472 2473\backslash 2474end{minted} 2475\end_layout 2476 2477\begin_layout Plain Layout 2478 2479 2480\backslash 2481end{mdframed} 2482\end_layout 2483 2484\end_inset 2485 2486 2487\end_layout 2488 2489\begin_layout Subsection 2490VkFFT application management functions 2491\end_layout 2492 2493\begin_layout Standard 2494VkFFT has a unified plan management model - all different transform types/ 2495 dimensionalities/ precision use the same calls with configuration done 2496 through VkFFTConfiguration struct. 2497 This section shows how to initialize/use/free VkFFT with this unified model, 2498 while the next one will go into how to configure VkFFTConfiguration correctly. 2499 All of the functions operate on VkFFTApplication and VkFFTConfiguration 2500 assuming they have been zero-initialized before usage, so do not forget 2501 to do this when initializing: 2502\end_layout 2503 2504\begin_layout Standard 2505\begin_inset ERT 2506status open 2507 2508\begin_layout Plain Layout 2509 2510 2511\backslash 2512begin{mdframed}[backgroundcolor=bg] 2513\end_layout 2514 2515\begin_layout Plain Layout 2516 2517 2518\backslash 2519begin{minted}[tabsize=4,obeytabs,breaklines]{C} 2520\end_layout 2521 2522\begin_layout Plain Layout 2523 2524VkFFTConfiguration configuration = {}; 2525\end_layout 2526 2527\begin_layout Plain Layout 2528 2529VkFFTApplication app = {}; 2530\end_layout 2531 2532\begin_layout Plain Layout 2533 2534 2535\backslash 2536end{minted} 2537\end_layout 2538 2539\begin_layout Plain Layout 2540 2541 2542\backslash 2543end{mdframed} 2544\end_layout 2545 2546\end_inset 2547 2548 2549\end_layout 2550 2551\begin_layout Subsubsection 2552Function initializeVkFFT() 2553\end_layout 2554 2555\begin_layout Standard 2556\begin_inset ERT 2557status open 2558 2559\begin_layout Plain Layout 2560 2561 2562\backslash 2563begin{mdframed}[backgroundcolor=bg] 2564\end_layout 2565 2566\begin_layout Plain Layout 2567 2568 2569\backslash 2570begin{minted}[tabsize=4,obeytabs,breaklines]{C} 2571\end_layout 2572 2573\begin_layout Plain Layout 2574 2575VkFFTResult initializeVkFFT(VkFFTApplication* app, VkFFTConfiguration inputLaunc 2576hConfiguration) 2577\end_layout 2578 2579\begin_layout Plain Layout 2580 2581 2582\backslash 2583end{minted} 2584\end_layout 2585 2586\begin_layout Plain Layout 2587 2588 2589\backslash 2590end{mdframed} 2591\end_layout 2592 2593\end_inset 2594 2595 2596\end_layout 2597 2598\begin_layout Standard 2599Creates an FFT application (collection of forward and inverse plans). 2600 As forward and inverse FFTs may have different memory layouts, can have 2601 different normalizations - they are done as separate internal plans inside 2602 VkFFTApplication. 2603 This call assumes the application to be zero-initialized, so can be only 2604 done once on a particular application, until it is deleted. 2605\end_layout 2606 2607\begin_layout Standard 2608If the initializeVkFFT call fails, it frees all allocated by VkFFT CPU/GPU 2609 resources and sets the application to zero. 2610 VkFFTResult is returned with an error code corresponding to what went wrong. 2611\end_layout 2612 2613\begin_layout Standard 2614In case of success, VkFFTApplication will contain initialized plans with 2615 compiled kernels ready for execution with VKFFT_SUCCESS returned. 2616\end_layout 2617 2618\begin_layout Subsubsection 2619Function VkFFTAppend() 2620\end_layout 2621 2622\begin_layout Standard 2623\begin_inset ERT 2624status open 2625 2626\begin_layout Plain Layout 2627 2628 2629\backslash 2630begin{mdframed}[backgroundcolor=bg] 2631\end_layout 2632 2633\begin_layout Plain Layout 2634 2635 2636\backslash 2637begin{minted}[tabsize=4,obeytabs,breaklines]{C} 2638\end_layout 2639 2640\begin_layout Plain Layout 2641 2642VkFFTResult VkFFTAppend(VkFFTApplication* app, int inverse, VkFFTLaunchParams* 2643 launchParams) 2644\end_layout 2645 2646\begin_layout Plain Layout 2647 2648 2649\backslash 2650end{minted} 2651\end_layout 2652 2653\begin_layout Plain Layout 2654 2655 2656\backslash 2657end{mdframed} 2658\end_layout 2659 2660\end_inset 2661 2662 2663\end_layout 2664 2665\begin_layout Standard 2666Performs FFT in the int inverse direction (-1 for forward FFT, 1 for inverse 2667 FFT). 2668 FFT plans are selected from the VkFFTApplication collection automatically. 2669 VkFFTApplication must be initialized with initializeVkFFT call before. 2670 VkFFTLaunchParams struct allows for pre-launch configuration of some parameters 2671, namely: 2672\end_layout 2673 2674\begin_layout Itemize 2675buffer - similar to how FFTW/cuFFT expects input/output data pointers in 2676 *execC2C (and other) function calls, VkFFT allows specifying memory used 2677 for computations at launch. 2678 It must have the same size/layout/strides as defined during the application 2679 creation. 2680\end_layout 2681 2682\begin_layout Itemize 2683inputBuffer/outputBuffer/tempBuffer/kernel - other buffers can also be specified 2684 at launch. 2685 In addition to them having the same size/layout/strides as defined during 2686 the application creation, the application must be created with flags enabling 2687 the corresponding buffer usage: isInputFormatted/isOutputFormatted/userTempBuff 2688er/performConvolution respectively. 2689\end_layout 2690 2691\begin_layout Itemize 2692bufferOffset/tempBufferOffset/inputBufferOffset/outputBufferOffset/kernelOffset 2693 - specify if VkFFT has to offset the first element position inside the 2694 corresponding buffer. 2695 In bytes. 2696 Default 0. 2697 specifyOffsetsAtLaunch parameter must be enabled during the initializeVkFFT 2698 call before. 2699 2700\end_layout 2701 2702\begin_layout Standard 2703Depending on the API, the execution model may vary and require additional 2704 information at launch: 2705\end_layout 2706 2707\begin_layout Itemize 2708Vulkan API: VkFFT appends a sequence of vkCmdDispatch calls to the user-defined 2709 VkCommandBuffer (with respective push constants/descriptor sets/pipelines/memor 2710y barriers bindings). 2711 VkCommandBuffer must be provided as a pointer in VkFFTLaunchParams. 2712 VkCommandBuffer must be in the writing stage, started with vkBeginCommandBuffer 2713 call. 2714 After VkFFTAppend has finished, provided VkCommandBuffer will contain a 2715 sequence of operations performing FFT. 2716 The first call of the sequence has no input memory barrier, the last call 2717 has one, ensuring FFT has finished execution. 2718\end_layout 2719 2720\begin_layout Itemize 2721CUDA/HIP API: if the user wants to use streams, they have to be provided 2722 during the application configuration stage. 2723 VkFFTAppend performs a series of cuLaunchKernel, which are sequential if 2724 appended to one stream and synchronized if appended to multiple streams. 2725\end_layout 2726 2727\begin_layout Itemize 2728OpenCL API: similar to Vulkan, VkFFT appends a sequence of clEnqueueNDRangeKerne 2729l calls to user-defined cl_command_queue. 2730 Currently, they are all assumed to be sequential. 2731 cl_command_queue must be provided as a pointer in VkFFTLaunchParams. 2732 2733\end_layout 2734 2735\begin_layout Itemize 2736Level Zero API: similar to Vulkan, VkFFT appends a sequence of zeCommandListAppe 2737ndLaunchKernel calls to user-defined command list ze_command_list_handle_t. 2738 They have execution barriers between. 2739 ze_command_list_handle_t must be provided as a pointer in VkFFTLaunchParams. 2740 2741\end_layout 2742 2743\begin_layout Itemize 2744Metal API: similar to Vulkan, VkFFT appends a sequence of dispatchThreads 2745 calls to user-defined command encoder MTL::ComputeCommandEncoder. 2746 MTL::ComputeCommandEncoder and its MTL::CommandBuffer must be provided 2747 as a pointer in VkFFTLaunchParams. 2748 2749\end_layout 2750 2751\begin_layout Standard 2752If VkFFT fails during the VkFFTAppend call, it will not free the application 2753 and allocated there resources - use a separate call for that. 2754\end_layout 2755 2756\begin_layout Subsubsection 2757Function deleteVkFFT() 2758\end_layout 2759 2760\begin_layout Standard 2761\begin_inset ERT 2762status open 2763 2764\begin_layout Plain Layout 2765 2766 2767\backslash 2768begin{mdframed}[backgroundcolor=bg] 2769\end_layout 2770 2771\begin_layout Plain Layout 2772 2773 2774\backslash 2775begin{minted}[tabsize=4,obeytabs,breaklines]{C} 2776\end_layout 2777 2778\begin_layout Plain Layout 2779 2780void deleteVkFFT(VkFFTApplication* app) 2781\end_layout 2782 2783\begin_layout Plain Layout 2784 2785 2786\backslash 2787end{minted} 2788\end_layout 2789 2790\begin_layout Plain Layout 2791 2792 2793\backslash 2794end{mdframed} 2795\end_layout 2796 2797\end_inset 2798 2799 2800\end_layout 2801 2802\begin_layout Standard 2803Performs deallocation of resources used in the provided application. 2804 Returns application to the zero-initialized state. 2805\end_layout 2806 2807\begin_layout Subsubsection 2808Function VkFFTGetVersion() 2809\end_layout 2810 2811\begin_layout Standard 2812\begin_inset ERT 2813status open 2814 2815\begin_layout Plain Layout 2816 2817 2818\backslash 2819begin{mdframed}[backgroundcolor=bg] 2820\end_layout 2821 2822\begin_layout Plain Layout 2823 2824 2825\backslash 2826begin{minted}[tabsize=4,obeytabs,breaklines]{C} 2827\end_layout 2828 2829\begin_layout Plain Layout 2830 2831int VkFFTGetVersion() 2832\end_layout 2833 2834\begin_layout Plain Layout 2835 2836 2837\backslash 2838end{minted} 2839\end_layout 2840 2841\begin_layout Plain Layout 2842 2843 2844\backslash 2845end{mdframed} 2846\end_layout 2847 2848\end_inset 2849 2850 2851\end_layout 2852 2853\begin_layout Standard 2854Returns the version of the VkFFT library in the X.XX.XX format (without dots). 2855\end_layout 2856 2857\begin_layout Subsection 2858VkFFT configuration 2859\end_layout 2860 2861\begin_layout Standard 2862This section will cover all the parameters that can be specified in the 2863 VkFFTConfiguration struct. 2864 It will start with a short description of the struct (intended to be used 2865 as a cheat sheet), then go for each field in detail. 2866\end_layout 2867 2868\begin_layout Standard 2869\begin_inset ERT 2870status open 2871 2872\begin_layout Plain Layout 2873 2874 2875\backslash 2876begin{mdframed}[backgroundcolor=bg] 2877\end_layout 2878 2879\begin_layout Plain Layout 2880 2881 2882\backslash 2883begin{minted}[tabsize=4,obeytabs,breaklines]{C} 2884\end_layout 2885 2886\begin_layout Plain Layout 2887 2888typedef struct { 2889\end_layout 2890 2891\begin_layout Plain Layout 2892 2893// Required parameters: 2894\end_layout 2895 2896\begin_layout Plain Layout 2897 2898uint64_t FFTdim; // FFT dimensionality (1, 2 or 3) 2899\end_layout 2900 2901\begin_layout Plain Layout 2902 2903uint64_t size[3]; // WHD - system dimensions 2904\end_layout 2905 2906\begin_layout Plain Layout 2907 2908#if(VKFFT_BACKEND==0) //Vulkan API 2909\end_layout 2910 2911\begin_layout Plain Layout 2912 2913VkPhysicalDevice* physicalDevice; // Pointer to Vulkan physical device, 2914 obtained from vkEnumeratePhysicalDevices 2915\end_layout 2916 2917\begin_layout Plain Layout 2918 2919VkDevice* device; // Pointer to Vulkan device, created with vkCreateDevice 2920\end_layout 2921 2922\begin_layout Plain Layout 2923 2924VkQueue* queue; // Pointer to Vulkan queue, created with vkGetDeviceQueue 2925\end_layout 2926 2927\begin_layout Plain Layout 2928 2929VkCommandPool* commandPool; // Pointer to Vulkan command pool, created with 2930 vkCreateCommandPool 2931\end_layout 2932 2933\begin_layout Plain Layout 2934 2935VkFence* fence; // Pointer to Vulkan fence, created with vkCreateFence 2936\end_layout 2937 2938\begin_layout Plain Layout 2939 2940uint64_t isCompilerInitialized; // Specify if glslang compiler has been 2941 intialized before (0 - off, 1 - on). 2942 Default 0 2943\end_layout 2944 2945\begin_layout Plain Layout 2946 2947#elif(VKFFT_BACKEND==1) //CUDA API 2948\end_layout 2949 2950\begin_layout Plain Layout 2951 2952CUdevice* device; // Pointer to CUDA device, obtained from cuDeviceGet 2953\end_layout 2954 2955\begin_layout Plain Layout 2956 2957cudaStream_t* stream; // Pointer to streams (can be more than 1), where 2958 to execute the kernels. 2959 Deafult 0 2960\end_layout 2961 2962\begin_layout Plain Layout 2963 2964uint64_t num_streams; // Try to submit CUDA kernels in multiple streams 2965 for asynchronous execution. 2966 Default 1 2967\end_layout 2968 2969\begin_layout Plain Layout 2970 2971#elif(VKFFT_BACKEND==2) //HIP API 2972\end_layout 2973 2974\begin_layout Plain Layout 2975 2976hipDevice_t* device; // Pointer to HIP device, obtained from hipDeviceGet 2977\end_layout 2978 2979\begin_layout Plain Layout 2980 2981hipStream_t* stream; // Pointer to streams (can be more than 1), where to 2982 execute the kernels. 2983 Deafult 0 2984\end_layout 2985 2986\begin_layout Plain Layout 2987 2988uint64_t num_streams; // Try to submit HIP kernels in multiple streams for 2989 asynchronous execution. 2990 Default 1 2991\end_layout 2992 2993\begin_layout Plain Layout 2994 2995#elif(VKFFT_BACKEND==3) //OpenCL API 2996\end_layout 2997 2998\begin_layout Plain Layout 2999 3000cl_platform_id* platform; // NOT REQUIRED 3001\end_layout 3002 3003\begin_layout Plain Layout 3004 3005cl_device_id* device; // Pointer to OpenCL device, obtained from clGetDeviceIDs 3006\end_layout 3007 3008\begin_layout Plain Layout 3009 3010cl_context* context; // Pointer to OpenCL context, obtained from clCreateContext 3011\end_layout 3012 3013\begin_layout Plain Layout 3014 3015#elif(VKFFT_BACKEND==4) //Level Zero API 3016\end_layout 3017 3018\begin_layout Plain Layout 3019 3020ze_device_handle_t* device; // Pointer to Level Zero device, obtained from 3021 zeDeviceGet 3022\end_layout 3023 3024\begin_layout Plain Layout 3025 3026ze_context_handle_t* context; // Pointer to Level Zero context, obtained 3027 from zeContextCreate 3028\end_layout 3029 3030\begin_layout Plain Layout 3031 3032ze_command_queue_handle_t* commandQueue; // Pointer to Level Zero command 3033 queue with compute and copy capabilities, obtained from zeCommandQueueCreate 3034\end_layout 3035 3036\begin_layout Plain Layout 3037 3038uint32_t commandQueueID; // ID of the commandQueue with compute and copy 3039 capabilities 3040\end_layout 3041 3042\begin_layout Plain Layout 3043 3044#elif(VKFFT_BACKEND==5) 3045\end_layout 3046 3047\begin_layout Plain Layout 3048 3049MTL::Device* device; // Pointer to Metal device, obtained from MTL::CopyAllDevic 3050es 3051\end_layout 3052 3053\begin_layout Plain Layout 3054 3055MTL::CommandQueue* queue; // Pointer to Metal queue, obtained from device->newCo 3056mmandQueue() 3057\end_layout 3058 3059\begin_layout Plain Layout 3060 3061#endif 3062\end_layout 3063 3064\begin_layout Plain Layout 3065 3066\end_layout 3067 3068\begin_layout Plain Layout 3069 3070// Data parameters (buffers can be specified at launch): 3071\end_layout 3072 3073\begin_layout Plain Layout 3074 3075uint64_t userTempBuffer; // Buffer allocated by app automatically if needed 3076 to reorder Four step algorithm. 3077 Setting to non zero value enables manual user allocation (0 - off, 1 - 3078 on) 3079\end_layout 3080 3081\begin_layout Plain Layout 3082 3083uint64_t bufferNum; // Multiple buffer sequence storage is Vulkan only. 3084 Default 1 3085\end_layout 3086 3087\begin_layout Plain Layout 3088 3089uint64_t tempBufferNum; // Multiple buffer sequence storage is Vulkan only. 3090 Default 1, buffer allocated by app automatically if needed to reorder Four 3091 step algorithm. 3092 Setting to non zero value enables manual user allocation 3093\end_layout 3094 3095\begin_layout Plain Layout 3096 3097uint64_t inputBufferNum; // Multiple buffer sequence storage is Vulkan only. 3098 Default 1, if isInputFormatted is enabled 3099\end_layout 3100 3101\begin_layout Plain Layout 3102 3103uint64_t outputBufferNum; // Multiple buffer sequence storage is Vulkan 3104 only. 3105 Default 1, if isOutputFormatted is enabled 3106\end_layout 3107 3108\begin_layout Plain Layout 3109 3110uint64_t kernelNum; // Multiple buffer sequence storage is Vulkan only. 3111 Default 1, if performConvolution is enabled 3112\end_layout 3113 3114\begin_layout Plain Layout 3115 3116uint64_t* bufferSize; // Array of buffers sizes in bytes 3117\end_layout 3118 3119\begin_layout Plain Layout 3120 3121uint64_t* tempBufferSize; // Array of temp buffers sizes in bytes. 3122 Default set to bufferSize sum, buffer allocated by app automatically if 3123 needed to reorder Four step algorithm. 3124 Setting to non zero value enables manual user allocation 3125\end_layout 3126 3127\begin_layout Plain Layout 3128 3129uint64_t* inputBufferSize; // Array of input buffers sizes in bytes, if 3130 isInputFormatted is enabled 3131\end_layout 3132 3133\begin_layout Plain Layout 3134 3135uint64_t* outputBufferSize; // Array of output buffers sizes in bytes, if 3136 isOutputFormatted is enabled 3137\end_layout 3138 3139\begin_layout Plain Layout 3140 3141uint64_t* kernelSize; // Array of kernel buffers sizes in bytes, if performConvo 3142lution is enabled 3143\end_layout 3144 3145\begin_layout Plain Layout 3146 3147#if(VKFFT_BACKEND==0) //Vulkan API 3148\end_layout 3149 3150\begin_layout Plain Layout 3151 3152VkBuffer* buffer; // Pointer to array of buffers (or one buffer) used for 3153 computations 3154\end_layout 3155 3156\begin_layout Plain Layout 3157 3158VkBuffer* tempBuffer; // Needed if reorderFourStep is enabled to transpose 3159 the array. 3160 Same sum size or bigger as buffer (can be split in multiple). 3161 Default 0. 3162 Setting to non zero value enables manual user allocation 3163\end_layout 3164 3165\begin_layout Plain Layout 3166 3167VkBuffer* inputBuffer; // Pointer to array of input buffers (or one buffer) 3168 used to read data from if isInputFormatted is enabled 3169\end_layout 3170 3171\begin_layout Plain Layout 3172 3173VkBuffer* outputBuffer; // Pointer to array of output buffers (or one buffer) 3174 used to write data to if isOutputFormatted is enabled 3175\end_layout 3176 3177\begin_layout Plain Layout 3178 3179VkBuffer* kernel; // Pointer to array of kernel buffers (or one buffer) 3180 used to read kernel data from if performConvolution is enabled 3181\end_layout 3182 3183\begin_layout Plain Layout 3184 3185#elif(VKFFT_BACKEND==1) //CUDA API 3186\end_layout 3187 3188\begin_layout Plain Layout 3189 3190void** buffer; // Pointer to device buffer used for computations 3191\end_layout 3192 3193\begin_layout Plain Layout 3194 3195void** tempBuffer; // Needed if reorderFourStep is enabled to transpose 3196 the array. 3197 Same size as buffer. 3198 Default 0. 3199 Setting to non zero value enables manual user allocation 3200\end_layout 3201 3202\begin_layout Plain Layout 3203 3204void** inputBuffer; // Pointer to device buffer used to read data from if 3205 isInputFormatted is enabled 3206\end_layout 3207 3208\begin_layout Plain Layout 3209 3210void** outputBuffer; // Pointer to device buffer used to write data to if 3211 isOutputFormatted is enabled 3212\end_layout 3213 3214\begin_layout Plain Layout 3215 3216void** kernel; // Pointer to device buffer used to read kernel data from 3217 if performConvolution is enabled 3218\end_layout 3219 3220\begin_layout Plain Layout 3221 3222#elif(VKFFT_BACKEND==2) //HIP API 3223\end_layout 3224 3225\begin_layout Plain Layout 3226 3227void** buffer; // Pointer to device buffer used for computations 3228\end_layout 3229 3230\begin_layout Plain Layout 3231 3232void** tempBuffer; // Needed if reorderFourStep is enabled to transpose 3233 the array. 3234 Same size as buffer. 3235 Default 0. 3236 Setting to non zero value enables manual user allocation 3237\end_layout 3238 3239\begin_layout Plain Layout 3240 3241void** inputBuffer; // Pointer to device buffer used to read data from if 3242 isInputFormatted is enabled 3243\end_layout 3244 3245\begin_layout Plain Layout 3246 3247void** outputBuffer; // Pointer to device buffer used to write data to if 3248 isOutputFormatted is enabled 3249\end_layout 3250 3251\begin_layout Plain Layout 3252 3253void** kernel; // Pointer to device buffer used to read kernel data from 3254 if performConvolution is enabled 3255\end_layout 3256 3257\begin_layout Plain Layout 3258 3259#elif(VKFFT_BACKEND==3) //OpenCL API 3260\end_layout 3261 3262\begin_layout Plain Layout 3263 3264cl_mem* buffer; // Pointer to device buffer used for computations 3265\end_layout 3266 3267\begin_layout Plain Layout 3268 3269cl_mem* tempBuffer; // Needed if reorderFourStep is enabled to transpose 3270 the array. 3271 Same size as buffer. 3272 Default 0. 3273 Setting to non zero value enables manual user allocation 3274\end_layout 3275 3276\begin_layout Plain Layout 3277 3278cl_mem* inputBuffer; // Pointer to device buffer used to read data from 3279 if isInputFormatted is enabled 3280\end_layout 3281 3282\begin_layout Plain Layout 3283 3284cl_mem* outputBuffer; // Pointer to device buffer used to write data to 3285 if isOutputFormatted is enabled 3286\end_layout 3287 3288\begin_layout Plain Layout 3289 3290cl_mem* kernel; // Pointer to device buffer used to read kernel data from 3291 if performConvolution is enabled 3292\end_layout 3293 3294\begin_layout Plain Layout 3295 3296#elif(VKFFT_BACKEND==4) 3297\end_layout 3298 3299\begin_layout Plain Layout 3300 3301void** buffer; // Pointer to device buffer used for computations 3302\end_layout 3303 3304\begin_layout Plain Layout 3305 3306void** tempBuffer; // Needed if reorderFourStep is enabled to transpose 3307 the array. 3308 Same size as buffer. 3309 Default 0. 3310 Setting to non zero value enables manual user allocation 3311\end_layout 3312 3313\begin_layout Plain Layout 3314 3315void** inputBuffer; // Pointer to device buffer used to read data from if 3316 isInputFormatted is enabled 3317\end_layout 3318 3319\begin_layout Plain Layout 3320 3321void** outputBuffer; // Pointer to device buffer used to read data from 3322 if isOutputFormatted is enabled 3323\end_layout 3324 3325\begin_layout Plain Layout 3326 3327void** kernel; // Pointer to device buffer used to read kernel data from 3328 if performConvolution is enabled 3329\end_layout 3330 3331\begin_layout Plain Layout 3332 3333#elif(VKFFT_BACKEND==5) 3334\end_layout 3335 3336\begin_layout Plain Layout 3337 3338MTL::Buffer** buffer; // Pointer to device buffer used for computations 3339\end_layout 3340 3341\begin_layout Plain Layout 3342 3343MTL::Buffer** tempBuffer; // Needed if reorderFourStep is enabled to transpose 3344 the array. 3345 Same size as buffer. 3346 Default 0. 3347 Setting to non zero value enables manual user allocation 3348\end_layout 3349 3350\begin_layout Plain Layout 3351 3352MTL::Buffer** inputBuffer; // Pointer to device buffer used to read data 3353 from if isInputFormatted is enabled 3354\end_layout 3355 3356\begin_layout Plain Layout 3357 3358MTL::Buffer** outputBuffer; // Pointer to device buffer used to read data 3359 from if isOutputFormatted is enabled 3360\end_layout 3361 3362\begin_layout Plain Layout 3363 3364MTL::Buffer** kernel; // Pointer to device buffer used to read kernel data 3365 from if performConvolution is enabled 3366\end_layout 3367 3368\begin_layout Plain Layout 3369 3370#endif 3371\end_layout 3372 3373\begin_layout Plain Layout 3374 3375uint64_t bufferOffset; // Specify if VkFFT has to offset the first element 3376 position inside the buffer. 3377 In bytes. 3378 Default 0 3379\end_layout 3380 3381\begin_layout Plain Layout 3382 3383uint64_t tempBufferOffset; // Specify if VkFFT has to offset the first element 3384 position inside the temp buffer. 3385 In bytes. 3386 Default 0 3387\end_layout 3388 3389\begin_layout Plain Layout 3390 3391uint64_t inputBufferOffset; // Specify if VkFFT has to offset the first 3392 element position inside the input buffer. 3393 In bytes. 3394 Default 0 3395\end_layout 3396 3397\begin_layout Plain Layout 3398 3399uint64_t outputBufferOffset; // Specify if VkFFT has to offset the first 3400 element position inside the output buffer. 3401 In bytes. 3402 Default 0 3403\end_layout 3404 3405\begin_layout Plain Layout 3406 3407uint64_t kernelOffset; // Specify if VkFFT has to offset the first element 3408 position inside the kernel. 3409 In bytes. 3410 Default 0 3411\end_layout 3412 3413\begin_layout Plain Layout 3414 3415uint64_t specifyOffsetsAtLaunch; // Specify if offsets will be selected 3416 with launch parameters VkFFTLaunchParams (0 - off, 1 - on). 3417 Default 0 3418\end_layout 3419 3420\begin_layout Plain Layout 3421 3422\end_layout 3423 3424\begin_layout Plain Layout 3425 3426// Optional: (default 0 if not stated otherwise) 3427\end_layout 3428 3429\begin_layout Plain Layout 3430 3431uint64_t coalescedMemory; // In bytes, for Nvidia and AMD is equal to 32, 3432 Intel is equal 64, scaled for half precision. 3433 Going to work regardless, but if specified by user correctly, the performance 3434 will be higher. 3435\end_layout 3436 3437\begin_layout Plain Layout 3438 3439uint64_t aimThreads; // Aim at this many threads per block. 3440 Default 128 3441\end_layout 3442 3443\begin_layout Plain Layout 3444 3445uint64_t numSharedBanks; // How many banks shared memory has. 3446 Default 32 3447\end_layout 3448 3449\begin_layout Plain Layout 3450 3451uint64_t inverseReturnToInputBuffer; // return data to the input buffer 3452 in inverse transform (0 - off, 1 - on). 3453 isInputFormatted must be enabled 3454\end_layout 3455 3456\begin_layout Plain Layout 3457 3458uint64_t numberBatches; // N - used to perform multiple batches of initial 3459 data. 3460 Default 1 3461\end_layout 3462 3463\begin_layout Plain Layout 3464 3465uint64_t useUint64; // Use 64-bit addressing mode in generated kernels 3466\end_layout 3467 3468\begin_layout Plain Layout 3469 3470uint64_t omitDimension[3]; // Disable FFT for this dimension (0 - FFT enabled, 3471 1 - FFT disabled). 3472 Default 0. 3473 Doesn't work for R2C for now. 3474 Doesn't work with convolutions. 3475\end_layout 3476 3477\begin_layout Plain Layout 3478 3479uint64_t performBandwidthBoost; // Try to reduce coalsesced number by a 3480 factor of X to get bigger sequence in one upload for strided axes. 3481 Default: -1 for DCT, 2 for Bluestein's algorithm (or -1 if DCT), 0 otherwise 3482 3483\end_layout 3484 3485\begin_layout Plain Layout 3486 3487\end_layout 3488 3489\begin_layout Plain Layout 3490 3491uint64_t doublePrecision; // Perform calculations in double precision (0 3492 - off, 1 - on). 3493\end_layout 3494 3495\begin_layout Plain Layout 3496 3497uint64_t halfPrecision; // Perform calculations in half precision (0 - off, 3498 1 - on) 3499\end_layout 3500 3501\begin_layout Plain Layout 3502 3503uint64_t halfPrecisionMemoryOnly; // Use half precision only as input/output 3504 buffer. 3505 Input/Output have to be allocated as half, buffer/tempBuffer have to be 3506 allocated as float (out-of-place mode only). 3507 Specify isInputFormatted and isOutputFormatted to use (0 - off, 1 - on) 3508\end_layout 3509 3510\begin_layout Plain Layout 3511 3512uint64_t doublePrecisionFloatMemory; // Use FP64 precision for all calculations, 3513 while all memory storage is done in FP32. 3514\end_layout 3515 3516\begin_layout Plain Layout 3517 3518uint64_t performR2C; // Perform R2C/C2R decomposition (0 - off, 1 - on) 3519\end_layout 3520 3521\begin_layout Plain Layout 3522 3523uint64_t performDCT; // Perform DCT transformation (X - DCT type, 1-4) 3524\end_layout 3525 3526\begin_layout Plain Layout 3527 3528uint64_t disableMergeSequencesR2C; // Disable merging of two real sequences 3529 to reduce calculations (0 - off, 1 - on) 3530\end_layout 3531 3532\begin_layout Plain Layout 3533 3534uint64_t normalize; // Normalize inverse transform (0 - off, 1 - on) 3535\end_layout 3536 3537\begin_layout Plain Layout 3538 3539uint64_t disableReorderFourStep; // Disables unshuffling of Four step algorithm. 3540 Requires tempbuffer allocation (0 - off, 1 - on) 3541\end_layout 3542 3543\begin_layout Plain Layout 3544 3545uint64_t useLUT; // Switches from calculating sincos to using precomputed 3546 LUT tables (0 - off, 1 - on). 3547 Configured by initialization routine 3548\end_layout 3549 3550\begin_layout Plain Layout 3551 3552uint64_t makeForwardPlanOnly; // Generate code only for forward FFT (0 - 3553 off, 1 - on) 3554\end_layout 3555 3556\begin_layout Plain Layout 3557 3558uint64_t makeInversePlanOnly; // Generate code only for inverse FFT (0 - 3559 off, 1 - on) 3560\end_layout 3561 3562\begin_layout Plain Layout 3563 3564uint64_t bufferStride[3]; // Buffer strides - default set to x - x*y - x*y*z 3565 values 3566\end_layout 3567 3568\begin_layout Plain Layout 3569 3570uint64_t isInputFormatted; // Specify if input buffer is padded - 0 - padded, 3571 1 - not padded. 3572 For example if it is not padded for R2C if out-of-place mode is selected 3573 (only if numberBatches==1 and numberKernels==1) 3574\end_layout 3575 3576\begin_layout Plain Layout 3577 3578uint64_t isOutputFormatted; // Specify if output buffer is padded - 0 - 3579 padded, 1 - not padded. 3580 For example if it is not padded for R2C if out-of-place mode is selected 3581 (only if numberBatches==1 and numberKernels==1) 3582\end_layout 3583 3584\begin_layout Plain Layout 3585 3586uint64_t inputBufferStride[3]; // Input buffer strides. 3587 Used if isInputFormatted is enabled. 3588 Default set to bufferStride values 3589\end_layout 3590 3591\begin_layout Plain Layout 3592 3593uint64_t outputBufferStride[3]; // Output buffer strides. 3594 Used if isInputFormatted is enabled. 3595 Default set to bufferStride values 3596\end_layout 3597 3598\begin_layout Plain Layout 3599 3600uint64_t considerAllAxesStrided; // Will create plan for non-strided axis 3601 similar as a strided axis - used with disableReorderFourStep to get the 3602 same layout for Bluestein kernel (0 - off, 1 - on) 3603\end_layout 3604 3605\begin_layout Plain Layout 3606 3607uint64_t keepShaderCode; // Will keep shader code and print all executed 3608 shaders during the plan execution in order (0 - off, 1 - on) 3609\end_layout 3610 3611\begin_layout Plain Layout 3612 3613uint64_t printMemoryLayout; // Will print order of buffers used in shaders 3614 (0 - off, 1 - on) 3615\end_layout 3616 3617\begin_layout Plain Layout 3618 3619\end_layout 3620 3621\begin_layout Plain Layout 3622 3623uint64_t saveApplicationToString; // Will save all compiled binaries to 3624 VkFFTApplication.saveApplicationString (will be allocated by VkFFT, deallocated 3625 with deleteVkFFT call). 3626 VkFFTApplication.applicationStringSize will contain size of binary in bytes. 3627 Currently disabled in Metal backend. 3628 (0 - off, 1 - on) 3629\end_layout 3630 3631\begin_layout Plain Layout 3632 3633uint64_t loadApplicationFromString; // Will load all binaries from loadApplicati 3634onString instead of recompiling them (must be allocated by user, must contain 3635 what saveApplicationToString call generated previously in VkFFTApplication.saveA 3636pplicationString). 3637 Currently disabled in Metal backend. 3638 (0 - off, 1 - on). 3639 Mutually exclusive with saveApplicationToString 3640\end_layout 3641 3642\begin_layout Plain Layout 3643 3644void* loadApplicationString; // Memory binary array through which user can 3645 load VkFFT binaries, must be provided by user if loadApplicationFromString 3646 = 1. 3647 Use rb/wb flags to load/save. 3648\end_layout 3649 3650\begin_layout Plain Layout 3651 3652\end_layout 3653 3654\begin_layout Plain Layout 3655 3656uint64_t disableSetLocale; // disables all VkFFT attempts to set locale 3657 to C - user must ensure that VkFFT has C locale during the plan initialization. 3658 This option is needed for multithreading. 3659 Default 0. 3660 3661\end_layout 3662 3663\begin_layout Plain Layout 3664 3665\end_layout 3666 3667\begin_layout Plain Layout 3668 3669//optional Bluestein optimizations: (default 0 if not stated otherwise) 3670\end_layout 3671 3672\begin_layout Plain Layout 3673 3674uint64_t fixMaxRadixBluestein; // controls the padding of sequences in Bluestein 3675 convolution. 3676 If specified, padded sequence will be made of up to fixMaxRadixBluestein 3677 primes. 3678 Default: 2 for CUDA and Vulkan/OpenCL/HIP up to 1048576 combined dimension 3679 FFT system, 7 for Vulkan/OpenCL/HIP past after. 3680 Min = 2, Max = 13. 3681\end_layout 3682 3683\begin_layout Plain Layout 3684 3685uint64_t forceBluesteinSequenceSize; // force the sequence size to pad to 3686 in Bluestein's algorithm. 3687 Must be at least 2*N-1 and decomposable with primes 2-13. 3688\end_layout 3689 3690\begin_layout Plain Layout 3691 3692uint64_t useCustomBluesteinPaddingPattern; // force the sequence sizes to 3693 pad to in Bluestein's algorithm, but on a range. 3694 This number specifies the number of elements in primeSizes and in paddedSizes 3695 arrays. 3696 primeSizes - array of non-decomposable as radix scheme sizes - 17, 23, 3697 31 etc. 3698 paddedSizes - array of lengths to pad to. 3699 paddedSizes[i] will be the padding size for all non-decomposable sequences 3700 from primeSizes[i] to primeSizes[i+1] (will use default scheme after last 3701 one) - 42, 60, 64 for primeSizes before and 37+ will use default scheme 3702 (for example). 3703 Default is vendor and API-based specified in autoCustomBluesteinPaddingPattern. 3704\end_layout 3705 3706\begin_layout Plain Layout 3707 3708uint64_t* primeSizes; // described in useCustomBluesteinPaddingPattern 3709\end_layout 3710 3711\begin_layout Plain Layout 3712 3713uint64_t* paddedSizes; // described in useCustomBluesteinPaddingPattern 3714\end_layout 3715 3716\begin_layout Plain Layout 3717 3718\end_layout 3719 3720\begin_layout Plain Layout 3721 3722uint64_t fixMinRaderPrimeMult; // start direct multiplication Rader's algorithm 3723 for radix primes from this number. 3724 This means that VkFFT will inline custom Rader kernels if sequence is divisible 3725 by these primes. 3726 Default is 17, as VkFFT has kernels for 2-13. 3727 If you make it less than 13, VkFFT will switch from these kernels to Rader. 3728\end_layout 3729 3730\begin_layout Plain Layout 3731 3732uint64_t fixMaxRaderPrimeMult; // switch from Mult Rader's algorithm for 3733 radix primes from this number. 3734 Current limitation for Rader is maxThreadNum/2+1, realistically you would 3735 want to switch somewhere on 30-100 range. 3736 Default is vendor-specific (currently ~40) 3737\end_layout 3738 3739\begin_layout Plain Layout 3740 3741\end_layout 3742 3743\begin_layout Plain Layout 3744 3745uint64_t fixMinRaderPrimeFFT; // start FFT convolution version of Rader 3746 for radix primes from this number. 3747 Better than direct multiplication version for almost all primes (except 3748 small ones, like 17-23 on some GPUs). 3749 Must be bigger or equal to fixMinRaderPrimeMult. 3750 Deafult 29 on AMD and 17 on other GPUs. 3751 3752\end_layout 3753 3754\begin_layout Plain Layout 3755 3756uint64_t fixMaxRaderPrimeFFT; // switch to Bluestein's algorithm for radix 3757 primes from this number. 3758 Switch may happen earlier if prime can't fit in shared memory. 3759 Default is 16384, which is bigger than most current GPU's shared memory. 3760\end_layout 3761 3762\begin_layout Plain Layout 3763 3764\end_layout 3765 3766\begin_layout Plain Layout 3767 3768// Optional zero padding control parameters: (default 0 if not stated otherwise) 3769\end_layout 3770 3771\begin_layout Plain Layout 3772 3773uint64_t performZeropadding[3]; // Don't read some data/perform computations 3774 if some input sequences are zeropadded for each axis (0 - off, 1 - on) 3775\end_layout 3776 3777\begin_layout Plain Layout 3778 3779uint64_t fft_zeropad_left[3]; // Specify start boundary of zero block in 3780 the system for each axis 3781\end_layout 3782 3783\begin_layout Plain Layout 3784 3785uint64_t fft_zeropad_right[3]; // Specify end boundary of zero block in 3786 the system for each axis 3787\end_layout 3788 3789\begin_layout Plain Layout 3790 3791uint64_t frequencyZeroPadding; // Set to 1 if zeropadding of frequency domain, 3792 default 0 - spatial zeropadding 3793\end_layout 3794 3795\begin_layout Plain Layout 3796 3797\end_layout 3798 3799\begin_layout Plain Layout 3800 3801// Optional convolution control parameters: (default 0 if not stated otherwise) 3802\end_layout 3803 3804\begin_layout Plain Layout 3805 3806uint64_t performConvolution; // Perform convolution in this application 3807 (0 - off, 1 - on). 3808 Disables reorderFourStep parameter 3809\end_layout 3810 3811\begin_layout Plain Layout 3812 3813uint64_t coordinateFeatures; // C - coordinate, or dimension of features 3814 vector. 3815 In matrix convolution - size of a vector 3816\end_layout 3817 3818\begin_layout Plain Layout 3819 3820uint64_t matrixConvolution; // If equal to 2 perform 2x2, if equal to 3 3821 perform 3x3 matrix-vector convolution. 3822 Overrides coordinateFeatures 3823\end_layout 3824 3825\begin_layout Plain Layout 3826 3827uint64_t symmetricKernel; // Specify if kernel in 2x2 or 3x3 matrix convolution 3828 is symmetric 3829\end_layout 3830 3831\begin_layout Plain Layout 3832 3833uint64_t numberKernels; // N - only used in convolution step - specify how 3834 many kernels were initialized before. 3835 Expands one input to multiple (batched) output 3836\end_layout 3837 3838\begin_layout Plain Layout 3839 3840uint64_t kernelConvolution; // Specify if this application is used to create 3841 kernel for convolution, so it has the same properties. 3842 performConvolution has to be set to 0 for kernel creation 3843\end_layout 3844 3845\begin_layout Plain Layout 3846 3847\end_layout 3848 3849\begin_layout Plain Layout 3850 3851// Register overutilization (experimental): (default 0 if not stated otherwise) 3852\end_layout 3853 3854\begin_layout Plain Layout 3855 3856uint64_t registerBoost; // Specify if register file size is bigger than 3857 shared memory and can be used to extend it X times (on Nvidia 256KB register 3858 file can be used instead of 32KB of shared memory, set this constant to 3859 4 to emulate 128KB of shared memory). 3860 Defaults: Nvidia - 4 in Vulkan/OpenCL, 1 in CUDA backend; AMD - 2 if shared 3861 memory >= 64KB, else 4 in Vulkan/OpenCL backend, 1 in HIP backend; Intel 3862 - 1 if shared memory >= 64KB, else 2 in Vulkan/OpenCL/Level Zero backends, 3863 1 in Metal; Default 1 3864\end_layout 3865 3866\begin_layout Plain Layout 3867 3868uint64_t registerBoostNonPow2; // Specify if register overutilization should 3869 be used on non power of 2 sequences (0 - off, 1 - on) 3870\end_layout 3871 3872\begin_layout Plain Layout 3873 3874uint64_t registerBoost4Step; // Specify if register file overutilization 3875 should be used in big sequences (>2^14), same definition as registerBoost. 3876 Default 1 3877\end_layout 3878 3879\begin_layout Plain Layout 3880 3881//not used techniques: 3882\end_layout 3883 3884\begin_layout Plain Layout 3885 3886uint64_t swapTo3Stage4Step; // Specify at which power of 2 to switch from 3887 2 upload to 3 upload 4-step FFT, in case if making max sequence size lower 3888 than coalesced sequence helps to combat TLB misses. 3889 Default 0 - disabled. 3890 Must be at least 17 3891\end_layout 3892 3893\begin_layout Plain Layout 3894 3895uint64_t devicePageSize; // In KB, the size of a page on the GPU. 3896 Setting to 0 disables local buffer split in pages 3897\end_layout 3898 3899\begin_layout Plain Layout 3900 3901uint64_t localPageSize; // In KB, the size to split page into if sequence 3902 spans multiple devicePageSize pages 3903\end_layout 3904 3905\begin_layout Plain Layout 3906 3907\end_layout 3908 3909\begin_layout Plain Layout 3910 3911// Automatically filled based on device info (still can be reconfigured 3912 by user): 3913\end_layout 3914 3915\begin_layout Plain Layout 3916 3917uint64_t computeCapabilityMajor; // CUDA/HIP compute capability of the device 3918\end_layout 3919 3920\begin_layout Plain Layout 3921 3922uint64_t computeCapabilityMinor; // CUDA/HIP compute capability of the device 3923 3924\end_layout 3925 3926\begin_layout Plain Layout 3927 3928uint64_t maxComputeWorkGroupCount[3]; // maxComputeWorkGroupCount from VkPhysica 3929lDeviceLimits 3930\end_layout 3931 3932\begin_layout Plain Layout 3933 3934uint64_t maxComputeWorkGroupSize[3]; // maxComputeWorkGroupCount from VkPhysical 3935DeviceLimits 3936\end_layout 3937 3938\begin_layout Plain Layout 3939 3940uint64_t maxThreadsNum; // Max number of threads from VkPhysicalDeviceLimits 3941\end_layout 3942 3943\begin_layout Plain Layout 3944 3945uint64_t sharedMemorySizeStatic; // Available for static allocation shared 3946 memory size, in bytes 3947\end_layout 3948 3949\begin_layout Plain Layout 3950 3951uint64_t sharedMemorySize; // Available for allocation shared memory size, 3952 in bytes 3953\end_layout 3954 3955\begin_layout Plain Layout 3956 3957uint64_t sharedMemorySizePow2; // Power of 2 which is less or equal to sharedMem 3958orySize, in bytes 3959\end_layout 3960 3961\begin_layout Plain Layout 3962 3963uint64_t warpSize; // Number of threads per warp/wavefront. 3964\end_layout 3965 3966\begin_layout Plain Layout 3967 3968uint64_t halfThreads; // Intel fix 3969\end_layout 3970 3971\begin_layout Plain Layout 3972 3973uint64_t allocateTempBuffer; // Buffer allocated by app automatically if 3974 needed to reorder Four step algorithm. 3975 Parameter to check if it has been allocated 3976\end_layout 3977 3978\begin_layout Plain Layout 3979 3980uint64_t reorderFourStep; // Unshuffle Four step algorithm. 3981 Requires tempbuffer allocation (0 - off, 1 - on). 3982 Default 1. 3983\end_layout 3984 3985\begin_layout Plain Layout 3986 3987int64_t maxCodeLength; // Specify how big can be buffer used for code generation 3988 (in char). 3989 Default 1000000 chars. 3990 3991\end_layout 3992 3993\begin_layout Plain Layout 3994 3995int64_t maxTempLength; // Specify how big can be buffer used for intermediate 3996 string sprintfs be (in char). 3997 Default 5000 chars. 3998 If code segfaults for some reason - try increasing this number. 3999\end_layout 4000 4001\begin_layout Plain Layout 4002 4003uint64_t autoCustomBluesteinPaddingPattern; // default value for useCustomBluest 4004einPaddingPattern 4005\end_layout 4006 4007\begin_layout Plain Layout 4008 4009uint64_t useRaderUintLUT; // allocate additional LUT to store g_pow 4010\end_layout 4011 4012\begin_layout Plain Layout 4013 4014uint64_t vendorID; // vendorID 0x10DE - NVIDIA, 0x8086 - Intel, 0x1002 - 4015 AMD, etc 4016\end_layout 4017 4018\begin_layout Plain Layout 4019 4020#if(VKFFT_BACKEND==0) //Vulkan API 4021\end_layout 4022 4023\begin_layout Plain Layout 4024 4025VkDeviceMemory tempBufferDeviceMemory; // Filled at app creation 4026\end_layout 4027 4028\begin_layout Plain Layout 4029 4030VkCommandBuffer* commandBuffer; // Filled at app execution 4031\end_layout 4032 4033\begin_layout Plain Layout 4034 4035VkMemoryBarrier* memory_barrier; // Filled at app creation 4036\end_layout 4037 4038\begin_layout Plain Layout 4039 4040#elif(VKFFT_BACKEND==1) //CUDA API 4041\end_layout 4042 4043\begin_layout Plain Layout 4044 4045cudaEvent_t* stream_event; // Filled at app creation 4046\end_layout 4047 4048\begin_layout Plain Layout 4049 4050uint64_t streamCounter; // Filled at app creation 4051\end_layout 4052 4053\begin_layout Plain Layout 4054 4055uint64_t streamID; // Filled at app creation 4056\end_layout 4057 4058\begin_layout Plain Layout 4059 4060#elif(VKFFT_BACKEND==2) //HIP API 4061\end_layout 4062 4063\begin_layout Plain Layout 4064 4065hipEvent_t* stream_event; // Filled at app creation 4066\end_layout 4067 4068\begin_layout Plain Layout 4069 4070uint64_t streamCounter; // Filled at app creation 4071\end_layout 4072 4073\begin_layout Plain Layout 4074 4075uint64_t streamID; // Filled at app creation 4076\end_layout 4077 4078\begin_layout Plain Layout 4079 4080#elif(VKFFT_BACKEND==3) //OpenCL API 4081\end_layout 4082 4083\begin_layout Plain Layout 4084 4085cl_command_queue* commandQueue; // Filled at app creation 4086\end_layout 4087 4088\begin_layout Plain Layout 4089 4090#elif(VKFFT_BACKEND==4) 4091\end_layout 4092 4093\begin_layout Plain Layout 4094 4095ze_command_list_handle_t* commandList; // Filled at app creation 4096\end_layout 4097 4098\begin_layout Plain Layout 4099 4100#elif(VKFFT_BACKEND==5) 4101\end_layout 4102 4103\begin_layout Plain Layout 4104 4105MTL::CommandBuffer* commandBuffer; // Filled at app execution 4106\end_layout 4107 4108\begin_layout Plain Layout 4109 4110MTL::ComputeCommandEncoder* commandEncoder; // Filled at app execution 4111\end_layout 4112 4113\begin_layout Plain Layout 4114 4115#endif 4116\end_layout 4117 4118\begin_layout Plain Layout 4119 4120} VkFFTConfiguration; 4121\end_layout 4122 4123\begin_layout Plain Layout 4124 4125 4126\backslash 4127end{minted} 4128\end_layout 4129 4130\begin_layout Plain Layout 4131 4132 4133\backslash 4134end{mdframed} 4135\end_layout 4136 4137\end_inset 4138 4139 4140\end_layout 4141 4142\begin_layout Subsubsection 4143Driver API parameters 4144\end_layout 4145 4146\begin_layout Standard 4147In order to work, VkFFT needs some structures that are provided by the driver. 4148 They are backend API-dependent. 4149 VkFFT will return corresponding VkFFTResult if one of these structures 4150 are not provided (value equal to zero) unless it is stated that there is 4151 a default value assigned. 4152 VkFFT will not modify provided values directly. 4153\end_layout 4154 4155\begin_layout Standard 4156Vulkan API will need the following information: 4157\end_layout 4158 4159\begin_layout Itemize 4160VkPhysicalDevice* physicalDevice - Pointer to Vulkan physical device, obtained 4161 from vkEnumeratePhysicalDevices() 4162\end_layout 4163 4164\begin_layout Itemize 4165VkDevice* device - Pointer to Vulkan device, created with vkCreateDevice() 4166\end_layout 4167 4168\begin_layout Itemize 4169VkQueue* queue - Pointer to Vulkan queue, created with vkGetDeviceQueue() 4170\end_layout 4171 4172\begin_layout Itemize 4173VkCommandPool* commandPool - Pointer to Vulkan command pool, created with 4174 vkCreateCommandPool() 4175\end_layout 4176 4177\begin_layout Itemize 4178VkFence* fence - Pointer to Vulkan fence, created with vkCreateFence() 4179\end_layout 4180 4181\begin_layout Itemize 4182uint64_t isCompilerInitialized - Specify if glslang compiler has been intialized 4183 before (0 - off, 1 - on). 4184 Default 0 - VkFFT will call glslang_initialize_process() at initializeVkFFT() 4185 and glslang_finialize_process() at deleteVkFFT() calls. 4186\end_layout 4187 4188\begin_layout Standard 4189CUDA API will need the following information: 4190\end_layout 4191 4192\begin_layout Itemize 4193CUdevice* device - Pointer to CUDA device, obtained from cuDeviceGet() 4194\end_layout 4195 4196\begin_layout Itemize 4197cudaStream_t* stream - Pointer to streams (can be more than 1), where to 4198 execute the kernels. 4199 Default 0. 4200 Streams must be associated with the provided device. 4201 There is no real benefit in having more than one, however. 4202 4203\end_layout 4204 4205\begin_layout Itemize 4206uint64_t num_streams - Try to submit CUDA kernels in multiple streams for 4207 asynchronous execution. 4208 Default 1 4209\end_layout 4210 4211\begin_layout Standard 4212HIP API will need the following information: 4213\end_layout 4214 4215\begin_layout Itemize 4216hipDevice_t* device - Pointer to HIP device, obtained from hipDeviceGet() 4217\end_layout 4218 4219\begin_layout Itemize 4220hipStream_t* stream - Pointer to streams (can be more than 1), where to 4221 execute the kernels. 4222 Default 0. 4223 Streams must be associated with the provided device. 4224 There is no real benefit in having more than one, however. 4225 4226\end_layout 4227 4228\begin_layout Itemize 4229uint64_t num_streams - Try to submit HIP kernels in multiple streams for 4230 asynchronous execution. 4231 Default 1 4232\end_layout 4233 4234\begin_layout Standard 4235OpenCL API will need the following information: 4236\end_layout 4237 4238\begin_layout Itemize 4239cl_device_id* device - Pointer to OpenCL device, obtained from clGetDeviceIDs() 4240\end_layout 4241 4242\begin_layout Itemize 4243cl_context* context - Pointer to OpenCL context, obtained from clCreateContext() 4244\end_layout 4245 4246\begin_layout Standard 4247Level Zero API will need the following information: 4248\end_layout 4249 4250\begin_layout Itemize 4251ze_device_handle_t* device - Pointer to Level Zero device, obtained from 4252 zeDeviceGet() 4253\end_layout 4254 4255\begin_layout Itemize 4256ze_context_handle_t* context - Pointer to Level Zero context, obtained from 4257 zeContextGet() 4258\end_layout 4259 4260\begin_layout Itemize 4261ze_command_queue_handle_t* commandQueue - Pointer to Level Zero command 4262 queuewith compute and copy capabilities, obtained from zeCommandQueueCreate() 4263\end_layout 4264 4265\begin_layout Itemize 4266uint32_t commandQueueID - ID of the commandQueue with compute and copy capabilit 4267ies 4268\end_layout 4269 4270\begin_layout Standard 4271Metal API will need the following information: 4272\end_layout 4273 4274\begin_layout Itemize 4275MTL::Device* device - Pointer to Metal device, obtained from MTL::CopyAllDevices 4276\end_layout 4277 4278\begin_layout Itemize 4279MTL::CommandQueue* queue - Pointer to Metal queue, obtained from device->newComm 4280andQueue() 4281\end_layout 4282 4283\begin_layout Subsubsection 4284Memory management parameters 4285\end_layout 4286 4287\begin_layout Standard 4288There are five buffer types user can provide to VkFFT: 4289\end_layout 4290 4291\begin_layout Itemize 4292the main buffer (buffer) 4293\end_layout 4294 4295\begin_layout Itemize 4296temporary buffer used for calculations requiring out-of-place writes (tempBuffer 4297) 4298\end_layout 4299 4300\begin_layout Itemize 4301separate input buffer, from which initial read is performed (inputBuffer) 4302\end_layout 4303 4304\begin_layout Itemize 4305separate output buffer, to which final write is performed (outputBuffer) 4306\end_layout 4307 4308\begin_layout Itemize 4309kernel buffer, used for calculation of convolutions and cross-correlations 4310 (kernel) 4311\end_layout 4312 4313\begin_layout Standard 4314These buffers must be passed by a pointer: in Vulkan API they are provided 4315 as VkBuffer*, in CUDA, HIP and Level Zero they are provided as void**, 4316 in OpenCL they are provided as cl_mem*, in Metal they are provided as MTL::Buff 4317er*. 4318 Even though the underlying structure (VkBuffer, void*, cl_mem, MTL::Buffer*) 4319 is not a memory but just a number that the driver can use to access correspondi 4320ng allocated memory on the GPU, passing them by a pointer allows for the 4321 user to query multiple GPU allocated buffers for VkFFT to use. 4322 Currently, it is only supported in Vulkan API - each of five buffer types 4323 can be made out of multiple separate memory allocations. 4324 For example, it is possible to combine multiple small unused at the point 4325 of FFT calculation buffers to form a tempBuffer. 4326 This option also allows Vulkan API to overcome the limit of 4GB for a single 4327 memory allocation - due to the fact that Vulkan can only use 32-bit numbers 4328 for addressing (other APIs support 64-bit addressing). 4329 4330\end_layout 4331 4332\begin_layout Standard 4333To use the buffers other than the main buffer, the user has to specify this 4334 in configuration at the application creation stage (set to zero by default, 4335 optional parameters): 4336\end_layout 4337 4338\begin_layout Itemize 4339uint64_t userTempBuffer - enables manual temporary buffer allocation (otherwise 4340 it is managed by VkFFT) 4341\end_layout 4342 4343\begin_layout Itemize 4344uint64_t isInputFormatted - specifies that initial read is performed from 4345 a separate buffer (inputBuffer) 4346\end_layout 4347 4348\begin_layout Itemize 4349uint64_t isOutputFormatted - specifies that final write is performed to 4350 a separate buffer (outputBuffer) 4351\end_layout 4352 4353\begin_layout Itemize 4354uint64_t performConvolution - enables convolution calculations, which requires 4355 precomputed kernel (kernel) 4356\end_layout 4357 4358\begin_layout Standard 4359Buffer sizes (bufferSize/tempBufferSize/inputBufferSize/outputBufferSize/kernelS 4360ize) are provided as a uint64_t pointer to an array, where each element 4361 corresponds to the buffer size of the buffer with the same placement in 4362 the buffer array. 4363 Buffer sizes have to be provided in Vulkan API (due to the stricter memory 4364 management model and multiple buffer support) and are optional in other 4365 backends (they can be useful to determine when to switch for 64-bit addressing). 4366\end_layout 4367 4368\begin_layout Standard 4369Buffer number (bufferNum/tempBufferNum/inputBufferNum/outputBufferNum/kernelNum) 4370 corresponds to how many elements are in the buffer and buffer size array. 4371 By default it is set to 1 and is not required to be provided by the user. 4372 Non-Vulkan backends currently don't support values other than default. 4373 Optional parameter. 4374\end_layout 4375 4376\begin_layout Standard 4377Buffer offset (bufferOffset/ tempBufferOffset/ inputBufferOffset/ outputBufferOf 4378fset/ kernelOffset) specifies offset from the start of the buffer sequence. 4379 It must be specified in bytes and must be divisible by the number type 4380 size used in the corresponding array (otherwise, the offset will be truncated). 4381 It is provided as a single uint64_t value. 4382 Can be provided at launch time, if specifyOffsetsAtLaunch parameter is 4383 enabled during initialization call. 4384 Optional parameters. 4385\end_layout 4386 4387\begin_layout Standard 4388User can provide custom dimension strides for buffer/inputBuffer/outputBuffer 4389 buffers - uint64_t[3] array. 4390 Strides are specified in elements used in the array (not bytes). 4391 The first element corresponds to the stride between elements in the H direction 4392, the second corresponds to the D direction and the third to C (or N, if 4393 the number of elements in C is 1). 4394 The first axis is assumed to be non-strided. 4395 Must be at least of the same size as default strides, otherwise the behavior 4396 is undefined. 4397 Optional parameters. 4398\end_layout 4399 4400\begin_layout Standard 4401uint64_t inverseReturnToInputBuffer - an option that allows setting the 4402 final output buffer of the inverse transform to the same buffer, initial 4403 read of forward transform is performed from (inputBuffer, if isInputFormatted 4404 enabled). 4405 Optional parameter. 4406\end_layout 4407 4408\begin_layout Subsubsection 4409General FFT parameters 4410\end_layout 4411 4412\begin_layout Standard 4413This section describes part of the configuration structure responsible for 4414 FFT specification. 4415 4416\end_layout 4417 4418\begin_layout Standard 4419uint64_t FFTdim - dimensionality of the transform (1, 2 or 3). 4420 Required parameter. 4421\end_layout 4422 4423\begin_layout Standard 4424uint64_t size[3] - WHD dimensions of the transform. 4425 Required parameter. 4426\end_layout 4427 4428\begin_layout Standard 4429uint64_t numberBatches - N parameter of the transform. 4430 By default, it is set to 1. 4431 Optional parameter. 4432\end_layout 4433 4434\begin_layout Standard 4435uint64_t performR2C - perform R2C/C2R decomposition. 4436 performDCT must be set to 0. 4437 Default 0, set to 1 to enable. 4438 Optional parameter. 4439\end_layout 4440 4441\begin_layout Standard 4442uint64_t performDCT - perform DCT transformation. 4443 performR2C must be set to 0. 4444 Default 0, set to X for DCT-X (currently supported X: 1, 2, 3 and 4). 4445 Optional parameter. 4446\end_layout 4447 4448\begin_layout Standard 4449uint64_t normalize - enabling this parameter will make the inverse transform 4450 divide the result by the FFT length. 4451 Default 0, set to 1 to enable. 4452 Optional parameter. 4453\end_layout 4454 4455\begin_layout Subsubsection 4456Precision parameters (and some things that can affect it): 4457\end_layout 4458 4459\begin_layout Standard 4460uint64_t doublePrecision - perform calculations in double precision. 4461 Default 0, set to 1 to enable. 4462 In Vulkan/OpenCL/Level Zero your device must support double precision functiona 4463lity. 4464 Metal API does not support double precision. 4465 Optional parameter. 4466\end_layout 4467 4468\begin_layout Standard 4469uint64_t doublePrecisionFloatMemory - perform calculations in double precision, 4470 but all intermediate and final storage in float. 4471 Input/Output/main buffers must have single-precision layout. 4472 doublePrecision must be set to 0. 4473 This option increases precision, but not that much to be recommended for 4474 actual use. 4475 Default 0, set to 1 to enable. 4476 In Vulkan/OpenCL/Level Zero your device must support double precision functiona 4477lity. 4478 Metal API does not support double precision. 4479 Experimental feature. 4480 Optional parameter. 4481\end_layout 4482 4483\begin_layout Standard 4484uint64_t halfPrecision - half-precision in VkFFT is implemented only as 4485 memory optimization. 4486 All calculations are done in single precision (similar way as doublePrecisionFl 4487oatMemory works for double and single precision). 4488 Default 0, set to 1 to enable. 4489 Works only in Vulkan API now, experimental feature (half precision seems 4490 to have bad precision for the first FFT element). 4491 Optional parameter. 4492\end_layout 4493 4494\begin_layout Standard 4495uint64_t halfPrecisionMemoryOnly - another way of performing half-precision 4496 in VkFFT, it will use half-precision only for initial and final memory 4497 storage in input/output buffer. 4498 Input/Output have to be allocated as half, buffer/tempBuffer have to be 4499 allocated as float (out-of-place mode only). 4500 Specify isInputFormatted and isOutputFormatted to use. 4501 So, for example, intermediate storage between axes FFTs in the multidimensional 4502 case will be done in single precision, as opposed to half-precision in 4503 the base halfPrecision case. 4504 halfPrecision must be set to 1. 4505 Default 0, set to 1 to enable. 4506 Works only in Vulkan API now, experimental feature. 4507 Optional parameter. 4508\end_layout 4509 4510\begin_layout Standard 4511uint64_t useLUT - switches from calculating sines and cosines (via special 4512 function units in single precision or as a polynomial approximation in 4513 double precision) to using precomputed Look-Up Tables. 4514 Default 0 in single precision, 1 in double precision, set to 1 to enable. 4515 Set to 1 by default for Intel GPUs. 4516 If you have issues with single-precision accuracy on your GPU, try enabling 4517 this parameter (mobile GPUs may be affected). 4518 Optional parameter. 4519\end_layout 4520 4521\begin_layout Subsubsection 4522Advanced parameters (code will work fine without using them) 4523\end_layout 4524 4525\begin_layout Standard 4526uint64_t omitDimension[3] - parameter, that disables the FFT calculation 4527 for a particular axis (WHD). 4528 Note, that omitted dimensions still need to be included in FFTdim and size. 4529 This parameter simply works as a switch during execution - by not executing 4530 the particular dimension code. 4531 It doesn't work with the non-strided axis (W) of R2C/C2R mode. 4532 It doesn't work with convolution calculations. 4533 Default 0, set to 1 to enable. 4534 Optional parameter. 4535\end_layout 4536 4537\begin_layout Standard 4538uint64_t useUint64 - forces VkFFT to use 64-bit addressing in generated 4539 kernels. 4540 It is automatically enabled if the estimated buffer size is more than 4GB. 4541 Doesn't work with the Vulkan backend. 4542 By default, it is set to 0. 4543 Optional parameter. 4544\end_layout 4545 4546\begin_layout Standard 4547uint64_t coalescedMemory - number of bytes to coalesce per one transaction. 4548 For Nvidia and AMD is equal to 32, Intel is equal to 64. 4549 Going to work regardless, but if specified by the user correctly, the performan 4550ce will be higher. 4551 Default 64 for other GPUs. 4552 For half-precision should be multiplied by two. 4553 Should be a power of two. 4554 Optional parameter. 4555\end_layout 4556 4557\begin_layout Standard 4558uint64_t numSharedBanks - configure the number of shared banks on the target 4559 GPU. 4560 Default 32. 4561 Minor performance boost as it solves shared memory conflicts for the power 4562 of two systems. 4563 Optional parameter. 4564\end_layout 4565 4566\begin_layout Standard 4567uint64_t aimThreads - try to aim all kernels at this amount of threads. 4568 Gains/losses are not predictable, just a parameter to play with (it is 4569 not guaranteed that the target kernel will use that many threads). 4570 Default 128. 4571 Optional parameter. 4572\end_layout 4573 4574\begin_layout Standard 4575uint64_t useUint64 - forces 64-bit addressing in generated kernels. 4576 Should be enabled automatically for systems spanning more than 4GB, but 4577 it is better to have an option to force it as a failsafe. 4578 Doesn't work in Vulkan API (use multiple buffer binding). 4579 Default 0, set to 1 to enable. 4580 Optional parameter. 4581\end_layout 4582 4583\begin_layout Standard 4584uint64_t performBandwidthBoost - try to reduce coalsesced number by a factor 4585 of X to get bigger sequence in one upload for strided axes. 4586 Default: -1(inf) for DCT, 2 for Bluestein's algorithm (or -1 if DCT), 0 4587 otherwise 4588\end_layout 4589 4590\begin_layout Standard 4591uint64_t disableMergeSequencesR2C - disable the optimization that performs 4592 merging of two real sequences to reduce calculations (in R2C/C2R and R2R). 4593 If enabled, calculations will be performed by simply setting the imaginary 4594 component to zero. 4595 Default 0, set to 1 to enable. 4596 Optional parameter. 4597\end_layout 4598 4599\begin_layout Standard 4600uint64_t disableReorderFourStep - disables unshuffling of the Four Step 4601 FFT algorithm (last transposition of data). 4602 With this option enabled, tempBuffer will not be needed (unless it is required 4603 by Bluestein's multi-upload FFT algorithm). 4604 Default 0, set to 1 to enable. 4605 Automatically enabled for convolution calculations and Bluestein's algorithm. 4606 Optional parameter. 4607\end_layout 4608 4609\begin_layout Standard 4610uint64_t makeForwardPlanOnly - generate code only for forward FFT. 4611 Default 0, set to 1 to enable. 4612 Mutually exclusive with makeInversePlanOnly. 4613 Optional parameter. 4614\end_layout 4615 4616\begin_layout Standard 4617uint64_t makeInversePlanOnly - generate code only for inverse FFT. 4618 Default 0, set to 1 to enable. 4619 Mutually exclusive with makeForwardPlan. 4620 Optional parameter. 4621\end_layout 4622 4623\begin_layout Standard 4624uint64_t considerAllAxesStrided - will create a plan for a non-strided axis 4625 similar to a strided axis (used with disableReorderFourStep to get the 4626 same layout for Bluestein kernel). 4627 Default 0, set to 1 to enable. 4628 Optional parameter. 4629\end_layout 4630 4631\begin_layout Standard 4632uint64_t keepShaderCode - debugging option, will keep shader code and print 4633 all executed shaders during the plan execution in order. 4634 Default 0, set to 1 to enable. 4635 Optional parameter. 4636\end_layout 4637 4638\begin_layout Standard 4639uint64_t printMemoryLayout - debugging option, will print order of buffers 4640 used in kernels. 4641 Default 0, set to 1 to enable. 4642 Optional parameter. 4643 4644\end_layout 4645 4646\begin_layout Standard 4647uint64_t saveApplicationToString - will save all compiled binaries to VkFFTAppli 4648cation.saveApplicationString (will be allocated by VkFFT, deallocated with 4649 deleteVkFFT call). 4650 VkFFTApplication.applicationStringSize will contain size of binary in bytes. 4651 Currently disabled in Metal backend. 4652 Default 0, set to 1 to enable. 4653 Optional parameter. 4654\end_layout 4655 4656\begin_layout Standard 4657uint64_t loadApplicationFromString - will load all binaries from loadApplication 4658String instead of recompiling them (loadApplicationString must be allocated 4659 by user, must contain what saveApplicationToString call generated previously 4660 in VkFFTApplication.saveApplicationString). 4661 Currently disabled in Metal backend. 4662 Default 0, set to 1 to enable. 4663 Optional parameter. 4664 Mutually exclusive with saveApplicationToString 4665\end_layout 4666 4667\begin_layout Standard 4668void* loadApplicationString - memory binary array through which user can 4669 load VkFFT binaries, must be provided by user if loadApplicationFromString 4670 = 1. 4671 Use rb/wb flags to load/save. 4672\end_layout 4673 4674\begin_layout Standard 4675uint64_t disableSetLocale - disables all VkFFT attempts to set locale to 4676 C - user must ensure that VkFFT has C locale during the plan initialization. 4677 This option is needed for multithreading. 4678 Default 0. 4679\end_layout 4680 4681\begin_layout Subsubsection 4682Rader control parameters 4683\end_layout 4684 4685\begin_layout Standard 4686uint64_t fixMinRaderPrimeMult - start direct multiplication Rader's algorithm 4687 for radix primes from this number. 4688 This means that VkFFT will inline custom Rader kernels if sequence is divisible 4689 by these primes. 4690 Default is 17, as VkFFT has kernels for 2-13. 4691 If you make it less than 13, VkFFT will switch from these kernels to Rader. 4692\end_layout 4693 4694\begin_layout Standard 4695uint64_t fixMaxRaderPrimeMult - switch from Mult Rader's algorithm for radix 4696 primes from this number. 4697 Current limitation for Rader is maxThreadNum/2+1, realistically you would 4698 want to switch somewhere on 30-100 range. 4699 Default is vendor-specific (currently ~40) 4700\end_layout 4701 4702\begin_layout Standard 4703uint64_t fixMinRaderPrimeFFT - start FFT convolution version of Rader for 4704 radix primes from this number. 4705 Better than direct multiplication version for almost all primes (except 4706 small ones, like 17-23 on some GPUs). 4707 Must be bigger or equal to fixMinRaderPrimeMult. 4708 Deafult 29 on AMD and 17 on other GPUs. 4709 4710\end_layout 4711 4712\begin_layout Standard 4713uint64_t fixMaxRaderPrimeFFT - switch to Bluestein's algorithm for radix 4714 primes from this number. 4715 Switch may happen earlier if prime can't fit in shared memory. 4716 Default is 16384, which is bigger than most current GPU's shared memory. 4717\end_layout 4718 4719\begin_layout Subsubsection 4720Bluestein control parameters 4721\end_layout 4722 4723\begin_layout Standard 4724If the sequence can not be decomposed as a multiplication of primes up to 4725 13, FFT is performed as a convolution. 4726 The sequence to pad to with the best performance is usually device-dependent. 4727 VkFFT uses parameters manually tuned for all sequences between 2 and 4096 4728 for both double and single precision on Nvidia A100 (Nvidia profile) and 4729 AMD MI250 (default profile). 4730 To control this process, VkFFT allows for the following parameters specificatio 4731n: 4732\end_layout 4733 4734\begin_layout Standard 4735uint64_t fixMaxRadixBluestein - controls the padding of sequences in Bluestein 4736 convolution. 4737 If specified, padded sequence will be made of up to fixMaxRadixBluestein 4738 primes. 4739 Default: 2 for CUDA and Vulkan/OpenCL/HIP up to 1048576 combined dimension 4740 FFT system, 7 for Vulkan/OpenCL/HIP past after. 4741 Min = 2, Max = 13. 4742\end_layout 4743 4744\begin_layout Standard 4745uint64_t forceBluesteinSequenceSize - force the sequence size to pad to 4746 in Bluestein's algorithm. 4747 Must be at least 2*N-1 and decomposable with primes 2-13. 4748\end_layout 4749 4750\begin_layout Standard 4751uint64_t useCustomBluesteinPaddingPattern - force the sequence sizes to 4752 pad to in Bluestein's algorithm, but on a range. 4753 This number specifies the number of elements in primeSizes and in paddedSizes 4754 arrays. 4755 primeSizes - array of non-decomposable as radix scheme sizes - 17, 23, 4756 31 etc. 4757 paddedSizes - array of lengths to pad to. 4758 paddedSizes[i] will be the padding size for all non-decomposable sequences 4759 from primeSizes[i] to primeSizes[i+1] (will use default scheme after last 4760 one) - 42, 60, 64 for primeSizes before and 37+ will use default scheme 4761 (for example). 4762 Default is vendor and API-based specified in autoCustomBluesteinPaddingPattern. 4763\end_layout 4764 4765\begin_layout Standard 4766uint64_t* primeSizes - described in useCustomBluesteinPaddingPattern 4767\end_layout 4768 4769\begin_layout Standard 4770uint64_t* paddedSizes - described in useCustomBluesteinPaddingPattern 4771\end_layout 4772 4773\begin_layout Subsubsection 4774Zero padding parameters 4775\end_layout 4776 4777\begin_layout Standard 4778uint64_t performZeropadding[3] - do not read/write some data/perform computation 4779s if some part of the sequence is known to have zeros. 4780 Set separately for each axis (WHD). 4781 If enabled, all 1D sequences in this direction will be considered padded 4782 (independent of other zero-padded axes). 4783 Default 0, set to 1 to enable. 4784 Optional parameter. 4785 4786\end_layout 4787 4788\begin_layout Standard 4789uint64_t fft_zeropad_left[3] - specify start boundary of zero block in the 4790 system for each axis. 4791 Default 0, set to the value between 0 and size[X]-1. 4792 Optional parameter. 4793\end_layout 4794 4795\begin_layout Standard 4796uint64_t fft_zeropad_right[3] - specify end boundary of zero block in the 4797 system for each axis. 4798 Default 0, set to the value between fft_zeropad_left[X] and size[X]-1. 4799 Optional parameter. 4800\end_layout 4801 4802\begin_layout Standard 4803uint64_t frequencyZeroPadding - enables zero padding of the frequency domain, 4804 so the first read of inverse FFT will consider the parts of the system 4805 from fft_zeropad_left to fft_zeropad_right as zero. 4806 Default 0 - spatial zero padding, set to 1 to enable. 4807 Optional parameter. 4808 4809\end_layout 4810 4811\begin_layout Subsubsection 4812Convolution parameters 4813\end_layout 4814 4815\begin_layout Standard 4816uint64_t performConvolution - main parameter that enables convolutions in 4817 the application. 4818 If enabled, you must specify kernel buffer, number of kernel buffers and 4819 kernel sizes (in Vulkan API). 4820 Disables reordering of the Four Step FFT algorithm. 4821 Default 0, set to 1 to enable. 4822 Optional parameter. 4823 4824\end_layout 4825 4826\begin_layout Standard 4827uint64_t conjugateConvolution - default 0, set to 1 to enable enables conjugatio 4828n of the sequence FFT is currently done on, 2 to enable conjugation of the 4829 convolution kernel. 4830 Optional parameter. 4831 4832\end_layout 4833 4834\begin_layout Standard 4835uint64_t crossPowerSpectrumNormalization - normalize the FFT * kernel multiplica 4836tion in frequency domain. 4837 Default 0, set to 1 to enable. 4838 Optional parameter. 4839 4840\end_layout 4841 4842\begin_layout Standard 4843uint64_t coordinateFeatures - max coordinate (C), or dimension of the features 4844 vector. 4845 In matrix convolution - the size of the vector. 4846 The main purpose is to support Matrix-Vector convolutions. 4847 Use numberBatches parameter in tasks, not requiring two separate coordinate-lik 4848e enumerations of data. 4849 Default 1. 4850 Optional parameter. 4851\end_layout 4852 4853\begin_layout Standard 4854uint64_t matrixConvolution - set to 2 to perform 2x2, set to 3 to perform 4855 3x3 matrix-vector convolution. 4856 Matrix-vector convolution is a form of point-wise multiplication in the 4857 Fourier space, used by the convolution theorem, where multiplication takes 4858 the form of Matrix-vector multiplication. 4859 Overrides coordinateFeatures during execution. 4860 Default 0. 4861 Optional parameter. 4862 4863\end_layout 4864 4865\begin_layout Standard 4866uint64_t symmetricKernel - specify if kernel in 2x2 or 3x3 matrix convolution 4867 is symmetric. 4868 You need to store data as xx, xy, yy (upper-triangular) if enabled and 4869 as xx, xy, yx, yy (along rows then along columns, from left to right) if 4870 disabled. 4871 Default 0, set to 1 to enable. 4872 Optional parameter. 4873 4874\end_layout 4875 4876\begin_layout Standard 4877uint64_t numberKernels - specify how many kernels were initialized before 4878 performing one input/multiple output convolutions. 4879 Overwrites numberBatches (N). 4880 Only used in convolution step and the following inverse transforms. 4881 Default 1. 4882 Optional parameter. 4883 4884\end_layout 4885 4886\begin_layout Standard 4887uint64_t kernelConvolution - specify if this application is used to create 4888 kernel for convolution, so it has the same properties/memory layout. 4889 performConvolution has to be set to 0 for the kernel creation. 4890 Default 0, set to 1 to enable. 4891 Optional parameter, but it is a required parameter for kernel generation. 4892\end_layout 4893 4894\begin_layout Subsubsection 4895Register overutilization 4896\end_layout 4897 4898\begin_layout Standard 4899Only works in C2C mode, without convolution support. 4900 Enabled in Vulkan, OpenCL and Level Zero APIs only (it works in other APIs, 4901 but worse, does not work in Metal). 4902 Experimental feature. 4903\end_layout 4904 4905\begin_layout Standard 4906uint64_t registerBoost - specify if the register file size is bigger than 4907 shared memory and can be used to extend it X times (on Nvidia 256KB register 4908 file can be used instead of 32KB of shared memory, set this constant to 4909 4 to emulate 128KB of shared memory). 4910 Default 1 - no overutilization. 4911 In Vulkan, OpenCL and Level Zero it is set to 4 on Nvidia GPUs, to 2 if 4912 the driver shows 64KB or more of shared memory on AMD, to 2 if the driver 4913 shows less than 64KB of shared memory on AMD, to 1 if the driver shows 4914 64KB or more of shared memory on Intel, to 2 if the driver shows less than 4915 64KB of shared memory on Intel. 4916 Optional parameter. 4917\end_layout 4918 4919\begin_layout Standard 4920uint64_t registerBoostNonPow2 - specify if register overutilization should 4921 be used on non-power of 2 sequences. 4922 Default 0, set to 1 to enable. 4923 Optional parameter. 4924\end_layout 4925 4926\begin_layout Standard 4927uint64_t registerBoost4Step - specify if register file overutilization should 4928 be used in big sequences (>2^14), same definition as registerBoost. 4929 Default 1. 4930 Optional parameter. 4931\end_layout 4932 4933\begin_layout Subsubsection 4934Extra advanced parameters (filled automatically) 4935\end_layout 4936 4937\begin_layout Standard 4938uint64_t computeCapabilityMajor - CUDA/HIP compute capability of the device 4939\end_layout 4940 4941\begin_layout Standard 4942uint64_t computeCapabilityMinor - CUDA/HIP compute capability of the device 4943 4944\end_layout 4945 4946\begin_layout Standard 4947uint64_t maxComputeWorkGroupCount[3] - how many workgroups can be launched 4948 at one dispatch. 4949 Automatically derived from the driver, can be artificially lowered. 4950 Then VkFFT will perform a logical split and extension of the number of 4951 workgroups to cover the required range. 4952\end_layout 4953 4954\begin_layout Standard 4955uint64_t maxComputeWorkGroupSize[3] - max dimensions of the workgroup. 4956 Automatically derived from the driver. 4957 Can be modified if there are some issues with the driver (as there were 4958 with ROCm 4.0, when it returned 1024 for maxComputeWorkGroupSize and actually 4959 supported only up to 256 threads). 4960\end_layout 4961 4962\begin_layout Standard 4963uint64_t maxThreadsNum - max number of threads per block. 4964 Similar to maxComputeWorkGroupSize, but aggregated. 4965 Automatically derived from the driver. 4966\end_layout 4967 4968\begin_layout Standard 4969uint64_t sharedMemorySizeStatic - available for static allocation shared 4970 memory size, in bytes. 4971 Automatically derived from the driver. 4972 Can be controlled by the user, if desired. 4973\end_layout 4974 4975\begin_layout Standard 4976uint64_t sharedMemorySize - available for allocation shared memory size, 4977 in bytes. 4978 VkFFT uses dynamic shared memory in CUDA/HIP as it allows for bigger allocation 4979s. 4980 Automatically derived from the driver. 4981 Can be controlled by the user, if desired. 4982\end_layout 4983 4984\begin_layout Standard 4985uint64_t sharedMemorySizePow2 - the power of 2 which is less or equal to 4986 sharedMemorySize, in bytes. 4987 Automatically computed. 4988\end_layout 4989 4990\begin_layout Standard 4991uint64_t warpSize - number of threads per warp/wavefront. 4992 Automatically derived from the driver, but can be modified (can increase 4993 performance, though unpredictable as defaults have good values). 4994 Must be a power of two. 4995\end_layout 4996 4997\begin_layout Standard 4998uint64_t halfThreads - Intel GPU fix, tries to reduce the amount of dispatched 4999 threads in half to solve performance degradation in the Four Step FFT algorithm. 5000 Default 0 for other GPUs, try enabling it if performance degrades in the 5001 Four Step FFT algorithm for your GPU as well. 5002 5003\end_layout 5004 5005\begin_layout Standard 5006int64_t maxCodeLength - specify how big can the buffer used for code generation 5007 be (in char). 5008 Default 1000000 chars. 5009 5010\end_layout 5011 5012\begin_layout Standard 5013int64_t maxTempLength - specify how big can the buffer used for intermediate 5014 string sprintf's be (in char). 5015 Default 5000 chars. 5016 If code segfaults for some reason - try increasing this number. 5017\end_layout 5018 5019\begin_layout Standard 5020uint64_t autoCustomBluesteinPaddingPattern - default value for useCustomBluestei 5021nPaddingPattern 5022\end_layout 5023 5024\begin_layout Standard 5025uint64_t useRaderUintLUT - allocate additional LUT to store g_pow 5026\end_layout 5027 5028\begin_layout Standard 5029uint64_t vendorID - vendorID 0x10DE - NVIDIA, 0x8086 - Intel, 0x1002 - AMD, 5030 etc. 5031\end_layout 5032 5033\begin_layout Standard 5034\begin_inset Newpage newpage 5035\end_inset 5036 5037 5038\end_layout 5039 5040\begin_layout Section 5041VkFFT Benchmark/Precision Suite and utils_VkFFT helper routines 5042\end_layout 5043 5044\begin_layout Standard 5045The only licensed (MIT) part of the VkFFT repository is the VkFFT header 5046 file - core library. 5047 Other files are either external helper libraries (half, glslang, with their 5048 respective licenses) or unlicensed code that is intended for simple copy-pastin 5049g (benchmark_scripts, utils_VkFFT.h). 5050 It is the easiest way to understand how to use VkFFT by taking the provided 5051 scripts and tinker them to the particular task. 5052 The current version of the benchmark and precision verification suite has 5053 the following codes available: 5054\end_layout 5055 5056\begin_layout Itemize 5057user_benchmark_VkFFT - generalization of the main configuration parameters 5058 that can be used to launch simplest in-place transforms for the most important 5059 supported functionality 5060\end_layout 5061 5062\begin_layout Itemize 5063Sample 0 - FFT + iFFT C2C benchmark 1D batched in single precision 5064\end_layout 5065 5066\begin_layout Itemize 5067Sample 1 - FFT + iFFT C2C benchmark 1D batched in double precision 5068\end_layout 5069 5070\begin_layout Itemize 5071Sample 2 - FFT + iFFT C2C benchmark 1D batched in half precision 5072\end_layout 5073 5074\begin_layout Itemize 5075Sample 3 - FFT + iFFT C2C multidimensional benchmark in single precision 5076\end_layout 5077 5078\begin_layout Itemize 5079Sample 4 - FFT + iFFT C2C multidimensional benchmark in single precision, 5080 native zeropadding 5081\end_layout 5082 5083\begin_layout Itemize 5084Sample 5 - FFT + iFFT C2C benchmark 1D batched in single precision, no reshuffli 5085ng 5086\end_layout 5087 5088\begin_layout Itemize 5089Sample 6 - FFT + iFFT R2C / C2R benchmark, in-place. 5090\end_layout 5091 5092\begin_layout Itemize 5093Sample 7 - FFT + iFFT C2C Bluestein benchmark in single precision 5094\end_layout 5095 5096\begin_layout Itemize 5097Sample 8 - FFT + iFFT C2C Bluestein benchmark in double precision 5098\end_layout 5099 5100\begin_layout Itemize 5101Sample 10 - multiple buffers (4 by default) split version of benchmark 0 5102\end_layout 5103 5104\begin_layout Itemize 5105Sample 11 - VkFFT / xFFT / FFTW C2C precision test in single precision (xFFT 5106 can be cuFFT or rocFFT) 5107\end_layout 5108 5109\begin_layout Itemize 5110Sample 12 - VkFFT / xFFT / FFTW C2C precision test in double precision (xFFT 5111 can be cuFFT or rocFFT) 5112\end_layout 5113 5114\begin_layout Itemize 5115Sample 13 - VkFFT / cuFFT / FFTW C2C precision test in half precision 5116\end_layout 5117 5118\begin_layout Itemize 5119Sample 14 - VkFFT / FFTW C2C radix 3 / 5 / 7 / 11 / 13 / Bluestein precision 5120 test in single precision 5121\end_layout 5122 5123\begin_layout Itemize 5124Sample 15 - VkFFT / xFFT / FFTW R2C+C2R precision test in single precision, 5125 out-of-place. 5126 (xFFT can be cuFFT or rocFFT) 5127\end_layout 5128 5129\begin_layout Itemize 5130Sample 16 - VkFFT / FFTW R2R DCT-I, II, III and IV precision test in single 5131 precision 5132\end_layout 5133 5134\begin_layout Itemize 5135Sample 17 - VkFFT / FFTW R2R DCT-I, II, III and IV precision test in double 5136 precision 5137\end_layout 5138 5139\begin_layout Itemize 5140Sample 18 - VkFFT / FFTW C2C radix 3 / 5 / 7 / 11 / 13 / Bluestein precision 5141 test in double precision 5142\end_layout 5143 5144\begin_layout Itemize 5145Sample 50 - convolution example with identity kernel 5146\end_layout 5147 5148\begin_layout Itemize 5149Sample 51 - zero padding convolution example with identity kernel 5150\end_layout 5151 5152\begin_layout Itemize 5153Sample 52 - batched convolution example with identity kernel 5154\end_layout 5155 5156\begin_layout Itemize 5157Sample 100 - VkFFT FFT + iFFT R2R DCT multidimensional benchmark in single 5158 precision 5159\end_layout 5160 5161\begin_layout Itemize 5162Sample 101 - VkFFT FFT + iFFT R2R DCT multidimensional benchmark in double 5163 precision 5164\end_layout 5165 5166\begin_layout Itemize 5167Sample 1000 - FFT + iFFT C2C benchmark 1D batched in single precision: all 5168 supported systems from 2 to 4096 5169\end_layout 5170 5171\begin_layout Itemize 5172Sample 1001 - FFT + iFFT C2C benchmark 1D batched in single precision: all 5173 supported systems from 2 to 4096 5174\end_layout 5175 5176\begin_layout Itemize 5177Sample 1003 - FFT + iFFT C2C benchmark 1D batched in single precision: all 5178 supported systems from 2 to 4096 5179\end_layout 5180 5181\begin_layout Subsection 5182utils_VkFFT helper routines 5183\end_layout 5184 5185\begin_layout Standard 5186Launching even the simplest Vulkan application can be a non-trivial task. 5187 To help with this, utils_VkFFT contains the routines that can help to create 5188 the simplest Vulkan application, allocate memory, record command buffers 5189 and launch them. 5190 Code has some comments explaining what is going on at each step. 5191 It also has some useful struct defines (like vkGPU) that keep the most 5192 important handles used in Vulkan Compute. 5193 This section may be expanded in the future to the proper step-by-step guide 5194 on Vulkan Compute simple application creation. 5195 I also encourage to check https://github.com/DTolm/VulkanComputeSamples-Transpos 5196ition repository for another example of a compute algorithm (matrix transpositio 5197n) implemented with Vulkan API. 5198\end_layout 5199 5200\begin_layout Standard 5201utils_VkFFT also has a routine that prints the list of available devices. 5202\end_layout 5203 5204\begin_layout Standard 5205vkGPU struct has the following definition: 5206\end_layout 5207 5208\begin_layout Standard 5209\begin_inset ERT 5210status open 5211 5212\begin_layout Plain Layout 5213 5214 5215\backslash 5216begin{mdframed}[backgroundcolor=bg] 5217\end_layout 5218 5219\begin_layout Plain Layout 5220 5221 5222\backslash 5223begin{minted}[tabsize=4,obeytabs,breaklines]{C} 5224\end_layout 5225 5226\begin_layout Plain Layout 5227 5228typedef struct { 5229\end_layout 5230 5231\begin_layout Plain Layout 5232 5233#if(VKFFT_BACKEND==0) //Vulkan API 5234\end_layout 5235 5236\begin_layout Plain Layout 5237 5238VkInstance instance; //a connection between the application and the Vulkan 5239 library 5240\end_layout 5241 5242\begin_layout Plain Layout 5243 5244VkPhysicalDevice physicalDevice; //a handle for the graphics card used in 5245 the application 5246\end_layout 5247 5248\begin_layout Plain Layout 5249 5250VkPhysicalDeviceProperties physicalDeviceProperties; //bastic device properties 5251\end_layout 5252 5253\begin_layout Plain Layout 5254 5255VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties; //basic 5256 memory properties of the device 5257\end_layout 5258 5259\begin_layout Plain Layout 5260 5261VkDevice device; //a logical device, interacting with physical device 5262\end_layout 5263 5264\begin_layout Plain Layout 5265 5266VkDebugUtilsMessengerEXT debugMessenger; //extension for debugging 5267\end_layout 5268 5269\begin_layout Plain Layout 5270 5271uint64_t queueFamilyIndex; //if multiple queues are available, specify the 5272 used one 5273\end_layout 5274 5275\begin_layout Plain Layout 5276 5277VkQueue queue; //a place, where all operations are submitted 5278\end_layout 5279 5280\begin_layout Plain Layout 5281 5282VkCommandPool commandPool; //an opaque objects that command buffer memory 5283 is allocated from 5284\end_layout 5285 5286\begin_layout Plain Layout 5287 5288VkFence fence; //a vkGPU->fence used to synchronize dispatches 5289\end_layout 5290 5291\begin_layout Plain Layout 5292 5293std::vector<const char*> enabledDeviceExtensions; 5294\end_layout 5295 5296\begin_layout Plain Layout 5297 5298uint64_t enableValidationLayers; 5299\end_layout 5300 5301\begin_layout Plain Layout 5302 5303#elif(VKFFT_BACKEND==1) //CUDA API 5304\end_layout 5305 5306\begin_layout Plain Layout 5307 5308CUdevice device; 5309\end_layout 5310 5311\begin_layout Plain Layout 5312 5313CUcontext context; 5314\end_layout 5315 5316\begin_layout Plain Layout 5317 5318#elif(VKFFT_BACKEND==2) //HIP API 5319\end_layout 5320 5321\begin_layout Plain Layout 5322 5323hipDevice_t device; 5324\end_layout 5325 5326\begin_layout Plain Layout 5327 5328hipCtx_t context; 5329\end_layout 5330 5331\begin_layout Plain Layout 5332 5333#elif(VKFFT_BACKEND==3) //OpenCL API 5334\end_layout 5335 5336\begin_layout Plain Layout 5337 5338cl_platform_id platform; 5339\end_layout 5340 5341\begin_layout Plain Layout 5342 5343cl_device_id device; 5344\end_layout 5345 5346\begin_layout Plain Layout 5347 5348cl_context context; 5349\end_layout 5350 5351\begin_layout Plain Layout 5352 5353cl_command_queue commandQueue; 5354\end_layout 5355 5356\begin_layout Plain Layout 5357 5358#elif(VKFFT_BACKEND==4) //Level Zero API 5359\end_layout 5360 5361\begin_layout Plain Layout 5362 5363ze_driver_handle_t driver; 5364\end_layout 5365 5366\begin_layout Plain Layout 5367 5368ze_device_handle_t device; 5369\end_layout 5370 5371\begin_layout Plain Layout 5372 5373ze_context_handle_t context; 5374\end_layout 5375 5376\begin_layout Plain Layout 5377 5378ze_command_queue_handle_t commandQueue; 5379\end_layout 5380 5381\begin_layout Plain Layout 5382 5383uint32_t commandQueueID; 5384\end_layout 5385 5386\begin_layout Plain Layout 5387 5388#elif(VKFFT_BACKEND==5) //Metal API 5389\end_layout 5390 5391\begin_layout Plain Layout 5392 5393MTL::Device* device; 5394\end_layout 5395 5396\begin_layout Plain Layout 5397 5398MTL::CommandQueue* queue; 5399\end_layout 5400 5401\begin_layout Plain Layout 5402 5403#endif 5404\end_layout 5405 5406\begin_layout Plain Layout 5407 5408uint64_t device_id; //an id of a device, reported by devices_list call 5409\end_layout 5410 5411\begin_layout Plain Layout 5412 5413} VkGPU; 5414\end_layout 5415 5416\begin_layout Plain Layout 5417 5418 5419\backslash 5420end{minted} 5421\end_layout 5422 5423\begin_layout Plain Layout 5424 5425 5426\backslash 5427end{mdframed} 5428\end_layout 5429 5430\end_inset 5431 5432 5433\begin_inset Newpage newpage 5434\end_inset 5435 5436 5437\end_layout 5438 5439\begin_layout Section 5440VkFFT Code Examples 5441\end_layout 5442 5443\begin_layout Standard 5444This section will provide some simple pseudocode for VkFFT usage, which 5445 will once again outline important steps required to launch FFT with VkFFT. 5446 More information (and fully working code) can be found in this folder of 5447 the VkFFT repository: 5448\end_layout 5449 5450\begin_layout Standard 5451/benchmark_samples/vkFFT_scripts/src/ 5452\end_layout 5453 5454\begin_layout Subsection 5455Driver initializations 5456\end_layout 5457 5458\begin_layout Standard 5459Before launching VkFFT, do not forget to do all necessary driver initializations. 5460 The following code specifies them for all the supported backends, though 5461 the final implementation may be different depending on the particular user's 5462 configuration. 5463\end_layout 5464 5465\begin_layout Standard 5466\begin_inset ERT 5467status open 5468 5469\begin_layout Plain Layout 5470 5471 5472\backslash 5473begin{mdframed}[backgroundcolor=bg] 5474\end_layout 5475 5476\begin_layout Plain Layout 5477 5478 5479\backslash 5480begin{minted}[tabsize=4,obeytabs,breaklines]{C} 5481\end_layout 5482 5483\begin_layout Plain Layout 5484 5485#if(VKFFT_BACKEND==0) //Vulkan API 5486\end_layout 5487 5488\begin_layout Plain Layout 5489 5490VkResult res = VK_SUCCESS; 5491\end_layout 5492 5493\begin_layout Plain Layout 5494 5495//create instance - a connection between the application and the Vulkan 5496 library 5497\end_layout 5498 5499\begin_layout Plain Layout 5500 5501res = createInstance(vkGPU, sample_id); 5502\end_layout 5503 5504\begin_layout Plain Layout 5505 5506if (res != 0) { 5507\end_layout 5508 5509\begin_layout Plain Layout 5510 5511 //printf("Instance creation failed, error code: %" PRIu64 " 5512\backslash 5513n", res); 5514\end_layout 5515 5516\begin_layout Plain Layout 5517 5518 return VKFFT_ERROR_FAILED_TO_CREATE_INSTANCE; 5519\end_layout 5520 5521\begin_layout Plain Layout 5522 5523} 5524\end_layout 5525 5526\begin_layout Plain Layout 5527 5528//set up the debugging messenger 5529\end_layout 5530 5531\begin_layout Plain Layout 5532 5533res = setupDebugMessenger(vkGPU); 5534\end_layout 5535 5536\begin_layout Plain Layout 5537 5538if (res != 0) { 5539\end_layout 5540 5541\begin_layout Plain Layout 5542 5543//printf("Debug messenger creation failed, error code: %" PRIu64 " 5544\backslash 5545n", res); 5546\end_layout 5547 5548\begin_layout Plain Layout 5549 5550 return VKFFT_ERROR_FAILED_TO_SETUP_DEBUG_MESSENGER; 5551\end_layout 5552 5553\begin_layout Plain Layout 5554 5555} 5556\end_layout 5557 5558\begin_layout Plain Layout 5559 5560//check if there are GPUs that support Vulkan and select one 5561\end_layout 5562 5563\begin_layout Plain Layout 5564 5565res = findPhysicalDevice(vkGPU); 5566\end_layout 5567 5568\begin_layout Plain Layout 5569 5570if (res != 0) { 5571\end_layout 5572 5573\begin_layout Plain Layout 5574 5575//printf("Physical device not found, error code: %" PRIu64 " 5576\backslash 5577n", res); 5578\end_layout 5579 5580\begin_layout Plain Layout 5581 5582 return VKFFT_ERROR_FAILED_TO_FIND_PHYSICAL_DEVICE; 5583\end_layout 5584 5585\begin_layout Plain Layout 5586 5587} 5588\end_layout 5589 5590\begin_layout Plain Layout 5591 5592//create logical device representation 5593\end_layout 5594 5595\begin_layout Plain Layout 5596 5597res = createDevice(vkGPU, sample_id); 5598\end_layout 5599 5600\begin_layout Plain Layout 5601 5602if (res != 0) { 5603\end_layout 5604 5605\begin_layout Plain Layout 5606 5607//printf("Device creation failed, error code: %" PRIu64 " 5608\backslash 5609n", res); 5610\end_layout 5611 5612\begin_layout Plain Layout 5613 5614 return VKFFT_ERROR_FAILED_TO_CREATE_DEVICE; 5615\end_layout 5616 5617\begin_layout Plain Layout 5618 5619} 5620\end_layout 5621 5622\begin_layout Plain Layout 5623 5624//create fence for synchronization 5625\end_layout 5626 5627\begin_layout Plain Layout 5628 5629res = createFence(vkGPU); 5630\end_layout 5631 5632\begin_layout Plain Layout 5633 5634if (res != 0) { 5635\end_layout 5636 5637\begin_layout Plain Layout 5638 5639//printf("Fence creation failed, error code: %" PRIu64 " 5640\backslash 5641n", res); 5642\end_layout 5643 5644\begin_layout Plain Layout 5645 5646 return VKFFT_ERROR_FAILED_TO_CREATE_FENCE; 5647\end_layout 5648 5649\begin_layout Plain Layout 5650 5651} 5652\end_layout 5653 5654\begin_layout Plain Layout 5655 5656//create a place, command buffer memory is allocated from 5657\end_layout 5658 5659\begin_layout Plain Layout 5660 5661res = createCommandPool(vkGPU); 5662\end_layout 5663 5664\begin_layout Plain Layout 5665 5666if (res != 0) { 5667\end_layout 5668 5669\begin_layout Plain Layout 5670 5671 //printf("Fence creation failed, error code: %" PRIu64 " 5672\backslash 5673n", res); 5674\end_layout 5675 5676\begin_layout Plain Layout 5677 5678 return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_POOL; 5679\end_layout 5680 5681\begin_layout Plain Layout 5682 5683} 5684\end_layout 5685 5686\begin_layout Plain Layout 5687 5688vkGetPhysicalDeviceProperties(vkGPU->physicalDevice, &vkGPU->physicalDevicePrope 5689rties); 5690\end_layout 5691 5692\begin_layout Plain Layout 5693 5694vkGetPhysicalDeviceMemoryProperties(vkGPU->physicalDevice, &vkGPU->physicalDevic 5695eMemoryProperties); 5696\end_layout 5697 5698\begin_layout Plain Layout 5699 5700glslang_initialize_process(); 5701\end_layout 5702 5703\begin_layout Plain Layout 5704 5705//compiler can be initialized before VkFFT 5706\end_layout 5707 5708\begin_layout Plain Layout 5709 5710\end_layout 5711 5712\begin_layout Plain Layout 5713 5714#elif(VKFFT_BACKEND==1) //CUDA API 5715\end_layout 5716 5717\begin_layout Plain Layout 5718 5719CUresult res = CUDA_SUCCESS; 5720\end_layout 5721 5722\begin_layout Plain Layout 5723 5724cudaError_t res2 = cudaSuccess; 5725\end_layout 5726 5727\begin_layout Plain Layout 5728 5729res = cuInit(0); 5730\end_layout 5731 5732\begin_layout Plain Layout 5733 5734if (res != CUDA_SUCCESS) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 5735\end_layout 5736 5737\begin_layout Plain Layout 5738 5739res2 = cudaSetDevice((int)vkGPU->device_id); 5740\end_layout 5741 5742\begin_layout Plain Layout 5743 5744if (res2 != cudaSuccess) return VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID; 5745\end_layout 5746 5747\begin_layout Plain Layout 5748 5749res = cuDeviceGet(&vkGPU->device, (int)vkGPU->device_id); 5750\end_layout 5751 5752\begin_layout Plain Layout 5753 5754if (res != CUDA_SUCCESS) return VKFFT_ERROR_FAILED_TO_GET_DEVICE; 5755\end_layout 5756 5757\begin_layout Plain Layout 5758 5759res = cuCtxCreate(&vkGPU->context, 0, (int)vkGPU->device); 5760\end_layout 5761 5762\begin_layout Plain Layout 5763 5764if (res != CUDA_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT; 5765\end_layout 5766 5767\begin_layout Plain Layout 5768 5769#elif(VKFFT_BACKEND==2) //HIP API 5770\end_layout 5771 5772\begin_layout Plain Layout 5773 5774hipError_t res = hipSuccess; 5775\end_layout 5776 5777\begin_layout Plain Layout 5778 5779res = hipInit(0); 5780\end_layout 5781 5782\begin_layout Plain Layout 5783 5784if (res != hipSuccess) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 5785\end_layout 5786 5787\begin_layout Plain Layout 5788 5789res = hipSetDevice((int)vkGPU->device_id); 5790\end_layout 5791 5792\begin_layout Plain Layout 5793 5794if (res != hipSuccess) return VKFFT_ERROR_FAILED_TO_SET_DEVICE_ID; 5795\end_layout 5796 5797\begin_layout Plain Layout 5798 5799res = hipDeviceGet(&vkGPU->device, (int)vkGPU->device_id); 5800\end_layout 5801 5802\begin_layout Plain Layout 5803 5804if (res != hipSuccess) return VKFFT_ERROR_FAILED_TO_GET_DEVICE; 5805\end_layout 5806 5807\begin_layout Plain Layout 5808 5809res = hipCtxCreate(&vkGPU->context, 0, (int)vkGPU->device); 5810\end_layout 5811 5812\begin_layout Plain Layout 5813 5814if (res != hipSuccess) return VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT; 5815\end_layout 5816 5817\begin_layout Plain Layout 5818 5819#elif(VKFFT_BACKEND==3) //OpenCL API 5820\end_layout 5821 5822\begin_layout Plain Layout 5823 5824cl_int res = CL_SUCCESS; 5825\end_layout 5826 5827\begin_layout Plain Layout 5828 5829cl_uint numPlatforms; 5830\end_layout 5831 5832\begin_layout Plain Layout 5833 5834res = clGetPlatformIDs(0, 0, &numPlatforms); 5835\end_layout 5836 5837\begin_layout Plain Layout 5838 5839if (res != CL_SUCCESS) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 5840\end_layout 5841 5842\begin_layout Plain Layout 5843 5844cl_platform_id* platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) 5845 * numPlatforms); 5846\end_layout 5847 5848\begin_layout Plain Layout 5849 5850if (!platforms) return VKFFT_ERROR_MALLOC_FAILED; 5851\end_layout 5852 5853\begin_layout Plain Layout 5854 5855res = clGetPlatformIDs(numPlatforms, platforms, 0); 5856\end_layout 5857 5858\begin_layout Plain Layout 5859 5860if (res != CL_SUCCESS) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 5861\end_layout 5862 5863\begin_layout Plain Layout 5864 5865uint64_t k = 0; 5866\end_layout 5867 5868\begin_layout Plain Layout 5869 5870for (uint64_t j = 0; j < numPlatforms; j++) { 5871\end_layout 5872 5873\begin_layout Plain Layout 5874 5875 cl_uint numDevices; 5876\end_layout 5877 5878\begin_layout Plain Layout 5879 5880 res = clGetDeviceIDs(platforms[j], CL_DEVICE_TYPE_ALL, 0, 0, &numDevices); 5881\end_layout 5882 5883\begin_layout Plain Layout 5884 5885 cl_device_id* deviceList = (cl_device_id*)malloc(sizeof(cl_device_id) * 5886 numDevices); 5887\end_layout 5888 5889\begin_layout Plain Layout 5890 5891 if (!deviceList) return VKFFT_ERROR_MALLOC_FAILED; 5892\end_layout 5893 5894\begin_layout Plain Layout 5895 5896 res = clGetDeviceIDs(platforms[j], CL_DEVICE_TYPE_ALL, numDevices, deviceList, 5897 0); 5898\end_layout 5899 5900\begin_layout Plain Layout 5901 5902 if (res != CL_SUCCESS) return VKFFT_ERROR_FAILED_TO_GET_DEVICE; 5903\end_layout 5904 5905\begin_layout Plain Layout 5906 5907 for (uint64_t i = 0; i < numDevices; i++) { 5908\end_layout 5909 5910\begin_layout Plain Layout 5911 5912 if (k == vkGPU->device_id) { 5913\end_layout 5914 5915\begin_layout Plain Layout 5916 5917 vkGPU->platform = platforms[j]; 5918\end_layout 5919 5920\begin_layout Plain Layout 5921 5922 vkGPU->device = deviceList[i]; 5923\end_layout 5924 5925\begin_layout Plain Layout 5926 5927 vkGPU->context = clCreateContext(NULL, 1, &vkGPU->device, NULL, NULL, 5928 &res); 5929\end_layout 5930 5931\begin_layout Plain Layout 5932 5933 if (res != CL_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT; 5934\end_layout 5935 5936\begin_layout Plain Layout 5937 5938 cl_command_queue commandQueue = clCreateCommandQueue(vkGPU->context, 5939 vkGPU->device, 0, &res); 5940\end_layout 5941 5942\begin_layout Plain Layout 5943 5944 if (res != CL_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QUEUE; 5945\end_layout 5946 5947\begin_layout Plain Layout 5948 5949 vkGPU->commandQueue = commandQueue; 5950\end_layout 5951 5952\begin_layout Plain Layout 5953 5954 k++; 5955\end_layout 5956 5957\begin_layout Plain Layout 5958 5959 } 5960\end_layout 5961 5962\begin_layout Plain Layout 5963 5964 else { 5965\end_layout 5966 5967\begin_layout Plain Layout 5968 5969 k++; 5970\end_layout 5971 5972\begin_layout Plain Layout 5973 5974 } 5975\end_layout 5976 5977\begin_layout Plain Layout 5978 5979 } 5980\end_layout 5981 5982\begin_layout Plain Layout 5983 5984 free(deviceList); 5985\end_layout 5986 5987\begin_layout Plain Layout 5988 5989} 5990\end_layout 5991 5992\begin_layout Plain Layout 5993 5994free(platforms); 5995\end_layout 5996 5997\begin_layout Plain Layout 5998 5999#elif(VKFFT_BACKEND==4) 6000\end_layout 6001 6002\begin_layout Plain Layout 6003 6004 ze_result_t res = ZE_RESULT_SUCCESS; 6005\end_layout 6006 6007\begin_layout Plain Layout 6008 6009 res = zeInit(0); 6010\end_layout 6011 6012\begin_layout Plain Layout 6013 6014 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 6015\end_layout 6016 6017\begin_layout Plain Layout 6018 6019 uint32_t numDrivers = 0; 6020\end_layout 6021 6022\begin_layout Plain Layout 6023 6024 res = zeDriverGet(&numDrivers, 0); 6025\end_layout 6026 6027\begin_layout Plain Layout 6028 6029 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 6030\end_layout 6031 6032\begin_layout Plain Layout 6033 6034 ze_driver_handle_t* drivers = (ze_driver_handle_t*)malloc(numDrivers * 6035 sizeof(ze_driver_handle_t)); 6036\end_layout 6037 6038\begin_layout Plain Layout 6039 6040 if (!drivers) return VKFFT_ERROR_MALLOC_FAILED; 6041\end_layout 6042 6043\begin_layout Plain Layout 6044 6045 res = zeDriverGet(&numDrivers, drivers); 6046\end_layout 6047 6048\begin_layout Plain Layout 6049 6050 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_INITIALIZE; 6051\end_layout 6052 6053\begin_layout Plain Layout 6054 6055 uint64_t k = 0; 6056\end_layout 6057 6058\begin_layout Plain Layout 6059 6060 for (uint64_t j = 0; j < numDrivers; j++) { 6061\end_layout 6062 6063\begin_layout Plain Layout 6064 6065 uint32_t numDevices = 0; 6066\end_layout 6067 6068\begin_layout Plain Layout 6069 6070 res = zeDeviceGet(drivers[j], &numDevices, nullptr); 6071\end_layout 6072 6073\begin_layout Plain Layout 6074 6075 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_GET_DEVICE; 6076\end_layout 6077 6078\begin_layout Plain Layout 6079 6080 ze_device_handle_t* deviceList = (ze_device_handle_t*)malloc(numDevices 6081 * sizeof(ze_device_handle_t)); 6082\end_layout 6083 6084\begin_layout Plain Layout 6085 6086 if (!deviceList) return VKFFT_ERROR_MALLOC_FAILED; 6087\end_layout 6088 6089\begin_layout Plain Layout 6090 6091 res = zeDeviceGet(drivers[j], &numDevices, deviceList); 6092\end_layout 6093 6094\begin_layout Plain Layout 6095 6096 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_GET_DEVICE; 6097\end_layout 6098 6099\begin_layout Plain Layout 6100 6101 for (uint64_t i = 0; i < numDevices; i++) { 6102\end_layout 6103 6104\begin_layout Plain Layout 6105 6106 if (k == vkGPU->device_id) { 6107\end_layout 6108 6109\begin_layout Plain Layout 6110 6111 vkGPU->driver = drivers[j]; 6112\end_layout 6113 6114\begin_layout Plain Layout 6115 6116 vkGPU->device = deviceList[i]; 6117\end_layout 6118 6119\begin_layout Plain Layout 6120 6121 ze_context_desc_t contextDescription = {}; 6122\end_layout 6123 6124\begin_layout Plain Layout 6125 6126 contextDescription.stype = ZE_STRUCTURE_TYPE_CONTEXT_DESC; 6127\end_layout 6128 6129\begin_layout Plain Layout 6130 6131 res = zeContextCreate(vkGPU->driver, &contextDescription, &vkGPU->context); 6132\end_layout 6133 6134\begin_layout Plain Layout 6135 6136 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_CONTEXT; 6137\end_layout 6138 6139\begin_layout Plain Layout 6140 6141\end_layout 6142 6143\begin_layout Plain Layout 6144 6145 uint32_t queueGroupCount = 0; 6146\end_layout 6147 6148\begin_layout Plain Layout 6149 6150 res = zeDeviceGetCommandQueueGroupProperties(vkGPU->device, &queueGroupCount 6151, 0); 6152\end_layout 6153 6154\begin_layout Plain Layout 6155 6156 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QU 6157EUE; 6158\end_layout 6159 6160\begin_layout Plain Layout 6161 6162\end_layout 6163 6164\begin_layout Plain Layout 6165 6166 ze_command_queue_group_properties_t* cmdqueueGroupProperties = (ze_command_q 6167ueue_group_properties_t*) malloc(queueGroupCount * sizeof(ze_command_queue_group 6168_properties_t)); 6169\end_layout 6170 6171\begin_layout Plain Layout 6172 6173 if (!cmdqueueGroupProperties) return VKFFT_ERROR_MALLOC_FAILED; 6174\end_layout 6175 6176\begin_layout Plain Layout 6177 6178 res = zeDeviceGetCommandQueueGroupProperties(vkGPU->device, &queueGroupCount 6179, cmdqueueGroupProperties); 6180\end_layout 6181 6182\begin_layout Plain Layout 6183 6184 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QU 6185EUE; 6186\end_layout 6187 6188\begin_layout Plain Layout 6189 6190\end_layout 6191 6192\begin_layout Plain Layout 6193 6194 uint32_t commandQueueID = -1; 6195\end_layout 6196 6197\begin_layout Plain Layout 6198 6199 for (uint32_t i = 0; i < queueGroupCount; ++i) { 6200\end_layout 6201 6202\begin_layout Plain Layout 6203 6204 if ((cmdqueueGroupProperties[i].flags && ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLA 6205G_COMPUTE) && (cmdqueueGroupProperties[i].flags && ZE_COMMAND_QUEUE_GROUP_PROPERT 6206Y_FLAG_COPY)) { 6207\end_layout 6208 6209\begin_layout Plain Layout 6210 6211 commandQueueID = i; 6212\end_layout 6213 6214\begin_layout Plain Layout 6215 6216 break; 6217\end_layout 6218 6219\begin_layout Plain Layout 6220 6221 } 6222\end_layout 6223 6224\begin_layout Plain Layout 6225 6226 } 6227\end_layout 6228 6229\begin_layout Plain Layout 6230 6231 if (commandQueueID == -1) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QUEUE; 6232\end_layout 6233 6234\begin_layout Plain Layout 6235 6236 vkGPU->commandQueueID = commandQueueID; 6237\end_layout 6238 6239\begin_layout Plain Layout 6240 6241 ze_command_queue_desc_t commandQueueDescription = {}; 6242\end_layout 6243 6244\begin_layout Plain Layout 6245 6246 commandQueueDescription.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC; 6247\end_layout 6248 6249\begin_layout Plain Layout 6250 6251 commandQueueDescription.ordinal = commandQueueID; 6252\end_layout 6253 6254\begin_layout Plain Layout 6255 6256 commandQueueDescription.priority = ZE_COMMAND_QUEUE_PRIORITY_NORMAL; 6257\end_layout 6258 6259\begin_layout Plain Layout 6260 6261 commandQueueDescription.mode = ZE_COMMAND_QUEUE_MODE_DEFAULT; 6262\end_layout 6263 6264\begin_layout Plain Layout 6265 6266 res = zeCommandQueueCreate(vkGPU->context, vkGPU->device, &commandQueueDescr 6267iption, &vkGPU->commandQueue); 6268\end_layout 6269 6270\begin_layout Plain Layout 6271 6272 if (res != ZE_RESULT_SUCCESS) return VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_QU 6273EUE; 6274\end_layout 6275 6276\begin_layout Plain Layout 6277 6278 free(cmdqueueGroupProperties); 6279\end_layout 6280 6281\begin_layout Plain Layout 6282 6283 k++; 6284\end_layout 6285 6286\begin_layout Plain Layout 6287 6288 } 6289\end_layout 6290 6291\begin_layout Plain Layout 6292 6293 else { 6294\end_layout 6295 6296\begin_layout Plain Layout 6297 6298 k++; 6299\end_layout 6300 6301\begin_layout Plain Layout 6302 6303 } 6304\end_layout 6305 6306\begin_layout Plain Layout 6307 6308 } 6309\end_layout 6310 6311\begin_layout Plain Layout 6312 6313\end_layout 6314 6315\begin_layout Plain Layout 6316 6317 free(deviceList); 6318\end_layout 6319 6320\begin_layout Plain Layout 6321 6322 } 6323\end_layout 6324 6325\begin_layout Plain Layout 6326 6327 free(drivers); 6328\end_layout 6329 6330\begin_layout Plain Layout 6331 6332#endif 6333\end_layout 6334 6335\begin_layout Plain Layout 6336 6337 6338\backslash 6339end{minted} 6340\end_layout 6341 6342\begin_layout Plain Layout 6343 6344 6345\backslash 6346end{mdframed} 6347\end_layout 6348 6349\end_inset 6350 6351 6352\end_layout 6353 6354\begin_layout Subsection 6355Simple FFT application example: 1D (one dimensional) C2C (complex to complex) 6356 FP32 (single precision) FFT 6357\end_layout 6358 6359\begin_layout Standard 6360This example performs the simplest case of FFT. 6361 It shows all the necessary fields that the user must fill during the configurat 6362ion and the submission process. 6363 Other samples will build on this one, as driver parameters initialization 6364 and code execution commands are the same for all configurations (except 6365 for the launch parameters that can be configured after application creation). 6366\end_layout 6367 6368\begin_layout Standard 6369\begin_inset ERT 6370status open 6371 6372\begin_layout Plain Layout 6373 6374 6375\backslash 6376begin{mdframed}[backgroundcolor=bg] 6377\end_layout 6378 6379\begin_layout Plain Layout 6380 6381 6382\backslash 6383begin{minted}[tabsize=4,obeytabs,breaklines]{C} 6384\end_layout 6385 6386\begin_layout Plain Layout 6387 6388//zero-initialize configuration + FFT application 6389\end_layout 6390 6391\begin_layout Plain Layout 6392 6393VkFFTConfiguration configuration = {}; 6394\end_layout 6395 6396\begin_layout Plain Layout 6397 6398VkFFTApplication app = {}; 6399\end_layout 6400 6401\begin_layout Plain Layout 6402 6403\end_layout 6404 6405\begin_layout Plain Layout 6406 6407configuration.FFTdim = 1; //FFT dimension, 1D, 2D or 3D 6408\end_layout 6409 6410\begin_layout Plain Layout 6411 6412configuration.size[0] = Nx; //FFT size 6413\end_layout 6414 6415\begin_layout Plain Layout 6416 6417uint64_t bufferSize = (uint64_t)sizeof(float) * 2 * configuration.size[0]; 6418\end_layout 6419 6420\begin_layout Plain Layout 6421 6422\end_layout 6423 6424\begin_layout Plain Layout 6425 6426//Device management + code submission 6427\end_layout 6428 6429\begin_layout Plain Layout 6430 6431configuration.device = &vkGPU->device; 6432\end_layout 6433 6434\begin_layout Plain Layout 6435 6436\end_layout 6437 6438\begin_layout Plain Layout 6439 6440#if(VKFFT_BACKEND==0) //Vulkan API 6441\end_layout 6442 6443\begin_layout Plain Layout 6444 6445configuration.queue = &vkGPU->queue; 6446\end_layout 6447 6448\begin_layout Plain Layout 6449 6450configuration.fence = &vkGPU->fence; 6451\end_layout 6452 6453\begin_layout Plain Layout 6454 6455configuration.commandPool = &vkGPU->commandPool; 6456\end_layout 6457 6458\begin_layout Plain Layout 6459 6460configuration.physicalDevice = &vkGPU->physicalDevice; 6461\end_layout 6462 6463\begin_layout Plain Layout 6464 6465configuration.isCompilerInitialized = isCompilerInitialized; //glslang compiler 6466 can be initialized before VkFFT plan creation. 6467 if not, VkFFT will create and destroy one after initialization 6468\end_layout 6469 6470\begin_layout Plain Layout 6471 6472#elif(VKFFT_BACKEND==3) //OpenCL API 6473\end_layout 6474 6475\begin_layout Plain Layout 6476 6477configuration.context = &vkGPU->context; 6478\end_layout 6479 6480\begin_layout Plain Layout 6481 6482#elif(VKFFT_BACKEND==4) 6483\end_layout 6484 6485\begin_layout Plain Layout 6486 6487configuration.context = &vkGPU->context; 6488\end_layout 6489 6490\begin_layout Plain Layout 6491 6492configuration.commandQueue = &vkGPU->commandQueue; 6493\end_layout 6494 6495\begin_layout Plain Layout 6496 6497configuration.commandQueueID = vkGPU->commandQueueID; 6498\end_layout 6499 6500\begin_layout Plain Layout 6501 6502#endif 6503\end_layout 6504 6505\begin_layout Plain Layout 6506 6507\end_layout 6508 6509\begin_layout Plain Layout 6510 6511allocateBuffer(buffer, bufferSize); //Pseudocode for buffer allocation, 6512 differs between APIs 6513\end_layout 6514 6515\begin_layout Plain Layout 6516 6517transferDataFromCPU(buffer, cpu_buffer); //Pseudocode for data transfer 6518 from CPU to GPU, differs between APIs 6519\end_layout 6520 6521\begin_layout Plain Layout 6522 6523\end_layout 6524 6525\begin_layout Plain Layout 6526 6527#if(VKFFT_BACKEND==0) //Vulkan API needs bufferSize at initialization 6528\end_layout 6529 6530\begin_layout Plain Layout 6531 6532configuration.bufferSize = &bufferSize; 6533\end_layout 6534 6535\begin_layout Plain Layout 6536 6537#endif 6538\end_layout 6539 6540\begin_layout Plain Layout 6541 6542\end_layout 6543 6544\begin_layout Plain Layout 6545 6546VkFFTResult resFFT = initializeVkFFT(&app, configuration); 6547\end_layout 6548 6549\begin_layout Plain Layout 6550 6551\end_layout 6552 6553\begin_layout Plain Layout 6554 6555VkFFTLaunchParams launchParams = {}; 6556\end_layout 6557 6558\begin_layout Plain Layout 6559 6560launchParams.buffer = &buffer; 6561\end_layout 6562 6563\begin_layout Plain Layout 6564 6565#if(VKFFT_BACKEND==0) //Vulkan API 6566\end_layout 6567 6568\begin_layout Plain Layout 6569 6570launchParams.commandBuffer = &commandBuffer; 6571\end_layout 6572 6573\begin_layout Plain Layout 6574 6575#elif(VKFFT_BACKEND==3) //OpenCL API 6576\end_layout 6577 6578\begin_layout Plain Layout 6579 6580launchParams.commandQueue = &commandQueue; 6581\end_layout 6582 6583\begin_layout Plain Layout 6584 6585#elif(VKFFT_BACKEND==4) //Level Zero API 6586\end_layout 6587 6588\begin_layout Plain Layout 6589 6590launchParams->commandList = &commandList; 6591\end_layout 6592 6593\begin_layout Plain Layout 6594 6595#elif(VKFFT_BACKEND==5) //Metal API 6596\end_layout 6597 6598\begin_layout Plain Layout 6599 6600launchParams->commandBuffer = commandBuffer; 6601\end_layout 6602 6603\begin_layout Plain Layout 6604 6605launchParams->commandEncoder = commandEncoder; 6606\end_layout 6607 6608\begin_layout Plain Layout 6609 6610#endif 6611\end_layout 6612 6613\begin_layout Plain Layout 6614 6615resFFT = VkFFTAppend(app, -1, &launchParams); 6616\end_layout 6617 6618\begin_layout Plain Layout 6619 6620\end_layout 6621 6622\begin_layout Plain Layout 6623 6624//add synchronization relevant to your API - vkWaitForFences/cudaDeviceSynchroni 6625ze/hipDeviceSynchronize/clFinish 6626\end_layout 6627 6628\begin_layout Plain Layout 6629 6630transferDataToCPU(cpu_buffer, buffer); //Pseudocode for data transfer from 6631 GPU to CPU, differs between APIs 6632\end_layout 6633 6634\begin_layout Plain Layout 6635 6636\end_layout 6637 6638\begin_layout Plain Layout 6639 6640freeBuffer(buffer, bufferSize); //Pseudocode for buffer deallocation, differs 6641 between APIs 6642\end_layout 6643 6644\begin_layout Plain Layout 6645 6646\end_layout 6647 6648\begin_layout Plain Layout 6649 6650deleteVkFFT(&app); 6651\end_layout 6652 6653\begin_layout Plain Layout 6654 6655 6656\backslash 6657end{minted} 6658\end_layout 6659 6660\begin_layout Plain Layout 6661 6662 6663\backslash 6664end{mdframed} 6665\end_layout 6666 6667\end_inset 6668 6669 6670\end_layout 6671 6672\begin_layout Subsection 6673Advanced FFT application example: ND, C2C/R2C/R2R, different precisions, 6674 batched FFT 6675\end_layout 6676 6677\begin_layout Standard 6678This example shows how to configure the main parameters of interest in the 6679 VkFFT library: multidimensional case, different types of transforms, different 6680 precision, perform batched transforms. 6681\end_layout 6682 6683\begin_layout Standard 6684In the code below X, Y and Z are the dimensions of FFT, B - number of batches, 6685 R2C - real to complex mode 0 or 1 (on/off), DCT - 0, 1, 2, 3 or 4 (off/DCT 6686 type), P - precision (0 - single, 1 - double, 2 - half). 6687\end_layout 6688 6689\begin_layout Standard 6690\begin_inset ERT 6691status open 6692 6693\begin_layout Plain Layout 6694 6695 6696\backslash 6697begin{mdframed}[backgroundcolor=bg] 6698\end_layout 6699 6700\begin_layout Plain Layout 6701 6702 6703\backslash 6704begin{minted}[tabsize=4,obeytabs,breaklines]{C} 6705\end_layout 6706 6707\begin_layout Plain Layout 6708 6709//zero-initialize configuration + FFT application 6710\end_layout 6711 6712\begin_layout Plain Layout 6713 6714VkFFTConfiguration configuration = {}; 6715\end_layout 6716 6717\begin_layout Plain Layout 6718 6719VkFFTApplication app = {}; 6720\end_layout 6721 6722\begin_layout Plain Layout 6723 6724\end_layout 6725 6726\begin_layout Plain Layout 6727 6728configuration.FFTdim = 1; //FFT dimension, 1D, 2D or 3D 6729\end_layout 6730 6731\begin_layout Plain Layout 6732 6733configuration.size[0] = X; 6734\end_layout 6735 6736\begin_layout Plain Layout 6737 6738configuration.size[1] = Y; 6739\end_layout 6740 6741\begin_layout Plain Layout 6742 6743configuration.size[2] = Z; 6744\end_layout 6745 6746\begin_layout Plain Layout 6747 6748if (Y > 1) configuration.FFTdim++; 6749\end_layout 6750 6751\begin_layout Plain Layout 6752 6753if (Z > 1) configuration.FFTdim++; 6754\end_layout 6755 6756\begin_layout Plain Layout 6757 6758configuration.numberBatches = B; 6759\end_layout 6760 6761\begin_layout Plain Layout 6762 6763configuration.performR2C = R2C; 6764\end_layout 6765 6766\begin_layout Plain Layout 6767 6768configuration.performDCT = DCT; 6769\end_layout 6770 6771\begin_layout Plain Layout 6772 6773if (P == 1) configuration.doublePrecision = 1; 6774\end_layout 6775 6776\begin_layout Plain Layout 6777 6778if (P == 2) configuration.halfPrecision = 1; 6779\end_layout 6780 6781\begin_layout Plain Layout 6782 6783\end_layout 6784 6785\begin_layout Plain Layout 6786 6787uint64_t bufferSize = 0; 6788\end_layout 6789 6790\begin_layout Plain Layout 6791 6792\end_layout 6793 6794\begin_layout Plain Layout 6795 6796if (R2C) { 6797\end_layout 6798 6799\begin_layout Plain Layout 6800 6801 bufferSize = (uint64_t)(storageComplexSize / 2) * (configuration.size[0] 6802 + 2) * configuration.size[1] * configuration.size[2] * configuration.numberBatches 6803; 6804\end_layout 6805 6806\begin_layout Plain Layout 6807 6808} 6809\end_layout 6810 6811\begin_layout Plain Layout 6812 6813else { 6814\end_layout 6815 6816\begin_layout Plain Layout 6817 6818 if (DCT) { 6819\end_layout 6820 6821\begin_layout Plain Layout 6822 6823 bufferSize = (uint64_t)(storageComplexSize / 2) * configuration.size[0] 6824 * configuration.size[1] * configuration.size[2] * configuration.numberBatches; 6825\end_layout 6826 6827\begin_layout Plain Layout 6828 6829 } 6830\end_layout 6831 6832\begin_layout Plain Layout 6833 6834 else { 6835\end_layout 6836 6837\begin_layout Plain Layout 6838 6839 bufferSize = (uint64_t)storageComplexSize * configuration.size[0] * configurati 6840on.size[1] * configuration.size[2] * configuration.numberBatches; 6841\end_layout 6842 6843\begin_layout Plain Layout 6844 6845 } 6846\end_layout 6847 6848\begin_layout Plain Layout 6849 6850} // storageComplexSize - 4/8/16 for FP16/FP32/FP64 respectively. 6851\end_layout 6852 6853\begin_layout Plain Layout 6854 6855\end_layout 6856 6857\begin_layout Plain Layout 6858 6859//Device management + code submission - code is identical to the previous 6860 example 6861\end_layout 6862 6863\begin_layout Plain Layout 6864 6865 6866\backslash 6867end{minted} 6868\end_layout 6869 6870\begin_layout Plain Layout 6871 6872 6873\backslash 6874end{mdframed} 6875\end_layout 6876 6877\end_inset 6878 6879 6880\end_layout 6881 6882\begin_layout Subsection 6883Advanced FFT application example: out-of-place R2C FFT with custom strides 6884\end_layout 6885 6886\begin_layout Standard 6887In this example, VkFFT is configured to calculate a 3D out-of-place R2C 6888 FFT of a system with custom strides. 6889 VkFFT reads data from the inputBuffer and produces the result in the buffer. 6890 6891\end_layout 6892 6893\begin_layout Standard 6894\begin_inset ERT 6895status open 6896 6897\begin_layout Plain Layout 6898 6899 6900\backslash 6901begin{mdframed}[backgroundcolor=bg] 6902\end_layout 6903 6904\begin_layout Plain Layout 6905 6906 6907\backslash 6908begin{minted}[tabsize=4,obeytabs,breaklines]{C} 6909\end_layout 6910 6911\begin_layout Plain Layout 6912 6913//zero-initialize configuration + FFT application 6914\end_layout 6915 6916\begin_layout Plain Layout 6917 6918VkFFTConfiguration configuration = {}; 6919\end_layout 6920 6921\begin_layout Plain Layout 6922 6923VkFFTApplication app = {}; 6924\end_layout 6925 6926\begin_layout Plain Layout 6927 6928\end_layout 6929 6930\begin_layout Plain Layout 6931 6932configuration.FFTdim = 3; //FFT dimension, 1D, 2D or 3D 6933\end_layout 6934 6935\begin_layout Plain Layout 6936 6937configuration.size[0] = Nx; 6938\end_layout 6939 6940\begin_layout Plain Layout 6941 6942configuration.size[1] = Ny; 6943\end_layout 6944 6945\begin_layout Plain Layout 6946 6947configuration.size[2] = Nz; 6948\end_layout 6949 6950\begin_layout Plain Layout 6951 6952\end_layout 6953 6954\begin_layout Plain Layout 6955 6956configuration.performR2C = 1; 6957\end_layout 6958 6959\begin_layout Plain Layout 6960 6961\end_layout 6962 6963\begin_layout Plain Layout 6964 6965//out-of-place - we need to specify that input buffer is separate from the 6966 main buffer 6967\end_layout 6968 6969\begin_layout Plain Layout 6970 6971configuration.isInputFormatted = 1; 6972\end_layout 6973 6974\begin_layout Plain Layout 6975 6976configuration.inputBufferStride[0] = configuration.size[0]; 6977\end_layout 6978 6979\begin_layout Plain Layout 6980 6981configuration.inputBufferStride[1] = configuration.inputBufferStride[0] * 6982 configuration.size[1]; 6983\end_layout 6984 6985\begin_layout Plain Layout 6986 6987configuration.inputBufferStride[2] = configuration.inputBufferStride[1] * 6988 configuration.size[2]; 6989\end_layout 6990 6991\begin_layout Plain Layout 6992 6993 6994\end_layout 6995 6996\begin_layout Plain Layout 6997 6998configuration.bufferStride[0] = (uint64_t) (configuration.size[0] / 2) + 1; 6999\end_layout 7000 7001\begin_layout Plain Layout 7002 7003configuration.bufferStride[1] = configuration.bufferStride[0] * configuration.size[ 70041]; 7005\end_layout 7006 7007\begin_layout Plain Layout 7008 7009configuration.bufferStride[2] = configuration.bufferStride[1]* configuration.size[2 7010]; 7011\end_layout 7012 7013\begin_layout Plain Layout 7014 7015\end_layout 7016 7017\begin_layout Plain Layout 7018 7019uint64_t inputBufferSize = (uint64_t)sizeof(float) * configuration.size[0] 7020 * configuration.size[1] * configuration.size[2]; 7021\end_layout 7022 7023\begin_layout Plain Layout 7024 7025\end_layout 7026 7027\begin_layout Plain Layout 7028 7029uint64_t bufferSize = (uint64_t)sizeof(float) * 2 * (configuration.size[0]/2+1) 7030 * configuration.size[1] * configuration.size[2]; 7031\end_layout 7032 7033\begin_layout Plain Layout 7034 7035\end_layout 7036 7037\begin_layout Plain Layout 7038 7039//Device management + code submission - code is identical to the first example, 7040 except that you need to allocate two buffers (and provide them in the launch 7041 configuration). 7042\end_layout 7043 7044\begin_layout Plain Layout 7045 7046\end_layout 7047 7048\begin_layout Plain Layout 7049 7050 7051\backslash 7052end{minted} 7053\end_layout 7054 7055\begin_layout Plain Layout 7056 7057 7058\backslash 7059end{mdframed} 7060\end_layout 7061 7062\end_inset 7063 7064 7065\end_layout 7066 7067\begin_layout Subsection 7068Advanced FFT application example: 3D zero-padded FFT 7069\end_layout 7070 7071\begin_layout Standard 7072In this example, VkFFT is configured to calculate a 3D FFT of a system. 7073 The meaningful data is located in the first octant of the buffer, the rest 7074 is padded with zeros. 7075 This configuration removes the circular part of the convolution and allows 7076 modelling of open systems. 7077\end_layout 7078 7079\begin_layout Standard 7080\begin_inset ERT 7081status open 7082 7083\begin_layout Plain Layout 7084 7085 7086\backslash 7087begin{mdframed}[backgroundcolor=bg] 7088\end_layout 7089 7090\begin_layout Plain Layout 7091 7092 7093\backslash 7094begin{minted}[tabsize=4,obeytabs,breaklines]{C} 7095\end_layout 7096 7097\begin_layout Plain Layout 7098 7099//zero-initialize configuration + FFT application 7100\end_layout 7101 7102\begin_layout Plain Layout 7103 7104VkFFTConfiguration configuration = {}; 7105\end_layout 7106 7107\begin_layout Plain Layout 7108 7109VkFFTApplication app = {}; 7110\end_layout 7111 7112\begin_layout Plain Layout 7113 7114\end_layout 7115 7116\begin_layout Plain Layout 7117 7118configuration.FFTdim = 3; //FFT dimension, 1D, 2D or 3D 7119\end_layout 7120 7121\begin_layout Plain Layout 7122 7123configuration.size[0] = Nx; 7124\end_layout 7125 7126\begin_layout Plain Layout 7127 7128configuration.size[1] = Ny; 7129\end_layout 7130 7131\begin_layout Plain Layout 7132 7133configuration.size[2] = Nz; 7134\end_layout 7135 7136\begin_layout Plain Layout 7137 7138\end_layout 7139 7140\begin_layout Plain Layout 7141 7142configuration.performZeropadding[0] = 1; //Perform padding with zeros on 7143 GPU. 7144 Still need to properly align input data (no need to fill padding area with 7145 meaningful data) but this will increase performance due to the lower amount 7146 of the memory reads/writes and omitting sequences only consisting of zeros. 7147\end_layout 7148 7149\begin_layout Plain Layout 7150 7151configuration.performZeropadding[1] = 1; 7152\end_layout 7153 7154\begin_layout Plain Layout 7155 7156configuration.performZeropadding[2] = 1; 7157\end_layout 7158 7159\begin_layout Plain Layout 7160 7161configuration.fft_zeropad_left[0] = (uint64_t)ceil(configuration.size[0] / 7162 2.0); 7163\end_layout 7164 7165\begin_layout Plain Layout 7166 7167configuration.fft_zeropad_right[0] = configuration.size[0]; 7168\end_layout 7169 7170\begin_layout Plain Layout 7171 7172configuration.fft_zeropad_left[1] = (uint64_t)ceil(configuration.size[1] / 7173 2.0); 7174\end_layout 7175 7176\begin_layout Plain Layout 7177 7178configuration.fft_zeropad_right[1] = configuration.size[1]; 7179\end_layout 7180 7181\begin_layout Plain Layout 7182 7183configuration.fft_zeropad_left[2] = (uint64_t)ceil(configuration.size[2] / 7184 2.0); 7185\end_layout 7186 7187\begin_layout Plain Layout 7188 7189configuration.fft_zeropad_right[2] = configuration.size[2]; 7190\end_layout 7191 7192\begin_layout Plain Layout 7193 7194\end_layout 7195 7196\begin_layout Plain Layout 7197 7198uint64_t bufferSize = (uint64_t)storageComplexSize * configuration.size[0] 7199 * configuration.size[1] * configuration.size[2]; 7200\end_layout 7201 7202\begin_layout Plain Layout 7203 7204\end_layout 7205 7206\begin_layout Plain Layout 7207 7208\end_layout 7209 7210\begin_layout Plain Layout 7211 7212//Device management + code submission - code is identical to the first example 7213\end_layout 7214 7215\begin_layout Plain Layout 7216 7217\end_layout 7218 7219\begin_layout Plain Layout 7220 7221 7222\backslash 7223end{minted} 7224\end_layout 7225 7226\begin_layout Plain Layout 7227 7228 7229\backslash 7230end{mdframed} 7231\end_layout 7232 7233\end_inset 7234 7235 7236\end_layout 7237 7238\begin_layout Subsection 7239Convolution application example: 3x3 matrix-vector convolution in 1D 7240\end_layout 7241 7242\begin_layout Standard 7243In this example, VkFFT is configured to calculate a kernel, represented 7244 by a 3x3 matrix and a system, represented by a 3D vector. 7245 Their convolution is a matrix-vector multiplication in the frequency domain. 7246\end_layout 7247 7248\begin_layout Standard 7249\begin_inset ERT 7250status open 7251 7252\begin_layout Plain Layout 7253 7254 7255\backslash 7256begin{mdframed}[backgroundcolor=bg] 7257\end_layout 7258 7259\begin_layout Plain Layout 7260 7261 7262\backslash 7263begin{minted}[tabsize=4,obeytabs,breaklines]{C} 7264\end_layout 7265 7266\begin_layout Plain Layout 7267 7268//zero-initialize configuration + FFT application, we need two - one for 7269 kernel calculation 7270\end_layout 7271 7272\begin_layout Plain Layout 7273 7274VkFFTConfiguration kernel_configuration = {}; 7275\end_layout 7276 7277\begin_layout Plain Layout 7278 7279VkFFTConfiguration convolution_configuration = {}; 7280\end_layout 7281 7282\begin_layout Plain Layout 7283 7284VkFFTApplication app_kernel = {}; 7285\end_layout 7286 7287\begin_layout Plain Layout 7288 7289VkFFTApplication app_convolution = {}; 7290\end_layout 7291 7292\begin_layout Plain Layout 7293 7294\end_layout 7295 7296\begin_layout Plain Layout 7297 7298\end_layout 7299 7300\begin_layout Plain Layout 7301 7302kernel_configuration.FFTdim = 1; //FFT dimension, 1D, 2D or 3D 7303\end_layout 7304 7305\begin_layout Plain Layout 7306 7307kernel_configuration.size[0] = Nx; //FFT size 7308\end_layout 7309 7310\begin_layout Plain Layout 7311 7312\end_layout 7313 7314\begin_layout Plain Layout 7315 7316uint64_t bufferSize = (uint64_t)sizeof(float) * 2 * kernel_configuration.size[0]; 7317\end_layout 7318 7319\begin_layout Plain Layout 7320 7321\end_layout 7322 7323\begin_layout Plain Layout 7324 7325//configure kernel 7326\end_layout 7327 7328\begin_layout Plain Layout 7329 7330kernel_configuration.kernelConvolution = 1; //specify if this plan is used 7331 to create kernel for convolution 7332\end_layout 7333 7334\begin_layout Plain Layout 7335 7336kernel_configuration.coordinateFeatures = 9; //Specify dimensionality of 7337 the input feature vector (default 1). 7338 Each component is stored not as a vector, but as a separate system and 7339 padded on it's own according to other options (i.e. 7340 for x*y system of 3-vector, first x*y elements correspond to the first 7341 dimension, then goes x*y for the second, etc). 7342\end_layout 7343 7344\begin_layout Plain Layout 7345 7346//coordinateFeatures number is an important constant for convolution. 7347 If we perform 1x1 convolution, it is equal to number of features, but matrixCon 7348volution should be equal to 1. 7349 For matrix convolution, it must be equal to matrixConvolution parameter. 7350 If we perform 2x2 convolution, it is equal to 3 for symmetric kernel (stored 7351 as xx, xy, yy) and 4 for nonsymmetric (stored as xx, xy, yx, yy). 7352 Similarly, 6 (stored as xx, xy, xz, yy, yz, zz) and 9 (stored as xx, xy, 7353 xz, yx, yy, yz, zx, zy, zz) for 3x3 convolutions. 7354 7355\end_layout 7356 7357\begin_layout Plain Layout 7358 7359kernel_configuration.normalize = 1; 7360\end_layout 7361 7362\begin_layout Plain Layout 7363 7364\end_layout 7365 7366\begin_layout Plain Layout 7367 7368//Initialize app_kernel and perform a single forward FFT like in examples 7369 before. 7370 You pass kernel as a buffer for the preparation stage. 7371\end_layout 7372 7373\begin_layout Plain Layout 7374 7375\end_layout 7376 7377\begin_layout Plain Layout 7378 7379convolution_configuration = kernel_configuration; 7380\end_layout 7381 7382\begin_layout Plain Layout 7383 7384convolution_configuration.kernelConvolution = 0; 7385\end_layout 7386 7387\begin_layout Plain Layout 7388 7389convolution_configuration.performConvolution = 1; 7390\end_layout 7391 7392\begin_layout Plain Layout 7393 7394convolution_configuration.symmetricKernel = 0;//Specify if convolution kernel 7395 is symmetric. 7396 In this case we only pass upper triangle part of it in the form of: (xx, 7397 xy, yy) for 2d and (xx, xy, xz, yy, yz, zz) for 3d. 7398\end_layout 7399 7400\begin_layout Plain Layout 7401 7402convolution_configuration.matrixConvolution = 3;//we do matrix convolution, 7403 so kernel is 9 numbers (3x3), but vector dimension is 3 7404\end_layout 7405 7406\begin_layout Plain Layout 7407 7408convolution_configuration.coordinateFeatures = 3;//equal to matrixConvolution 7409 size 7410\end_layout 7411 7412\begin_layout Plain Layout 7413 7414\end_layout 7415 7416\begin_layout Plain Layout 7417 7418//Initialize app_convolution and perform a single forward FFT like in examples 7419 before. 7420 You pass kernel as kernel and system to be convolved with it as buffer 7421\end_layout 7422 7423\begin_layout Plain Layout 7424 7425\end_layout 7426 7427\begin_layout Plain Layout 7428 7429 7430\backslash 7431end{minted} 7432\end_layout 7433 7434\begin_layout Plain Layout 7435 7436 7437\backslash 7438end{mdframed} 7439\end_layout 7440 7441\end_inset 7442 7443 7444\end_layout 7445 7446\begin_layout Subsection 7447Convolution application example: R2C cross-correlation between two sets 7448 of N images 7449\end_layout 7450 7451\begin_layout Standard 7452In this example, VkFFT is configured to calculate a kernel, represented 7453 by three 2D vectors (RGB values of a pixel) and a system, also represented 7454 by three 2D vectors. 7455 There are N kernels and N systems. 7456 Their cross-correlation is a conjugate convolution in the frequency domain. 7457 Images are usually stored as real, not complex numbers, so code uses R2C 7458 optimization as well. 7459\end_layout 7460 7461\begin_layout Standard 7462\begin_inset ERT 7463status open 7464 7465\begin_layout Plain Layout 7466 7467 7468\backslash 7469begin{mdframed}[backgroundcolor=bg] 7470\end_layout 7471 7472\begin_layout Plain Layout 7473 7474 7475\backslash 7476begin{minted}[tabsize=4,obeytabs,breaklines]{C} 7477\end_layout 7478 7479\begin_layout Plain Layout 7480 7481//zero-initialize configuration + FFT application, we need two - one for 7482 kernel calculation 7483\end_layout 7484 7485\begin_layout Plain Layout 7486 7487VkFFTConfiguration kernel_configuration = {}; 7488\end_layout 7489 7490\begin_layout Plain Layout 7491 7492VkFFTConfiguration convolution_configuration = {}; 7493\end_layout 7494 7495\begin_layout Plain Layout 7496 7497VkFFTApplication app_kernel = {}; 7498\end_layout 7499 7500\begin_layout Plain Layout 7501 7502VkFFTApplication app_convolution = {}; 7503\end_layout 7504 7505\begin_layout Plain Layout 7506 7507\end_layout 7508 7509\begin_layout Plain Layout 7510 7511\end_layout 7512 7513\begin_layout Plain Layout 7514 7515kernel_configuration.FFTdim = 2; //FFT dimension, 1D, 2D or 3D 7516\end_layout 7517 7518\begin_layout Plain Layout 7519 7520kernel_configuration.size[0] = Nx; 7521\end_layout 7522 7523\begin_layout Plain Layout 7524 7525kernel_configuration.size[1] = Ny; 7526\end_layout 7527 7528\begin_layout Plain Layout 7529 7530kernel_configuration.coordinateFeatures = 3; 7531\end_layout 7532 7533\begin_layout Plain Layout 7534 7535kernel_configuration.numberBatches = N; 7536\end_layout 7537 7538\begin_layout Plain Layout 7539 7540kernel_configuration.performR2C = 1; 7541\end_layout 7542 7543\begin_layout Plain Layout 7544 7545kernel_configuration.normalize = 1; 7546\end_layout 7547 7548\begin_layout Plain Layout 7549 7550\end_layout 7551 7552\begin_layout Plain Layout 7553 7554uint64_t bufferSize = (uint64_t)sizeof(float) * 2 * (kernel_configuration.size[0] 7555/2+1) * kernel_configuration.size[1] * kernel_configuration.coordinateFeatures 7556 * kernel_configuration.numberBatches; 7557\end_layout 7558 7559\begin_layout Plain Layout 7560 7561 7562\end_layout 7563 7564\begin_layout Plain Layout 7565 7566kernel_configuration.kernelConvolution = 1; //specify if this plan is used 7567 to create kernel for convolution 7568\end_layout 7569 7570\begin_layout Plain Layout 7571 7572\end_layout 7573 7574\begin_layout Plain Layout 7575 7576//Initialize app_kernel and perform a single forward FFT like in examples 7577 before. 7578 Pad in-place R2C system like this: 7579\end_layout 7580 7581\begin_layout Plain Layout 7582 7583\end_layout 7584 7585\begin_layout Plain Layout 7586 7587for (uint64_t n = 0; n < kernel_configuration.numberBatches; n++) { 7588\end_layout 7589 7590\begin_layout Plain Layout 7591 7592 for (uint64_t c = 0; c < kernel_configuration.coordinateFeatures; c++) { 7593\end_layout 7594 7595\begin_layout Plain Layout 7596 7597 for (uint64_t j = 0; j < kernel_configuration.size[1]; j++) { 7598\end_layout 7599 7600\begin_layout Plain Layout 7601 7602 for (uint64_t i = 0; i < kernel_configuration.size[0]; i++) { 7603\end_layout 7604 7605\begin_layout Plain Layout 7606 7607 kernel_padded_GPU[i + j * 2 * (kernel_configuration.size[0]/2 + 1) + 7608 c * 2 * (kernel_configuration.size[0]/2 + 1) * kernel_configuration.size[1] 7609 + n * 2 * (kernel_configuration.size[0]/2 + 1) * kernel_configuration.size[1] 7610 * kernel_configuration.coordinateFeatures] = kernel_input[i + j * kernel_configu 7611ration.size[0] + c * kernel_configuration.size[0] * kernel_configuration.size[1] 7612 + n * kernel_configuration.size[0] * kernel_configuration.size[1] * kernel_config 7613uration.coordinateFeatures]; 7614\end_layout 7615 7616\begin_layout Plain Layout 7617 7618 } 7619\end_layout 7620 7621\begin_layout Plain Layout 7622 7623 } 7624\end_layout 7625 7626\begin_layout Plain Layout 7627 7628 } 7629\end_layout 7630 7631\begin_layout Plain Layout 7632 7633} 7634\end_layout 7635 7636\begin_layout Plain Layout 7637 7638convolution_configuration = kernel_configuration; 7639\end_layout 7640 7641\begin_layout Plain Layout 7642 7643convolution_configuration.kernelConvolution = 0; 7644\end_layout 7645 7646\begin_layout Plain Layout 7647 7648convolution_configuration.performConvolution = 1; 7649\end_layout 7650 7651\begin_layout Plain Layout 7652 7653convolution_configuration.conjugateConvolution = 1; 7654\end_layout 7655 7656\begin_layout Plain Layout 7657 7658\end_layout 7659 7660\begin_layout Plain Layout 7661 7662//Initialize app_convolution and perform a single forward FFT like in examples 7663 before. 7664 Pad the system in the same way as the kernel 7665\end_layout 7666 7667\begin_layout Plain Layout 7668 7669\end_layout 7670 7671\begin_layout Plain Layout 7672 7673 7674\backslash 7675end{minted} 7676\end_layout 7677 7678\begin_layout Plain Layout 7679 7680 7681\backslash 7682end{mdframed} 7683\end_layout 7684 7685\end_inset 7686 7687 7688\end_layout 7689 7690\begin_layout Subsection 7691Simple FFT application binary reuse application 7692\end_layout 7693 7694\begin_layout Standard 7695This example shows how to save/load binaries generated by VkFFT. 7696 This can reduce time taken by initializeVkFFT call by removing RTC components 7697 from it. 7698 Be sure that rest of the configuration stays the same to reuse the binary. 7699 Use rb/wb flags to load/save. 7700 This does not currently work in Metal backend. 7701\end_layout 7702 7703\begin_layout Standard 7704\begin_inset ERT 7705status open 7706 7707\begin_layout Plain Layout 7708 7709 7710\backslash 7711begin{mdframed}[backgroundcolor=bg] 7712\end_layout 7713 7714\begin_layout Plain Layout 7715 7716 7717\backslash 7718begin{minted}[tabsize=4,obeytabs,breaklines]{C} 7719\end_layout 7720 7721\begin_layout Plain Layout 7722 7723VkFFTConfiguration configuration = {}; 7724\end_layout 7725 7726\begin_layout Plain Layout 7727 7728VkFFTApplication app = {}; 7729\end_layout 7730 7731\begin_layout Plain Layout 7732 7733\end_layout 7734 7735\begin_layout Plain Layout 7736 7737//configuration is initialized like in other examples 7738\end_layout 7739 7740\begin_layout Plain Layout 7741 7742configuration.saveApplicationToString = 1; 7743\end_layout 7744 7745\begin_layout Plain Layout 7746 7747//configuration.loadApplicationFromString = 1; //choose one to save/load 7748 binary file 7749\end_layout 7750 7751\begin_layout Plain Layout 7752 7753\end_layout 7754 7755\begin_layout Plain Layout 7756 7757if (configuration.loadApplicationFromString) { 7758\end_layout 7759 7760\begin_layout Plain Layout 7761 7762 FILE* kernelCache; 7763\end_layout 7764 7765\begin_layout Plain Layout 7766 7767 uint64_t str_len; 7768\end_layout 7769 7770\begin_layout Plain Layout 7771 7772 kernelCache = fopen("VkFFT_binary", "rb"); 7773\end_layout 7774 7775\begin_layout Plain Layout 7776 7777 fseek(kernelCache, 0, SEEK_END); 7778\end_layout 7779 7780\begin_layout Plain Layout 7781 7782 str_len = ftell(kernelCache); 7783\end_layout 7784 7785\begin_layout Plain Layout 7786 7787 fseek(kernelCache, 0, SEEK_SET); 7788\end_layout 7789 7790\begin_layout Plain Layout 7791 7792 configuration.loadApplicationString = malloc(str_len); 7793\end_layout 7794 7795\begin_layout Plain Layout 7796 7797 fread(configuration.loadApplicationString, str_len, 1, kernelCache); 7798\end_layout 7799 7800\begin_layout Plain Layout 7801 7802 fclose(kernelCache); 7803\end_layout 7804 7805\begin_layout Plain Layout 7806 7807} 7808\end_layout 7809 7810\begin_layout Plain Layout 7811 7812\end_layout 7813 7814\begin_layout Plain Layout 7815 7816resFFT = initializeVkFFT(&app, configuration); 7817\end_layout 7818 7819\begin_layout Plain Layout 7820 7821if (resFFT != VKFFT_SUCCESS) return resFFT; 7822\end_layout 7823 7824\begin_layout Plain Layout 7825 7826\end_layout 7827 7828\begin_layout Plain Layout 7829 7830if (configuration.loadApplicationFromString) 7831\end_layout 7832 7833\begin_layout Plain Layout 7834 7835 free(configuration.loadApplicationString); 7836\end_layout 7837 7838\begin_layout Plain Layout 7839 7840\end_layout 7841 7842\begin_layout Plain Layout 7843 7844if (configuration.saveApplicationToString) { 7845\end_layout 7846 7847\begin_layout Plain Layout 7848 7849 FILE* kernelCache; 7850\end_layout 7851 7852\begin_layout Plain Layout 7853 7854 kernelCache = fopen("VkFFT_binary", "wb"); 7855\end_layout 7856 7857\begin_layout Plain Layout 7858 7859 fwrite(app.saveApplicationString, app.applicationStringSize, 1, kernelCache); 7860\end_layout 7861 7862\begin_layout Plain Layout 7863 7864 fclose(kernelCache); 7865\end_layout 7866 7867\begin_layout Plain Layout 7868 7869} 7870\end_layout 7871 7872\begin_layout Plain Layout 7873 7874\end_layout 7875 7876\begin_layout Plain Layout 7877 7878//application is launched like in other examples 7879\end_layout 7880 7881\begin_layout Plain Layout 7882 7883 7884\backslash 7885end{minted} 7886\end_layout 7887 7888\begin_layout Plain Layout 7889 7890 7891\backslash 7892end{mdframed} 7893\end_layout 7894 7895\end_inset 7896 7897 7898\end_layout 7899 7900\end_body 7901\end_document