I have a subroutine as below:
subroutine lm(n, x, y, res)
integer(8) :: n
real(8) :: x(n), y(n), res
real(8) :: sum_x, sum_y, sum_xx, sum_xy
integer(8) :: i
sum_x = 0d0
sum_y = 0d0
sum_xx = 0d0
sum_xy = 0d0
!$omp parallel do reduction(+:sum_x)
do i = 1, n
sum_x = sum_x + x(i)
end do
!$omp end parallel do
!$omp parallel do reduction(+:sum_y)
do i = 1, n
sum_y = sum_y + y(i)
end do
!$omp end parallel do
!$omp parallel do reduction(+:sum_xx)
do i = 1, n
sum_xx = sum_xx + x(i)*x(i)
end do
!$omp end parallel do
!$omp parallel do reduction(+:sum_xy)
do i = 1, n
sum_xy = sum_xy + x(i)*y(i)
end do
!$omp end parallel do
res = (n*sum_xy - sum_x*sum_y)/(n*sum_xx - sum_x*sum_x)
end subroutine
If a parallel sum function can be used, the subroutine will be much less verbose. Is there a plan of adding a parallel sum function to the language or std? C++ is already doing this, see:
https://en.cppreference.com/w/cpp/experimental/reduce