Why does my program return inaccurate result?












2















I wrote the well-known spectral-norm algorithm in Fortran after I initially wrote (and optimized) it in MATLAB. The speedup after naive conversion to Fortran is at least 18X, but the problem is that the output of the Fortran program is not accurate. The correct output should be 1.274224153 but my Fortran program outputs 1.273722712, what am I doing wrong in Fortran?



program perf_spectralnorm
implicit none
integer, parameter :: n = 5500, dp = kind(0.d0)
real(dp) :: u(n) = 1, v(n), w(n), vBv, vv, res
integer :: i, j, nvec(n)

nvec = [(i, i=1,n)]
do i = 1,10
call Au(w, u) ! change w
call Atu(v, w) ! change v
call Au(w, v) ! change w
call Atu(u, w) ! change u
end do
vBv = dot_product(u, v)
vv = dot_product(v, v)
res = sqrt(vBv/vv)

print '(f12.9)', res

contains

elemental real(dp) function A(i, j)
integer, intent(in) :: i, j
A = 1.0_dp / ((i+j) * (i+j+1.0_dP)/2 + i + 1)
end

subroutine Au(w, u)
real(dp) :: w(:), u(:)
do i = 1,n
w(i) = dot_product(A(i-1,nvec-1) , u)
end do
end

subroutine Atu(v, w)
real(dp) :: v(:), w(:)
do i = 1,n
v(i) = dot_product(A(nvec-1,i-1) , w)
end do
end

end program perf_spectralnorm


My original implementation in MATLAB with correct output is as follows:



n = 5500; 
fprintf("%.9fn", perf_spectralnorm(n))

function res = A(i,j)
res = 1 ./ ((i+j) .* (i+j+1)/2 + i + 1);
end

function w = Au(u,w)
n = length(u);
j = 1:n;
for i = 1:n
w(i) = dot( A(i-1,j-1), u );
end
end

function v = Atu(w,v)
n = length(w);
j = 1:n;
for i = 1:n
v(i) = dot( A(j-1,i-1), w );
end
end

function res = perf_spectralnorm(n)
u = ones(n,1);
v = zeros(n,1);
w = zeros(n,1);
for i = 1:10
w = Au(u,w);
v = Atu(w,v);
w = Au(v,w);
u = Atu(w,u);
end
vBv = dot(u,v);
vv = dot(v,v);
res = sqrt(vBv/vv);
end









share|improve this question

























  • What Fortran compiler are you using, and have you turned on all debugging features? With gfortran and -fcheck=all -Wall, I get a runtime error "At line 8 of file a.f90 Fortran runtime error: Loop variable has been modified"

    – evets
    Nov 24 '18 at 20:07








  • 1





    Yep, subroutines Au and Atu USE variable i via host association. You need to locally declare i in these routines.

    – evets
    Nov 24 '18 at 20:10













  • @evets -- Thank you very much, you are right, I tend to use implicit variables (very bad habit) inside subroutines. In my case, the value of I in the main program propagated its value to the called subroutines. Please go a head and write an answer.

    – AboAmmar
    Nov 24 '18 at 20:18
















2















I wrote the well-known spectral-norm algorithm in Fortran after I initially wrote (and optimized) it in MATLAB. The speedup after naive conversion to Fortran is at least 18X, but the problem is that the output of the Fortran program is not accurate. The correct output should be 1.274224153 but my Fortran program outputs 1.273722712, what am I doing wrong in Fortran?



program perf_spectralnorm
implicit none
integer, parameter :: n = 5500, dp = kind(0.d0)
real(dp) :: u(n) = 1, v(n), w(n), vBv, vv, res
integer :: i, j, nvec(n)

nvec = [(i, i=1,n)]
do i = 1,10
call Au(w, u) ! change w
call Atu(v, w) ! change v
call Au(w, v) ! change w
call Atu(u, w) ! change u
end do
vBv = dot_product(u, v)
vv = dot_product(v, v)
res = sqrt(vBv/vv)

print '(f12.9)', res

contains

elemental real(dp) function A(i, j)
integer, intent(in) :: i, j
A = 1.0_dp / ((i+j) * (i+j+1.0_dP)/2 + i + 1)
end

subroutine Au(w, u)
real(dp) :: w(:), u(:)
do i = 1,n
w(i) = dot_product(A(i-1,nvec-1) , u)
end do
end

