PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

CUDA-x86.

for loop breaks
Goto page 1, 2  Next
 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
THX 1138



Joined: 30 Jun 2011
Posts: 103

PostPosted: Fri Oct 19, 2012 9:32 am    Post subject: for loop breaks Reply with quote

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?

Last edited by THX 1138 on Fri Oct 19, 2012 10:49 am; edited 1 time in total
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6120
Location: The Portland Group Inc.

PostPosted: Fri Oct 19, 2012 9:58 am    Post subject: Reply with quote

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
Back to top
View user's profile
THX 1138



Joined: 30 Jun 2011
Posts: 103

PostPosted: Fri Oct 19, 2012 10:25 am    Post subject: For looops with break statements. Reply with quote

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
Back to top
View user's profile
THX 1138



Joined: 30 Jun 2011
Posts: 103

PostPosted: Fri Oct 19, 2012 11:17 am    Post subject: Reply with quote

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
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6120
Location: The Portland Group Inc.

PostPosted: Fri Oct 19, 2012 1:24 pm    Post subject: Reply with quote

Hi THX 1138,

Quote:
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.

Quote:
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
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group