cpp: #include <stdio.h>\n#define min(x, y) (((x) < (y)) ? (x) : (y))\n\n/*\nuse of omp target + teams + distribute + parallel for\n*/\nint main(int argc, char* argv[])\n{\n  int i, i2;\n  int len = 2560;\n  double sum =0.0, sum2=0.0;\n  double a[len], b[len];\n  /*Initialize with some values*/\n  for (i=0; i<len; i++)\n  {\n    a[i]= ((double)i)/2.0;\n    b[i]= ((double)i)/3.0;\n  }\n\n#pragma omp target map(to: a[0:len], b[0:len]) map(tofrom: sum)\n#pragma omp teams num_teams(10) thread_limit(256) reduction (+:sum) \n#pragma omp distribute\n  for (i2=0; i2< len; i2+=256)  \n#pragma omp parallel for reduction (+:sum)\n    for (i=i2;i< min(i2+256, len); i++)\n      sum += a[i]*b[i];\n\n/* CPU reference computation */  \n#pragma omp parallel for reduction (+:sum2)\n    for (i=0;i< len; i++)\n      sum2 += a[i]*b[i];\n  printf ("sum=%f sum2=%f\n", sum, sum2);\n  return 0;\n}\n
fortran: program DRB097_target_teams_distribute_orig_no\n    use omp_lib\n    use ISO_C_Binding\n    implicit none\n    integer, parameter :: dp = kind(1.0d0)\n    integer, parameter :: double_kind = c_double\n    integer (kind=8) :: i, i2, len, l_limit, tmp\n    real(dp) :: sum, sum2\n    real(dp), dimension(:), allocatable :: a, b\n\n    len = 2560\n    sum = real(0.0,dp)\n    sum2 = real(0.0,dp)\n\n    allocate (a(len))\n    allocate (b(len))\n\n    do i = 1, len\n        a(i) = real(i,dp)/real(2.0,dp)\n        b(i) = real(i,dp)/real(3.0,dp)\n    end do\n\n    !$omp target map(to: a(0:len), b(0:len)) map(tofrom: sum)\n    !$omp teams num_teams(10) thread_limit(256) reduction (+:sum)\n    !$omp distribute\n        do i2 = 1, len, 256\n            !$omp parallel do reduction (+:sum)\n            do i = i2+1, merge(i2+256,len,i2+256<len)\n                sum = sum+a(i)*b(i)\n            end do\n            !$omp end parallel do\n        end do\n    !$omp end distribute\n    !$omp end teams\n    !$omp end target\n\n    !$omp parallel do reduction (+:sum2)\n    do i = 1, len\n        sum2 = sum2+a(i)*b(i)\n    end do\n    !$omp end parallel do\n\n    print*,'sum =',int(sum),'; sum2 =',int(sum2)\n\n    deallocate(a,b)\nend program