How to find the number of cores at runtime in Haskell

前端 未结 5 873
终归单人心
终归单人心 2021-02-13 06:16

Does Haskell have a method for determining the number of CPU cores present on a machine at runtime?

5条回答
  •  太阳男子
    2021-02-13 06:31

    You could copy'n'paste this code into a file called numCores and compile it with your Haskell code. Than you can use the FFI to import its definition and use it directly in your Haskell code:

    {-# LANGUAGE ForeignFunctionInterface #-}
    import Control.Applicative ((<$>))
    import Foreign.C.Types (CInt)
    
    foreign import ccall "getNumCores" c_getNumCores :: IO CInt
    getNumCores :: IO Int
    getNumCores = fromEnum <$> c_getNumCores
    

提交回复
热议问题