! --------------- ! Example program ! Save attribute ! --------------- program test implicit none integer, parameter:: n=3 integer:: i real(kind(1.d0)):: A(n) do do i = 1, n call random_number(A(i)) end do call collect(A,n) end do end program test ! --------------------------------- ! collect the average of 3D vectors ! --------------------------------- subroutine collect(vect,n) implicit none integer, intent(in):: n real(kind(1.d0)), intent(in)::vect(n) real(kind(1.d0)), allocatable, save, dimension(:):: vsum integer, save:: count=0 if(count==0) allocate(vsum(n)) vsum = vsum + vect count = count + 1 if(count> 100000000) stop '100000000 done' if(mod(count,100000)==0) write(*,'("count=",i0," average=",3(1x,f0.8))') count,vsum/count end subroutine collect