0

I want to run this specific nested for loop in GPU using matlab, can anyboy help me,

        Phi=rand(100,100); FluxD=rand(100,100); FluxC=rand(100,100);
        Ima = 100;
        Jma = 100;

        for i=1:Ima-1
             for j=1:Jma-1
                  Phi(i,j) =Phi(i,j)+dt*(FluxD(i,j)-FluxC(i,j));
             end
         end  
0

1 Answer 1

2

You need to do two things here - firstly, build your data on the GPU, and then for best performance, operate on it in a vectorised manner, like this:

% Build input data arrays directly on the GPU
Phi = rand(100, 'gpuArray');
FluxD = rand(100, 'gpuArray');
FluxC = rand(100, 'gpuArray');
Ima = 100;
Jma = 100;
% For convenience, make index vectors for i and j
ii = 1:Ima-1;
jj = 1:Jma-1;
% Compute Phi in a vectorised manner
Phi(ii, jj) = Phi(ii, jj) + dt * (FluxD(ii,jj) - FluxC(ii,jj));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.