0

Hi everyone I got a question at the exam that I could not solve about parallel programming. Can someone help me with this .

Question: For the following code segment, use OpenMP pragmas to make the loop parallel, or explain why the code segment is not suitable for parallel execution:

flag = 0 for(i=0;(i<n) & (!flag);i++){ a[i] = 2.3 *i; if(a[i]<b[i])flag = 1; }

1
  • 1
    I'm not sure it's possible with this loop due to flag. Regardless, see: Loop Parallelism. Commented Nov 21, 2021 at 11:37

1 Answer 1

1

As written the loop cannot trivially be parallelised with OpenMP because the test-expr in the loop (i.e. (i<n) & !flag) does not conform with the OpenMP restrictions :-

test-expr One of the following: var relational-op ub ub relational-op var relational-op One of the following: <, <=, >, >=, != 

(OpenMP standard). At a semantic level this is because the test on flag prevents the loop iteration count from being determinable at entry to the loop, which is what OpenMP requires.

In recent OpenMP standards there is a cancel construct which could be used here, something like (untested, uncompiled code)

bool flag = false; #pragma omp parallel for for(int i=0;(i<n);i++){ a[i] = 2.3 *i; if (a[i]<b[i]) { #pragma omp atomic write flag = true; } #pragma omp cancel for if (flag) } 

However it seems unlikely that a loop with this little work in it will be profitable for parallelisation in a real code (rather than an exam question).

Sign up to request clarification or add additional context in comments.

2 Comments

Your parallel version can give different result than serial one. The serial version stops at the smallest i where b[i]>2.3*i, but your parallel version does not. I think the only possibility to use parallelism here is to first find the smallest i where b[i]>2.3*i (or use n ), then fill array a in parallel.
Yes, @laci, you are right; I also made it impossible to tell, by making i have loop scope :-) (That's the disadvantage of these small, non-compilable, examples; it's hard to tell what they\re trying to do). If you want the first element parallelisation really makes little sense. (Your solution will work, but seems very unlikely to be faster than serial code...)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.