View previous topic :: View next topic |
Author |
Message |
Kenneth_Smith
Joined: 18 May 2012 Posts: 801 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Thu Jan 23, 2025 12:58 am Post subject: Array constructor issue |
|
|
Paul,
One for you to look at as I think the following code demonstrates a bug with FTN95.
Perhaps I should temper my present enthusiasum for eliminating do loops!
Code: | program p
implicit none
integer, parameter :: n = 1000
real*8 :: x(n), random_y(n), mean_y(n)
integer :: i
! Generate random data, a sample count in x, and running mean_y
call random_number(random_y)
x = [(i, i = 1, n)]
!do i = 1, n
! mean_y(i) = sum(random_y(1:i))/x(i)
!end do
! When the do loop above is replaced by the following "equivalent code" which
! makes use of an array constructor FTN 9.06 returns the running mean values
! as all zero.
! FTN 9.03 also fails with this code.
! Checkmate reports Error 112 Reference to undefined variable.
! All variables on the RHS have previously been assigned values.
mean_y = [(sum(random_y(1:i)) / x(i), i = 1, n)]
do i = 1, n
print*, i, mean_y(i)
end do
end program p |
|
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Thu Jan 23, 2025 1:50 am Post subject: |
|
|
Ken,
I am using Version 9.05 /64 /debug and get an access violation error on the array constructor.
I also tried the following with the same outcome.
sum_y = [ ( sum(random_y(1:i) ), i = 1, n) ]
Both work with Gfortran
A DO loop would be much quicker !
I should update to Ver 9.06 and see the result. |
|
Back to top |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 801 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Thu Jan 23, 2025 12:19 pm Post subject: |
|
|
John,
Thanks for confirming that there is a FTN95 issue with this
Yes, you are correct, for large n the repeated calculation of sum( random_y( 1:i ) ) will be crippling in the array constructor.
This would indeed be much faster:
Code: | acc = 0.d0
do i = 1, n, 1
acc = acc + random_y(i)
mean_y(i) = acc / x(i)
end do |
|
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8182 Location: Salford, UK
|
Posted: Thu Jan 23, 2025 12:47 pm Post subject: |
|
|
Ken
It looks like this approach has defeated FTN95. I have made a note to investigate. |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8182 Location: Salford, UK
|
Posted: Wed May 07, 2025 11:25 am Post subject: |
|
|
This failure has now been fixed for the next release of FTN95. |
|
Back to top |
|
 |
Kenneth_Smith
Joined: 18 May 2012 Posts: 801 Location: Hamilton, Lanarkshire, Scotland.
|
Posted: Thu May 08, 2025 9:59 am Post subject: |
|
|
Paul,
Thanks for this and other recent fixes. |
|
Back to top |
|
 |
|