Return multiple results from compiled function [duplicate]

I wrote a function with Compile that produces a large array of data and later does some computation on it. Now, I want to return from the compiled function not only the final processed data, but also the raw data from which it originates. I know that Compile cannot deal with ragged arrays, that is I can not simply state {x,y} as the final line of the compiled function. What are the best strategies to resolve this problem if I don't won't to break up the one compiled function into two functions?

marked as duplicate by Szabolcs, MarcoB, m_goldberg, Henrik Schumacher, J. M. will be back soon♦Feb 15 at 4:56

• I edited this for clarity (as I found it a bit confusing). Please check that I did not break your intended meaning. – Szabolcs Feb 14 at 11:33
• @Szabolcs: Sorry, yes I was trying to Export it first on-the-fly, but the function is not supported within Compiled. Now I would be glad to find a way of returning it as the output of Compile and then process it further from there within the MMA kernel. – Display Name Feb 14 at 11:34
• Can you give a representative example? Something trivial that returns that same shape of data that you actually have. Maybe there's a simple way to encode all pieces into a single tensor. You can actually return them as {x,y}, and I believe that the system will break from the compiled evaluation only at the very last operation. It does issue an annoying message though. – Szabolcs Feb 14 at 11:40
• Not necessarily. Basically, it is about two copy operations. The Flattening and ArrayReshape ing is really almost for free since packed arrays are stored internally as a flat vector anyways. – Henrik Schumacher Feb 14 at 11:48
• @Szabolcs My understand is that "proceeding with uncompiled evaluation" means that the evaluation of the CompiledFunction cf is stopped and restarted completely from the beginning by executing cf[[7]] as fallback. So that would make things even more expensive. – Henrik Schumacher Feb 14 at 11:55

Not an answer but too long for a comment.

This shows that in case of CompiledFunction::cflist, a CompiledFunction cf will fall back to execution of cf[[7]]:

cf = Compile[{}, {{1, 2}, {3}}];
Quiet[cf[[7]] =
Function["args", Print["Meep!"]; "body"] /.
{"args" -> cf[[7, 1]], "body" -> cf[[7, 2]]};
];
cf[];


Meep!

• @Szabolcs See here for an example. – Henrik Schumacher Feb 14 at 12:09