Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: multiple outputs from compiled function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114319] Re: multiple outputs from compiled function
  • From: "Eric Michielssen" <emichiel at eecs.umich.edu>
  • Date: Wed, 1 Dec 2010 02:09:49 -0500 (EST)

Thanks for the suggestion.

I was thinking more about a routine that populates a huge sparse matrix, say
for finite element analysis. SparseArray does not work within Compile, and
that's understandable. 

So the next best thing would be to have a compiled function generate a long
list of positions ( pos = {pos1,pos2,pos3...} with posi = {n1,n2} ) and a
list of values ( val = {val1,val2,val3,...} ), and then construct the
SparseArray externally to a compiled function. 

But how do I get my compiled function to return both pos and val? Upon
reaching Return[{pos,val}], Mathematica reverts to the noncompiled function,
in essence starting over from scratch. (I can only think of a few not so
elegant solutions, none of which would apply in situations more complex than
the above).

Eric

-----Original Message-----
From: Oliver Ruebenkoenig [mailto:ruebenko at wolfram.com] 
Sent: Tuesday, November 30, 2010 6:24 AM
To: mathgroup at smc.vnet.net
Subject: [mg114319] [mg114315] Re: multiple outputs from compiled function

On Tue, 30 Nov 2010, Eric Michielssen wrote:

> I have a compiled function that internally generates 10 large lists (list1
> though list10), all of different ranks and sizes. Problem is that a
compiled
> function can only return one packed array, so returning them to the main
> code is an issue. Of course, I could combine/pack all these lists into one
> list by Flatten[list1,...,list10], return the new structure, and then
unpack
> it in the main code. Is there a less cumbersome way of doing this?
>
> Eric Michielssen
>
>
>
>
Eric,

Here are some thoughts:

It is correct that compile can only work with rectangular tensors. See the 
following:

f = Compile[{{t1, _Real, 2}, {t2, _Real, 2}}, Join[t1, t2]]

f[RandomReal[{0, 1}, {2, 2}], RandomReal[{0, 1}, {2, 1}]]

compared to:

f[RandomReal[{0, 1}, {2, 2}], RandomReal[{0, 1}, {2, 2}]]

But if the result of the base CompiledFunction is a list, and the list 
results come out different lengths, it returns a list of PackedArrays,

cf = Compile[{{x, _Real}, {n, _Integer}},
    Module[{y = 1.}, Table[y = (y + x/y)/2, {n}]],
    RuntimeAttributes -> Listable];

res = cf[{2., 3.}, {3, 2}]

{Developer`PackedArrayQ[res], Map[Developer`PackedArrayQ, res]}

resp = cf[{2., 3.}, 3]

Developer`PackedArrayQ[resp]

What do exactly in your case depends on the code.

Hope this helps,
Oliver



  • Prev by Date: How to bring up specific doc page programmatically?
  • Next by Date: Re: SelectionMove[] busted with InputField and DockedCells?
  • Previous by thread: Re: How to bring up specific doc page programmatically?
  • Next by thread: Re: multiple outputs from compiled function