high performance multi-dimensional (2d/3d) fft-shift … · 2019-01-18 · high performance...

4
High Performance Multi-dimensional (2D/3D) FFT-Shift Implementation on Graphics Processing Units (GPUs) Marwan Abdellah 1, 2 , Salah Saleh 2, 3 , Ayman Eldeib 2 * and Amr Shaarawi 2 1 Ecole Polytechnique Fédérale de Lausanne (EPFL) 2 Systems & Biomedical Engineering Department, Faculty of Engineering, Cairo University 3 Friedrich-Alexander-Universität Erlangen-Nürnberg (FAU) marwan.abdellah@epfl.ch, [email protected] * [email protected], [email protected] Abstract—Frequency domain analysis is one of the most common analysis techniques in signal and image processing. Fast Fourier Transform (FFT) is a well know tool used to perform such analysis by obtaining the frequency spectrum for time- or spatial-domain signals and vice versa. FFT-Shift is a subsequent operation used to handle the resulting arrays from this stage as it centers the DC component of the resulting array at the origin of the spectrum. The modern Graphics Processing Units (GPUs) can be easily exploited to efficiently execute this operation considering the Compute Unified Device Architecture (CUDA) technology that was released by NVIDIA. In this work, we present an efficient high performance implementation for two- and three- dimensional FFT-Shift on the GPU exploiting its highly parallel architecture relying on the CUDA platform. We use Fourier volume rendering as an example to demonstrate the significance of this proposed implementation. It achieves a speed-up of 65X for the 2D case & 219X for the 3D case. Keywords—FFT, FFT-Shift, CUFFT, FFTW, CUDA, GPU, Speedup. I. I NTRODUCTION Frequency domain analysis has been used for a variety of different signal and image processing applications. Fourier transform (FT) is used as a tool in such analysis to transform a time- or spatial-domain signal into the frequency domain and vice versa. Generally, Fourier transform is performed using the Fast Fourier Transform (FFT) algorithm which has a complexity of O(NLogN) as a more performing alternative for the Discrete Fourier Transform (DFT) algorithm; which is O(N 2 ) computationally complex [1, 2]. As a result of their parallel nature, such signal and image processing applications could be exposed to run efficiently on modern programmable Graphics Processing Units (GPUs) exploiting their powerful computing power and also their massively parallel architec- ture [3]. Application-wise, special attention has been given to Fourier Volume Rendering (FVR) as a use case to illustrate the significance of our presented work. CUFFT [4] is a GPU-based library that implements the FFT algorithm based on the Compute Unified Device Architecture (CUDA) technology; which was released by NVIDIA in 2007. Compared to other software implementations of FFT libraries, high speedups can be achieved if an application of interest involving FFT operations can rely on this GPU-based library. Normally, frequency domain analysis applications use FFT- Shift operations to center the DC component of the resulting spectrum at the origin. Such operation is invoked in MATLAB via the command fftshift() for 2D and 3D arrays [5]. However, there is no supported implementation for this module as a ready-to-use function in the CUFFT library. This issue constraints having a full application pipeline running entirely on the GPU and in turn will eventually limit the performance of such application. In this work, we present a GPU-based accelerated implementation for multi-dimensional – basically 2D/3D – FFT-Shift modules that can be used alongside with the CUFFT library for accelerating signal and image process- ing applications on the GPU. II. BACKGROUND A. Rationale In general, several image processing tasks and algorithms expose high degree of data locality and parallelism; which give them the advantage of being easily mapped to massively par- allel computing architectures or hardware platforms [6]. Such algorithms are processed in Single Instruction Multiple Data (SIMD) or Single Program Multiple Data (SPMD) fashions; which vectorize the process to the instruction and program levels respectively by replicating the same instruction – or program – on the different pixels of an image. This significant feature makes GPUs the optimum selection to handle and solve parallel algorithms due to their massively parallel computing architectures [3]. However, trying to efficiently exploit the GPU to leverage the performance of the application running on it remains a challenging issue that requires full awareness with its parallel design and underlying architecture [7]. For instance, data transfers between the CPU and the GPU should be minimized. This issue is considered to prevent limiting the performance of the pipeline of the application pipeline to the bandwidth of the Peripheral Component Interconnect Express (PCI-e) connection that links the CPU with the GPU [8]. 171

