Passing Large Arrays to Function

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing Large Arrays to Function

Thomas D. Dean-2
It looks like octave makes a copy of an array when passing it to a
function.  At least modifying an array within a function does not change
the external array unless it is defined as global.

Defining a variable global over several (many?) functions is a shortcut
to disaster.

Is there a way to pass an array by reference so modifications within a
function change the external array?

Tom Dean

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Passing Large Arrays to Function

Max Görner
Octave does copy on write. So passing variables anywhere does not
duplicate it, be it an assignment or a passing to a function. Only right
before the first change the variable gets duplicated.

So you can pass that big value as often as you want to if you don't
change it. This might not be as good as having references, but it's
quite decent anyway. You could try that using a very simple recursion.

BTW: Does Octave notice that it can operate on the passed arguments
directly? Matlab states to notice that in

    var = foo(var)

the variable 'var' can be manipulated directly, thus saving speed and
memory.

sincerly

Max Görner

--
PGP: https://wwwpub.zih.tu-dresden.de/~maxgoern/2FE6023F.asc

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing Large Arrays to Function

Olaf Till-2
On Tue, Jun 28, 2016 at 08:59:44AM +0200, Max Görner wrote:
> Matlab states to notice that in
>
>    var = foo(var)
>
> the variable 'var' can be manipulated directly, thus saving speed and
> memory.

Indeed? And if foo is terminated due to an error after var was changed
inside foo, var outside foo remains changed? Can you give a link to
the web-page with this information?

Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing Large Arrays to Function

Max Görner
The behaviour is described at

    http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/

sincerely

Max Görner

--
PGP: https://wwwpub.zih.tu-dresden.de/~maxgoern/2FE6023F.asc

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing Large Arrays to Function

Sebastian Schöps
In reply to this post by Thomas D. Dean-2
Reply | Threaded
Open this post in threaded view
|

Re: Passing Large Arrays to Function

Olaf Till-2
In reply to this post by Max Görner
On Tue, Jun 28, 2016 at 10:21:46AM +0200, Max Görner wrote:
> The behaviour is described at
>
>    http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/

To revisit this, partially repeating myself: I believe it's inevitable
that at least one (deep) copy of the array is made if an arbitrary
interpreted function is executed. Otherwise, the array can't be
reliably left unchanged if an error occurs in the function.

At the link you've given it is stated (if I understood it correctly)
that for the 'behaviour' to work the function must have been called by
another function. This would _not_ suffice in Octave to generate an
'outer' (deep) copy of the array, but maybe they have some mechanism
in Matlab to do this in this way.

Since (as I think) one copy must be generated anyway, I think you
could achieve equivalent behavior in Octave for operations (algebraic
operations or basic function calls) which change only certain
(indexed) elements of an array: Perform all such operations within one
function -- then a copy of the whole array should only be generated at
the first of these operations.

Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (836 bytes) Download Attachment