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