01-26-2024 06:17 PM
Does code that only connects to a first call initializer terminal get called after the first call, even when the values it produces is no longer used?
For example, if I call a VI with the code below in a loop, the first call of that loop should initialize the feedback node with an array of strings.
Since the value produced by all the blocks to the left of the feedback node are only needed on the first call to initialize the feedback node, I would think they would only be executed on the first call.
When I execute it in a debugger, those initialization blocks on the left seem to get run on every iteration, even though the value they produce is no longer used.
Is this because I am running it in a debugger or does LabVIEW always run the code regardless of whether the output is used?
Solved! Go to Solution.
01-26-2024 10:43 PM - edited 01-26-2024 10:47 PM
While the LabVIEW compiler is very smart, we cannot tell what's left after all optimizations. If you want to guarantee that code only runs once per session place it in a case structure with the selector wired to "first call?".
Can you explain what problem you are trying to solve?
01-28-2024 01:45 AM - edited 01-28-2024 01:47 AM
@M_S_C wrote:
Is this because I am running it in a debugger or does LabVIEW always run the code regardless of whether the output is used?
Like altenbach said, you can't actually know (unless you want to start disassembling and analyzing the machine code), but I think it's safe to assume that the code is always run, exactly like you see when debugging.
For the code to only run once, LV would have to see that it's connected the init terminal and then analyze it backwards to figure out what the whole block of code is and then verify that it has no side effects (meaning basically that everything is done by value), including in the subVIs. If it does have side effects, then it would have to be executed, because the fact that it's connected to the init terminal might just be part of the reason the code is there.
While LV probably can do this technically, I doubt that this is an optimization which has been implemented.
Personally, I don't use the init terminal for code which I explicitly only want to run once and instead use a case structure as suggested.
01-28-2024 05:01 AM - edited 01-28-2024 05:03 AM
That code can NOT be determined to have no side effects. It seems a device control of some sort so the fact that you use the output as initializer is not relevant. It does other things too and it would most likely be a logical error to simply eliminate that code. In case of uncertainty, LabVIEW has to choose the safe route and that is to execute the code anyways.
If you want to have code executed just once, program accordingly and don’t try to abuse side effects that would be in this case bugs with a very high chance to be fixed rather sooner than later.
01-28-2024 06:45 AM
@rolfk wrote:
That code can NOT be determined to have no side effects. It seems a device control of some sort...
They're actually the data type parsing VIs (under the variant palette, similar to the OpenG variant VIs), but there's earlier code we don't see and even if we did, I doubt LV does this optimization. Even if it was shown to have it, I agree that it's prudent to explicitly do what you want and not rely on possible optimizations.
In this case, I'm guessing the OP simply wants LV not to run the same code repeatedly in order to improve performance.
01-28-2024 11:40 AM - edited 01-28-2024 11:46 AM
If you really want to run code only once, here's what I typically do:
Note that this example is simplified, because the FOR loop will be constant folded once debugging is disabled, so the case/feedback makes no difference. However, if the "once" code is more complicated (maybe involving calls to subVIs or even interacts with hardware, etc.) the code will be more efficient.
My programs are blazingly fast because I use similar code in many places where entire expensive sections are cached unless one of the inputs change (which happens rarely). Here's a generic subVI that shows the pattern.
01-28-2024 01:37 PM - edited 01-28-2024 01:41 PM
In this case, I'm guessing the OP simply wants LV not to run the same code repeatedly in order to improve performance.
The statement above is correct.
The code to the left that you don't see is just a cluster unbundled from another cluster. The code is meant to create an array containing the names of the elements in the cluster.
I ended up putting in the case structure.
Thanks for the quick help!