subroutine Atu(v, w)
real(dp) :: v(:), w(:)
do i = 1,n
v(i) = dot_product(A(nvec-1,i-1) , w)
end do
end

end program perf_spectralnorm


My original implementation in MATLAB with correct output is as follows:



n = 5500; 
fprintf("%.9fn", perf_spectralnorm(n))

function res = A(i,j)
res = 1 ./ ((i+j) .* (i+j+1)/2 + i + 1);
end

function w = Au(u,w)
n = length(u);
j = 1:n;
for i = 1:n
w(i) = dot( A(i-1,j-1), u );
end
end

function v = Atu(w,v)
n = length(w);
j = 1:n;
for i = 1:n
v(i) = dot( A(j-1,i-1), w );
end
end

function res = perf_spectralnorm(n)
u = ones(n,1);
v = zeros(n,1);
w = zeros(n,1);
for i = 1:10
w = Au(u,w);
v = Atu(w,v);
w = Au(v,w);
u = Atu(w,u);
end
vBv = dot(u,v);
vv = dot(v,v);
res = sqrt(vBv/vv);
end









share|improve this question

























  • What Fortran compiler are you using, and have you turned on all debugging features? With gfortran and -fcheck=all -Wall, I get a runtime error "At line 8 of file a.f90 Fortran runtime error: Loop variable has been modified"

    – evets
    Nov 24 '18 at 20:07








  • 1





    Yep, subroutines Au and Atu USE variable i via host association. You need to locally declare i in these routines.

    – evets
    Nov 24 '18 at 20:10













  • @evets -- Thank you very much, you are right, I tend to use implicit variables (very bad habit) inside subroutines. In my case, the value of I in the main program propagated its value to the called subroutines. Please go a head and write an answer.

    – AboAmmar
    Nov 24 '18 at 20:18














2












2








2


0






I wrote the well-known spectral-norm algorithm in Fortran after I initially wrote (and optimized) it in MATLAB. The speedup after naive conversion to Fortran is at least 18X, but the problem is that the output of the Fortran program is not accurate. The correct output should be 1.274224153 but my Fortran program outputs 1.273722712, what am I doing wrong in Fortran?



program perf_spectralnorm
implicit none
integer, parameter :: n = 5500, dp = kind(0.d0)
real(dp) :: u(n) = 1, v(n), w(n), vBv, vv, res
integer :: i, j, nvec(n)

nvec = [(i, i=1,n)]
do i = 1,10
call Au(w, u) ! change w
call Atu(v, w) ! change v
call Au(w, v) ! change w
call Atu(u, w) ! change u
end do
vBv = dot_product(u, v)
vv = dot_product(v, v)
res = sqrt(vBv/vv)

print '(f12.9)', res

contains

elemental real(dp) function A(i, j)
integer, intent(in) :: i, j
A = 1.0_dp / ((i+j) * (i+j+1.0_dP)/2 + i + 1)
end

subroutine Au(w, u)
real(dp) :: w(:), u(:)
do i = 1,n
w(i) = dot_product(A(i-1,nvec-1) , u)
end do
end

subroutine Atu(v, w)
real(dp) :: v(:), w(:)
do i = 1,n
v(i) = dot_product(A(nvec-1,i-1) , w)
end do
end

end program perf_spectralnorm


My original implementation in MATLAB with correct output is as follows:



n = 5500; 
fprintf("%.9fn", perf_spectralnorm(n))

function res = A(i,j)
res = 1 ./ ((i+j) .* (i+j+1)/2 + i + 1);
end

function w = Au(u,w)
n = length(u);
j = 1:n;
for i = 1:n
w(i) = dot( A(i-1,j-1), u );
end
end

function v = Atu(w,v)
n = length(w);
j = 1:n;
for i = 1:n
v(i) = dot( A(j-1,i-1), w );
end
end

function res = perf_spectralnorm(n)
u = ones(n,1);
v = zeros(n,1);
w = zeros(n,1);
for i = 1:10
w = Au(u,w);
v = Atu(w,v);
w = Au(v,w);
u = Atu(w,u);
end
vBv = dot(u,v);
vv = dot(v,v);
res = sqrt(vBv/vv);
end









share|improve this question
















