LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Community Nugget: Wired Terminals in Subdiagrams-Clearing up the mud

Many of us have read the famous "Clear as mud thread" but we never came back an nuggetized it.  I reciently wrote an internal memo on the subject and thought it ould serve as a nugget starter. 

 

Conditionally read/written terminals on the connector pane are BAD!

What are they?

This snippet on the left contains two examples of conditionally reached terminals.  Array – numeric will not be read of Method is Get and Array – numeric 2 will not be written if Method is Set.  Many years ago creating your functional global variables in this manner was considered to be good style however; best practices changed when the LabVIEW community looked at performance.   

Better practice is to have all terminals that are wired to the connector pane on the root block diagram as shown on the right.

 AE Conditional.pngAE Unconditional.png

 

There is even a high priority VI Analyzer test to catch this.  Why?  Well, lets benchmark the code above with these benchmark vis and run the VI profile tool.

 BMK C.png

BMK UC.png

The utility that you might not recognize is C:\Program Files (x86)\National Instruments\Labview 2011\vi.lib\Utility\High Resolution Relative Seconds.vi, it calls the system precision timer and has a finer resolution than get tick count.  It’s not been promoted to the palates because it will not function on an OS that does not have a Precision timer.  Most modern machines do.

The results:

 Analizer.png

Here we can see that using conditionally terminals slightly increases the execution speed of the sub-vi   On other hand, this is more than made up for by the callers performance hit! Set-up time for calling the subvi with conditional terminals goes up by orders of magnitude.


"Should be" isn't "Is" -Jay
Message 1 of 12
(9,353 Views)

So may VIs from the vi.lib have wired terminals in a case structure... Many we should place on idea on the exchange to ask NI to fix all of them!

 

Nice nugget!


We have two ears and one mouth so that we can listen twice as much as we speak.

Epictetus

Antoine Chalons

Message 2 of 12
(9,345 Views)

Good information and well put together.  Thanks for the nugget.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 12
(9,306 Views)

@TiTou wrote:

So may VIs from the vi.lib have wired terminals in a case structure... Many we should place on idea on the exchange to ask NI to fix all of them!

 

Nice nugget!


Not so fast there TiTou!  One thing I left out of the previous post is just what causes the pain in the caller.  With conditionally read terminals the Caller must protect its copy of the data in case there is a breakpoint in the sub-vi (See the clear as mud thread.)  Its that data copy/ buffer allocation that slows down the caller.  The time penalty is related to the size of the data.  In the example above we have a 2D array of a million DBLs read and written 500 times.  Re-running with a 1x1 array (essentially a scalar) and 50000 writes/reads yields these results from the profiler.

Profile2.png

 

YUP: the scalar data copy is faster than reading or writing a terminal with a scalar.  SO, for very small data sets the wired terminals might be better off in only the case they are used if they are not a "Required" terminal.  But I feel it is good practice to design my AE's for large data sets - They allway seem to get large anywaySmiley Wink


"Should be" isn't "Is" -Jay
Message 4 of 12
(9,243 Views)

Hi Jeff,

 

Good topic to Nuggetize!

 

Could you try three changes to your benchmarking code to see if they ifluence the numbers?

 

1) Move the Init array to prior to the seq frame to ensue that time is not in the calculation.

 

2) Use a control instead of a constant to set the array size so LV does not play constant folding tricks on us.

 

3) Move the output terminal to AFTER the seq so that GUi updates do not enter the measurement.

 

Again, thank you!

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 5 of 12
(9,239 Views)

@Ben wrote:

Hi Jeff,

 

Good topic to Nuggetize!

 

Could you try three changes to your benchmarking code to see if they ifluence the numbers?

 

1) Move the Init array to prior to the seq frame to ensue that time is not in the calculation.

 

2) Use a control instead of a constant to set the array size so LV does not play constant folding tricks on us.

 

3) Move the output terminal to AFTER the seq so that GUi updates do not enter the measurement.

 

Again, thank you!

 

Ben


Your wish is my command!

Additionally Mod 4) Use a control to set itterations was incorporated.

 

NOTE: That optomizer scares me too sometimes with benchmark code hence, the random array is also generated to keep the compiler from tossing out the whole darned loop. In theory the fact that the AE could be called from another vi should flag the compiler that this may not be loop invarient code but I hate taking chances. 
BMark Ben Mods Cond.png

BMark Ben Mods Uncond.png

 


"Should be" isn't "Is" -Jay
Message 6 of 12
(9,222 Views)

Thank you!

 

how do the numbers look now?

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 7 of 12
(9,214 Views)

For 500 iterations of 1000x1000

Conditional  8.78697Sec

Unconditional 7.13198Sec

@20% imprrovement for the loop

Profile3.png

 

The delta in time from the original seems to prove that I was correct in assuming that using constants on the Array size allowed the optomizer to compile the array as a constant and did not effect benchmark time.


"Should be" isn't "Is" -Jay
Message 8 of 12
(9,203 Views)

Jeff Bohrer wrote:
With conditionally read terminals the Caller must protect its copy of the data in case there is a breakpoint in the sub-vi


So does this difference still hold if debugging is turned off?  There should be no possibility of a breakpoint (or probe) on that wire, so no need to copy data.

 

0 Kudos
Message 9 of 12
(9,151 Views)
Unfortunately yes. Try it yourself. The LabVIEW dev team was cautious and that optimization is apparently not implemented. Actually I kind of like the fact that the compiler behaves the same for both cases, otherwise debugging could get very painful!

"Should be" isn't "Is" -Jay
Message 10 of 12
(9,141 Views)