feval vs. Ffunction_name in C++

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

feval vs. Ffunction_name in C++

Rik-4
jwe,

Happened to be looking at the code for the dot product in dot.cc today. 
There is an if/elseif tree to dispatch complex/real and single/double
inputs directly to Fortran routines.  However, for other cases, like int8,
there is an unoptimized path as shown below:

--- Start Code ---
  else
    {
      // Non-optimized evaluation.
      octave_value_list tmp;
      tmp(1) = dim + 1;
      tmp(0) = do_binary_op (octave_value::op_el_mul, argx, argy);

      tmp = octave::feval ("sum", tmp, 1);
      if (! tmp.empty ())
        retval = tmp(0);
    }
--- End Code ---

Do we need to use feval in this case or would it be slightly more efficient
to just call Fsum directly?

Presumably this code in dot.cc is never called if someone has overloaded
the dot function for their class.  I suppose it is possible that someone
has overloaded sum, but why wouldn't you just overload the base
mathematical functions plus() and then rely on Fsum to do the right thing?

--Rik




Reply | Threaded
Open this post in threaded view
|

Re: feval vs. Ffunction_name in C++

John W. Eaton
Administrator
On 1/22/19 1:13 PM, Rik wrote:

> jwe,
>
> Happened to be looking at the code for the dot product in dot.cc today.
> There is an if/elseif tree to dispatch complex/real and single/double
> inputs directly to Fortran routines.  However, for other cases, like int8,
> there is an unoptimized path as shown below:
>
> --- Start Code ---
>    else
>      {
>        // Non-optimized evaluation.
>        octave_value_list tmp;
>        tmp(1) = dim + 1;
>        tmp(0) = do_binary_op (octave_value::op_el_mul, argx, argy);
>
>        tmp = octave::feval ("sum", tmp, 1);
>        if (! tmp.empty ())
>          retval = tmp(0);
>      }
> --- End Code ---
>
> Do we need to use feval in this case or would it be slightly more efficient
> to just call Fsum directly?
>
> Presumably this code in dot.cc is never called if someone has overloaded
> the dot function for their class.

Correct.

> I suppose it is possible that someone
> has overloaded sum, but why wouldn't you just overload the base
> mathematical functions plus() and then rely on Fsum to do the right thing?

There is no reason to expect that overloading sum would affect dot as
there is no guarantee that dot is implemented by calling sum.

I think the reason for using feval here is just that the code was
written before we provided declarations for built-in functions and so it
was recommended to use feval instead.

jwe