I wrote the well-known spectral-norm algorithm in Fortran after I initially wrote (and optimized) it in MATLAB. The speedup after naive conversion to Fortran is at least 18X, but the problem is that the output of the Fortran program is not accurate. The correct output should be 1.274224153 but my Fortran program outputs 1.273722712, what am I doing wrong in Fortran?



program perf_spectralnorm
implicit none
integer, parameter :: n = 5500, dp = kind(0.d0)
real(dp) :: u(n) = 1, v(n), w(n), vBv, vv, res
integer :: i, j, nvec(n)

nvec = [(i, i=1,n)]
do i = 1,10
call Au(w, u) ! change w
call Atu(v, w) ! change v
call Au(w, v) ! change w
call Atu(u, w) ! change u
end do
vBv = dot_product(u, v)
vv = dot_product(v, v)
res = sqrt(vBv/vv)

print '(f12.9)', res

contains

elemental real(dp) function A(i, j)
integer, intent(in) :: i, j
A = 1.0_dp / ((i+j) * (i+j+1.0_dP)/2 + i + 1)
end

subroutine Au(w, u)
real(dp) :: w(:), u(:)
do i = 1,n
w(i) = dot_product(A(i-1,nvec-1) , u)
end do
end

subroutine Atu(v, w)
real(dp) :: v(:), w(:)
do i = 1,n
v(i) = dot_product(A(nvec-1,i-1) , w)
end do
end

end program perf_spectralnorm


My original implementation in MATLAB with correct output is as follows:



n = 5500; 
fprintf("%.9fn", perf_spectralnorm(n))

function res = A(i,j)
res = 1 ./ ((i+j) .* (i+j+1)/2 + i + 1);
end

function w = Au(u,w)
n = length(u);
j = 1:n;
for i = 1:n
w(i) = dot( A(i-1,j-1), u );
end
end

function v = Atu(w,v)
n = length(w);
j = 1:n;
for i = 1:n
v(i) = dot( A(j-1,i-1), w );
end
end

function res = perf_spectralnorm(n)
u = ones(n,1);
v = zeros(n,1);
w = zeros(n,1);
for i = 1:10
w = Au(u,w);
v = Atu(w,v);
w = Au(v,w);
u = Atu(w,u);
end
vBv = dot(u,v);
vv = dot(v,v);
res = sqrt(vBv/vv);
end






matlab fortran subroutine






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 19:51







AboAmmar

















asked Nov 24 '18 at 19:17









AboAmmarAboAmmar

439311




439311













  • What Fortran compiler are you using, and have you turned on all debugging features? With gfortran and -fcheck=all -Wall, I get a runtime error "At line 8 of file a.f90 Fortran runtime error: Loop variable has been modified"

    – evets
    Nov 24 '18 at 20:07








  • 1





    Yep, subroutines Au and Atu USE variable i via host association. You need to locally declare i in these routines.

    – evets
    Nov 24 '18 at 20:10













  • @evets -- Thank you very much, you are right, I tend to use implicit variables (very bad habit) inside subroutines. In my case, the value of I in the main program propagated its value to the called subroutines. Please go a head and write an answer.

    – AboAmmar
    Nov 24 '18 at 20:18



















  • What Fortran compiler are you using, and have you turned on all debugging features? With gfortran and -fcheck=all -Wall, I get a runtime error "At line 8 of file a.f90 Fortran runtime error: Loop variable has been modified"

    – evets
    Nov 24 '18 at 20:07








  • 1





    Yep, subroutines Au and Atu USE variable i via host association. You need to locally declare i in these routines.

    – evets
    Nov 24 '18 at 20:10













  • @evets -- Thank you very much, you are right, I tend to use implicit variables (very bad habit) inside subroutines. In my case, the value of I in the main program propagated its value to the called subroutines. Please go a head and write an answer.

    – AboAmmar
    Nov 24 '18 at 20:18

















What Fortran compiler are you using, and have you turned on all debugging features? With gfortran and -fcheck=all -Wall, I get a runtime error "At line 8 of file a.f90 Fortran runtime error: Loop variable has been modified"

– evets
Nov 24 '18 at 20:07







What Fortran compiler are you using, and have you turned on all debugging features? With gfortran and -fcheck=all -Wall, I get a runtime error "At line 8 of file a.f90 Fortran runtime error: Loop variable has been modified"

– evets
Nov 24 '18 at 20:07






1




1





Yep, subroutines Au and Atu USE variable i via host association. You need to locally declare i in these routines.

