What is the best way to break (return) from OpenMP loop?
-
Is there a common way to handle such situation?
SomeObject* objects;
omp parallel for
for (i = 0; i < N; i++) {
int res = RunFunction(objects[i]);
if (res)
return res; //return from open mp loop is forbidden
}Once
RunFunction
failed stop processing and notify applicationЧесноков
-
Is there a common way to handle such situation?
SomeObject* objects;
omp parallel for
for (i = 0; i < N; i++) {
int res = RunFunction(objects[i]);
if (res)
return res; //return from open mp loop is forbidden
}Once
RunFunction
failed stop processing and notify applicationЧесноков
I'm no expert on openMP stuff... can't you just break ? or simply change the loop value so that next iteration will not continue ?
int res;
SomeObject* objects;
omp parallel for
for (i = 0; i < N; i++) {
res = RunFunction(objects[i]);
if (res)
i = N;
}return res;
Max.
Watched code never compiles.
-
I'm no expert on openMP stuff... can't you just break ? or simply change the loop value so that next iteration will not continue ?
int res;
SomeObject* objects;
omp parallel for
for (i = 0; i < N; i++) {
res = RunFunction(objects[i]);
if (res)
i = N;
}return res;
Max.
Watched code never compiles.
break and return are no allowed. I'm looking for the common practices and patterns in that scenario to not to envent something uncommon.
Чесноков
-
Is there a common way to handle such situation?
SomeObject* objects;
omp parallel for
for (i = 0; i < N; i++) {
int res = RunFunction(objects[i]);
if (res)
return res; //return from open mp loop is forbidden
}Once
RunFunction
failed stop processing and notify applicationЧесноков
I'm no OpenMP expert, but isn't this an example of an algorithm that can't be paralleled as it is written. Imagine two threads, so it runs (0,1), then (2,3), but what if the return value of RunFunction(object[2]) says that the loop should stop, we have probably already executed RunFunction(object[3]). It is not easy to come up with a way that stops a parallel loop with the same result as stopping a sequential loop.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
Is there a common way to handle such situation?
SomeObject* objects;
omp parallel for
for (i = 0; i < N; i++) {
int res = RunFunction(objects[i]);
if (res)
return res; //return from open mp loop is forbidden
}Once
RunFunction
failed stop processing and notify applicationЧесноков
the return value of your code corresponds to the logical OR of all those RunFunction invocations. If RunFunction() has no side-effects, you could use a global flag, and even abort all threads once a true value is encountered. If there are side-effects, your code is strictly sequential and cannot be parallellized; at best you could split RunFunction() in a first half without side-effects (that part can be run in parallel), and a second half that produces the side-effects (that part needs to be executed in strict sequence). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I'm no OpenMP expert, but isn't this an example of an algorithm that can't be paralleled as it is written. Imagine two threads, so it runs (0,1), then (2,3), but what if the return value of RunFunction(object[2]) says that the loop should stop, we have probably already executed RunFunction(object[3]). It is not easy to come up with a way that stops a parallel loop with the same result as stopping a sequential loop.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
it needs to prevent further processing in case of the single error even the next some other parallel returned correctly
Чесноков
-
it needs to prevent further processing in case of the single error even the next some other parallel returned correctly
Чесноков
Chesnokov Yuriy wrote:
it needs to prevent further processing in case of the single error even the next some other parallel returned correctly
That's why you want to do it, now you have to find a way to tell the compiler how to do it in one of the standard ways that it knows how to parallelise. Currently you are telling it that if RunFunction(object[n]) is false then it must not execute RunFunction(object[n+1]), and I don't think any compiler will know how to parallelise that.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."