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 and CPU Usage

Subject: parfor and CPU Usage

From: Saad

Date: 31 Aug, 2011 11:19:10

Message: 1 of 8

Dear all,

I hope you are well. I am using parfor at the moment. In the performance tab (Task manager), I have 4 small windows which means that I can run 4 iterations in the same time. However, I was surprised to see that the CPU usage is only 25%, I thought that it should be 100% no?. Is there anything wrong with my approach? or maybe I misunderstood the use of parfor? Thanks a lot on any comment or suggestion you could make.

Kind REgards

Saad

PS: Please have a look at the code below:

parfor i=1:4
                                   
            P=cal4(H, n{i});
            
            S(i,:)= cal5(P,n{i});
            
             
            
  end
[j, idx]=min(S);

Some people suggested to me that maybe the parfor loop is finished and thats the code after it that is executed but it is not the case here because I have small code after the parfor loop. Thank you for any comment you could make.

Subject: parfor and CPU Usage

From: Edric M Ellis

Date: 31 Aug, 2011 15:42:00

Message: 2 of 8

"Saad " <saad.badaoui07@imperial.ac.uk> writes:

> Dear all, I hope you are well. I am using parfor at the moment. In the
> performance tab (Task manager), I have 4 small windows which means
> that I can run 4 iterations in the same time. However, I was surprised
> to see that the CPU usage is only 25%, I thought that it should be
> 100% no?. Is there anything wrong with my approach? or maybe I
> misunderstood the use of parfor? Thanks a lot on any comment or
> suggestion you could make.

Did you first call

matlabpool open local 4

(Or similar) ?

Cheers,

Edric.

Subject: parfor and CPU Usage

From: Saad

Date: 31 Aug, 2011 18:13:25

Message: 3 of 8

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwpqjlwptj.fsf@uk-eellis-deb6-64.dhcp.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> writes:
>
> > Dear all, I hope you are well. I am using parfor at the moment. In the
> > performance tab (Task manager), I have 4 small windows which means
> > that I can run 4 iterations in the same time. However, I was surprised
> > to see that the CPU usage is only 25%, I thought that it should be
> > 100% no?. Is there anything wrong with my approach? or maybe I
> > misunderstood the use of parfor? Thanks a lot on any comment or
> > suggestion you could make.
>
> Did you first call
>
> matlabpool open local 4
>
> (Or similar) ?
>
> Cheers,
>
> Edric.

Hi Edric,

Thank you very much for tip. Now It is using the whole CPU! I am sorry to bother you again but another issue showed up. In my parfor loop (below):

parfor i=1:8
                                   
            P=cal4(H, n{i});
            
            S(i,:)= cal5(P,n{i});
            
             
            
end

In the function "cal5", I am using fminunc to find the minimum of unconstrained multivariate function. When I run one iteration the code above works fine but as soon as I run the parfor, the code stops with the following message:

"fminunc stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 2700 (the default value)."

I increased the MaxFunval to 100 000 but it still stops and shows the same message. I suspect that fmincun is evaluating many functions in the same time ( and in parallel way) is it the reason why is it showing that message? Do I have to increase MaxFunval to very very high number? or is there something deeper than that? Thanks so much

Kind REgards

S

Subject: parfor and CPU Usage

From: Edric M Ellis

Date: 1 Sep, 2011 08:02:28

Message: 4 of 8

"Saad " <saad.badaoui07@imperial.ac.uk> writes:

> Thank you very much for tip. Now It is using the whole CPU! I am sorry to bother you again but another issue showed up. In my parfor loop (below):
> parfor i=1:8
> P=cal4(H, n{i});
> S(i,:)= cal5(P,n{i});
> end
>
> In the function "cal5", I am using fminunc to find the minimum of
> unconstrained multivariate function. When I run one iteration the code
> above works fine but as soon as I run the parfor, the code stops with
> the following message:
>
> "fminunc stopped because it exceeded the function evaluation limit,
> options.MaxFunEvals = 2700 (the default value)."
>
> I increased the MaxFunval to 100 000 but it still stops and shows the
> same message. I suspect that fmincun is evaluating many functions in
> the same time ( and in parallel way) is it the reason why is it
> showing that message? Do I have to increase MaxFunval to very very
> high number? or is there something deeper than that? Thanks so much

I must admit that I'm not familiar with customizing fminunc in this
way. One thing you need to be sure though is that you make this change
in the body of the PARFOR loop - remember that the workers evaluating
the body are separate MATLAB processes, so if you've made any changes to
'global state' in the client MATLAB, these aren't automatically
reflected in the workers.

Cheers,

Edric.

Subject: parfor and CPU Usage

From: Saad

Date: 1 Sep, 2011 09:14:26

