yum-mirror/VkFFT

git clone https://git.yummers.dev/yum-mirror/VkFFT

Tolmachev DmitriiLevel Zero support in VkFFT -This update adds Intel Level Zero backend in VkFFT (VKFFT_BACKEND 4) -Level Zero backend has similar performance compared to other backends (tested on UHD610 GPU) -Level Zero backend passes all VkFFT tests OpenCL passes (tested on UHD610 GPU) -Added kernel caching option to user_benchmark_VkFFT (-save and -load parameters) -platform pointer is no longer required in OpenCL configurationb5de819

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