Upload: others

Post on 01-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: High Performance Multi-dimensional (2D/3D) FFT-Shift … · 2019-01-18 · High Performance Multi-dimensional (2D/3D) FFT-Shift Implementation on Graphics Processing Units (GPUs)

High Performance Multi-dimensional (2D/3D)FFT-Shift Implementation on

Graphics Processing Units (GPUs)Marwan Abdellah1, 2†

, Salah Saleh2, 3‡, Ayman Eldeib2∗ and Amr Shaarawi2

1Ecole Polytechnique Fédérale de Lausanne (EPFL)2Systems & Biomedical Engineering Department, Faculty of Engineering, Cairo University

3Friedrich-Alexander-Universität Erlangen-Nürnberg (FAU)†[email protected], ‡[email protected]

[email protected], ¶[email protected]

Abstract—Frequency domain analysis is one of the mostcommon analysis techniques in signal and image processing. FastFourier Transform (FFT) is a well know tool used to performsuch analysis by obtaining the frequency spectrum for time- orspatial-domain signals and vice versa. FFT-Shift is a subsequentoperation used to handle the resulting arrays from this stageas it centers the DC component of the resulting array at theorigin of the spectrum. The modern Graphics Processing Units(GPUs) can be easily exploited to efficiently execute this operationconsidering the Compute Unified Device Architecture (CUDA)technology that was released by NVIDIA. In this work, we presentan efficient high performance implementation for two- and three-dimensional FFT-Shift on the GPU exploiting its highly parallelarchitecture relying on the CUDA platform. We use Fouriervolume rendering as an example to demonstrate the significanceof this proposed implementation. It achieves a speed-up of 65Xfor the 2D case & 219X for the 3D case.

Keywords—FFT, FFT-Shift, CUFFT, FFTW, CUDA, GPU,Speedup.

I. INTRODUCTION

Frequency domain analysis has been used for a variety ofdifferent signal and image processing applications. Fouriertransform (FT) is used as a tool in such analysis to transforma time- or spatial-domain signal into the frequency domainand vice versa. Generally, Fourier transform is performedusing the Fast Fourier Transform (FFT) algorithm which hasa complexity of O(NLogN) as a more performing alternativefor the Discrete Fourier Transform (DFT) algorithm; which isO(N2) computationally complex [1, 2]. As a result of theirparallel nature, such signal and image processing applicationscould be exposed to run efficiently on modern programmableGraphics Processing Units (GPUs) exploiting their powerfulcomputing power and also their massively parallel architec-ture [3]. Application-wise, special attention has been given toFourier Volume Rendering (FVR) as a use case to illustratethe significance of our presented work.

CUFFT [4] is a GPU-based library that implements the FFTalgorithm based on the Compute Unified Device Architecture(CUDA) technology; which was released by NVIDIA in 2007.Compared to other software implementations of FFT libraries,

high speedups can be achieved if an application of interestinvolving FFT operations can rely on this GPU-based library.Normally, frequency domain analysis applications use FFT-Shift operations to center the DC component of the resultingspectrum at the origin. Such operation is invoked in MATLABvia the command fftshift() for 2D and 3D arrays [5].However, there is no supported implementation for this moduleas a ready-to-use function in the CUFFT library. This issueconstraints having a full application pipeline running entirelyon the GPU and in turn will eventually limit the performanceof such application. In this work, we present a GPU-basedaccelerated implementation for multi-dimensional – basically2D/3D – FFT-Shift modules that can be used alongside withthe CUFFT library for accelerating signal and image process-ing applications on the GPU.

II. BACKGROUND

A. Rationale