Message: 5 of 8

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwliu8wuzv.fsf@uk-eellis-deb6-64.dhcp.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> writes:
>
> > Thank you very much for tip. Now It is using the whole CPU! I am sorry to bother you again but another issue showed up. In my parfor loop (below):
> > parfor i=1:8
> > P=cal4(H, n{i});
> > S(i,:)= cal5(P,n{i});
> > end
> >
> > In the function "cal5", I am using fminunc to find the minimum of
> > unconstrained multivariate function. When I run one iteration the code
> > above works fine but as soon as I run the parfor, the code stops with
> > the following message:
> >
> > "fminunc stopped because it exceeded the function evaluation limit,
> > options.MaxFunEvals = 2700 (the default value)."
> >
> > I increased the MaxFunval to 100 000 but it still stops and shows the
> > same message. I suspect that fmincun is evaluating many functions in
> > the same time ( and in parallel way) is it the reason why is it
> > showing that message? Do I have to increase MaxFunval to very very
> > high number? or is there something deeper than that? Thanks so much
>
> I must admit that I'm not familiar with customizing fminunc in this
> way. One thing you need to be sure though is that you make this change
> in the body of the PARFOR loop - remember that the workers evaluating
> the body are separate MATLAB processes, so if you've made any changes to
> 'global state' in the client MATLAB, these aren't automatically
> reflected in the workers.
>
> Cheers,
>
> Edric.

Hi Edric

Thank you for your reply. Yes I agree and I have made the changes in the body of the PARFOR loop and when I increase the number of function evaluations to a very high number it seems that the code runs but it is silly to do it this way no? I am sure there is something deeper than that. If you have a minute, could you please forward the thread to the people who deals with optimization fminunc?

When the PARFOR loop was running, the code stopped and an error message showed up:

 ??? Error using ==> parallel_function at 594
The session that parfor is using has shut down

??? The client lost connection to lab 1.
This might be due to network problems, or the pmode parallel job might have
errored.

I am not sure but does it have to do with a computer power? Thank you for everyhting

Kind REgards

S

Subject: parfor and CPU Usage

From: Edric M Ellis

Date: 1 Sep, 2011 09:54:23

Message: 6 of 8

"Saad " <saad.badaoui07@imperial.ac.uk> writes:
> Edric M Ellis <eellis@mathworks.com> wrote in message <ytwliu8wuzv.fsf@uk-eellis-deb6-64.dhcp.mathworks.com>...
>> "Saad " <saad.badaoui07@imperial.ac.uk> writes:
>>
>> > Thank you very much for tip. Now It is using the whole CPU! I am
>> > sorry to bother you again but another issue showed up. In my
>> > parfor loop (below):
>> > parfor i=1:8
>> > P=cal4(H, n{i});
>> > S(i,:)= cal5(P,n{i});
>> > end
>> >
>> > In the function "cal5", I am using fminunc to find the minimum of
>> > unconstrained multivariate function. When I run one iteration the code
>> > above works fine but as soon as I run the parfor, the code stops with
>> > the following message: >
>> > "fminunc stopped because it exceeded the function evaluation limit,
>> > options.MaxFunEvals = 2700 (the default value)."
>> >
>> > I increased the MaxFunval to 100 000 but it still stops and shows the
>> > same message. I suspect that fmincun is evaluating many functions in
>> > the same time ( and in parallel way) is it the reason why is it
>> > showing that message? Do I have to increase MaxFunval to very very
>> > high number? or is there something deeper than that? Thanks so much
>>
>> I must admit that I'm not familiar with customizing fminunc in this
>> way. One thing you need to be sure though is that you make this change
>> in the body of the PARFOR loop - remember that the workers evaluating
>> the body are separate MATLAB processes, so if you've made any changes to
>> 'global state' in the client MATLAB, these aren't automatically
>> reflected in the workers.

> Thank you for your reply. Yes I agree and I have made the changes in
> the body of the PARFOR loop and when I increase the number of function
> evaluations to a very high number it seems that the code runs but it
> is silly to do it this way no? I am sure there is something deeper
> than that. If you have a minute, could you please forward the thread
> to the people who deals with optimization fminunc?

How is the setting made? If it's a global setting, you could do it using
an SPMD block, like so:

spmd
  doStuff();
end

which executes the function 'doStuff' exactly once on each worker.

> When the PARFOR loop was running, the code stopped and an error
> message showed up:

> ??? Error using ==> parallel_function at 594
> The session that parfor is using has shut down
>
> ??? The client lost connection to lab 1.
> This might be due to network problems, or the pmode parallel job might have
> errored.

This generally means that the MATLAB worker session has aborted for some
reason. Does this happen each time?

Cheers,

Edric.

Subject: parfor and CPU Usage

