[Rd] Speeding up matrix multiplies
Radford Neal
radford at cs.toronto.edu
Tue Aug 24 01:39:27 CEST 2010
Regarding my previous message on speeding up matrix multiplies, I've
realized that the size of the result matrix is not really the right
criterion for deciding to do the computation without using the Fortran
routine. A better criterion would be based on the ratio of the time
for the matrix multiply to the time for the ISNAN check. So I've
replaced my previous check by
/* Faster to just do it here if the time to do the matrix multiply
is not much greater than the time for the NA/NaN check below. */
do_it_here = nrx*ncy <= 5*(nrx+ncy);
In particular, all matrix x vector and vector x matrix products will
in this version be done in the matprod routine, not the Fortran routine.
And this is the right thing to do, since the time for the ISNAN check
before calling the Fortan routine will be comparable to the time for
the matrix multiply. So avoiding it will be desirable unless the Fortran
routine is really, really faster than the C code in matprod.
Of course, the real solution is to change the Fortran routine (which
seems to be in src/extra/blas/blas.f, and isn't all that complicated)
to handle NAs and NaNs as desired. Or to write a carefully optimized
C matrix multiply routine that does the right thing. For this,
knowing what the problem is supposed to be would help. My quick
attempt at using the bug reporting system didn't manage to find the
PR#4582 that is mentioned in the comment in the code, but I don't
really know how the bug report system is supposed to work.
Radford Neal
More information about the R-devel
mailing list