Function in parfor loop produces incorrect output, in normal for-loop output is correct

3 vues (au cours des 30 derniers jours)
I believe I am being hampered by the knowledge of how a parfor loop work, hopefully someone knowns in which direction to point me.
The problem: I have a script which uses a function to calculate the crash location of an object with {x,y,z} and {vx,vy,vz} coordinates and speeds. I use the simple script below to evaluate the effect of a decreasing time-step dt on the performance and precission. Put in a normal for-loop, the output is as expected. (I understand that I write over my output in the main loop, but ignore that for now).
for dt = [0.1 0.01 0.001 0.0001 0.00001 0.000001]
for l = 1:10
[x(l), y(l)] = f_crash_location(0,0,100,10,0,0,0.0038,dt);
end
end
The output is then x = 41.244... and y = 0, which is physically correct :). However, in the main simulation where I am using the function, l is not 10, but more like a couple million runs, so I thought, perfect for parallelization. But when I change the for to a parfor-loop like this:
for dt = [0.1 0.01 0.001 0.0001 0.00001 0.000001]
parfor l = 1:10
[x(l), y(l)] = f_crash_location(0,0,100,10,0,0,0.0038,dt);
end
end
The code is executed insanely fast, which seems nice, but the output is useless. x = 10^-5 is plainly wrong. (I figured out the parfor loop only does dt*x_dot once.)
The code of the function f_crash_location.m is:
function [x_n,y_n] = f_crash_location(x_n,y_n,z_n,x_dot_n,y_dot_n,z_dot_n,C_D,dt)
%%Declare variables
g = 9.807;
%%Numerical model
while z_n > 0
% Vector velocity
V = sqrt(x_dot_n^2 + y_dot_n^2 + z_dot_n^2);
% Accelerations
x_2dot_n = -V*x_dot_n*C_D;
y_2dot_n = -V*y_dot_n*C_D;
z_2dot_n = -g + -V*z_dot_n*C_D;
% Velocities
x_dot_n = x_dot_n + x_2dot_n*dt;
y_dot_n = y_dot_n + y_2dot_n*dt;
z_dot_n = z_dot_n + z_2dot_n*dt;
% Positions
x_n = x_n + x_dot_n*dt;
y_n = y_n + y_dot_n*dt;
z_n = z_n + z_dot_n*dt;
end
end
After playing around a bit I believe it has something to do with how variables are used differently in parfor then in for-loops, but I am stuck on how to proceed. What I am doing wrong that the parfor loop spits out, well, that brown stuff?
  4 commentaires
Stephen23
Stephen23 le 5 Sep 2018
Modifié(e) : Stephen23 le 5 Sep 2018
"Probably I use global variables in a wrong way together with a par-for loop?"
Global variables are the wrong way to do anything.
  • If you want to know what experienced programmers think about globals, then search the 'net.
  • If you want to know what experienced MATLAB users think of globals, then search this forum.
  • If you want to know what the writers of MATLAB think about globals, then read their blogs.
  • If you want to know why they all think that, then look at your own question: it is a classic example of how globals cause bugs that are hard to track down.
Simple rule to make your life easier and waste less time chasing down hard-to-find bugs: do NOT use global variables. You might find this useful, it describes much better alternatives to global variables:
Wouter Grimme
Wouter Grimme le 5 Sep 2018
Really learned a few things today, thank you for your help and explanation Stephen!

Connectez-vous pour commenter.

Réponse acceptée

Wouter Grimme
Wouter Grimme le 5 Sep 2018
Short answer:
Do not use global variables!

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by