{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Data.Array.Accelerate.Debug.Trace (
showFFloatSIBase,
putTraceMsg,
trace, traceIO,
traceEvent, traceEventIO,
) where
import Data.Array.Accelerate.Debug.Flags
import Numeric
#ifdef ACCELERATE_DEBUG
import Data.Array.Accelerate.Debug.Clock
import System.IO.Unsafe
import Text.Printf
import qualified Debug.Trace as D
#endif
showFFloatSIBase :: RealFloat a => Maybe Int -> a -> a -> ShowS
showFFloatSIBase :: Maybe Int -> a -> a -> ShowS
showFFloatSIBase Maybe Int
prec !a
base !a
k
= String -> ShowS
showString
(String -> ShowS) -> String -> ShowS
forall a b. (a -> b) -> a -> b
$ case Int
pow of
Int
4 -> ShowS
with String
"T"
Int
3 -> ShowS
with String
"G"
Int
2 -> ShowS
with String
"M"
Int
1 -> ShowS
with String
"k"
-1 -> ShowS
with String
"m"
-2 -> ShowS
with String
"µ"
-3 -> ShowS
with String
"n"
-4 -> ShowS
with String
"p"
Int
_ -> Maybe Int -> a -> ShowS
forall a. RealFloat a => Maybe Int -> a -> ShowS
showGFloat Maybe Int
prec a
k String
" "
where
!k' :: a
k' = a
k a -> a -> a
forall a. Fractional a => a -> a -> a
/ (a
base a -> Int -> a
forall a b. (Fractional a, Integral b) => a -> b -> a
^^ Int
pow)
!pow :: Int
pow = a -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (a -> a -> a
forall a. Floating a => a -> a -> a
logBase a
base a
k) :: Int
with :: ShowS
with String
unit = Maybe Int -> a -> ShowS
forall a. RealFloat a => Maybe Int -> a -> ShowS
showFFloat Maybe Int
prec a
k' (Char
' 'Char -> ShowS
forall a. a -> [a] -> [a]
:String
unit)
trace :: Flag -> String -> a -> a
#ifdef ACCELERATE_DEBUG
{-# NOINLINE trace #-}
trace f msg expr = unsafePerformIO $ do
traceIO f msg
return expr
#else
{-# INLINE trace #-}
trace :: Flag -> String -> a -> a
trace Flag
_ String
_ a
expr = a
expr
#endif
traceIO :: Flag -> String -> IO ()
#ifdef ACCELERATE_DEBUG
traceIO f msg = when f $ putTraceMsg msg
#else
{-# INLINE traceIO #-}
traceIO :: Flag -> String -> IO ()
traceIO Flag
_ String
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
#endif
traceEvent :: Flag -> String -> a -> a
#ifdef ACCELERATE_DEBUG
{-# NOINLINE traceEvent #-}
traceEvent f msg expr = unsafePerformIO $ do
traceEventIO f msg
return expr
#else
{-# INLINE traceEvent #-}
traceEvent :: Flag -> String -> a -> a
traceEvent Flag
_ String
_ a
expr = a
expr
#endif
putTraceMsg :: String -> IO ()
#ifdef ACCELERATE_DEBUG
putTraceMsg msg = do
timestamp <- getProgramTime
D.traceIO $ printf "[%8.3f] %s" timestamp msg
#else
putTraceMsg :: String -> IO ()
putTraceMsg String
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
#endif
traceEventIO :: Flag -> String -> IO ()
#ifdef ACCELERATE_DEBUG
traceEventIO f msg = do
when f $ D.traceEventIO msg
#else
{-# INLINE traceEventIO #-}
traceEventIO :: Flag -> String -> IO ()
traceEventIO Flag
_ String
_ = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
#endif