How is Octave Faster than Fortran?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How is Octave Faster than Fortran?

evansste
Normally, Fortran is leaps and bounds faster than Octave.  However, I've
noticed that when performing similar matrix manipulations with Fortran's
"spread" function, compared to Octave's "repmat" function, Octave runs about
twice as fast as my compiled Fortran version of the program.  Is anyone able
to give an explanation as to why that is?  Is there something that I need to
be doing in order to increase Fortran's performance?

First, here's my simple Fortran program:

program block
    double precision, parameter, dimension(1000,500) :: A = reshape([ ...
],[1000,500])
    logical, dimension(:,:,:), allocatable :: blockL
    integer, dimension(2) :: Adim

    Adim = shape(A)
    blockL = spread(A,3,Adim(1))==spread(transpose(A),1,Adim(1))

end program block


Now here's my corresponding program, written in Octave:

A = [ ... ];  % This is the same "A" that was used in Fortran
Adim1 = size(A,1);
blockL = repmat(A,[1 1 Adim1])==repmat(permute(A,[3 2 1]),[Adim1 1 1]);


Once compiled, the Fortran program takes about fifteen seconds to run.  The
Octave program takes about eight.

If I modify the Octave program by getting rid of "repmat" altogether
(something I don't think can be done with Fortran),

A = [ ... ];  % This is the same "A" that was used in Fortran
Adim1 = size(A,1);
blockL = A==permute(A,[3 2 1]);


the Octave program only takes 1.5 seconds to complete.  That's ten times
faster than my compiled Fortran program!

Shouldn't a compiled program always be faster than an interpreted one?  Any
ideas on what I may be doing wrong, or how I could speed up my Fortran
program?

The only reason I've started converting my Octave programs to Fortran, is to
speed them up.  What is Octave doing that my Fortran program isn't?

Thanks so much for your time and attention.  I appreciate any guidance that
anyone is able, or willing, to provide.



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: How is Octave Faster than Fortran?

Octave - General mailing list
On 9/30/19 1:01 PM, evansste wrote:
> Normally, Fortran is leaps and bounds faster than Octave.  However, I've
> noticed that when performing similar matrix manipulations with Fortran's
> "spread" function, compared to Octave's "repmat" function, Octave runs about
> twice as fast as my compiled Fortran version of the program.  Is anyone able
> to give an explanation as to why that is?  Is there something that I need to
> be doing in order to increase Fortran's performance?

The slow part of Octave is the interpreter going over your .m code and
dispatching the Octave built-in primitive operations. Once Octave is
running those primitive operations, it is as fast as possible.

When you write Fortran by hand, you may end up being slower than Octave
low level primitives: for instance, because the data layout in Fortran
may be less cache-friendly.


>
> First, here's my simple Fortran program:
>
> program block
>      double precision, parameter, dimension(1000,500) :: A = reshape([ ...
> ],[1000,500])
>      logical, dimension(:,:,:), allocatable :: blockL
>      integer, dimension(2) :: Adim
>
>      Adim = shape(A)
>      blockL = spread(A,3,Adim(1))==spread(transpose(A),1,Adim(1))
>
> end program block
>
>
> Now here's my corresponding program, written in Octave:
>
> A = [ ... ];  % This is the same "A" that was used in Fortran
> Adim1 = size(A,1);
> blockL = repmat(A,[1 1 Adim1])==repmat(permute(A,[3 2 1]),[Adim1 1 1]);
>
>
> Once compiled, the Fortran program takes about fifteen seconds to run.  The
> Octave program takes about eight.
>
> If I modify the Octave program by getting rid of "repmat" altogether
> (something I don't think can be done with Fortran),
>
> A = [ ... ];  % This is the same "A" that was used in Fortran
> Adim1 = size(A,1);
> blockL = A==permute(A,[3 2 1]);
>
>
> the Octave program only takes 1.5 seconds to complete.  That's ten times
> faster than my compiled Fortran program!
>
> Shouldn't a compiled program always be faster than an interpreted one?  Any
> ideas on what I may be doing wrong, or how I could speed up my Fortran
> program?
>
> The only reason I've started converting my Octave programs to Fortran, is to
> speed them up.  What is Octave doing that my Fortran program isn't?
>
> Thanks so much for your time and attention.  I appreciate any guidance that
> anyone is able, or willing, to provide.
>
>
>
> --
> Sent from: https://gcc01.safelinks.protection.outlook.com/?url=https%3A%2F%2Foctave.1599824.n4.nabble.com%2FOctave-General-f1599825.html&data=02%7C01%7Cprzemek.klosowski%40nist.gov%7Cf50e2493c3164302802108d745c83fd5%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C1%7C637054598676539967&sdata=hWzo%2BT7EVoCOy8ybIp%2BrgAe82UazwYrNYhdPS2dlwug%3D&reserved=0
>
>