Counting spad function calls during a computation

2 views
Skip to first unread message

Grégory Vanuxem

unread,
May 28, 2024, 8:01:09 PM5/28/24
to fricas...@googlegroups.com
Hello,

Is there an already implemented way to count the number of Spad
function calls needed to compute a more general computation? I could
add a global counter to a routine/macro like spadcall or something
like that, but I am wondering if this was already done? I don't think
it is simple. I have seen in the code some comments about this but I
do not know how to do it exactly. As an illustration, in the Complex(R
: CommutativeRing) domain there is this comment:

Ops ==>
x + y == [x.real + y.real, x.imag + y.imag]
-- by re-defining this here, we save 5 fn calls
x - y == [x.real - y.real, x.imag - y.imag]
x : % * y : % ==
[x.real * y.real - x.imag * y.imag,
x.imag * y.real + y.imag * x.real] -- here we save nine!

if R is DoubleFloat then
-- we inline from DoubleFloat here
Ops
else
-- general case
Ops
- Greg

Waldek Hebisch

unread,
May 28, 2024, 11:05:23 PM5/28/24
to fricas...@googlegroups.com
On Wed, May 29, 2024 at 02:00:29AM +0200, Grégory Vanuxem wrote:
> Hello,
>
> Is there an already implemented way to count the number of Spad
> function calls needed to compute a more general computation?

No.

> I could
> add a global counter to a routine/macro like spadcall or something
> like that, but I am wondering if this was already done? I don't think
> it is simple.

Well, redefining SPADCALL to also increment a counter looks simple,
but of limited use. First, it is likely to measurably increase time
taken by SPADCALL, so it is not good for normal use.

> I have seen in the code some comments about this but I
> do not know how to do it exactly. As an illustration, in the Complex(R
> : CommutativeRing) domain there is this comment:
>
> Ops ==>
> x + y == [x.real + y.real, x.imag + y.imag]
> -- by re-defining this here, we save 5 fn calls

The person who wrote it probably measured execution time and looked why
it is slower than expected. Looking at generated code one can easily
see it operations are inline or done via SPADCALL. If there are
SPADCALL-s one may try to re-arrange code to elliminate them.

And it is easy to see why this helps: generic code would have to
use SPADCALL to acces 'real' and 'imag'. In 'Complex' representation
is visible so code can use access to record components which is
done by inline code.

--
Waldek Hebisch

Qian Yun

unread,
May 29, 2024, 5:13:50 AM5/29/24
to fricas...@googlegroups.com
Seems like ")trace" can count function calls:

)trace COMPLEX )math )count
complex(2,3)^2
)trace )stats


--------------------- Traced function execution counts ---------------------
Complex.*,33 : 1 times
Complex.+,31 : 0 times
Complex.-,32 : 0 times
Complex.OMwrite,20 : 0 times
Complex.coerce,29 : 0 times
Complex.complex,30 : 1 times
Complex.exquo,41 : 0 times
Complex.imag,15 : 7 times
Complex.one?,28 : 0 times
Complex.real,13 : 7 times
Complex.zero?,26 : 0 times


- Qian

Grégory Vanuxem

unread,
May 29, 2024, 5:15:49 AM5/29/24
to fricas...@googlegroups.com
Le mer. 29 mai 2024 à 05:05, Waldek Hebisch <de...@fricas.org> a écrit :
>
> On Wed, May 29, 2024 at 02:00:29AM +0200, Grégory Vanuxem wrote:
> > Hello,
> >
> > Is there an already implemented way to count the number of Spad
> > function calls needed to compute a more general computation?
>
> No.

Ok,

> > I could
> > add a global counter to a routine/macro like spadcall or something
> > like that, but I am wondering if this was already done? I don't think
> > it is simple.
>
> Well, redefining SPADCALL to also increment a counter looks simple,
> but of limited use. First, it is likely to measurably increase time
> taken by SPADCALL, so it is not good for normal use.

Of course, I would not use a hard-coded counter like that.

> > I have seen in the code some comments about this but I
> > do not know how to do it exactly. As an illustration, in the Complex(R
> > : CommutativeRing) domain there is this comment:
> >
> > Ops ==>
> > x + y == [x.real + y.real, x.imag + y.imag]
> > -- by re-defining this here, we save 5 fn calls
>
> The person who wrote it probably measured execution time and looked why
> it is slower than expected. Looking at generated code one can easily
> see it operations are inline or done via SPADCALL. If there are
> SPADCALL-s one may try to re-arrange code to elliminate them.
>
> And it is easy to see why this helps: generic code would have to
> use SPADCALL to acces 'real' and 'imag'. In 'Complex' representation
> is visible so code can use access to record components which is
> done by inline code.

Ok, thanks for this information.

- Greg

Grégory Vanuxem

unread,
May 29, 2024, 5:23:46 AM5/29/24
to fricas...@googlegroups.com
Yes Qian, exactly, i forgot those options, Thanks!

After, when different categories, domains, packages that I do not know
a priori if they will be involved, chosen by the interpreter, that is
another point. But for me that's a good starting point.

- Greg
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/dc568fe3-b55f-4a3c-aafe-d8bdbcd7853a%40gmail.com.
Reply all
Reply to author
Forward
0 new messages