- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page