|
Hi Brian,
A parfor loop can only be used to run independent calculations in parallel. One way to decide whether the loops are independent is to ask yourself if it would still make sense if the loop was evaluated in a different order (because that can happen when using parfor). In your example, each loop depends on the previous loop (via the variable oldweights), so you cannot use a parfor loop at this level of the calculation.
If you need to speed up your calculation, you probably have other options. I would start by using the MATLAB profiler to see where the model is spending the most time. Maybe you'll see something that you could easily make more efficient. Here are some things that I often see when trying to speed up my code:
* Try to limit reading/writing files
* Try to "vectorize" code instead of using for loops whenever possible.
* Avoid using "eval"
* Pre-allocate variables to be the appropriate size instead of "growing" them in a for loop
(You can see more tips like these in the documentation section "Techniques for Improving Performance" http://matlab.my/IsnhT7 )
Another possibility is that you may be able to add a parfor loop inside of your "growth" function.
-Arthur
"Brian Roth" <broth2@lsu.edu> wrote in message <jm29t3$fbo$1@newscl01ah.mathworks.com>...
> Hello-
> I am developing an individual-based model of fish. Each time step (iteration), fish grow. I need to recall their weight from the last iteration for input into the next iteration. I know iterations need to be independent to run in a parfor loop, but I need to run this model in parallel to reduce computing time. My code is pretty long (there's more going on than just growth), so I can't put it here, but here's a summary.
>
>
> parfor i=1:populationsize;
> if i==1;
> oldweights=dlmread('initialweights.txt');
> end
> weights(i)=oldweights(i);
> newweights=feval('growth',weights(i));
> oldweights(i)=newweights;
> end
>
> I get an error saying that I'm referring to cleared variable 'oldweights'.
>
> Is there a workaround for this?
>
> Thanks,
>
> Brian
|