LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Does code that only connects to a first call initializer terminal only get called during initialization?

Solved!
Go to solution

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?

 

M_S_C_0-1706313105673.png

 

0 Kudos
Message 1 of 7
(1,091 Views)
Solution
Accepted by M_S_C

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?

0 Kudos
Message 2 of 7
(1,056 Views)

@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?

 

M_S_C_0-1706313105673.png

 


 

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.


___________________
Try to take over the world!
0 Kudos
Message 3 of 7
(972 Views)

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.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 7
(964 Views)

@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.


___________________
Try to take over the world!
0 Kudos
Message 5 of 7
(958 Views)

If you really want to run code only once, here's what I typically do:

 

altenbach_0-1706462503488.png

 

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.

 

 

altenbach_0-1706463977230.png

 

 

 

Message 6 of 7
(938 Views)

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!

 

0 Kudos
Message 7 of 7
(928 Views)