Skip to content

Instantly share code, notes, and snippets.

@stephenjbarr
Last active December 20, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenjbarr/6132611 to your computer and use it in GitHub Desktop.
Save stephenjbarr/6132611 to your computer and use it in GitHub Desktop.
Example of doing a ton of numerical integrals in parallel using monad-par and hmatrix/gsl
import Numeric.LinearAlgebra
import Numeric.GSL
import Control.Exception
import System.Environment
import Data.Maybe
import Control.Monad.Par
import Control.DeepSeq
----------------------------------------
quad = integrateQNG 1E-6
myfn :: Double -> Double
myfn x = x^2
thefn x = quad myfn (fst x) (snd x)
lowers = [0.01,0.02..] :: [Double]
uppers = [0.1,0.2..] :: [Double]
my_bounds = zip lowers uppers
f2 x = quad myfn 0.0 x
----------------------------------------
main :: IO ()
main = do
let n = 20
print (length (runPar $ parMapChunk n thefn (take 8000 my_bounds)))
parMapChunk :: (Eq b, NFData b) => Int -> (a -> b) -> [a] -> Par [b]
parMapChunk n f xs = fmap concat $ parMap (map f) (chunk n xs)
chunk :: Int -> [a] -> [[a]]
chunk _ [] = []
chunk n xs = as : chunk n bs where (as,bs) = splitAt n xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment