Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29298 Discussions

ANY vs a DO loop (from a memory requirement perspective)

OP1
New Contributor III
747 Views
I don't know how the ANY function is implemented in Fortran. Let's assume I have a really large double precision matrix A. I want to detect if there is any element such as (for instance) A(i,j)>0.

I have several options. One is to use IF (ANY(A>0.0d+0)) THEN ... In this case, does that mean that a local copy of the logical expression A>0.0d+0 is placed on the stack and then examined(in which case an error would occur, given the size of A)? Or is ANY optimized and the elements are just examined one by one?
The syntax of ANYis elegant, but is it efficient? (I use it all the time for small arrays).
The other option is to create a double loop of course and examine the elements of A until the criteria is met.

What would be the best way to do this?

Thanks,

Olivier
0 Kudos
3 Replies
TimP
Honored Contributor III
747 Views
I assume you mean the array is large enough (500,500) to be treated by OpenMP parallel outer loop/ vector inner loop.
As OpenMP supports .or. as a reduction operator, you might want to restrict the use of ANY to sections of the array:
logical somepos
somepos = .false.
!$omp parallel do reduction(.or. : somepos) if(n>500)
do j=1,n
somepos = ANY(A(:,j) > 0) .or. somepos
enddo

I don't know whether any particular compiler is more likely to vectorize the inner loop by some scheme other than ANY(), nor do I know if any compiler supports reduction operations in autoparallel, such that there may be hope of avoiding OpenMP.

If you don't want it threaded, in principle, a compiler ought to be capable of optimizing it the way you wrote it, treating the entire array as 1D if there are no gaps. Allocation/population/deallocation of a local logical array to keep all the comparisons (even temporarily) would not be an efficient way to go about it. Vectorization probably implies a short logical vector array and accumulation of 4 or so batched results in each thread. If you were a fan of SSE2 intrinsics in C, you would have to write out the details.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
747 Views
Quoting - opmkl
I don't know how the ANY function is implemented in Fortran.

The syntax of ANYis elegant, but is it efficient? (I use it all the time for small arrays).
The other option is to create a double loop of course and examine the elements of A until the criteria is met.


I'd stick to ANY. I'm fairly certain -- but I can't be positive -- it does not create a temporary, at least not in the basic cases. If it weren't implemented that way (i.e. working the same as the do-loop), you would have strong case to complain about implementation.

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
747 Views
Without actually trying it, I'd guess that in some cases a temp would be created and not in others. The language semantics have the expression created and then "reduced". The compiler looks for common uses of this and tries to optimize.

I agree with Jugoslav - use ANY if it is clear to the reader what is wanted. I would not worry about stack usage unless you discovered that it was a problem - and if so, please let us know so that we can improve the compiler.
0 Kudos
Reply