Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
speeding up matrix multiplication (newbie question)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-02-20 (18:46)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] speeding up matrix multiplication (newbie question)
> I'm working on speeding up some code, and I wanted to check with
> someone before implementation.
> As you can see below, the code primarily spends its time multiplying
> relatively small matrices. Precision is of course important but not
> an incredibly crucial issue, as the most important thing is relative
> comparison between things which *should* be pretty different.

You need to post your matrix multiplication code so that the regulars
on this list can tear it to pieces :-)

>From the profile you gave, it looks like you parameterized your matrix
multiplication code over the + and * operations over matrix elements.
This is good for genericity but not so good for performance, as it
will result in more boxing (heap allocation) of floating-point values.
The first thing you should try is write a version of matrix
multiplication that is specialized for type "float".

Then, there are several ways to write the textbook matrix
multiplication algorithm, some of which perform less boxing than
others.  Again, post your code and we'll let you know.

> Currently I'm just using native (double-precision) ocaml floats and
> the native ocaml arrays for a first pass on the problem.  Now I'm
> thinking about moving to using float32 bigarrays, and I'm hoping
> that the code will double in speed. I'd like to know: is that
> realistic? Any other suggestions?

It won't double in speed: arithmetic operations will take exactly the
same time in single or double precision.  What single-precision
bigarrays buy you is halving the memory footprint of your matrices.
That could result in better cache behavior and therefore slightly
better speed, but it depends very much on the sizes and number of your

- Xavier Leroy