Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
parfor help with biological problem

Subject: parfor help with biological problem

From: Brian Roth

Date: 10 Apr, 2012 21:49:23

Message: 1 of 2

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

Subject: parfor help with biological problem

From: Arthur Goldsipe

Date: 11 Apr, 2012 12:02:11

Message: 2 of 2

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us