LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Darren's Occasional Nugget 08/15/2007

Hi everybody!  It's been a while since I posted an occasional nugget, but it looks like y'all have been getting some great ones in the meantime from the usual suspects.  I wanted to post today about three great new easy-to-use features in LabVIEW 8.5 that can improve your VI performance.
 
Feature:  For Loop with Break
Why you should use it:  How many times have you written a subVI that iterates over array contents in a While Loop, and stops when you find an element of interest *or* when you reach the end of the array?  I've written that code dozens (maybe hundreds) of times, and I always have to stop and double-check my boolean logic that ends the loop to make sure it doesn't run infinitely, handles empty input arrays, outputs the correct element, etc.  An example of that kind of code is shown below.  In LabVIEW 8.5, we don't have to worry about those logic pitfalls with the For Loop with Break.  In addition to making the code easier to write (and read), the equivalent For Loop with Break code will execute more efficiently than the While Loop code due to internal memory optimizations that can be done with a For Loop that are not possible with a While Loop.
 
 
Feature:  Feedback Nodes outside of Loops
Help TopicFeedback Node
Why you should use it:  I've been using Functional Globals (AKA "LabVIEW 2"-style globals) in my code for several years.  In LabVIEW 8.5, we now have Feedback Nodes that can exist outside of loops.  Not only does this clean up my code by allowing me to remove that extraneous loop, but the Feedback Node executes slightly more efficiently than it's Functional Global counterpart due to the fact that a small amount of overhead associated with executing the loop is eliminated.  Here's some sample code.
 
 
Feature:  In Place Element Structure
Why you should use it:  If you need to modify an element inside a cluster, typically you will unbundle the element, make a change to it, then bundle it back into the cluster.  Similarly, if you need to modify an array element, you index out that element, make a change, then use Replace Array Subset to stick it back in.  Using these methods allows you to change the data without making an extra copy of the cluster (or array), but memory still needs to be allocated for the item you pull out (and then stick back in) to the cluster or array.  In LabVIEW 8.5, we can use the In Place Element Structure to ensure that when we perform these kinds of operations, memory is reused in such a way that extra copies of those elements are not made.  When you're dealing with complex data structures like clusters of arrays of clusters of etc., this can be a huge performance benefit.  At the What's New in LabVIEW presentation at NI Week last week, I saw a demonstration where some code that processed a large array of complex clusters became 40 times faster when the index/unbundle/bundle/replace code was replaced with a couple of In Place Element Structures.  One of my teammates recently replaced some similar code in his codebase and his execution speed increased by a factor of 8.  I've already replaced several instances of this code in my current project, here's an example in which I use an In Place Element Structure nested inside another one.
 
 
I've been keeping a tally of the number of times I've used these new features in my current project.  Taking into account the fact that NI Week took up a lot of my time last week, within the past two weeks, I've already dropped 11 For Loop with Breaks, 8 In Place Element Structures, and 6 Feedback Nodes.  I'm really excited about continuing to incorporate these performance-enhancing features into my daily development.
 
-D

P.S. - Check out past nuggets here.

Message Edited by Darren on 08-15-2007 01:49 PM

Message 1 of 27
(17,904 Views)

Thanks Darren,

The "In Place Element Structure" is certainly an interesting one.

These will fun to work with.  Thanks for the continued "occasional" nugget!

RayR

Message 2 of 27
(17,840 Views)
I'd also point out another useful feature of Feedback Nodes (they're making quite the comeback!):

Imagine you have loops nested inside other loops, with case structures on every level and multiple cases in each. Now imagine buried deep in one of the cases of the innermost loop you have some data that you are keeping track of, such as a counter that you increment. The data is only used and modified in this one case, but you need to globally initialize it when the outermost loop starts running.

Previously your best choice was to create shift registers for every single loop from the outermost to the innermost loop to keep track of this one value. And to make things worse, you had to wire that value through every case of a case structure that didn't lead to the one point the data was being used.

Now Feedback Nodes provide you with a way to better scope your data. You can use this data in one and only one place deep in the hierarchy, but still globally initialize it on the outermost loop (or any owning loop you choose). Feedback Nodes now allow you to move the initializer terminal out to other owning loops. See this pic for an example of this benefit:

Message Edited by Jarrod S. on 08-15-2007 04:45 PM

Jarrod S.
National Instruments
Message 3 of 27
(17,822 Views)
Jarrod,

Now THAT's an interesting use for feedback nodes.  I can see this being used to solve a lot of headaches wiring up state machines.....

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
Message 4 of 27
(17,763 Views)

Great nugget Darren & Jarrod, eventhough I had already seen this post by Tomi Maila !Smiley Wink

Worth 8.5 stars apiece !Smiley Very Happy

http://forums.ni.com/ni/board/message?board.id=170&message.id=263403&query.id=349042#M263403

- Partha ( CLD until Oct 2024 🙂 )
Message 5 of 27
(17,737 Views)
Jarrod, to be fair, if you know the counter is only inside the inner case structure, you could use a single-run loop inside the case to keep the value instead of using the outer loop.

This is not to say I don't like the unconnected feedback node. It was one of the features I liked most in the beta and I was a little surprised it was not really mentioned at NIWeek.

___________________
Try to take over the world!
Message 6 of 27
(17,645 Views)
The In Place Element structure looks very cool. I do that kind of think with array elements quite frequently. The For Loop with break might come in handy as well. But FEEDBACK loops??? Smiley Surprised Something about the arrow pointing in the opposite direction of data flow just rubs me the wrong way. But if it is replacing the While Loop and shift register (AND it's more efficient) it will be cool as well. I guess I'll just have to adjust. Smiley Happy
PaulG.

LabVIEW versions 5.0 - 2023

“All programmers are optimists”
― Frederick P. Brooks Jr.
Message 7 of 27
(17,634 Views)
The in place element structure is truly a cool feature Smiley Happy
 
One thing (and I know I'm opening a can of worms here) that would make it cooler for me would be a selection like the "mark as modifier" that would lock the memory for other in place element structures until the IP structure released it...

--------------------

"I have made no decision...
My main concern now is to open as many options to the future as I can."
-- from Stephen Baxter's "The Time Ships"
0 Kudos
Message 8 of 27
(17,608 Views)

Great new features might be enough to make the transition from 8.2 to 8.5.  A couple features that ni could have added allong these lines (maybe on 9.0?) a conditional autoindex out of loops and a concatnate autoindex out of loops.  But is is nice to see progress in the structures pallet.

 

Paul 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 9 of 27
(17,607 Views)
My point tst was when you needed to have global initialization from the outermost loop, which you wouldn't get by simply having an uninitialized shift register in a single-run loop inside the case structure. That loop inside the case structure would never know when to reset its data. You'd have to pipe some "Reset" command down from the outermost loop on its first run to the inside single-most loop so that it could reset its count. All that wiring might negate the whole point, though it would still be less than having shift registers in all the loops and so on.
Jarrod S.
National Instruments
0 Kudos
Message 10 of 27
(17,608 Views)