I want to process a couple of hundred binary data chunks ("scenarios") for a Monte Carlo simulation. Each scenario consists of 1 million floats. Here's how I create a dummy binary file for the scenario data:
import Data.Binary
import qualified Data.ByteString.Lazy as B
import Data.Array.Unboxed
scenSize = 1000000
scens = 100
main = do
let xs = array (1,scenSize) [(i, 0.0) | i <- [1..scenSize]] :: UArray Int Float
let l = take scens $ Prelude.repeat xs
B.writeFile "bintest.data" (encode l)
return ()
This works fine. Now I want to process the scenarios. Since there can be really a lot of scenarios (scens=1000 or so), the processing should be done lazily one chunk at a time. I tried decodeFile
, but this does not seem to work:
import Data.Binary
import qualified Data.Array.IArray as IA
import Data.Array.Unboxed as A
main = do
bs <- decodeFile "bintest.data" :: IO [UArray Int Float]
mapM_ doStuff bs
return ()
doStuff b =
Prelude.putStrLn $ show $ b IA.! 100000
This program seems to first load all the data in memory, and then prints all the numbers at the end of the run. It also uses a lot of memory and crashes for scens=500 on my 32-bit Ubuntu machine.
What am I doing wrong? Is there an easy way to make the program run lazily?