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.4 KiB199 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//ROCM parts
15#include "hip/hip_runtime.h"
16#include <hipfft.h>
17
18#include "user_benchmark_rocFFT.h"
19
20void user_benchmark_rocFFT(bool file_output, FILE* output, rocFFTUserSystemParameters* userParams, int device_id)
21{
22	hipSetDevice(device_id);
23	const int num_runs = 7;
24	double benchmark_result[2] = { 0,0 };//averaged result = sum(system_size/iteration_time)/num_benchmark_samples
25	uint64_t storageComplexSize;
26	switch (userParams->P) {
27	case 0:
28		storageComplexSize = (2 * sizeof(float));
29		break;
30	case 1:
31		storageComplexSize = (2 * sizeof(double));
32		break;
33	case 2:
34		storageComplexSize = (2 * 2);
35		break;
36	}
37	for (int n = 0; n < 2; n++) {
38		double run_time[num_runs][2];
39		for (int r = 0; r < num_runs; r++) {
40			hipfftHandle plan;
41			hipfftHandle plan2;
42			void* dataC;
43
44			int dims[3];
45			int FFTdim = 1;
46			if (userParams->Y > 1) FFTdim++;
47			if (userParams->Z > 1) FFTdim++;
48			switch (FFTdim) {
49			case 1:
50				dims[0] = userParams->X;
51				dims[1] = 1;
52				dims[2] = 1;
53				break;
54			case 2:
55				dims[0] = userParams->Y;
56				dims[1] = userParams->X;
57				dims[2] = 1;
58				break;
59			case 3:
60				dims[0] = userParams->Z;
61				dims[1] = userParams->Y;
62				dims[2] = userParams->X;
63				break;
64			}
65			uint64_t bufferSize;
66			if (userParams->R2C)
67				bufferSize = (uint64_t)(storageComplexSize / 2) * (userParams->X + 2) * userParams->Y * userParams->Z * userParams->B;
68			else
69				bufferSize = (uint64_t)storageComplexSize * userParams->X * userParams->Y * userParams->Z * userParams->B;
70
71			hipMalloc((void**)&dataC, bufferSize);
72			
73			if (hipGetLastError() != hipSuccess) {
74				fprintf(stderr, "ROCM error: Failed to allocate\n");
75				return;
76			}
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					hipfftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0], HIPFFT_R2C, userParams->B);
126					hipfftPlanMany(&plan2, FFTdim, dims, iembed[1], istride[1], idist[1], oembed[1], ostride[1], odist[1], HIPFFT_C2R, userParams->B);
127				}else{
128					hipfftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0], HIPFFT_C2C, userParams->B);
129				}
130				break;
131			case 1:
132				if (userParams->R2C){
133					hipfftPlanMany(&plan, FFTdim, dims,iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0],  HIPFFT_D2Z, userParams->B);
134					hipfftPlanMany(&plan2, FFTdim, dims, iembed[1], istride[1], idist[1], oembed[1], ostride[1], odist[1], HIPFFT_Z2D, userParams->B);
135				}else
136					hipfftPlanMany(&plan, FFTdim, dims, iembed[0], istride[0], idist[0], oembed[0], ostride[0], odist[0],  HIPFFT_Z2Z, userParams->B);
137				break;
138			}
139
140			float totTime = 0;
141			std::chrono::steady_clock::time_point timeSubmit = std::chrono::steady_clock::now();
142			for (int i = 0; i < userParams->N; i++) {
143				switch (userParams->P) {
144				case 0:
145					if (userParams->R2C){
146						hipfftExecR2C(plan, (hipfftReal*) dataC, (hipfftComplex*) dataC);
147						hipfftExecC2R(plan2, (hipfftComplex*) dataC, (hipfftReal*) dataC);
148					}else{
149						hipfftExecC2C(plan, (hipfftComplex*) dataC, (hipfftComplex*) dataC, -1);
150						hipfftExecC2C(plan, (hipfftComplex*) dataC, (hipfftComplex*) dataC, 1);
151					}
152					break;
153				case 1:
154					if (userParams->R2C){
155						hipfftExecD2Z(plan, (hipfftDoubleReal*) dataC, (hipfftDoubleComplex*) dataC);
156						hipfftExecZ2D(plan2, (hipfftDoubleComplex*) dataC, (hipfftDoubleReal*) dataC);
157					}else{
158						hipfftExecZ2Z(plan, (hipfftDoubleComplex*) dataC, (hipfftDoubleComplex*) dataC, -1);
159						hipfftExecZ2Z(plan, (hipfftDoubleComplex*) dataC, (hipfftDoubleComplex*) dataC, 1);
160					}
161					break;
162				}
163			}
164			hipDeviceSynchronize();
165			std::chrono::steady_clock::time_point timeEnd = std::chrono::steady_clock::now();
166			totTime = (std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeSubmit).count() * 0.001) / userParams->N;
167				
168			run_time[r][0] = totTime;
169			if (n > 0) {
170				if (r == num_runs - 1) {
171					double std_error = 0;
172					double avg_time = 0;
173					for (uint64_t t = 2; t < num_runs; t++) {
174						avg_time += run_time[t][0];
175					}
176					avg_time /= (num_runs-2);
177					for (uint64_t t = 2; t < num_runs; t++) {
178						std_error += (run_time[t][0] - avg_time) * (run_time[t][0] - avg_time);
179					}
180					std_error = sqrt(std_error / (num_runs-2));
181					if (file_output)
182						fprintf(output, "rocFFT 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));
183
184					printf("rocFFT 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					benchmark_result[0] += ((double)bufferSize / 1024) / avg_time;
186				}
187
188			}
189			hipfftDestroy(plan);
190			if (userParams->R2C)
191				hipfftDestroy(plan2);
192			hipFree(dataC);
193			hipDeviceSynchronize();
194			//hipfftComplex* output_rocFFT = (hipfftComplex*)(malloc(sizeof(hipfftComplex) * dims[0] * dims[1] * dims[2]));
195			//hipMemcpy(output_rocFFT, dataC, sizeof(hipfftComplex) * dims[0] * dims[1] * dims[2], hipMemcpyDeviceToHost);
196			//hipDeviceSynchronize();
197		}
198	}
199}