- https://www.mathworks.com/help/parallel-computing/gpuarray.html
- https://www.mathworks.com/help/parallel-computing/run-matlab-functions-on-a-gpu.html
- 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
- https://www.mathworks.com/help/parallel-computing/work-with-complex-numbers-on-a-gpu.html
i want to generate GPU code of faster computing using Matlab?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
asim asrar
am 9 Mai 2022
Beantwortet: Infinite_king
am 13 Okt. 2023
% 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
0 Kommentare
Akzeptierte Antwort
Infinite_king
am 13 Okt. 2023
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,
Hope this is helpful.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!