2

In order to maximize speed I am trying to vectorize the following (to enable the compiler to vectorize as it deems good):

integer  :: i,j
real :: a(4),b(4,5),c(4,5)
!... setting values to a and b...

do i=1,5
  do j=1,4
    c(j,i)=b(j,i)/a(i)
  end do
end do

I have tried the following

c=b/a

but that doesn't work:

error #6366: The shapes of the array expressions do not conform.

My thought was that since you can do a/i, (array / scalar), I was hoping that it was possible to do (2d array / array). To begin with the dimension of b and c were (5,4) and I thought that was the problem, that it needs to conform to the variable with smaller rank on the first ranks, but this didn't seem to be the case. As of now, I am wondering if it is at all possible??? Or do I have to stick with the do loops? (of course I could be satisfied with vectorize the inner loop)

Very happy with any comments or ideas with this. (I am using ifort 16 on windows)

3
  • 1
    c(j,:)=b(j,:)/a ? The compilers are normally happy to vectorize the loops so I wouldn't expect the performance to change too much. Commented Sep 1, 2015 at 13:28
  • yes .. that's what I though would be my option... or the opposite (because of column-major arrangement)... Ok, thanks Vladimir F Commented Sep 1, 2015 at 13:30
  • You might declare a(4,5) and initialize it appropriately, so that c=b/a would be correct. Your initial code is not right by the way as you declare a(4) and loop a(i) over i,5 Commented Sep 1, 2015 at 16:05

1 Answer 1

1

In case you haven't already got your answer, the seemingly non-vectorized code looks like this:

!Non-vectorized
do i=1,5
  do j=1,4
    c(j,i) = b(j,i) / a(j)
  enddo
enddo

and the seemingly vectorized version like this:

!Vectorized
do i=1,5
  c(:,i) = b(:,i) / a(:)
enddo

But the intel compiler vectorizes both of them. To make sure if a certain loop has been vectorized or not, use the flag -qopt-report-phase=vec. This generates the vectorization report about your compiled program and is a neat way of knowing if a certain loop has been vectorized or not.

The generated vectorization report of the above code is as shown:

.... Beginning stuff...
LOOP BEGIN at vect_div.f90(11,5)
remark #15542: loop was not vectorized: inner loop was already vectorized

 LOOP BEGIN at vect_div.f90(12,5)
    remark #15300: LOOP WAS VECTORIZED
 LOOP END
LOOP END

LOOP BEGIN at vect_div.f90(18,5)
remark #15542: loop was not vectorized: inner loop was already vectorized

 LOOP BEGIN at vect_div.f90(19,7)
    remark #15300: LOOP WAS VECTORIZED
 LOOP END
LOOP END

Here, (11,5), (12,5) etc. are the row and column numbers in my .f90 text file where the do keyword is present. As you may notice, the outer loops are not vectorized and the inner ones are. They are both vectorized without any noticeable difference too.

More detailed reports can be generated by changing the 'n' value in the ifort flag -qopt-report=[n]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.