PeriDyno 1.0.0
Loading...
Searching...
No Matches
VkFFT.cpp
Go to the documentation of this file.
1#include "VkFFT.h"
2#include "VkSystem.h"
3#include "VkContext.h"
4
5#include "VkFFT_Utils.h"
6
7namespace dyno
8{
10 {
11 }
12
14 {
16 }
17
19 {
21
22 vkGPU.enableValidationLayers = 0;
23 vkGPU.device_id = 0;
24
25 VkResult res = VK_SUCCESS;
26 //create instance - a connection between the application and the Vulkan library
28 // res = createInstance(vkGPU, sample_id);
29 // if (res != 0) {
30 // //printf("Instance creation failed, error code: %" PRIu64 "\n", res);
31 // return VKFFT_ERROR_FAILED_TO_CREATE_INSTANCE;
32 // }
33 //set up the debugging messenger
35 if (res != 0) {
36 //printf("Debug messenger creation failed, error code: %" PRIu64 "\n", res);
37 return false;
38 }
39 //check if there are GPUs that support Vulkan and select one
40 vkGPU.physicalDevice = VkSystem::instance()->getPhysicalDevice();
41 // res = findPhysicalDevice(vkGPU);
42 // if (res != 0) {
43 // //printf("Physical device not found, error code: %" PRIu64 "\n", res);
44 // return VKFFT_ERROR_FAILED_TO_FIND_PHYSICAL_DEVICE;
45 // }
46 //create logical device representation
49 vkGetDeviceQueue(vkGPU.device, (uint32_t)vkGPU.queueFamilyIndex, 0, &vkGPU.queue);
50 // res = createDevice(vkGPU, sample_id);
51 // if (res != 0) {
52 // //printf("Device creation failed, error code: %" PRIu64 "\n", res);
53 // return VKFFT_ERROR_FAILED_TO_CREATE_DEVICE;
54 // }
55 //create fence for synchronization
56 res = createFence(&vkGPU);
57 if (res != 0) {
58 //printf("Fence creation failed, error code: %" PRIu64 "\n", res);
59 return false;
60 }
61 //create a place, command buffer memory is allocated from
63 if (res != 0) {
64 //printf("Fence creation failed, error code: %" PRIu64 "\n", res);
66 }
67 vkGetPhysicalDeviceProperties(vkGPU.physicalDevice, &vkGPU.physicalDeviceProperties);
68 vkGetPhysicalDeviceMemoryProperties(vkGPU.physicalDevice, &vkGPU.physicalDeviceMemoryProperties);
69
70 glslang_initialize_process();//compiler can be initialized before VkFFT
71
72 return true;
73 }
74
76 {
77 bool isCompilerInitialized = true;
78
80 VkResult res = VK_SUCCESS;
81
82 const int num_benchmark_samples = 2;
83 const int num_runs = 3;
84 uint64_t benchmark_dimensions[num_benchmark_samples][4] = { {256, 256, 1, 2}, {64, 64, 1, 2} };
85 double benchmark_result = 0;//averaged result = sum(system_size/iteration_time)/num_benchmark_samples
86
87
88 //FFT + iFFT sample code.
89 //Setting up FFT configuration for forward and inverse FFT.
90 configuration.FFTdim = 2; //FFT dimension, 1D, 2D or 3D (default 1).
91 configuration.size[0] = array2d.nx(); //Multidimensional FFT dimensions sizes (default 1). For best performance (and stability), order dimensions in descendant size order as: x>y>z.
92 configuration.size[1] = array2d.ny();
93 configuration.size[2] = 1;
94
95 //After this, configuration file contains pointers to Vulkan objects needed to work with the GPU: VkDevice* device - created device, [uint64_t *bufferSize, VkBuffer *buffer, VkDeviceMemory* bufferDeviceMemory] - allocated GPU memory FFT is performed on. [uint64_t *kernelSize, VkBuffer *kernel, VkDeviceMemory* kernelDeviceMemory] - allocated GPU memory, where kernel for convolution is stored.
96 configuration.device = &vkGPU.device;
97 configuration.queue = &vkGPU.queue; //to allocate memory for LUT, we have to pass a queue, vkGPU->fence, commandPool and physicalDevice pointers
98 configuration.fence = &vkGPU.fence;
99 configuration.commandPool = &vkGPU.commandPool;
100 configuration.physicalDevice = &vkGPU.physicalDevice;
101 configuration.isCompilerInitialized = isCompilerInitialized;//compiler can be initialized before VkFFT plan creation. if not, VkFFT will create and destroy one after initialization
102
103 //Allocate buffer for the input data.
104 VkBuffer buffer = array2d.bufferHandle();
105 configuration.buffer = &buffer;
106
107 uint64_t bufferSize = array2d.bufferSize();
108 configuration.bufferSize = &bufferSize;
109 if (resFFT != VKFFT_SUCCESS) return false;
110 //free(buffer_input);
111
112 //Initialize applications. This function loads shaders, creates pipeline and configures FFT based on configuration file. No buffer allocations inside VkFFT library.
114 if (resFFT != VKFFT_SUCCESS) return false;
115
116 return true;
117 }
118
120 {
121 VkFFTResult resFFT = VKFFT_SUCCESS;
122 //Submit FFT+iFFT.
123 int tag = type == VkFFT_INVERSE ? -1 : 1;
124 VkFFTLaunchParams launchParams = {};
125 resFFT = performVulkanFFT(&vkGPU, &app, &launchParams, tag, 1);
126 if (resFFT != VKFFT_SUCCESS) return false;
127
128
129 return true;
130 }
131
133 {
134 bool resFFT = VKFFT_SUCCESS;
135 VkFFT* fft = new VkFFT;
136 resFFT = fft->createContext();
137 if (resFFT != true)
138 {
139 delete fft;
140 return nullptr;
141 }
142
143 resFFT = fft->createPipeline(array2d);
144 if (resFFT != true)
145 {
146 delete fft;
147 return nullptr;
148 }
149
150 return fft;
151 }
152
153}
static VkFFTResult initializeVkFFT(VkFFTApplication *app, VkFFTConfiguration inputLaunchConfiguration)
static void deleteVkFFT(VkFFTApplication *app)
VkFFTResult
Definition VkFFT_Defs.h:232
@ VKFFT_SUCCESS
Definition VkFFT_Defs.h:233
@ VKFFT_ERROR_FAILED_TO_CREATE_COMMAND_POOL
Definition VkFFT_Defs.h:287
VkResult createFence(VkGPU *vkGPU)
VkResult setupDebugMessenger(VkGPU *vkGPU)
VkResult createCommandPool(VkGPU *vkGPU)
VkResult getComputeQueueFamilyIndex(VkGPU *vkGPU)
VkFFTResult performVulkanFFT(VkGPU *vkGPU, VkFFTApplication *app, VkFFTLaunchParams *launchParams, int inverse, uint64_t num_iter)
VkDevice deviceHandle()
Definition VkContext.h:26
uint32_t nx() const
uint32_t bufferSize() override
uint32_t ny() const
bool createPipeline(VkDeviceArray2D< dyno::Vec2f > &array2d)
Definition VkFFT.cpp:75
bool update(VkFFT_Type type)
Definition VkFFT.cpp:119
bool createContext()
Definition VkFFT.cpp:18
VkFFTApplication app
Definition VkFFT.h:32
VkGPU vkGPU
Definition VkFFT.h:30
static VkFFT * createInstance(VkDeviceArray2D< dyno::Vec2f > &array2d)
Definition VkFFT.cpp:132
VkFFTConfiguration configuration
Definition VkFFT.h:31
VkPhysicalDevice getPhysicalDevice()
Definition VkSystem.h:37
VkContext * currentContext()
Definition VkSystem.h:21
VkInstance instanceHandle()
Definition VkSystem.h:27
static VkSystem * instance()
Definition VkSystem.cpp:10
VkBuffer bufferHandle() const
Definition VkVariable.h:37
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
VkFFT_Type
Definition VkFFT.h:10
@ VkFFT_INVERSE
Definition VkFFT.h:11