In general, several image processing tasks and algorithmsexpose high degree of data locality and parallelism; which givethem the advantage of being easily mapped to massively par-allel computing architectures or hardware platforms [6]. Suchalgorithms are processed in Single Instruction Multiple Data(SIMD) or Single Program Multiple Data (SPMD) fashions;which vectorize the process to the instruction and programlevels respectively by replicating the same instruction – orprogram – on the different pixels of an image. This significantfeature makes GPUs the optimum selection to handle and solveparallel algorithms due to their massively parallel computingarchitectures [3]. However, trying to efficiently exploit theGPU to leverage the performance of the application runningon it remains a challenging issue that requires full awarenesswith its parallel design and underlying architecture [7]. Forinstance, data transfers between the CPU and the GPU shouldbe minimized. This issue is considered to prevent limiting theperformance of the pipeline of the application pipeline to thebandwidth of the Peripheral Component Interconnect Express(PCI-e) connection that links the CPU with the GPU [8].

171

TammyReVeNGe
Template2
Page 2: High Performance Multi-dimensional (2D/3D) FFT-Shift … · 2019-01-18 · High Performance Multi-dimensional (2D/3D) FFT-Shift Implementation on Graphics Processing Units (GPUs)

Some image processing applications use frequency domainanalysis for some reason that suits their nature. This analysisimplies transforming the data back and forth between spatialand frequency domains. Applications involving frequency do-main analysis mostly use FFT. Various FFT libraries have beendeveloped and optimized to handle arrays of different datatypes with different sizes. Some software libraries have pro-vided convenient application programming interfaces (APIs) tohandle FFT operations like FFTW [9] or FFTW++ [10]. Theselibraries are conditionally optimized to run on multicore CPUsto scale up their performance considering the availability ofmulti-core CPUs. Alternatively, CUFFT is a GPU-acceleratedimplementation of the FFT algorithm relying on the CUDAarchitecture. In [11], it has been shown that CUFFT highlyoutperforms FFTW which makes CUFFT library the optimumselection for implementing FFT operations for any applica-tions. Unfortunately, this library doesn’t provide an off-the-shelf FFT-Shift function; which limits its usage to a certaindegree. In turn, if this operation is mandatory in the applicationpipeline, one has to either write a specific CUDA kernel thatshould handle this operation or send back the resulting arraysfrom the FFT plans to the CPU to have them shifted andthen send them back to the GPU. Writing a user-specifickernel to implement this operation for 1D signals is relativelyflexible and straight forward, but doing this step for multi-dimensional, basically 2D/3D, signals mapped into 1D arraysis tedious. Additionally, the alternative approach for doing theFFT-Shift by downloading the resulting data from the FFT planto the CPU, shifting it, and then uploading it back to the GPUintroduces high latency, performance limits and also breaksthe accelerated application pipeline. As a consequence, a well-designed FFT-Shift module for multi-dimensional flat arraysis mandatory to facilitate writing image processing pipelinesrunning entirely on the GPUs exploiting its high bandwidthand parallel architecture.

B. Case Study : Fourier Volume Rendering

Our selected case study can demonstrate the purpose ofthe presented work if a high performance implementation forthe Fourier volume rendering pipeline on the GPU is desired.This rendering pipeline’s working by switching its renderingstage into the frequency domain where the spectrum of thedataset is obtained in a pre-processing step. Afterwards, andduring the rendering loop, a k-space 2D projection-slice isextracted from the 3D spectral volume and back transformedto the spatial domain to have the final projection image gettingreconstructed. As a significant technique in medical imaging,it has been used widely in digital radiography during the past20 years. Fig. 1 shows x-ray-like projections generated formthis rendering pipeline. In this pipeline, FFT-Shift operationis executed three times: on a real 3D spatial-volume, on acomplex 3D spectrum and finally on a 2D real image. Mostof the stages of this pipeline could be implemented easily onthe GPU except the FFT-Shifts. In turn, an efficient multi-dimensional FFT-Shift implementation on the GPU is crucialfor leveraging the performance of this pipeline.

Figure 1. Reconstructions for a Medical data-set from the Fourier VolumeRendering (FVR) algorithm with different intensity scaling factors.

C. GPU & CUDA

