Den tir. 24. nov. 2020 kl. 15.12 skrev Dominik Pantůček <
dominik.pantu...@trustica.cz>:

> Hello,
>
> > If you are after numerical calculations for mxn matrices, then look at
> > `flomat`
> > which uses the same underlying C/Fortran library that NumPy uses.
> >
> > https://docs.racket-lang.org/manual-flomat/index.html
> >
> > If you need rrays with more dimensions than 2 use `math/array`.
> >
> > If you need simple computation of 1-dimensional data, you can use
> > standard Racket vectors (or flomats or arrays).
>
> I didn't know about `flomat'. Thanks for (yet another) good hint.
>
> However, for my project I needed really fast matrix multiplication (and
> other basic linear algebra functions). It turned out that most of the
> available options are sub-optimal at best. To my surprise `flomat` is
> one of those.
>
> Mostly I am concerned with 3D and 4D matrices and vectors.
>
> During the past few months I hacked together a fairly optimized
> module[1] for performing these operations that defines algebras for
> given dimension during macro expansion stage and all the procedures are
> constructed in a way that helps Racket (mainly CS) perform all
> operations unboxed.
>
> In the repository, there is a benchmark script, which yields the
> following (self-explanatory) results:
>
> ==== Welcome to Racket v7.9.0.5 [cs].
>
...

> What puzzles me the most: when I read `flomat' documentation, I thought
> it must beat my implementation by far margin - it's a native code for
> really basic number crunching. When using the `times!' variant of matrix
> multiplication, I would expect it to outperform anything implemented in
> in pure Racket.
>
> Is there something I miss or is it really the case, that carefully
> crafted Racket implementation is the fastest option right now?
>

I think the main reason that flomat matrices are slower than your flalgebra
ones
is the cost of the FFI call. The cost of calling a C foreign function
doesn't
depend on the size of the matrix though - so for large matrices the cost
is negligible. However for small matrices, it is faster to stay in the
Racket world.

I believe game libraries in C also implement their own 3x3 matrices rather
than using BLAS/LAPACK. BLAS can handle very large matrices and
makes an effort to give good results even for ill-conditioned matrices.


> I actually wanted to write a similar email some time ago - in the spring
> - about the typed math/matrix variant. But I was almost certain I am
> doing something wrong and should figure it out. Now I am more and more
> convinced that the performance of all number crunching libraries - as
> they can be used in Racket - might be the real issue here.
>

I have somewhat regretted that the matrices in math/matrix used math/array
arrays to represent matrices directly. A traditional approach would have
been
better. However math/matrix matrices are more general - they also work
when you need matrices over exact integers or over rationals.

In order from slowest to fastest (when used for floats over 3x3 matrices):

   math/matrix   (most general, very slow)
   flomat            (only floats, arbitrary size)
   flagebra         (floats, square only?)

To satisfy my curiosity, of the performance of flomat:

One way to get a little extra performance out of  flomat! is to use times!
instead of times. This avoids the repeated allocation. The code of times!
is:
[image: image.png]

For a more direct call to the C multiplication routine:

[image: image.png]
So `(flomat! A B C)` will compute A*B and store it in C.

Maybe there is a specialized A*B only routine in BLAS that
can be used instead?


The benchmarks suggest that the  `times` in flomat can be improved for
smaller matrices by calling specialized routines written in Racket.

If you (or anyone else) are interested in improving `flomat` for multiplying
smaller matrices, I would be glad to receive patches.

Btw - you have some fancy tricks in `flalgebra.rkt` !

/Jens Axel

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABefVgxaLO%2Bz%2BHm04iMFu5nrTPFH8KkusCGoSNyHH%3D_bdT3L9A%40mail.gmail.com.

Reply via email to