From 9556cf453f6c1499a2907acd72d7b11211387b0d Mon Sep 17 00:00:00 2001 From: hallgren Date: Fri, 11 Sep 2015 14:18:01 +0000 Subject: Parallel compilation: "gf -make -j" and "gf -make -j=n" now work as expected * "gf -make -j=n" uses n parallel threads. * "gf -make -j" adapts to the number of processors in the system. This mimics how "cabal build -j" and "ghc --make -j" works. Support for this is implemented in the new module GF.System.Concurrency and it depends on the function Control.Concurrent.setNumCapabilities, which is only available in GHC>=7.6 (base>=4.6). GF can still be compiled with GHC<7.6, but then you have to use +RTS -N -RTS to take advantage of multicore processors. To detect the number of processors in the system, the code depends on a foreign import of a C function in the GHC run-time system. --- src/compiler/GF/System/Concurrency.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/compiler/GF/System/Concurrency.hs (limited to 'src/compiler/GF/System') diff --git a/src/compiler/GF/System/Concurrency.hs b/src/compiler/GF/System/Concurrency.hs new file mode 100644 index 000000000..38e6559fc --- /dev/null +++ b/src/compiler/GF/System/Concurrency.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE CPP,ForeignFunctionInterface #-} +-- | A variant of 'Control.Concurrent.setNumCapabilities' that automatically +-- detects the number of processors in the system, and is available +-- even when compiling with GHC<7.6. +module GF.System.Concurrency( + -- * Controlling parallelism + setNumCapabilities,getNumberOfProcessors) where +import qualified Control.Concurrent as C +import Foreign.C.Types(CInt(..)) + + + + +-- | Set parallelism to a given number, or use the number of processors. +-- Returns 'False' if compiled with GHC<7.6 and the desired number of threads +-- hasn't already been set with @+RTS -N/n/ -RTS@. +setNumCapabilities opt_n = + do n <- maybe getNumberOfProcessors return opt_n +#if MIN_VERSION_base(4,6,0) + C.setNumCapabilities n + return True +#else + n_now <- C.getNumCapabilities + print (n,n_now) + return (n==n_now) +#endif + +-- | Returns the number of processors in the system. +getNumberOfProcessors = fmap fromEnum c_getNumberOfProcessors + +-- | According to comments in cabal-install cbits/getnumprocessors.c +-- this function is part of the RTS of GHC>=6.12. +foreign import ccall "getNumberOfProcessors" c_getNumberOfProcessors :: IO CInt -- cgit v1.2.3