Hallo everyone,

I have a problem concerning the performance of a program and have started suspecting that GHC's optimiser is confused by my types.
I have the following modules, types, and functions:

module MatrixSparse :

data Num a => Vector a = ...
data Num a => STMatrix s a = STMat !Int !Int !(STArray s Int (Vector a))

addSTcol :: Num a => a -> Int -> Int -> STMatrix s a -> ST s ()
addSTcol = ...


module MatrixMonad:

class (Num a, Monad (mc a)) => MatrixComp mc a where
    ...
    addCol ::  a -> Int -> Int -> mc a ()
    ...


module RealStateMatrixMonads :

newtype Num a => STMComp s a b = OMC (STMatrix s a -> ST s b)

instance Num a => Monad (STMComp s a) where 
    ... (feed the same (mutable) Matrix into everything)

instance Num a => MatrixComp (STMComp s) a where
    ...
    addCol=addColOMC
    ...	

addColOMC l m n = OMC $ addSTcol l m n


module Matrix :

makeLowerF :: (MatrixComp mc a, Fractional a) => mc a Int

makeLowerF = ...


Now, I want to specialise makeLowerF, so I add

{-# SPECIALIZE makeLowerF :: STMComp s Rational Int #-}

This seems (I have looked at dump-simpl) to achieve nothing.  The problem
seems to be that nothing of the information about the instances Monad (STMComp s a) and MatrixComp (STMComp s) a makes its way into RealStateMatrixMonads.hi. Is it possible, that some part of GHC is confused by the free s in STMComp s? With another instance of MatrixComp, namely
newtype StateComp a b = STC (a -> (a, b))
newtype OnlyMComp a b = OMC (StateComp (Matrix a) b)

instance Num a => MatrixComp OnlyMComp a where
    ...,
    
using non-mutable matrices, everything works fine (and therefore yields better performance, which means that I spent yesterday deoptimising my program :-()

I would really appreciate any help. Please ask, if you need any more information (ghc output, etc).


Some related questions. Some of them may be of the RTFM kind, but I hope they are not:

* Normally I would not import RealStateMatrixMonads in module Matrix, but I have to to be able to specialise. Is there any form of pragma import?

* If I want GHC to inline the (>>=) method of the instance Monad (STMComp s a), how do I name it? If I write
instance ... Monad (STMComp s a) where
    (>>=) = bindSTM
    ...
    
bindSTM = ...
{-# INLINE bindSTM #-},

does that do what I want, or does it only inline bindSTM into (>>=)?

* I somehow remember a compiler option that makes the compiler tell me what SPECIALIZE pragmas in other modules would be helpful. Am I dreaming?

* If I add an INLINE pragma, does that mean that SPECIALIZE pragmas for that function become unnecessary?

* If I add an {-# INLINE addSTcol #-} in module MatrixSparse and no paragmas in module RealStateMatrixMonads, does that make it less likely that addColOMC will be inlined in module Matrix?
* What should the information in RealStateMonads.hi look like?

 
Lots of questions, I know, so

Thank you very much in advance,

Carsten

---
Carsten Schultz, Fachbereich Mathematik, FU Berlin
http://www.math.fu-berlin.de/~cschultz/
"To match the silent eloquence of the created world 
 I have had to learn to speak."  Jeanette Winterson

Reply via email to