Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran coding question

JohnNichols
소중한 기여자 III
1,279 조회수

So I have a loop that does some drawing.  I loops 48 times, but I only want to draw some things on the 2, 6, 10 etc loops 

I can solve the problem with an if and set of or statements. But is there a simpler code idea to do this?

0 포인트
1 솔루션
mecej4
명예로운 기여자 III
1,243 조회수
logical :: todraw(48) = .false.
!...
todraw([2,4,10]) = .true.
!...
where (todraw)
   !Draw
end where

OR, esp. if Draw requires index i

do i = 1,48
   if(.not. todraw(i))cycle
   !Draw
end do

원본 게시물의 솔루션 보기

4 응답
Arjen_Markus
명예로운 기여자 II
1,274 조회수

If your loop allows it, you could do:

do i = 2,48,4
   do the drawing ...
enddo

but that assumes the iteration does not depend on previous steps.

If there is a dependency, then something like this comes to mind:

do i = 1,48
    ... preparation ...
    if ( mod(i-2,4) == 0 ) then
       draw the stuff
    endif
enddo
jimdempseyatthecove
명예로운 기여자 III
1,246 조회수

Or when the draw intervals are non-fixed. Create a table

do i=1,48
  ...
  if(draw(i)) then
     draw the stuff
  endif
  ...
end do

Note, the above method, permits you to have code outside the do i loop to enable, disable, change what and when drawing occurs.

Note, with 48 possible conditions, I would probably use a bit mask in an integer(8) variable. For much larger ranges than 48 either a logical(1) array or an array of bit masks. The determination factor the impact on L1/L2 cache.

Jim Dempsey

mecej4
명예로운 기여자 III
1,244 조회수
logical :: todraw(48) = .false.
!...
todraw([2,4,10]) = .true.
!...
where (todraw)
   !Draw
end where

OR, esp. if Draw requires index i

do i = 1,48
   if(.not. todraw(i))cycle
   !Draw
end do
JohnNichols
소중한 기여자 III
1,233 조회수

They are all better than my solution.   

The problem with @negar  raised some interesting issues with our University teaching.  An awful lot of really large problems are coded in Fortran, but using the Civil Engineering schools for instance, the Prof wants to be quick and dirty so they teach Python.  I had an interesting discussion with one of these students who had just finished the class and he was telling me that it was not necessary to declare variables with a type because they were always promoted to the correct type. 

His code had an infinite loop and I pointed out he had made a simple mistake, an interesting discussion occurred where he said that his professor said that it was automatic -- I said you had an error you need to investigate it.  

We are creating in interesting world, what happens when all the old Fortran programmers are dead. 

Intel should run an International Competition in Fortran - say 100000 for the winner. 

 

0 포인트
응답