LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Q about timing loops in both VI and subVI

Solved!
Go to solution

Well, this is probably something just about anyone here can answer immediately, but it seems to be a black hole in my LabVIEW knowledge, and has been bothering me for a while.

 

I certainly appreciate that if you have a VI with a loop without any timing information, it will loop as fast as possible, hogging the CPU's resources, leading to Bad Things (TM). However, if you have a VI with a loop containing a subVI with a second loop which does have timing information (say, polling a fast serial line @ 1 ms), do you also need a timer in the outer VI, or not? I almost always err on the side of caution and put one in, but if it's redundant, I'd naturally rather leave it out.

 

Thanks in advance,

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 1 of 8
(2,694 Views)
Solution
Accepted by topic author camerond

An interesting question that deserves an answer with a simple example.  So, I whipped one up to demonstrateSmiley Happy

!0.png

I Did take a minimallist approach!  Assume that this snippette represents a simple sub-vi with a loop that contains some timing and, a caller that is not sure if it is a greedy loop or not.  (A bajillion other examples could exist but this one is hopefully very clear) 

 

A simple run and inspection of the timing chart will prove that the delay in the outer loop will have no effect on the for loop performance as long as it delay is less than or equal to the total time the for loop takes. Go ahead and enable and disable the case containing the while loop delay.  You will not see a difference in the Timing chart.  In other words- the sub-vi provides all the "Pacing" that the module requires and the while loop is not greedy.

 

HOWEVER:  Now that you proved the while loop is not greedy set "numeric"=0 and try againSmiley Wink 


"Should be" isn't "Is" -Jay
Message 2 of 8
(2,674 Views)

Hi, Jeff,

 

What do you mean "GREEDY" in a while loop?

Does it mean the ability to rob the resource of the SubVI?

The sequence of "as long as the outer while time is less than or equal to the total time of the for loop takes" confused me a little, I thought no matter the timer of the while loop is, the timer of the for loop will work as expected.

Thanks for your sharing.

0 Kudos
Message 3 of 8
(2,662 Views)

Jeff put together an excellent example.  It also shows how a FOR loop behaves if you tell it to run 0 times (there are some fun bugs associated with that if you are not careful).

 

My 2 cents.  If you fully understand what is happening inside that loop all the way down the VI hierarchy and you know there is stuff in there that will control timing (DAQmx, VISA, Dequeue, waits), there is no need for the wait in the upper loop.  But if you are unsure at all, err on the side of caution and throw in a small delay.  Though, all you need is a 0ms delay to alleviate the greedy loop syndrome.


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 4 of 8
(2,648 Views)

William1225 wrote:

What do you mean "GREEDY" in a while loop?

Does it mean the ability to rob the resource of the SubVI?

The sequence of "as long as the outer while time is less than or equal to the total time of the for loop takes" confused me a little, I thought no matter the timer of the while loop is, the timer of the for loop will work as expected.


A greedy loop is a loop that will hog the CPU, not letting other resources work.  Since the subVI is inside of the loop, it won't be impacted.  So if there is timing/waiting inside of the subVI, then the main VI's loop will not be greedy.

 

The wait in the outer loop will run in parallel with the "subVI" since there is no data dependency.  So as long as the outter loop's wait is no longer than the time it takes the "subVI" to run, you will see no timing difference for the entire main loop.  If that wait is larger than the time that it takes for the "subVI" to run, then the main loop will start to loop at a slower rate.


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 5 of 8
(2,642 Views)

Thanks, Jeff,

 

That was clearer than anything I'd come up with to test it.

 

"Now that you proved the while loop is not greedy set "numeric"=0 and try again:smileywink: "

 

Well, when the numeric is "0", nothing (apparently) happens, since the charts are not updated from the for loop. But (here comes the "apparently") the program is running at warp speed, which you can see if you wire an indicator to the while loop index.

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
0 Kudos
Message 6 of 8
(2,623 Views)

@crossrulz wrote:

 The wait in the outer loop will run in parallel with the "subVI" since there is no data dependency.  So as long as the outter loop's wait is no longer than the time it takes the "subVI" to run, you will see no timing difference for the entire main loop.  If that wait is larger than the time that it takes for the "subVI" to run, then the main loop will start to loop at a slower rate.


Ahh! ( "Light Bul-lb!" * ) I hadn't thought about the wait timers running in parallel because of data dependency! Now it's all making sense, because I put it into a loop for a particular purpose, I just (incorrectly) thought of the wait timer as a "special case," when it really isn't. Thank you.

 

* Gru in "Despicable Me"

 

Cameron

 

To err is human, but to really foul it up requires a computer.
The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
Profanity is the one language all programmers know best.
An expert is someone who has made all the possible mistakes.

To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):

LabVIEW Unit 1 - Getting Started</ a>
Learn to Use LabVIEW with MyDAQ</ a>
Message 7 of 8
(2,615 Views)

@camerond wrote:

@crossrulz wrote:

 The wait in the outer loop will run in parallel with the "subVI" since there is no data dependency.  So as long as the outter loop's wait is no longer than the time it takes the "subVI" to run, you will see no timing difference for the entire main loop.  If that wait is larger than the time that it takes for the "subVI" to run, then the main loop will start to loop at a slower rate.


Ahh! ( "Light Bul-lb!" * ) I hadn't thought about the wait timers running in parallel because of data dependency! Now it's all making sense, because I put it into a loop for a particular purpose, I just (incorrectly) thought of the wait timer as a "special case," when it really isn't. Thank you.

 

* Gru in "Despicable Me"

 

Cameron

 


Congrats!  Now, go do that execise again with task manager up and look at CPU usage.  You will find "Bad Things" TM and a bit more on "Greedy Loops".  Then, Re-Read Crossrulz's posts.

 

Thanks for the explaination Tim.


"Should be" isn't "Is" -Jay
0 Kudos
Message 8 of 8
(2,607 Views)