Re: Pass by reference for compiled functions
- To: mathgroup at smc.vnet.net
- Subject: [mg114281] Re: Pass by reference for compiled functions
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Tue, 30 Nov 2010 04:01:43 -0500 (EST)
Eric Michielssen wrote: > For noncompiled functions, one can pass arguments "by reference" and then > modify them inside the routine by assigning the function the attribute > HoldAll. > > This does not work for compiled functions: it does not appear possible to > modify arguments to compiled functions, even if they are passed as "global > variables". That is, Mathematica responds to > > Myfunction = Compile[ > {{var1,_Real}}, > Module[{}, > Modify var 1 or var2; > Return[]], {var2,_Real}]; > > With > > Compile::argset: The assignment to var1 is illegal; it is not valid to > assign a value to an argument. >> And something similar for var2. > > HoldAll does not seem to apply here... > > This makes it memory-inefficient to modify large arrays in compiled > functions, as one would have to operate on a copy of the array. > > Is there a workaround in Mathematica 8? > > Eric Michielssen Not to my knowledge. I agree this mutability would be a really nice capability to have. I have wanted it myself since almost forever. Well, at least since the time I posted the message at the URL below. http://forums.wolfram.com/mathgroup/archive/2001/Feb/msg00195.html We have discussed the desire for call-by-reference, or something else that might allow for mutability of arrays, within Compile. People here seem toa gree it could be very useful. But I do not know whether we will attempt to go in this direction, or what might be the design or implementation issues (I suspect neither is trivial). One thing that might be new to version 8 is an ability to pass large arrays in Compile and not have them automatically copied. It is only when one needs to alter them that a copy is needed. So it is possible to invoke what amount to subroutines (that is, Compile calling functions that are also defined via Compile), using parge arrays as arguments, and not suffering a time or memory penalty. But again, this only holds if the arrays are not to be modified. I'm told some code along these lines might appear in an upcoming Wolfram blog. Daniel Lichtbau Wolfram Research