From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Stop loop with parallel iterations

Solved!
Go to solution

Any suggestions on ways to stop a for loop with parallel iterations enabled.

 

A conditional terminal isn't allowed so I am a bit at a loss.

 

The only thing I can think is to put all the code in a case structure so when I want to 'stop' the loop I cause the other case to run which has nothing in it. This would just result in the rest of the iterations to run very quickly. I feel like there is a better method though.

 

This is for a user cancel button if it is going to take too long.

0 Kudos
Message 1 of 6
(1,472 Views)

If you think about it, a for loop can stop from executing the next iteration but not the current iteration.

 

In a parallel for loop, all iterations are started in parallel and there is nothing to stop.

 

Are you asking to stop executing a piece of code within one iteration of the loop, sure, you keep checking a custom local/global variable at regular intervals and decide execute the rest of your code in that iteration.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 2 of 6
(1,463 Views)

I feel like there isn't a better method.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 3 of 6
(1,459 Views)

@santo_13 wrote:

If you think about it, a for loop can stop from executing the next iteration but not the current iteration.

 

In a parallel for loop, all iterations are started in parallel and there is nothing to stop.

 

Are you asking to stop executing a piece of code within one iteration of the loop, sure, you keep checking a custom local/global variable at regular intervals and decide execute the rest of your code in that iteration.


It's not usually "all iterations".  There are multiple iterations running in parallel, but that number is often less than the total number it iterations.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 4 of 6
(1,457 Views)
Solution
Accepted by Niatross

Iterations of a parallel FOR loop occur in random order, so if you are for example autoindexing at the right boundary, there is no way to tell what kind of partial result you get if you terminate early, so make sure to discard all output or at least output a sentinel value (e.g. NaN) for data that's missing.

 

My reentrant code inside parallel loops typically have a non-reentrant critical section implemented as an action engine that will cause all instances to generate error 43 (canceled by user) and terminate. You can call that same action engine in independent loop of the main VI with that listed for <esc> and sets the flag to terminate. Reset once the parallel for loop has completed.

 

But yes a reentrant subVI that bypasses all it's slow code (or just a case structure) if the the interruption signal is detected is probably the easiest.

 

 

altenbach_0-1639769211656.png

 

0 Kudos
Message 5 of 6
(1,442 Views)

@paul_cardinale wrote:

@santo_13 wrote:

If you think about it, a for loop can stop from executing the next iteration but not the current iteration.

 

In a parallel for loop, all iterations are started in parallel and there is nothing to stop.

 

Are you asking to stop executing a piece of code within one iteration of the loop, sure, you keep checking a custom local/global variable at regular intervals and decide execute the rest of your code in that iteration.


It's not usually "all iterations".  There are multiple iterations running in parallel, but that number is often less than the total number it iterations.


Agree with you, yes, not all iterations execute in parallel, theoretically possible if your processor has infinite threads.

 

Typically, you configure the number of parallel instances to match the logical threads supported by your CPU and the indexed data are executed in batches of the number of parallel instances.

 

 

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 6 of 6
(1,431 Views)