Thread Subject: how to use constant structure arrays in parfor loop

Subject: how to use constant structure arrays in parfor loop

From: Michael

Date: 25 May, 2009 19:00:03

Message: 1 of 5

Hello everybody!

I need help!! ;-)

I need a constant and global structure array in a parfor-loop in a function file. Since the parallel computing toolbox does not allow global and persistent variables in a parallel parfor-loop I struggle with this issue. ... The access to the structure array shall be very fast, so it is not possible to save it in a file and to read from it in the loop. It is important to mention that the structure array cannot a function input argument!!

Any ideas to solve this problem? :-)

Best regards!
Michael

Subject: how to use constant structure arrays in parfor loop

From: Edric M Ellis

Date: 1 Jun, 2009 08:29:34

Message: 2 of 5

"Michael" <xyMueller@web.de> writes:

> I need a constant and global structure array in a parfor-loop in a function
> file. Since the parallel computing toolbox does not allow global and
> persistent variables in a parallel parfor-loop I struggle with this
> issue. ... The access to the structure array shall be very fast, so it is not
> possible to save it in a file and to read from it in the loop.

Does this help:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/248094#639592

?

> It is important to mention that the structure array cannot a function input
> argument!!

I'm not sure I understand this restriction - why can't you pass the structure
array into a function? (That's what's happening internally in various places to
end up in the body of your parfor loop).

Cheers,

Edric.

Subject: how to use constant structure arrays in parfor loop

From: Michael

Date: 2 Jun, 2009 13:12:01

Message: 3 of 5

Many thanks for the link! I saw this thread before I have send my request. ... In principle this method would work, but I am afraid of this concept because memory will be temporarily allocated in each iteration in the subfunction, which might time inefficient. Better would be a CONSTANT variable (structure array). Maybe you know how to create a constant structure array?

Many thanks!
Michael

Edric M Ellis <eellis@mathworks.com> wrote in message <ytwbpp85pch.fsf@uk-eellis-deb4-64.mathworks.co.uk>...
> "Michael" <xyMueller@web.de> writes:
>
> > I need a constant and global structure array in a parfor-loop in a function
> > file. Since the parallel computing toolbox does not allow global and
> > persistent variables in a parallel parfor-loop I struggle with this
> > issue. ... The access to the structure array shall be very fast, so it is not
> > possible to save it in a file and to read from it in the loop.
>
> Does this help:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/248094#639592
>
> ?
>
> > It is important to mention that the structure array cannot a function input
> > argument!!
>
> I'm not sure I understand this restriction - why can't you pass the structure
> array into a function? (That's what's happening internally in various places to
> end up in the body of your parfor loop).
>
> Cheers,
>
> Edric.

Subject: how to use constant structure arrays in parfor loop

From: Edric M Ellis

Date: 2 Jun, 2009 13:24:20

Message: 4 of 5

"Michael" <xyMueller@web.de> writes:
> Edric M Ellis <eellis@mathworks.com> wrote in message <ytwbpp85pch.fsf@uk-eellis-deb4-64.mathworks.co.uk>...
>> "Michael" <xyMueller@web.de> writes:
>> > It is important to mention that the structure array cannot a function input
>> > argument!!
>>
>> I'm not sure I understand this restriction - why can't you pass the structure
>> array into a function? (That's what's happening internally in various places to
>> end up in the body of your parfor loop).

> Many thanks for the link! I saw this thread before I have send my
> request. ... In principle this method would work, but I am afraid of this
> concept because memory will be temporarily allocated in each iteration in the
> subfunction, which might time inefficient. Better would be a CONSTANT variable
> (structure array). Maybe you know how to create a constant structure array?

Passing a large variable into a function in MATLAB doesn't necessarily cause
memory allocation because MATLAB uses copy-on-write to implement value
semantics. See part 2 of this FAQ answer:

http://matlabwiki.mathworks.com/MATLAB_FAQ#Can_MATLAB_pass_by_reference.3F

perhaps that helps.

Cheers,

Edric.

Subject: how to use constant structure arrays in parfor loop

From: Michael

Date: 2 Jun, 2009 21:12:01

Message: 5 of 5

Many thanks! ... I am always astonished that copy-on-write is working so well! :-) And the suggested idea also works in my case. Nevertheless, I have to criticize that writing a new function, instead of just using a constant global structure array, is non straight forward - at least for me. Would be nice to have more freedom in programming in some cases, ... e.g. I still think that in some cases call-by-reference
(for variables) would be nice - just as an additional degree of freedom ;)

Cheers,
Michael



Edric M Ellis <eellis@mathworks.com> wrote in message <ytw7hzurcor.fsf@uk-eellis-deb4-64.mathworks.co.uk>...
> "Michael" <xyMueller@web.de> writes:
> > Edric M Ellis <eellis@mathworks.com> wrote in message <ytwbpp85pch.fsf@uk-eellis-deb4-64.mathworks.co.uk>...
> >> "Michael" <xyMueller@web.de> writes:
> >> > It is important to mention that the structure array cannot a function input
> >> > argument!!
> >>
> >> I'm not sure I understand this restriction - why can't you pass the structure
> >> array into a function? (That's what's happening internally in various places to
> >> end up in the body of your parfor loop).
>
> > Many thanks for the link! I saw this thread before I have send my
> > request. ... In principle this method would work, but I am afraid of this
> > concept because memory will be temporarily allocated in each iteration in the
> > subfunction, which might time inefficient. Better would be a CONSTANT variable
> > (structure array). Maybe you know how to create a constant structure array?
>
> Passing a large variable into a function in MATLAB doesn't necessarily cause
> memory allocation because MATLAB uses copy-on-write to implement value
> semantics. See part 2 of this FAQ answer:
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ#Can_MATLAB_pass_by_reference.3F
>
> perhaps that helps.
>
> Cheers,
>
> Edric.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
callbyreference Michael 2 Jun, 2009 17:14:09
persistent Michael 25 May, 2009 15:04:03
global Michael 25 May, 2009 15:04:03
parfor Michael 25 May, 2009 15:04:03
parallel computing Michael 25 May, 2009 15:04:03
function input ... Michael 25 May, 2009 15:04:03
constants Michael 25 May, 2009 15:04:03
rssFeed for this Thread

Contact us at files@mathworks.com