GPUs are dedicated to handle graphics primitives no more.Thanks to the rapid evolution of their supporting APIs thatmade it straightforward to access their underlying powerfulparallel architecture to address different categories of dataparallel algorithms to be handled efficiently by this platform.The GPU is designed to process a huge amount of floatingpoint operations in parallel relying on hundreds of computingcores running concurrently. This can be illustrated in Fig.2 by the side by side comparison between the CPU andthe GPU architectures. CUDA is a general-purpose parallelcomputing architecture that delivers a novel parallel program-ming paradigm and a new instruction set architecture that caneasily leverage the parallel power of the computing engineembedded in the CUDA-enabled GPUs to solve complexscientific and engineering applications. It becomes straightfor-ward to decompose the computations into fine-grained threadsand execute theses threads on the device in parallel. CUDArefers to the software platform and the hardware architectureused to execute the kernels. A kernel launches thousands ofthreads for parallel execution organized in three levels: grids,blocks, and warps. The grids are two- or three-dimensionalarrangements of threads blocks where every block consistsof an upper limit of threads 512 or 1024 depending on thecompute capability version of the device, and finally eachgroup of threads form a wrap. Such architecture is calledSingle Instruction Multiple Thread (SIMT); which allows theGPU to execute single instruction on multiple concurrentthreads operating on different data. Every running thread hasaccess to some built-in variable assigned by the CUDA runtime system and used for thread indexing. CUDA also has anefficient memory hierarchy divided in global, shared, register,texture, and constant memory.

III. IMPLEMENTATION

To guarantee full compatibility with the CUFFT library,our implementation considers direct operation on the resultingflat arrays from the CUFFT plans for different data typessuch as cufftReal, cufftDouble, cufftComplex andcufftDoubleComplex. It is also backward compatible torun on CUDA devices with compute capability of 1.1 thatsupport only 2D grids and maximum overall block size of

172

Page 3: High Performance Multi-dimensional (2D/3D) FFT-Shift … · 2019-01-18 · High Performance Multi-dimensional (2D/3D) FFT-Shift Implementation on Graphics Processing Units (GPUs)

Figure 2. CPU and GPU architectures in comparison.

512 threads per block. The implementation is also automatedto detect the optimum block and grid configurations for thedifferent sizes. To have the kernel execution times optimized,the implementation considers also adaptable block size forarrays less than 162 or 163, and fixed block size of 162 forlarger arrays. However, as a limitation of this implementation,it works only with powers-of-two unified-dimensions arrays.This condition implies zero padding the input array if it hasnon-unified dimensions or at least one of its dimensions isnon-power-of-two.

For the 2D case, the 2D flat array is decomposed into4 quadrants (Q1 ↔ Q4 & Q2 ↔ Q3) at which the 1Dindex of each element in the array is known a priori. Duringkernel execution, this element is indexed and replaced by itscorresponding element of the complementary quadrant of thearray. Fig. 3 illustrates how the 2D flat array is decomposedinto 4 quadrants and how this array is logically mapped into aflat array. For the 3D case, the same logic followed in the 2Dcase has been used here to handle the 3D array after slicingit into a stack of 2D slices at which each kernel executioniteration handles only a single element in a 2D slice untilgetting the volume stack processed. This implies executingN kernels on the N2 slices composing the volume instead ofexecuting a single kernel for handling the whole volume. Thisapproach is used as a workaround to overcome the limitedcapabilities of CUDA devices that don’t support 3D grids;yet, it is more efficient taking into considerations the availableresources per kernel like the number of instructions per kerneland the amount of local memory per thread. Fig. 4 showshow the 3D array (allocated in the memory as a flat array)is being divided into a stack of N 2D slices where each sliceis decomposed into 4 quadrants Q[1−4] and processed in asingle kernel execution. Algorithms 1 & 2 explain in detailsthe FFT_SHIFT_2D FFT_SHIFT_3D kernels. Note thatthe variables BLOCKIDX, BLOCKWIDTH and THREADIDX arebuilt-in variables calculated by the CUDA runtime system onthe fly [12].

IV. RESULTS & DISCUSSION

