Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to use constant structure arrays in parfor loop

112 views
Skip to first unread message

Michael

unread,
May 25, 2009, 3:00:03 PM5/25/09
to
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

Edric M Ellis

unread,
Jun 1, 2009, 4:29:34 AM6/1/09
to
"Michael" <xyMu...@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.

Michael

unread,
Jun 2, 2009, 9:12:01 AM6/2/09
to
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 <eel...@mathworks.com> wrote in message <ytwbpp8...@uk-eellis-deb4-64.mathworks.co.uk>...

Edric M Ellis

unread,
Jun 2, 2009, 9:24:20 AM6/2/09
to
"Michael" <xyMu...@web.de> writes:
> Edric M Ellis <eel...@mathworks.com> wrote in message <ytwbpp8...@uk-eellis-deb4-64.mathworks.co.uk>...
>> "Michael" <xyMu...@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.

Michael

unread,
Jun 2, 2009, 5:12:01 PM6/2/09
to
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 <eel...@mathworks.com> wrote in message <ytw7hzu...@uk-eellis-deb4-64.mathworks.co.uk>...

0 new messages