LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Use of sub-VIs to reduce block diagram size in top level VI

I have been reading about how to improve my coding style. The LabVIEW style book is a really helpful in this respect. One of the suggestions in that book is to use more sub-VIs to improve modularity, and consequently reduce the block diagram size. 

My doubt is if that won't add to the VI execution time due to the sub-VI overheads.

I would also love to hear the other methods you all use to limit the block diagram to one screen.

0 Kudos
Message 1 of 16
(2,691 Views)

There is minimal effect to execution speed by making good use of subVIs.
VI Execution
(See end of 1st paragraph)
However the benefits in code organization, readability and modularity are enormous.
Although slightly unpopular I quite often use a stacked sequence as a main structure with typically initialisation, operation and shutdown frames. The operation frame containing a while loop with an event structure to handle user IO. I find this general template provides an excellent starting point for code that requires significant user interaction. Other operations can be handled in separate while loops or timed loop threads.
There are of course several standard design pattern templates available from File > New ...

0 Kudos
Message 2 of 16
(2,680 Views)

@NeilR wrote:

Although slightly unpopular I quite often use a stacked sequence as a main structure with typically initialisation, operation and shutdown frames.


When I first encountered the Stacked Sequence, I was simply appalled, particularly by what they did to wires traversing them.  In one instance, I remember replacing them with a For Loop surrounding a Case Statement and using the Case as the "Frame Iterator" [I don't do that anymore, but I only had a year or two of LabVIEW under my belt].  Note that NI has removed them from the Structure Palette (I think they are still available, but Out of Sight, Out of Mind).

     Keep reading Blume's book -- when you reach the end, do as I did (twice) -- wait a month, and read it again.  Short of having an In House LabVIEW Guru to give you constant feedback, this may be the best "short-cut" to learning good LabVIEW Style, which directly translates (in my opinion) to better LabVIEW Code.

 

Bob Schor

0 Kudos
Message 3 of 16
(2,653 Views)

You can do some tests if you're really interested. A subVI call adds a few nanoseconds, but that can be removed if you make the function/SubVI Inline. Usually that's negligable, but as Always there's some cases in which those few extra ns's is what's needed.

Knowing about it though, can be quite good, as the suggestion then becomes to have a subVI that accepts an Array and does the looping internally instead of having a function inside a loop … i'll have to do some test to try out my own hypothesis. 🙂

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 16
(2,648 Views)

 


@Bob_Schor wrote:

When I first encountered the Stacked Sequence, I was simply appalled, particularly by what they did to wires traversing them.  

 

I have inherited a code full of stacked sequences and with no design pattern to speak of. I struggled to work with it, initially before deciding that rewriting the code will be easier. I had to redo a lot, but I am pretty happy with the end result. 

 

     Keep reading Blume's book -- when you reach the end, do as I did (twice) -- wait a month, and read it again.  Short of having an In House LabVIEW Guru to give you constant feedback, this may be the best "short-cut" to learning good LabVIEW Style, which directly translates (in my opinion) to better LabVIEW Code.

 

Yes, I will. Thanks.

 

 


@Yamaeda wrote:

A subVI call adds a few nanoseconds, but that can be removed if you make the function/SubVI Inline.

/Y


I can't use inline because the calling VI is dynamic.

0 Kudos
Message 5 of 16
(2,628 Views)

@t.d.b wrote:

 wrote:

A subVI call adds a few nanoseconds, but that can be removed if you make the function/SubVI Inline.

/Y


I can't use inline because the calling VI is dynamic.


The point Yamaeda was really making is that Call/Pass Variables/Return takes such a minute fraction of the CPU that unless you deliberately wrote a sub-VI that did a lot of "thrashing" of its input/output variables, you would be hard-pressed to be able to measure a Time Penalty, whereas not encapsulating code in sub-VIs will cost you far more time in coding, debugging, trying to understand a 4-screen Block Diagram with 140 wires running all over ...  Your Sanity is worth a few nanoseconds!

 

Bob Schor

Message 6 of 16
(2,608 Views)

@Bob_Schor wrote:

@NeilR wrote:

Although slightly unpopular I quite often use a stacked sequence as a main structure with typically initialisation, operation and shutdown frames.


When I first encountered the Stacked Sequence, I was simply appalled, particularly by what they did to wires traversing them.  In one instance, I remember replacing them with a For Loop surrounding a Case Statement and using the Case as the "Frame Iterator" [I don't do that anymore, but I only had a year or two of LabVIEW under my belt].  Note that NI has removed them from the Structure Palette (I think they are still available, but Out of Sight, Out of Mind).

     Keep reading Blume's book -- when you reach the end, do as I did (twice) -- wait a month, and read it again.  Short of having an In House LabVIEW Guru to give you constant feedback, this may be the best "short-cut" to learning good LabVIEW Style, which directly translates (in my opinion) to better LabVIEW Code.

 

Bob Schor


As you said, Bob, the stacked sequence is so not recommended that NI has kept it only for backwards compatibility and has deleted it from the Structures palette.  A state machine can replace all instances where you would be tempted to use a stacked sequence structure, but has the advantage that you can call any state from another.  Plus, state machines are so easily scalable, it's ridiculous.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 7 of 16
(2,593 Views)

 


@Bob_Schor wrote:

The point Yamaeda was really making is that Call/Pass Variables/Return takes such a minute fraction of the CPU that unless you deliberately wrote a sub-VI that did a lot of "thrashing" of its input/output variables, you would be hard-pressed to be able to measure a Time Penalty, whereas not encapsulating code in sub-VIs will cost you far more time in coding, debugging, trying to understand a 4-screen Block Diagram with 140 wires running all over ...  Your Sanity is worth a few nanoseconds!

 

Bob Schor


I was worried about the timing because I need to run a Data acquisition VI every 5 ms. As per Blume's book, it is better to go on making sub-VI's till they become cohesive. So for a really big application, that will mean a lot of sub-VIs.

But if each sub-VI is only taking a few nano-sec, then it will need a million sub-VIs to reach ms level of delay. So, yes, I'll opt for my sanity. Smiley Very Happy

Message 8 of 16
(2,583 Views)

I thought the stacked sequence comment would provoke an interesting response !

What I would say is that I pass no wires to or from any of the stacked frames. I get that. Smiley Very Happy

It's simply a "fixed state machine" if you like that applies to pretty much all situations in the top-level VI.
Generally in frame 1 I do things like Reinit All to Default, hide the front panel - resize it to largest decoration (thanks OpenG !) and then fade in using graduated transparency ... mmmm nice ...  plus store things like Tab terminals and the like.

Frame 2 is where the magic happens. Smiley Very Happy ... and usually contains a state machine or event structure.

Frame 3 is usually no more than a front panel fade out and environment dependent shutdown.
I totally agree that the use of stacked frames can extend a wiring nightmare from 2 dimensions to 3. I would say though that judicious use of stacks by experienced developers can keep things very tidy, but if you're uncomfortable then stick to state machines.

0 Kudos
Message 9 of 16
(2,582 Views)

@Yamaeda wrote:

You can do some tests if you're really interested. A subVI call adds a few nanoseconds, but that can be removed if you make the function/SubVI Inline. Usually that's negligable, but as Always there's some cases in which those few extra ns's is what's needed.

Knowing about it though, can be quite good, as the suggestion then becomes to have a subVI that accepts an Array and does the looping internally instead of having a function inside a loop … i'll have to do some test to try out my own hypothesis. 🙂


Unless you have a subVI that contains a minimum amount of code inside and you call it in a tight loop billions of times, you will be hard pressured to measure any increase in execution time for that subVI compared to having its code executed directly in the diagram. And for those cases you have the inline option.

If your subVI contains anything but non trivial code, its own execution time will be usually magnitudes higher than the overhead of calling it as subVI.

Rolf Kalbermatter
My Blog
Message 10 of 16
(2,579 Views)