haskell-numerics / hmatrix

Linear algebra and numerical computation
381 stars 104 forks source link

maxIndex and maxElement copy Matricies #214

Open HuwCampbell opened 7 years ago

HuwCampbell commented 7 years ago

This makes them a bit slow.

I was using these in my neural network for the max pooling layer, and it was taking up to a quarter of the run time in a simple network just to do the copy before running the actual operation.

albertoruiz commented 7 years ago

This is strange. I think they use "toScalarR", etc., and only create a size-1 vector for the result in "toScalarAux". Unless there is a bug these functions should be very fast.

HuwCampbell commented 7 years ago

I don't think it's all the time, just when the (2D) matrix is column oriented due the the flatten inside maxIndex'.

https://github.com/albertoruiz/hmatrix/blob/2f773c0148a1a50b84226f69852997d53b0653fb/packages/base/src/Internal/Numeric.hs#L340

I have punched out to C with a ForeignPtr for my particular use case, so this isn't affecting me personally anymore.

albertoruiz commented 7 years ago

Ah, I see now. This implementation is very bad and must be changed. Thanks for your bug report!