yum-mirror/VkFFT

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

Tolmachev DmitriiRadix 6, 8, 9, 10, 12, 14, 15, 16, 32 support + Bluestein tuning -Added support for more composite radix kenrels. Improves performance by reducing shared memory communications -Added Bluestein sequence advanced tuning: can now specify the sequence to pad to. Added default tuned values for FP32 and FP64 for Nvidia A100 and AMD MI250 for sequences up to 4096 -Improved LUT usage: do not upload the first radix, coalesced upload to shared memory for small stage requests. -Bugfixes: C2R check for big radix, matrix convolution coordinate assignment, specification of device_id in cuFFT/rocFFT scripts9640836

v1.2.26
7.2 KiB198 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//CUDA parts
15#include "cuda_runtime.h"
16#include "device_launch_parameters.h"
17#include <cufft.h>
18
19#include "user_benchmark_cuFFT.h"
20
21void user_benchmark_cuFFT(bool file_output, FILE* output, cuFFTUserSystemParameters* userParams, int device_id)
22{
23	
24	cudaSetDevice(device_id);
25	const int num_runs = 3;
26	double benchmark_result[2] = { 0,0 };//averaged result = sum(system_size/iteration_time)/num_benchmark_samples
27	uint64_t storageComplexSize;
28	switch (userParams->P) {
29	case 0:
30		storageComplexSize = (2 * sizeof(float));
31		break;
32	case 1:
33		storageComplexSize = (2 * sizeof(double));
34		break;
35	case 2:
36		storageComplexSize = (2 * 2);
37		break;
38	}
39	for (int n = 0; n < 2; n++) {
40		double run_time[num_runs][2];
41		for (int r = 0; r < num_runs; r++) {
42			cufftHandle plan;
43			cufftHandle plan2;
44			void* dataC;
45
46			int dims[3];
47			int FFTdim = 1;
48			if (userParams->Y > 1) FFTdim++;
49			if (userParams->Z > 1) FFTdim++;
50			switch (FFTdim) {
51			case 1:
52				dims[0] = userParams->X;
53				dims[1] = 1;
54				dims[2] = 1;
55				break;
56			case 2:
57				dims[0] = userParams->Y;
58				dims[1] = userParams->X;
59				dims[2] = 1;
60				break;
61			case 3:
62				dims[0] = userParams->Z;
63				dims[1] = userParams->Y;
64				dims[2] = userParams->X;
65				break;
66			}
67			uint64_t bufferSize;
68			if (userParams->R2C)
69				bufferSize = (uint64_t)(storageComplexSize / 2) * (userParams->X + 2) * userParams->Y * userParams->Z * userParams->B;
70			else 
71				bufferSize = (uint64_t)storageComplexSize * userParams->X * userParams->Y * userParams->Z * userParams->B;
72			
73			cudaMalloc((void**)&dataC, bufferSize);
74			if (cudaGetLastError() != cudaSuccess) {
75				fprintf(stderr, "Cuda error: Failed to allocate\n");
76				return;
77			}
78			//forward + inverse
79			int iembed[2][3];
80			int istride[2] = { 1, 1 };
81			int idist[2] = {bufferSize / userParams->B / storageComplexSize, bufferSize / userParams->B / storageComplexSize};
82			if (userParams->R2C) idist[0] *= 2;
83			int oembed[2][3];
84			int ostride[2] = { 1, 1 };
85			int odist[2] = { bufferSize / userParams->B / storageComplexSize, bufferSize / userParams->B / storageComplexSize };
86			if (userParams->R2C) odist[1] *= 2;
87			switch (FFTdim) {
88			case 1:
89				iembed[0][0] = (userParams->R2C) ? dims[0] + 2 : dims[0];
90				oembed[0][0] = (userParams->R2C) ? (dims[0] + 2) / 2 : dims[0];
91
92				iembed[1][0] = (userParams->R2C) ? (dims[0] + 2) / 2 : dims[0];
93				oembed[1][0] = (userParams->R2C) ? dims[0] + 2 : dims[0];
94				break;
95			case 2:
96				iembed[0][0] = dims[0];
97				iembed[0][1] = (userParams->R2C) ? dims[1] + 2 : dims[1];
98				oembed[0][0] = dims[0];
99				oembed[0][1] = (userParams->R2C) ? (dims[1] + 2) / 2 : dims[1];
100
101				iembed[1][0] = dims[0];
102				iembed[1][1] = (userParams->R2C) ? (dims[1] + 2) / 2 : dims[1];
103				oembed[1][0] = dims[0];
104				oembed[1][1] = (userParams->R2C) ? dims[1] + 2 : dims[1];
105				break;
106			case 3:
107				iembed[0][0] = idist[0];
108				iembed[0][1] = dims[1];
109				iembed[0][2] = (userParams->R2C) ? dims[2] + 2 : dims[2];
110				oembed[0][0] = odist[0];
111				oembed[0][1] = dims[1];
112				oembed[0][2] = (userParams->R2C) ? (dims[2] + 2)/2 : dims[2];
113
114				iembed[1][0] = idist[0];
115				iembed[1][1] = dims[1];
116				iembed[1][2] = (userParams->R2C) ? (dims[2] + 2)/2 : dims[2];
117				oembed[1][0] = odist[0];
118				oembed[1][1] = dims[1];
119				oembed[1][2] = (userParams->R2C) ? dims[2] + 2 : dims[2];
120				break;
121			}
122			switch (userParams->P) {
123			case 0:
124				if (userParams->R2C) {
125					cufftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0], CUFFT_R2C, userParams->B);
126					cufftPlanMany(&plan2, FFTdim, dims, iembed[1], istride[1], idist[1], oembed[1], ostride[1], odist[1], CUFFT_C2R, userParams->B);
127				}
128				else {
129					cufftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0], CUFFT_C2C, userParams->B);
130				}
131				break;
132			case 1:
133				if (userParams->R2C) {
134					cufftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0], CUFFT_D2Z, userParams->B);
135					cufftPlanMany(&plan2, FFTdim, dims, iembed[1], istride[1], idist[1], oembed[1], ostride[1], odist[1], CUFFT_Z2D, userParams->B);
136				}
137				else
138					cufftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0], CUFFT_Z2Z, userParams->B);
139				break;
140			}
141
142			float totTime = 0;
143			std::chrono::steady_clock::time_point timeSubmit = std::chrono::steady_clock::now();
144			for (int i = 0; i < userParams->N; i++) {
145				switch (userParams->P) {
146				case 0:
147					if (userParams->R2C){
148						cufftExecR2C(plan, (cufftReal*) dataC, (cufftComplex*) dataC);
149						cufftExecC2R(plan2, (cufftComplex*) dataC, (cufftReal*) dataC);
150					}else{
151						cufftExecC2C(plan, (cufftComplex*) dataC, (cufftComplex*) dataC, -1);
152						cufftExecC2C(plan, (cufftComplex*) dataC, (cufftComplex*) dataC, 1);
153					}
154					break;
155				case 1:
156					if (userParams->R2C){
157						cufftExecD2Z(plan, (cufftDoubleReal*) dataC, (cufftDoubleComplex*) dataC);
158						cufftExecZ2D(plan2, (cufftDoubleComplex*) dataC, (cufftDoubleReal*) dataC);
159					}else{
160						cufftExecZ2Z(plan, (cufftDoubleComplex*) dataC, (cufftDoubleComplex*) dataC, -1);
161						cufftExecZ2Z(plan, (cufftDoubleComplex*) dataC, (cufftDoubleComplex*) dataC, 1);
162					}
163					break;
164				}
165			}
166			cudaDeviceSynchronize();
167			std::chrono::steady_clock::time_point timeEnd = std::chrono::steady_clock::now();
168			totTime = (std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeSubmit).count() * 0.001) / userParams->N;
169				
170			run_time[r][0] = totTime;
171			if (n > 0) {
172				if (r == num_runs - 1) {
173					double std_error = 0;
174					double avg_time = 0;
175					for (uint64_t t = 0; t < num_runs; t++) {
176						avg_time += run_time[t][0];
177					}
178					avg_time /= num_runs;
179					for (uint64_t t = 0; t < num_runs; t++) {
180						std_error += (run_time[t][0] - avg_time) * (run_time[t][0] - avg_time);
181					}
182					std_error = sqrt(std_error / num_runs);
183					if (file_output)
184						fprintf(output, "cuFFT 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\n",  userParams->X, userParams->Y, userParams->Z, userParams->B, bufferSize / 1024 / 1024, avg_time, std_error, userParams->N, (uint64_t)(((double)bufferSize / 1024) / avg_time), ((double)bufferSize / 1024.0 / 1024.0 / 1.024 * 4 * FFTdim / avg_time));
185
186					printf("cuFFT 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\n", userParams->X, userParams->Y, userParams->Z, userParams->B, bufferSize / 1024 / 1024, avg_time, std_error, userParams->N, (uint64_t)(((double)bufferSize / 1024) / avg_time), ((double)bufferSize / 1024.0 / 1024.0 / 1.024 * 4 * FFTdim / avg_time));
187					benchmark_result[0] += ((double)bufferSize / 1024) / avg_time;
188				}
189
190			}
191			cufftDestroy(plan);
192			if (userParams->R2C)
193				cufftDestroy(plan2);
194			cudaFree(dataC);
195			cudaDeviceSynchronize();
196		}
197	}
198}