Why is the != operator not allowed with OpenMP?

前端 未结 5 1926
清歌不尽
清歌不尽 2020-12-01 01:05

I was trying to compiled the following code:

#pragma omp parallel shared (j)
{
   #pragma omp for schedule(dynamic)
   for(i = 0; i != j; i++)
   {
    // do         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 01:35

    If I were to see the statement

    for(i = 0; i != j; i++)
    

    used instead of the statement

    for(i = 0; i < j; i++)
    

    I would be left wondering why the programmer had made that choice, never mind that it can mean the same thing. It may be that OpenMP is making a hard syntactic choice in order to force a certain clarity of code.

    Here's code which raises challenges for the use of != and may help explain why it isn't allowed.

    #include 
    
    int main(){
        int j=10;
       #pragma omp parallel for
       for(int i = 0; i < j; i++){
        printf("%d\n",i++);
       }
    }
    

    notice that i is incremented in both the for statement as well as within the loop itself leading to the possibility (but not the guarantee) of an infinite loop.

    If the predicate is < then the loop's behavior can still be well-defined in a parallel context without the compiler having to check within the loop for changes to i and determining how those changes will affect the loop's bounds.

    If the predicate is != then the loop's behavior is no longer well-defined and it may be infinite in extent, preventing easy parallel subdivision.

提交回复
热议问题