Explicitly calling vector libraries for vectorization

To use the vector technology in your applications, you can either rewrite the algorithm manually or rely on the automatic vectorization of the compiler. Although automatic vectorization can provide the highest performing solution, proper hand coding can also bring good performance.

The following example shows how to explicitly call the vector libraries to make use of the vector functionality provided by the target hardware.

function dotp(x,y,n) result(s)
       real*8 x(*),y(*),s
       vector(real(8)) sv,xv,yv
       integer i,n 

       sv = vec_splats(0.0D0)
       do i=1,n,2
           xv = vec_xld2(0,x(i))
           yv = vec_xld2(0,y(i))
           sv = vec_madd(xv,yv,sv)
       enddo
       s = vec_extract(sv,0)+vec_extract(sv,1)
       if (mod(n,2) .eq. 1) then
           s = s + x(n)*y(n)
       endif
end function 

program dot
       real*8 x(100),y(100),s
       integer i
       do i=1,100
           x(i)=0.5*i
           y(i)=2.0
       enddo
       s = dotp(x,y,100)
       print *,s
end  

The program performs the dot product for two arrays of REAL. At each iteration, two elements from the arrays are loaded into two REAL vector variables. The program then uses a multiply add operation to calculate the product of the two vectors and add the product with the previous sum. At the end of the loop the two elements of the vector that hold the partial sums are added to form the complete sum value. If the size of the input vectors do not evenly fit in the vector variables, a single scalar product is performed to complete the dot product computation.