Large discrepency between CPU and GPU with fftn and ifftn?

조회 수: 4 (최근 30일)
Nathan Zechar
Nathan Zechar 2020년 5월 23일
답변: Andrea Picciau 2020년 5월 26일
Hello,
I am using a GPU to compute multipul 3D FFTs. I've come across a problem where the output I am recieving from the GPU FFT is vastly different in terms of shape and values. I am unsure why this is the case and if there is any way to mitigate this problem.
I have included a small portion of code where data is generated, and the fftn and ifftn are applied to this data. Below the code are surface plots which show the difference in output. Is there a method that can be applied to where the output from the GPU FFT will be closer to the output of the CPU FFT?
N = 128;
x = (pi/4).*(0:N-1);
z = (3*pi/4).*(0:N-1);
tol = 1.0e-8;
kx = [tol 1:N/2 -N/2+1:-1].*2.*pi.*(1i)./N;
[X,Y,Z] = ndgrid(x,x,z);
X = cos(2.*pi.*X./(32*pi));
Y = cos(2.*pi.*Y./(32*pi));
Z = exp(-(Z-(48*pi)).^2./100./(96*pi));
A = 4.*Z.*(Y+1i.*X);
%% What the output should look like
A0 = A;
A0 = fftn(A0);
A0 = A0.*kx';
A0(1,:,:) = 0;
A0 = ifftn(A0);
A0 = real(A0);
A01 = squeeze(A0(:,:,64));
subplot(1,2,1)
surf(A01)
title('CPU')
%% GPU output is signigicantly different
A1 = gpuArray(A);
A1 = fftn(A1);
A1 = A1.*kx';
A1(1,:,:) = 0;
A1 = ifftn(A1);
A1 = real(A1);
A11 = squeeze(A1(:,:,64));
subplot(1,2,2)
surf(A11)
title('GPU')
%% Compares output of both
max(abs(A0-A1),[],'all')
  댓글 수: 2
David Goodmanson
David Goodmanson 2020년 5월 23일
Hi Nathan,
in your definition of A11, could you explain why you have VM on the right hand side in place of A1?
Nathan Zechar
Nathan Zechar 2020년 5월 25일
Hey David, I appoligoze. It is now edited.
This is just a portion of a much larger code. I still had variables in my workspace when I tried to run it for the previous unedited example. It should produce the same results as shown in the graph.
I am using an RTX 2070 Max-Q if graphics cards make a difference.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrea Picciau
Andrea Picciau 2020년 5월 26일
Hi Nathan,
The results differ for two related reasons:
  • GPUs and CPUs have quite different architectures,
  • the CPU and GPU algorithms are slightly different.
On my GPU, I'm getting a max abs error of 1.6584e-15, which is within the expected tolerance and our error analysis.
As a rule of thumb, you should (almost always) expect a discrepancy between the GPU and the CPU that goes from 1e-14 to 1e-15 for computations in double precision.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by