From: Saad

Date: 1 Sep, 2011 10:33:10

Message: 7 of 8

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwhb4wwptc.fsf@uk-eellis-deb6-64.dhcp.mathworks.com>...
> "Saad " <saad.badaoui07@imperial.ac.uk> writes:
> > Edric M Ellis <eellis@mathworks.com> wrote in message <ytwliu8wuzv.fsf@uk-eellis-deb6-64.dhcp.mathworks.com>...
> >> "Saad " <saad.badaoui07@imperial.ac.uk> writes:
> >>
> >> > Thank you very much for tip. Now It is using the whole CPU! I am
> >> > sorry to bother you again but another issue showed up. In my
> >> > parfor loop (below):
> >> > parfor i=1:8
> >> > P=cal4(H, n{i});
> >> > S(i,:)= cal5(P,n{i});
> >> > end
> >> >
> >> > In the function "cal5", I am using fminunc to find the minimum of
> >> > unconstrained multivariate function. When I run one iteration the code
> >> > above works fine but as soon as I run the parfor, the code stops with
> >> > the following message: >
> >> > "fminunc stopped because it exceeded the function evaluation limit,
> >> > options.MaxFunEvals = 2700 (the default value)."
> >> >
> >> > I increased the MaxFunval to 100 000 but it still stops and shows the
> >> > same message. I suspect that fmincun is evaluating many functions in
> >> > the same time ( and in parallel way) is it the reason why is it
> >> > showing that message? Do I have to increase MaxFunval to very very
> >> > high number? or is there something deeper than that? Thanks so much
> >>
> >> I must admit that I'm not familiar with customizing fminunc in this
> >> way. One thing you need to be sure though is that you make this change
> >> in the body of the PARFOR loop - remember that the workers evaluating
> >> the body are separate MATLAB processes, so if you've made any changes to
> >> 'global state' in the client MATLAB, these aren't automatically
> >> reflected in the workers.
>
> > Thank you for your reply. Yes I agree and I have made the changes in
> > the body of the PARFOR loop and when I increase the number of function
> > evaluations to a very high number it seems that the code runs but it
> > is silly to do it this way no? I am sure there is something deeper
> > than that. If you have a minute, could you please forward the thread
> > to the people who deals with optimization fminunc?
>
> How is the setting made? If it's a global setting, you could do it using
> an SPMD block, like so:
>
> spmd
> doStuff();
> end
>
> which executes the function 'doStuff' exactly once on each worker.
>
> > When the PARFOR loop was running, the code stopped and an error
> > message showed up:
>
> > ??? Error using ==> parallel_function at 594
> > The session that parfor is using has shut down
> >
> > ??? The client lost connection to lab 1.
> > This might be due to network problems, or the pmode parallel job might have
> > errored.
>
> This generally means that the MATLAB worker session has aborted for some
> reason. Does this happen each time?
>
> Cheers,
>
> Edric.

Sorry Edric I dont understand what you mean by "global setting" . However, the ONLY global stuff that I have in my code are the uploaded data that I declare as global variables. These data stay unchanged for each function evaluation so they basically act as constant. I am not sure but I dont think the problem comes from there.

>
> This generally means that the MATLAB worker session has aborted for some
> reason. Does this happen each time?

I tried running the code twice and it happens at each time. You mentioned "MATLAB worker session has aborted for some reason" Could you please be more specific on the reason? Because my parfor loop looks simple and all I am doing is evaluating the function at different parameters so I am confident that there is no communication and everyhting runs in a parallel way. Thank you

Kind Regards

S

Subject: parfor and CPU Usage

From: Edric M Ellis

Date: 1 Sep, 2011 12:29:57

Message: 8 of 8

"Saad " <saad.badaoui07@imperial.ac.uk> writes:
> [...]
> Sorry Edric I dont understand what you mean by "global
> setting". However, the ONLY global stuff that I have in my code are
> the uploaded data that I declare as global variables. These data stay
> unchanged for each function evaluation so they basically act as
> constant. I am not sure but I dont think the problem comes from there.

If you have GLOBAL (or PERSISTENT) data declared in the 'client' MATLAB
session, that is not available in the 'worker' MATLAB sessions that are
operating on your PARFOR loop iterations. If you're relying on that,
that's a problem.


>> This generally means that the MATLAB worker session has aborted for some
>> reason. Does this happen each time?
>
> I tried running the code twice and it happens at each time. You
> mentioned "MATLAB worker session has aborted for some reason" Could
> you please be more specific on the reason?

Unfortunately not, because by the time we hit that error, the 'worker'
MATLAB process has exited without telling us why. So, you might find it
helpful to print debugging messages from within your PARFOR loop to work
out where you're getting up to.

Cheers,

Edric.

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