Suppose I have the function
heavyFunction[x_]:=Module[{n},Do[Pause[0.1],{n,100}]]
representing some complicated and time-demanding computation. I want in some circumstances to Monitor the progress of this function. Other times I don't, for example because heavyFunction is used by other computationally-heavy functions and I'd rather monitor the progress of those instead.
I'm also not looking for a solution like the simple
Monitor[heavyFunction[whatever],n]
because the quantity I'd use to monitor the progress can be not as simple as the loop index, and because the computation of such a quantity may depend on local variables defined in a Module inside the function.
The naive solution I'd have liked to use is something like
Options[heavyFunction]={monitor->True};
heavyFunction[x_,OptionsPattern[]]:=Module[{n},
If[OptionValue@monitor,Monitor[#,n],Identity]&@
Do[Pause[0.1],{n,10}]
]
but it doesn't show the progress.
How can this be achieved?
Answer
In general, it looks like you need to separate the process of execution and monitoring. The only general way I see to do this is to make your function an object (a pair of functions sharing a mutable state), so that one function would execute the code, while the other would report the internal state to the user.
Here is one possibility:
obj =
Module[{n},
<|
"execute" :> Do[Pause[0.1], {n, 100}],
"monitor" :> n
|>
];
and then
Dynamic[obj["monitor"]]
to monitor and
obj["execute"]
to execute the code. You will notice that the output of Dynamic[..] will reflect the changes.
In this method, one has to be careful with the possible leaking of local symbols n. However, when one sets $HistoryLength to some small finite number, and provided that these variables are not captured in some dynamic interfaces still being in use, these symbols are garbage-collected once no longer referenced. Still, this is something to watch out for.
The good part of this method is that the code execution and monitoring aspects are completely decoupled, so you could also monitor these variables when your function is being used inside some other code.
Comments
Post a Comment