1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
{-# OPTIONS_GHC -funbox-strict-fields -O #-}
{-# LANGUAGE ExistentialQuantification #-}
{- OPTIONS_GHC -ddump-simpl -ddump-asm -}
module Main (main) where
import GHC.Float (float2Int, int2Float)
import System.Environment
import Prelude hiding (null
,lines,unlines
,writeFile
)
import Control.Exception (assert, bracket, )
import Foreign.Marshal.Array (advancePtr)
import Foreign.Ptr (minusPtr)
import Foreign.Storable (Storable(..))
import Control.Monad (when)
import System.IO (openBinaryFile, hClose,
hPutBuf,
Handle, IOMode(..))
import System.IO.Unsafe (unsafePerformIO)
import Foreign.Ptr (Ptr)
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, )
import Foreign.Marshal.Array (copyArray)
import qualified Foreign.ForeignPtr as F
main :: IO ()
main = do args <- getArgs
case args of
["1"] -> mainMonolithic1Generator
["2"] -> mainMonolithic1Composed
_ -> error "Huh?"
type Phase = (Float, Float, Float)
{-# INLINE saw #-}
saw :: Num a => a -> a
saw t = 1-2*t
{-# INLINE fraction #-}
fraction :: Float -> Float
fraction x = x - int2Float (float2Int x)
{-# INLINE generator0Freq #-}
generator0Freq :: Float -> Float -> Maybe (Float, Float)
generator0Freq freq =
\p -> Just (saw p, fraction (p+freq))
infixl 6 `mix`, `mixGen`
{-# INLINE mix #-}
mix ::
(Num y) =>
(s -> Maybe (y, s)) ->
(t -> Maybe (y, t)) ->
((s,t) -> Maybe (y, (s,t)))
mix f g (s0,t0) =
do (a,s1) <- f s0
(b,t1) <- g t0
return ((a+b), (s1,t1))
data Generator a =
forall s.
Generator (s -> Maybe (a, s)) s
{-# INLINE runGeneratorMonolithic #-}
runGeneratorMonolithic :: Int -> Generator Float -> Vector Float
runGeneratorMonolithic size' (Generator f s) =
fst $ unfoldrN size' f s
{- SPECIALISE INLINE generator0Gen :: Float -> Float -> Generator Float -}
{-# INLINE generator0Gen #-}
generator0Gen :: Float -> Float -> Generator Float
generator0Gen freq phase =
Generator (\p -> Just (saw p, fraction (p+freq))) phase
{- SPECIALISE INLINE mixGen :: Generator Float -> Generator Float -> Generator Float -}
{-# INLINE mixGen #-}
mixGen ::
(Num y) =>
Generator y ->
Generator y ->
Generator y
mixGen (Generator f s) (Generator g t) =
Generator (\(s0,t0) ->
do (a,s1) <- f s0
(b,t1) <- g t0
return ((a+b), (s1,t1))) (s,t)
{-# INLINE dl #-}
dl :: Phase
dl = (0.01008, 0.01003, 0.00990)
{-# INLINE initPhase2 #-}
initPhase2 :: (Phase, Phase)
initPhase2 =
((0,0.7,0.1), (0.3,0.4,0.6))
size :: Int
size = 10000000
mainMonolithic1Composed :: IO ()
mainMonolithic1Composed =
writeFile "speed.f32"
(fst $ unfoldrN size
(let (f0,f1,f2) = dl
in generator0Freq f0 `mix`
generator0Freq f1 `mix`
generator0Freq f2)
(let (p0,p1,p2) = fst initPhase2
in ((p0,p1),p2)))
mainMonolithic1Generator :: IO ()
mainMonolithic1Generator =
writeFile "speed.f32"
(runGeneratorMonolithic size
(let (f0,f1,f2) = dl
(p0,p1,p2) = fst initPhase2
in generator0Gen f0 p0 `mixGen`
generator0Gen f1 p1 `mixGen`
generator0Gen f2 p2))
empty :: (Storable a) => Vector a
empty = unsafeCreate 0 $ const $ return ()
{-# NOINLINE empty #-}
null :: Vector a -> Bool
null (SV _ _ l) = assert (l >= 0) $ l <= 0
{-# INLINE null #-}
unfoldrN :: (Storable b) => Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN n f x0 =
if n <= 0
then (empty, Just x0)
else unsafePerformIO $ createAndTrim' n $ \p -> go p n x0
where
go = arguments2 $ \p i -> \x ->
if i == 0
then return (0, n-i, Just x)
else
case f x of
Nothing -> return (0, n-i, Nothing)
Just (w,x') -> do poke p w
go (incPtr p) (i-1) x'
{-# INLINE unfoldrN #-}
hPut :: (Storable a) => Handle -> Vector a -> IO ()
hPut h v =
when (not (null v)) $
withStartPtr v $ \ ptrS l ->
let ptrE = advancePtr ptrS l
in hPutBuf h ptrS (minusPtr ptrE ptrS)
writeFile :: (Storable a) => FilePath -> Vector a -> IO ()
writeFile f txt =
bracket (openBinaryFile f WriteMode) hClose
(\h -> hPut h txt)
data Vector a = SV {-# UNPACK #-} !(ForeignPtr a)
{-# UNPACK #-} !Int -- offset
{-# UNPACK #-} !Int -- length
withStartPtr :: Storable a => Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr (SV x s l) f =
withForeignPtr x $ \p -> f (p `advancePtr` s) l
{-# INLINE withStartPtr #-}
incPtr :: (Storable a) => Ptr a -> Ptr a
incPtr v = advancePtr v 1
{-# INLINE incPtr #-}
unsafeCreate :: (Storable a) => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate l f = unsafePerformIO (create l f)
{-# INLINE unsafeCreate #-}
create :: (Storable a) => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create l f = do
fp <- mallocForeignPtrArray l
withForeignPtr fp $ \p -> f p
return $! SV fp 0 l
createAndTrim' :: (Storable a) => Int
-> (Ptr a -> IO (Int, Int, b))
-> IO (Vector a, b)
createAndTrim' l f = do
fp <- mallocForeignPtrArray l
withForeignPtr fp $ \p -> do
(off, l', res) <- f p
if assert (l' <= l) $ l' >= l
then return $! (SV fp 0 l, res)
else do ps <- create l' $ \p' -> copyArray p' (p `advancePtr` off) l'
return $! (ps, res)
{-# INLINE arguments2 #-}
arguments2 :: (a -> b -> x) -> a -> b -> x
arguments2 f = \a b -> (f $! a) $! b
{-# INLINE mallocForeignPtrArray #-}
mallocForeignPtrArray :: Storable a => Int -> IO (F.ForeignPtr a)
mallocForeignPtrArray = F.mallocForeignPtrArray
|