GPU computing monte carlo simulation

5 visualizzazioni (ultimi 30 giorni)
jaehyun yu
jaehyun yu il 9 Mar 2021
Commentato: Raymond Norris il 9 Mar 2021
i don't know why this code calculation time is same with cpu calculation
How can I make it calculate at gpu?
t=tic(); % measure the calculation time
P = [0.1 0.2 0.3 0.4 0.5]; % Probability of basic events
m = 0;
N = 1000; % Num of iteration
A = ones(N,1,'gpuArray'); % GPU calculation
for n = 1:N % iteration
for i = 1:numel(P)
r = rand();
if r <= P(i) % sampling
S(i) = true;
else
S(i) = false;
end
end
% gate # GAB
Sg = DetermineState (20,S(2),S(3));
Sg1 = Sg;
% gate # GAC
Sg = DetermineState (20,S(4),S(5));
Sg2 = Sg;
% gate # A
Sg = DetermineState2 (10,S(1),Sg1,Sg2);
St = Sg;
if St == true
m = m+1;
end
end
m = gather(count);
P = m/N;
value = P
gpuTime = toc(t)
DetermineState function is like this. But I think it is not problem.
function [Sg] = DetermineState(varargin)
% OR Gate
if varargin{1} == 10
if max(varargin{2},varargin{3}) == 0
u = 0;
else
u = 1;
end
% AND Gate
elseif varargin{2} == 20
if min(varargin{2},varargin{3}) == 0
u = 0;
else
u = 1;
end
% NOT Gate
else
u = 0;
end
Sg = u;
end
  2 Commenti
Walter Roberson
Walter Roberson il 9 Mar 2021
Modificato: Walter Roberson il 9 Mar 2021
for i = 1:numel(P)
r = rand();
if r <= P(i) % sampling
S(i) = true;
else
S(i) = false;
end
end
Is there a reason you did not vectorize that as
S = reshape(rand(size(P)) <= P, 1, []);
Raymond Norris
Raymond Norris il 9 Mar 2021
My sense is that this might not be the complete code, for several reasons.
  1. A is a gpuArray, but nowhere are you using it. Therefore, everything is on the CPU.
  2. Towards the end is the code
if St == true
m = m+1;
end
end
m = gather(count);
However, count is not assigned anywhere. I'm also puzzled why you'd assign m in the for-loop, but then override it when you assign it to the gathering of count (which presumable is on the GPU).
3. I'm gathering that this
Sg = DetermineState2 (10,S(1),Sg1,Sg2);
should be
Sg = DetermineState (10,S(1),Sg1,Sg2);
DetermineState2 isn't provided.
One last note (and again, it might be because the code sample isn't complete), the code as written takes almost no time (I realize that's all relative and perhaps this needs to be called many time), so this might not perform well on a GPU. GPU calculations perform much better when lots of (somewhat large) work is being assigned to it.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Parallel Computing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by