Our implementation was tested on a workstation shippedwith an Intel Core i7 CPU running at 3.20 GHz, 12 GB ofDDR3 and an NVIDIA GeForce GTX 570 GPU containing1GB of DDR5 and 480 CUDA cores. Benchmarking resultsare reported for a single-threaded naive CPU implementationand compared to our proposed GPU-based implementation.

Algorithm 1 FFT_SHIFT_2D(input,N)Require: size of input = N2

eq1← ((N ∗N)+N)/2eq2← ((N ∗N)−N)/2x← BLOCKIDX.x∗ BLOCKWIDTH+ THREADIDX.xy← BLOCKIDX.y∗ BLOCKHEIGHT+ THREADIDX.yindex← (y∗N)+ xif index in Q1 then

out put[index]← input[index+ eq1]else if index in Q2 then

out put[index]← input[index+ eq2]else if index in Q3 then

out put[index]← input[index− eq2]else if index in Q4 then

out put[index]← input[index− eq1]end ifreturn (out put)

Algorithm 2 FFT_SHIFT_3D(input,N)Require: size of input = N3

for z← 0 to (N−1) doeq1← ((N ∗N ∗N)+(N ∗N)+N)/2eq2← ((N ∗N ∗N)+(N ∗N)−N)/2eq3← ((N ∗N ∗N)− (N ∗N)+N)/2eq4← ((N ∗N ∗N)− (N ∗N)−N)/2x← BLOCKIDX.x∗ BLOCKWIDTH+ THREADIDX.xy← BLOCKIDX.y∗ BLOCKHEIGHT+ THREADIDX.yindex← (z∗ (N ∗N))+(y∗N)+ xif z < N/2 then

if index in Q1 thenout put[index]← input[index+ eq1]

else if index in Q2 thenout put[index]← input[index+ eq2]

else if index in Q3 thenout put[index]← input[index+ eq3]

else if index in Q4 thenout put[index]← input[index+ eq4]

end ifelse

if index in Q1 thenout put[index]← input[index− eq4]

else if index in Q2 thenout put[index]← input[index− eq3]

else if index in Q3 thenout put[index]← input[index− eq2]

else if index in Q4 thenout put[index]← input[index− eq1]

end ifend if

end forreturn (out put)

173

Page 4: High Performance Multi-dimensional (2D/3D) FFT-Shift … · 2019-01-18 · High Performance Multi-dimensional (2D/3D) FFT-Shift Implementation on Graphics Processing Units (GPUs)

Q1 Q2

Q3 Q4

N

N

x

y

Q1 Q2

Q3 Q4

Figure 3. Dividing the 2D image into 4 quadrants Q[1−4] and mapping thedivisions into a flat array containing the 2D image data where Q1&Q2 areswapped with Q4&Q3 respectively.

N

N

N

x

y

z

Q1 Q2

Q3 Q4

Each slice in z is decomposed into Q[1-4]

z = 0

z = N-1

Figure 4. Shifting the 3D volume by slicing the volume into N 2D stacksor slices of size N2 and processing each slice individually by a single kernelexeecution.

We also consider adding the data transfer (upload/download)time between the CPU and the GPU.

Fig. 5 & Fig. 6 show the timing benchmarks for theFFT_SHIFT_2D and FFT_SHIFT_3D modules respectively.In the 2D case and considering only the FFT-Shifting operationwithout adding the data transfer time, the CPU implementationoutperforms the GPU one for arrays less than 642. For largerarrays, the GPU implementation highly exceeds the CPUimplementation to achieve a speed-up of 65X for a 2D array ofsize 40962. The 3D case works in a similar fashion where theCPU implementation outperforms the GPU one for arrays lessthan 323 and the GPU implementation starts to excel for largerarrays until gaining a speed-up of 219X for a 3D array of size2562. Taking into consideration the data transfer time betweenthe GPU and the CPU to be added to the CPU implementation,the GPU excels the CPU for all the cases.

V. CONCLUSIONS

We present a high performance implementation of multi-dimensional (2D/3D) FFT-Shift modules on CUDA-enabledGPUs. It gives the opportunity for leveraging the performanceof applications involving FFT operations to run entirely onthe GPU. It achieves speed-ups of 65X & 219X for 2D & 3D

