I'm not explain what is different in this two code 1 written in c++ and the second in Fortran... I didn't get the point of the difference..
C++ :
# include <iosfwd>
# include <vector>
# include <cmath>
# include <iomanip>
# include <iostream>
std::vector<double> dot( int size, std::vector<double> x,
std::vector<double> aa, std::vector<int> ja)
{
std::vector<double> y(x.size());
for(auto i = 0; i < size ; i++ )
y.at(i) = aa.at(i) * x.at(i);
for(auto i=0 ; i < size ; i++ )
{
//for(auto k=ja.at(i) ; k< ja.at(i+1)-1 ; k++ )
auto k = ja.at(i);
do
{
y.at(i) += y.at(i) + aa.at(k) * x.at(ja.at(k)) ;
k++;
}
while(k < ja.at(i+1)-1) ;
}
}
int main()
{
std::vector<double> x = {0.,1.3,4.2,0.8} ;
std::vector<double> aa = {1.01,4.07,6.08,9.9,0.,2.34,3.12,1.06,2.2};
std::vector<int> ja = {6,7,7,8,10,3,1,1,3};
std::vector<double> y = dot(x.size(), x , aa , ja);
for(auto& i : x)
std::cout << i << ' ' ;
std::cout << std::endl;
}
Fortran code, I know that c++ start the index at 0 and Fortran at 1 but I think that I've consider this! by the way the right one is the fortran code as follow :
MODULE MSR
IMPLICIT NONE
CONTAINS
subroutine amuxms (n, x, y, a,ja)
real*8 x(*), y(*), a(*)
integer n, ja(*)
integer i, k
do 10 i=1, n
y(i) = a(i)*x(i)
10 continue
do 100 i = 1,n
do 99 k=ja(i), ja(i+1)-1
y(i) = y(i) + a(k) *x(ja(k))
99 continue
100 continue
return
end
END MODULE
PROGRAM MSRtest
USE MSR
IMPLICIT NONE
INTEGER :: i
REAL(KIND(0.D0)), DIMENSION(4) :: y, x= (/0.,1.3,4.2,0.8/)
REAL(KIND(0.D0)), DIMENSION(9) :: AA = (/ 1.01, 4.07, 6.08, 9.9, 0., 2.34, 3.12, 1.06, 2.2/)
INTEGER , DIMENSION(9) :: JA = (/6, 7, 7, 8, 10, 3, 1, 1, 3/)
CALL amuxms(4,x,y,aa,ja)
WRITE(6,FMT='(4F8.3)') (y(I), I=1,4)
END PROGRAM
I've solved I made a rough mistake ! using ja_
as index .. I forgot to subtrac 1 !
so the working function is :
# include <iosfwd>
# include <vector>
# include <cmath>
# include <iomanip>
# include <iostream>
std::vector<double> dot( int size, std::vector<double> x,
std::vector<double> aa, std::vector<int> ja)
{
std::vector<double> y(x.size());
for(auto i = 0; i < size ; i++ )
y.at(i) = aa.at(i) * x.at(i);
for(auto i=0 ; i < size ; i++ )
{
auto k = ja.at(i)-1;
do
{
y.at(i) += aa.at(k) * x.at(ja.at(k)-1) ;
k++;
}
while(k < ja.at(i+1)-1) ;
}
return y;
}
the difference of two codes is inside the use of ja_(index)
as index of other vector without taking into account that in the Fortran code ja_(1)
give us the first value of ja_
in the c++ the second ! @Vladimir F it is now clear ?