for loop breaks

In a c for loop is the break statement acceptable? I am converting a while loop into a for loop. The search using the for loop ends when the correct element in an array is found. Without the break statemet the loop would run (unneccasrily) to completion. This is a waste. So again, in a for loop can I use a break statement to break out of a loop?

Hi THX 1138,

Yes, break statements are allowed in for loops.

However, did you mean to ask if break statements are allowed in for loops contained within an OpenACC compute region? If so, then the answers is no. The break statement (liek a while loop) puts in a dependency where the execution of a particular iteration of the loop depends upon whether the iteration before it terminated or not.

  • Mat

No, this staement is just for an acc region.

As you say that is allowed. The while loop occurs in a function that is inlined from an acc region. It eliminated the error : function call is not allowed. So I guess if I put in a for loop with break statement that will compile.

Newport_j

The following code does not work, it is from NDX_TBLp.

/---------------------------------
Search in the negative direction.
---------------------------------
/
for (NDXo=0;NDXo<NTBL; NDXo++) {
if (NDXo > 0 && X < XTBL[NDXo])
NDXo = NDXo + 1;
break; /* exit leave the loop the search is over */
}

// while( NDXo > 0 && X < XTBL[NDXo] ) NDXo = NDXo - 1;

/---------------------------------
Search in the positive direction.
---------------------------------
/

// while( NDXo < NTBL-1 && X >= XTBL[NDXo+1] ) NDXo = NDXo + 1;



The output complains about line 60. The output is:

PRC_BRN2:
219, Accelerator region ignored
223, PRC_TYP2 inlined, size=138, file prc_typ2.c (44)
102, SND_SLW2 inlined, size=39, file snd_slw2.c (39)
48, NDX_TBLf inlined, size=14, file ndx_tbl.c (29)
54, Accelerator restriction: invalid loop
60, Accelerator restriction: unknown right hand side reference
109, BTM_DPTb inlined, size=18, file btm_dptb.c (41)
50, Accelerator restriction: invalid loop
56, NDX_TBLf inlined, size=14, file ndx_tbl.c (29)
56, Accelerator restriction: unknown right hand side reference
115, SND_SLW2 inlined, size=39, file snd_slw2.c (39)
48, NDX_TBLf inlined, size=14, file ndx_tbl.c (29)
54, Accelerator restriction: invalid loop
60, Accelerator restriction: unknown right hand side reference
173, SND_SLW2 inlined, size=39, file snd_slw2.c (39)
48, NDX_TBLf inlined, size=14, file ndx_tbl.c (29)
54, Accelerator restriction: invalid loop
60, Accelerator restriction: unknown right hand side reference
220, Accelerator restriction: function/procedure calls are not supported
Accelerator restriction: loop has multiple exits
311, Accelerator restriction: function/procedure calls are not supported


The line 60 which is complained about three times is the line that starts:


// while (NDXo< NTBL-> 0 &&

Now this line is commented out.

It compiles under gcc.

What must I do to get rid of this error?

Thanks in advance.

THX 1138

Hi THX 1138,

No, this staement is just for an acc region.
As you say that is allowed.

I said break statement are allowed in C for loops but not allowed in for loops within compute regions.

What must I do to get rid of this error?

The loop isn’t parallel due the loop dependency as I explained above. To make this code parallel, you’ll need to create an array out of NDXo of size NTBL and record which NDXo match the search criteria. After this loop you then add a second sequential loop to find the first matching case.

Generally, I wouldn’t accelerate this loop unless this is part of a larger sequence of accelerator code, the data is already on the device, and the cost to move the data back to the host out weight the extra computation.

  • Mat

I’m going to back track a bit here. I looked at the full source you sent and see that the “break” is in a for loop that gets inlined into a compute region. The “break” statement will prevent this for loop from being accelerated but does not prevent it from being executed sequentially within the body of the compute kernel. In other words, you just can’t break out of a loop that is used to create the schedule. While loops and breaks are fine for code within the kernel.

  • Mat

When I eliminate the line with the break in it and only that line - I still get the same set of errors. Exactly the same! I assume it is because the if statement still in there and is creating a dependemcy?

Thanks in advance.