0.00

3

0.00

6 0.01

9 0.07

1 0.27

6 1.13

4.18

22.8

23 96.0

73

0.08

2

0.09

1

0.11

6

0.22

1 0.63

7 2.15

7 7.43

8

34.6

61

143.

703

0.01

4

0.01

4

0.01

4

0.02

3 0.09

4999

999

0.26

6

0.28

9 0.64

7 2.19

5

5.9 6.5 8.3 9.6 6.7 8.1 25.7

53.6 65.5

0.001

0.01

0.1

1

10

100

1000

16 32 64 128 256 512 1024 2048 4096

Tim

e (m

s), S

peed

-Up

(X)

Array Size NxN

CPU CPU + Data Transfer GPU Speed-Up

Figure 5. Benchmarking results for the FFT_Shift_2D module on the CPU,the GPU and the CPU combined with the memory transfer time between theCPU and the GPU.

0.00

3

0.00

5

0.02

9

0.25

8

2.70

4

23.4

77

530.

461

0.05

2

0.05

6

0.09

9 0.46

9

3.64

7

29.5

46

575.

488

0.02

1

0.02

8

0.04

5 0.13

7 0.38

8

0.65

2 2.61

9

2.5 2 2.2 3.4 9.4

45.3 219.7

0.001

0.01

0.1

1

10

100

1000

4 8 16 32 64 128 256

Tim

e (m

s) -

Spee

d U

p (X

)

Array Size NxNxN

CPU CPU + Data Transfer GPU Speed-Up

Figure 6. Benchmarking results for the FFT_Shift_3D module on the CPU,the GPU and the CPU combined with the memory transfer time between theCPU and the GPU.

arrays respectively considering the time needed to transfer thedata between the CPU and the GPU.

VI. FUTURE WORK

As a future work, texture memory can be used as an alter-native to the global memory to exploit their low latency forspatially-localized elements. Additionally, our implementationshould consider handling arrays of non-unified dimensions toreduce the time needed for zero-padding the input arrays tofit the dimension uniformity condition.

REFERENCES[1] J. L. Semmlow, Biosignal and Biomedical Image Processing, ch. 3. 270

Madison Avenue, New York, NY 10016, U.S.A.: Marcel Dekker, 2004.[2] S. T. Karris, Signals and Systems, ch. 10. Fremont, California: Orchard,

2003.[3] Z. Yang, Y. Zhu, and Y. Pu, “Parallel Image Processing Based on

CUDA,” in Proc. of International Conference on Computer Science andSoftware Engineering, 2008, vol. 3, pp. 198–201, 2008.

[4] NVIDIA, “CUFFT Library, CUDA Toolkit 4.2,” March 2012.[5] MATHWORKS, “ffftshift R2012a Documentation Page.” http://www.

mathworks.ch/help/techdoc/ref/fftshift.html, March 2012.[6] J. M. Squyres, A. Lumsdaine, B. C. Mccandless, and R. L. Stevenson,

“Parallel and Distributed Algorithms for High Speed Image Processing,”1996.

[7] D. B. Kirk and W. mei W. Hwu, Programming Massively ParallelProcessor, ch. 1. 30 Corporate Drive, Suite 400, Burlington, MA 01803,USA: Elsevier Inc., 2010.

[8] NVIDIA, “CUDA C Best Practice Guide,” January 2012.[9] M. Frigo and S. G. Johnson., “FFTW.” http://www.fftw.org/. [Online;

accessed 10-July, 2011; V. 3.2].[10] “FFTW++: Fast Fourier Transform C++ Header Class for FFTW3.” http:

//fftwpp.sourceforge.net/. [Online; accessed 15-July, 2011; V. 1.9].[11] “CUFFT vs FFTW comparison .” http://www.sharcnet.ca/~merz/CUDA_

benchFFT/, 2008. [Online; accessed 10-July, 2012].[12] NVIDIA, “NVIDIA CUDA C Programming Guide,” April 2012.

174