LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Several loops in one block diagram? Is that ok?

Solved!
Go to solution

Feel free to educate me. Somewhere I read that it's bad to have several while-loops in one block diagram. I can't understand why.  

An application needs a maintenance loop. This loop checks if there is data in a stack and if so, it push it upwards to a server. Same loop can have logic to make some LED blinking in different speeds depending on a variable.

There is also one or two more loops that drives some statemachines. 

 

Is it better to have Timed loop instead of a while-loop with delay-functions in the maintenance-loop? dt can be about 50ms because of the LEDs.

It feels better to have timed loops since the delay-functions eats CPU. 

 

So, bad with more then one loop?

Timed Loop or normal + delay-function?

 

Thanks

 

0 Kudos
Message 1 of 12
(2,221 Views)

@Haddock01 wrote:

Feel free to educate me. Somewhere I read that it's bad to have several while-loops in one block diagram. I can't understand why.  


You're probably thinking of Event Structures, not While Loops. There's nothing inherently wrong with more than one While loop, in fact its quite common. The important thing is that each while loop can perform its actions independently from the others. If data needs to be moved from one loop to another, use specific data-transfer mechanisms (queues, notifiers, variables etc.).

 

Timed loops aren't really that useful if you are running an application under Windows unless your dt is relatively large. However, they do help with the re-scheduling of the next iteration. I'd say either or option in your case without knowing any more specifics.

Message 2 of 12
(2,210 Views)

Thank you. You confirmed my thoughts.

 

Why I think timed loop is better when U need a slow loop is because I belive the "delay" are produced outside LV and therefor consume less CPU. Perhaps I'm wrong. Some day I will test this in more detail.

0 Kudos
Message 3 of 12
(2,161 Views)

@Haddock01 wrote:

Why I think timed loop is better when U need a slow loop is because I belive the "delay" are produced outside LV and therefor consume less CPU. Perhaps I'm wrong. Some day I will test this in more detail.


Define "better". You are worrying about things that are insignificant in a typical program compared to everything else that's going on.. A timed loop is also part of LabVIEW. Nothing "outside". Typically a wait saves CPU. 😉

 

Very long waits are typically not recommended, because they prevent dependent code from e.g reacting to the user. There are better ways, but it is not really clear what you are trying to do.

0 Kudos
Message 4 of 12
(2,150 Views)

By "better" I mean less CPU-load.  I think/belive (perhaps completely wrong) that a delay function in a loop gives other processes more CPU-time.

I thinking "Why run a loop at max speed? That consumes much CPU."

This way of thinking may be due to other projects I've done in other languages where delays i threads made the app run more effectively.

 

Like you say, I might worry about things that don't matter.

 

Sorry about my English. Far from perfect. Some parts from Google Translate. 🙂

0 Kudos
Message 5 of 12
(2,144 Views)

Maybe related.

I heard that everything inside a timed loop is forced to execute on one PC-CPU core, so you should not put too much different things inside a timed loop. I think I can see this in one program of mine, seems to often run at 50% CPU on a dual core pc.

0 Kudos
Message 6 of 12
(2,138 Views)

Yeap. The high CPU load I can also see in some application I wrote. Perhaps you can get something out of doing a "Analyze VI".  As I remember It gave some suggestions....

0 Kudos
Message 7 of 12
(2,134 Views)
Solution
Accepted by topic author Haddock01

@Ola_A wrote:

I heard that everything inside a timed loop is forced to execute on one PC-CPU core, so you should not put too much different things inside a timed loop.


To be more exact, it makes everything in the loop single-threaded.  So any parallelization you might have had inside of the Timed Loop is completely eliminated.

 

For a Windows system, I find the Timed Loop to not be worth it.  You still can't trust the timing and it eliminates potential optimizations.  On an RT system, Timed Loops are important for time critical loops, not anything else.  Generally, just use a While loop with a wait of some type, which includes VISA Read, Dequeue Element, DAQmx Read when reading many samples, etc.

 

As far as multiple loops in a VI, I will play some semantic games on you here.  I generally don't recommend a multiple loops in a single VI.  It is often best to have each of your loops in a separate VI and then called from the top level VI.  This increases modularity and possible reuse.


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 8 of 12
(2,115 Views)

Many code architectures have mutiple toplevel parallel loops and that's fine. Each toplevel loop should have a mechanism that paces it to a defined speed appropriate for it's task. Typically that can be done with a small wait but many other possibilities exist (even structure, dequeue timeout, DAQ task, etc.etc.). What you should probably avoid is gigantic pyramids of loops inside loops inside loops, especially if they contain interactive code. A program needs to be able to breath and should never get trapped inside deep inner loops. Small (i.e. no wait) and fast inner loops are common and fine if they terminate rapidly (e.g. to solve a Newton-Raphson problem). 

0 Kudos
Message 9 of 12
(2,089 Views)

Thanks. I have one more question.  I don't feel about an application I made. It check registered filesizes each hour. To check the files go quick. After the filecheck I have a while-loop with a wait-function set at 10sec. This loop waits totally 1 hour. The outer loop repeats everything again.
This hour-loop feels bad and sometimes I can see CPU-load goes up till 30%. How can I avoid this? Can I construct the wait-loop in another way?

0 Kudos
Message 10 of 12
(2,071 Views)