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.
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.
12-17-2021 12:19 PM
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.
Solved! Go to Solution.
12-17-2021 01:06 PM
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.
12-17-2021 01:18 PM
I feel like there isn't a better method.
12-17-2021 01:21 PM
@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.
12-17-2021 01:33 PM
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.
12-17-2021 02:15 PM
@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.