フィルターのクリア

i want to generate GPU code of faster computing using Matlab?

1 回表示 (過去 30 日間)
asim asrar
asim asrar 2022 年 5 月 9 日
回答済み: Infinite_king 2023 年 10 月 13 日
% i have my matlab code as
N=100;
um = 1;
%um = 1e-6;
c = 3e8; % m/s
dx = um;
dy = um;
dz = um;
%
% dx = um;
% dy = um;
% dz = um;
dt = 1/4 * dx /c;
lamb =5.32*um;
k = 2*pi / lamb;
% omega region configuration
x = linspace(0,N-1,N);
y=x;
x=x*dx;
y=y*dy;
[X,Y] = meshgrid(x,y);
X=(X(:));
Y=(Y(:));
g=zeros(length(X),length(Y));
for ii=1:length(X)
a=X-X(ii);
g(ii,:)=a;
end
gg=zeros(length(X),length(Y));
for ii=1:length(X)
aa=Y-Y(ii);
gg(ii,:)=aa;
end
r = sqrt(g.^2 + gg.^2 +5^2*dz);
nb=1.33; % refractive index background
siz=[N,N];
%siz=[N^2,N^2]; % size of the region of interest (containing the support of f)
dz=16*lamb/siz(1); % axial discretization step (o have a ROI of 16*lamb)
kdz=2*pi*nb/lamb; % wavenumber
%kg=9.9260
kg=kdz;
for i = 1:size(r)
for j = 1:size(r)
G(i,j)= (exp(1i*(kg*r(i,j)*nb)))/(4*pi*r(i,j));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% for this I want to generate GPU code for faster computation
% i tried it like-
tic();
N=50;
um = 1;
%um = 1e-6;
c = 3e8; % m/s
dx = um;
dy = um;
dz = um;
%
% dx = um;
% dy = um;
% dz = um;
dt = 1/4 * dx /c;
lamb =5.32*um;
k = 2*pi / lamb;
% omega region configuration
x = linspace(0,N-1,N);
y=x;
x=x*dx;
y=y*dy;
[X,Y] = meshgrid(x,y);
X= gpuArray(X);
Y= gpuArray(Y);
X=(X(:));
Y=(Y(:));
g=zeros(length(X),length(Y));
g=gpuArray(g);
for ii=1:length(X)
a=X-X(ii);
g(ii,:)=a;
end
gg=zeros(length(X),length(Y));
gg=gpuArray(gg);
for ii=1:length(X)
aa=Y-Y(ii);
gg(ii,:)=aa;
end
r = sqrt(g.^2 + gg.^2 +5^2*dz);
r= gpuArray(r);
nb=1.33; % refractive index background
siz=[N,N];
%siz=[N^2,N^2]; % size of the region of interest (containing the support of f)
dz=16*lamb/siz(1); % axial discretization step (o have a ROI of 16*lamb)
kdz=2*pi*nb/lamb; % wavenumber
%kg=9.9260
kg=kdz;
G=zeros(N^2,N^2);
G=gpuArray(G);
for i = 1:size(r)
for j = 1:size(r)
G(i,j)= (exp(1i*(kg*r(i,j)*nb)))/(4*pi*r(i,j));
end
end
toc();
% but GPU array assigment as GPU array is taking more time than normal
% code.
% where am i comitting the mistake

採用された回答

Infinite_king
Infinite_king 2023 年 10 月 13 日
Hi asim,
I understand that you want to decrease the runtime of your script by shifting the computation to GPU.
The GPU code's suboptimal performance can be attributed to the communication overhead between the CPU and GPU. It will take considerable time to pass the operation to the GPU. To enhance performance, it is advisable to consider reducing the total number of operations and simultaneously increasing the number of elements on which the given operation is executed.
For example, refer to the below code snippet,
for i = 1:size(r)
for j = 1:size(r)
G(i,j)= (exp(1i*(kg*r(i,j)*nb)))/(4*pi*r(i,j));
end
end
Here operations are being given to GPU. Instead, we can give one single operation as shown below to GPU and achieve the same results.
G = (exp(1i*(kg*r*nb)))./(4*pi*r)
Similarly, the following code is giving number of operations to GPU.
g=zeros(length(X),length(Y));
% allocating 'g' before calculation
g=gpuArray(g);
for ii=1:length(X)
a=X-X(ii);
g(ii,:)=a;
end
Instead, the g array can be computed in CPU and then the result can be transferred to GPU as shown below.
g=zeros(length(X),length(Y));
for ii=1:length(X)
a=X-X(ii);
g(ii,:)=a;
end
% transferring 'g' after calculation
g = gpuArray(g);
After making these changes I can see speedup in calculation of G, for the . For larger values of N, the speedup will increase even further.
If you have multiple operations that need to be applied to all elements of a given matrix, and you intend to offload them to the GPU, you can consolidate these operations into a single function using the 'arrayfun' function.
% calling the function using ‘arrayfun’
G2 = arrayfun(@some_fun,G2,r,kg,nb);
% function that contains all the required operations
function G = some_fun(G,r,kg,nb)
G= (exp(1i*(kg*r*nb)))./(4*pi*r);
.
.
.
Other ops
.
.
end
For more information refer the following resources,
  1. https://www.mathworks.com/help/parallel-computing/gpuarray.html
  2. https://www.mathworks.com/help/parallel-computing/run-matlab-functions-on-a-gpu.html
  3. https://www.mathworks.com/help/parallel-computing/illustrating-three-approaches-to-gpu-computing-the-mandelbrot-set.html#:~:text=naiveGPUTime%20%3D%200.2181-,Element%2Dwise%20Operation,-Noting%20that%20the
  4. https://www.mathworks.com/help/parallel-computing/work-with-complex-numbers-on-a-gpu.html
Hope this is helpful.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGPU Computing についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by