Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

allocatable function parameters and automatic vectorization

Can sb. explain the difference between passing pointer and allocatable subroutine parameters in Fortran? I do not understand why the following function does not vectorize in gfortran 7.2:

subroutine test0(fsm, im)
implicit none
real, dimension(:), pointer :: fsm
integer, intent(in) :: im
integer i

do i = 1,im
   fsm(i) = fsm(i)*2
end do
end subroutine test0

while it does vectorize (just like in C) if I use allocatable attribute for the dummy fsm argument. I compile using the following command line

gfortran -mavx -O3 -ftree-vectorize -c loops.f90 -fopt-info-vec-note

When using pointer gfortran reports a much higher vectorization cost. So is there an important difference in how the arguments are passed (e.g., indirection, pointer to pointer vs. passing by value), or is this a gfortran problem?

like image 937
angainor Avatar asked Jun 28 '26 09:06

angainor


1 Answers

A compiler may be able better to optimize when an object is contiguous. Vectorization here may, for example, be limited to the case where the object is known to be contiguous at compilation.

An array allocated by an allocate statement is always contiguous. A dummy argument which is a pointer array need not be contiguous. Here seems to be the difference observed.

However, an array pointer may be given the contiguous attribute. Such an array is then contiguous. A restriction on this pointer is that it may be pointer associated only with a contiguous target.

Similar experience may be had with assumed-shape arrays. Explicit shape arrays are also contiguous.

like image 97
francescalus Avatar answered Jul 01 '26 01:07

francescalus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!