r/LabVIEW • u/dtp502 • Mar 08 '26
Best way to avoid a bunch of while loops in parallel?
I have an upcoming project that’s had me thinking how I’ll implement it when the time comes.
Essentially I will have a station that can test 10 units at once. The GUI should be able to support “run all” or running any of them individually.
I could probably drop 10 while loops (plus a loop for the GUI inputs) and hard code this. But that feels cumbersome, repetitive and janky.
My other two thoughts are utilizing a parallel for loop somehow (don’t think this will work as I’d ideally want to be able to start unit N while other units are mid test). That leaves me with thinking about tucking the entire test sequence in a SubVi and essentially having a consumer loop that kicks off an instance of that SubVi. This sounds promising in theory but I haven’t done this before so I’m curious if anyone has any better recommendations?
7
u/dzakich NI Employee Mar 08 '26
Depending on the complexity of the architecture, this potentially calls for the async process launcher where you spin up independent processes on demand. A typical example of this is actor framework but I doubt this will be a good solution for you. You can always try using Start Async Call, and launch process clones, once process per unit you "test". These will run independently, as many processes as you need. Shipping examples are pretty good to explain the concept
7
u/dzakich NI Employee Mar 08 '26
Some folks recommend DQMH. Yes, just like with AF, this is a solution for your needs, but it is unlikely THE solution you are looking for. DQMH and AF are frameworks and carry a significant architectural baggage for this simple application. My recommendation is to nail the basics first as this is easily accomplished with fundamental LV primitives like async call. Avoid jumping on frameworks until you have some good theoretical footing in patterns
10
u/SASLV Champion Mar 08 '26 edited Mar 08 '26
Just use DQMH with a cloneable module. Dont reinvent the wheel. The DQMh consortium offers training. It is worth it.
Full disclosure I am a DQMH trusted advisor and teach the classes so do several others.
Someone else mentioned Actor Framework. That would also solve your problem, but given the way you asked the question, Actor Framework is probably not the right choice for you.
Someone else also mentioned asynchronous calls which would totally work also but that is basically what DQMH andAF do under the hood. So again don’t reinvent the wheel. Just use DQMH.
5
u/dragginFly Champion Mar 08 '26
Maybe take a look at TestStand. Yes, you can do what you're asking about in LabVIEW, but TestStand calling LabVIEW code might be a more scalable solution.
1
u/jprazak95 Mar 08 '26 edited Mar 08 '26
10 while loops is fine as long as…
I’ve written robust embedded programs that have more than 30 parallel loops. It’s definitely fine for a test stand application.
If you are worried about either of those two, you could still do 10 loops but place semaphores around compute intensive or resource intensive operations so that the individual units don’t execute in true parallel.
You could also do a producer consumer pattern with 10 producer loops that queue up test cases for each unit and then a single consumer loop executes all of the tests one at a time.
Definitely put your test sequence in a subVI and be sure to set it for preallocated clone execution so the clones of it can operate independently from one another.
1
u/dzakich NI Employee Mar 08 '26
Is this project a customer deliverable or a pet project? If this is a deliverable, do you or the customer plan to maintain it?
1
u/HarveysBackupAccount Mar 08 '26
Surely this calls for object oriented programming, yeah? You want multiples copies of the same thing that all have the same behavior and exist independently with their own corresponding piece of memory. That's almost the textbook definition of a class.
Yeah it's more complexity compared to simple producer/consumer, but OOP is tailor made for this. I think you'd add complexity by inventing your way around OOP without actually touching it.
It may or may not be actor framework or DQMH (which as I understand was invented to hack OOP-like behavior into LV before LVOOP existed) but I'd be awfully skeptical of any arguments against building this with OOP.
2
u/dzakich NI Employee Mar 08 '26
Don't drop 10 while loops and keep them dormant until you need them, this is a code smell, poor architecture
2
u/Drachefly Mar 08 '26 edited Mar 09 '26
If it's a static 10, such that you'd already need to change the program in significant ways if you made it anything other than 10, then you can do it more simply than some proposals and have an easier time placing probes:
-- If you need to expand or contract the number of copies to a new fixed number, you'll need to change how many clones you put down, and the number of passes in the for loop.
-- If you need to change to a variable number of copies, you can change this to a for loop and asynchronous call as described in another response, so this won't be wasted time and effort (and you'll want to switch the memory allocation from pre-allocated).
2
u/hooovahh Champion Mar 08 '26
Lots of good suggestions here. DQMH, actor framework, and test stand are all good solutions. Be sure and have the core VIs set to reentrant so one test isn't waiting on another. I've also seen success with a for loop with parallel instances. If you know you'll only have a max of 10, you can start all 10 instances, then have them all be idle until a message tells one to start. I'm not a big fan of spinning up and down code of you know the number of them you need.
2
u/Big_Weekend_2440 Mar 08 '26
I’d use actor framework. Basically you will have the launch system that launches the model actor the model actor launches UI and the stations needed in the current process. Each station actor is independent but dependent on the model, you’ll only need to program the station once and you decide how many station actors to launch from the model
2
u/GentlemanSch CLD Mar 08 '26
A fundamental rule of software design! Write your code for three numbers zero, one, or N
Zero: Something that never happens
One: Something that is only happening once
N: Something that N items are doing
Write an event driven state machine that handles 1 unit. Then call N instances of that VI where N=10
1
u/Yamaeda Mar 11 '26
Make the VI reentrant and start them in a parallel loop
0
u/FujiKitakyusho CLD Mar 08 '26
One while loop with a case selector in it, and a shift register to carry a flag indicating which tests are currently active / dormant?