– evets
Nov 24 '18 at 20:10







Yep, subroutines Au and Atu USE variable i via host association. You need to locally declare i in these routines.

– evets
Nov 24 '18 at 20:10















@evets -- Thank you very much, you are right, I tend to use implicit variables (very bad habit) inside subroutines. In my case, the value of I in the main program propagated its value to the called subroutines. Please go a head and write an answer.

– AboAmmar
Nov 24 '18 at 20:18





@evets -- Thank you very much, you are right, I tend to use implicit variables (very bad habit) inside subroutines. In my case, the value of I in the main program propagated its value to the called subroutines. Please go a head and write an answer.

– AboAmmar
Nov 24 '18 at 20:18












1 Answer
1






active

oldest

votes


















5














Subroutines Au and Atu use a variable i for the do-loop through host association. This modified the do-loop variable i in main program, which is invalid. To solve the issue, you need to declare i as a local variable in Au and Atu. For example,



subroutine Au(w, u)
real(dp), intent(out) :: w(:)
real(dp), intent(in) :: u(:)
integer i
do i = 1, n
w(i) = dot_product(A(nvec-1,i-1), u)
end do
end


Note, I've taken the liberty to also include the INTENT of the dummy arguments.






share|improve this answer
























  • I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

    – Rodrigo Rodrigues
    Nov 25 '18 at 0:26













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461567%2fwhy-does-my-program-return-inaccurate-result%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Subroutines Au and Atu use a variable i for the do-loop through host association. This modified the do-loop variable i in main program, which is invalid. To solve the issue, you need to declare i as a local variable in Au and Atu. For example,



subroutine Au(w, u)
real(dp), intent(out) :: w(:)
real(dp), intent(in) :: u(:)
integer i
do i = 1, n
w(i) = dot_product(A(nvec-1,i-1), u)
end do
end


Note, I've taken the liberty to also include the INTENT of the dummy arguments.






share|improve this answer
























  • I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

    – Rodrigo Rodrigues
    Nov 25 '18 at 0:26


















5














Subroutines Au and Atu use a variable i for the do-loop through host association. This modified the do-loop variable i in main program, which is invalid. To solve the issue, you need to declare i as a local variable in Au and Atu. For example,



subroutine Au(w, u)
real(dp), intent(out) :: w(:)
real(dp), intent(in) :: u(:)
integer i
do i = 1, n
w(i) = dot_product(A(nvec-1,i-1), u)
end do
end


Note, I've taken the liberty to also include the INTENT of the dummy arguments.






share|improve this answer
























  • I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

    – Rodrigo Rodrigues
    Nov 25 '18 at 0:26
















5












5








5







Subroutines Au and Atu use a variable i for the do-loop through host association. This modified the do-loop variable i in main program, which is invalid. To solve the issue, you need to declare i as a local variable in Au and Atu. For example,



subroutine Au(w, u)
real(dp), intent(out) :: w(:)
real(dp), intent(in) :: u(:)
integer i
do i = 1, n
w(i) = dot_product(A(nvec-1,i-1), u)
end do
end


Note, I've taken the liberty to also include the INTENT of the dummy arguments.






share|improve this answer













Subroutines Au and Atu use a variable i for the do-loop through host association. This modified the do-loop variable i in main program, which is invalid. To solve the issue, you need to declare i as a local variable in Au and Atu. For example,



subroutine Au(w, u)
real(dp), intent(out) :: w(:)
real(dp), intent(in) :: u(:)
integer i
do i = 1, n
w(i) = dot_product(A(nvec-1,i-1), u)
end do
end


Note, I've taken the liberty to also include the INTENT of the dummy arguments.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 20:38









evetsevets

30215




30215













  • I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

    – Rodrigo Rodrigues
    Nov 25 '18 at 0:26





















  • I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

    – Rodrigo Rodrigues
    Nov 25 '18 at 0:26



















I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

– Rodrigo Rodrigues
Nov 25 '18 at 0:26







I've done some tests and, by making nvec a parameter, I got a 33% speed-up with gfortran -Ofast comparing with the presented solution. I have 3.39sec with my not-that-impressive i5 - 1.6GHz. The same result is obtained by using only array implied-do constructors instead of doloops and nvec.

– Rodrigo Rodrigues
Nov 25 '18 at 0:26




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461567%2fwhy-does-my-program-return-inaccurate-result%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks