Last active
December 20, 2015 12:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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