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
Valued Contributor III
806 Views

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 Kudos
1 Solution
mecej4
Honored Contributor III
770 Views
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

View solution in original post

4 Replies
Arjen_Markus
Honored Contributor I
801 Views

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
Honored Contributor III
773 Views

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
Honored Contributor III
771 Views
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
Valued Contributor III
760 Views

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 Kudos
Reply