NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Proven Zealot
Jeff·Þ·Bohrer
Posts: 5,343

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.


"All shall be well and all shall be well and all manner of things shall be well" -Julian
Trusted Enthusiast
TiTou
Posts: 2,689

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

[ Edited ]

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!

________________________________________________________________
"Cambiar el mundo, amigo Sancho, que no es locura ni utopía. sino justicia."
Cervantes
Trusted Enthusiast
crossrulz
Posts: 3,771

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

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



Kudos always welcome for helpful posts...Kudos always welcome for any post.
Proven Zealot
Jeff·Þ·Bohrer
Posts: 5,343

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


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 anyway:smileywink:


"All shall be well and all shall be well and all manner of things shall be well" -Julian
Knight of NI
Knight of NI
Ben
Posts: 16,091

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

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

Ben Rayner
Who is NOT John Galt... yet... just building Rayner's Ridge

Proven Zealot
Jeff·Þ·Bohrer
Posts: 5,343

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


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

 


"All shall be well and all shall be well and all manner of things shall be well" -Julian
Knight of NI
Knight of NI
Ben
Posts: 16,091
0 Kudos

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

Thank you!

 

how do the numbers look now?

 

Ben

Ben Rayner
Who is NOT John Galt... yet... just building Rayner's Ridge

Proven Zealot
Jeff·Þ·Bohrer
Posts: 5,343

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

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.


"All shall be well and all shall be well and all manner of things shall be well" -Julian
Active Participant
GregS
Posts: 477
0 Kudos

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


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.

 

Proven Zealot
Jeff·Þ·Bohrer
Posts: 5,343

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

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!


"All shall be well and all shall be well and all manner of things shall be well" -Julian
By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page