Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multicore parallelism with stack runghc

I had been working on a script in which I hoped to take advantage of multiple processors in my machine by swapping out mapM with Async.mapConcurrently.

Observing no speed increase in that instance, I wanted to verify that runghc can indeed utilize multiple cores.

Given a file Foo.hs:

import Control.Concurrent

main = print =<< Control.Concurrent.getNumCapabilities

If I compile the file as follows:

stack ghc -- -threaded Foo.hs

and then run it as follows:

./Foo

it returns the result 1. This is expected, as no RTS options have been supplied. Running it instead as follows:

./Foo +RTS -N

returns the number 6, as there are 6 processors in my machine (agreeing with nproc).

However, when I run the script in "interpreted mode" like so:

GHCRTS="-N" stack runghc Foo.hs

It yields the following error text:

Running /home/kostmo/.stack/programs/x86_64-linux/ghc-nopie-8.0.2/bin/ghc-pkg --no-user-package-db list --global exited with ExitFailure 1

ghc-pkg: the flag -N requires the program to be built with -threaded

Is it possible to utilize multiple cores with stack "scripts"?

like image 471
kostmo Avatar asked Sep 20 '17 00:09

kostmo


1 Answers

Thanks for asking this question, I think stack should handle the GHCRTS environment variable specially, and opened this issue https://github.com/commercialhaskell/stack/issues/3444 and made this change https://github.com/commercialhaskell/stack/pull/3445

Unfortunately, it does not solve this case, because runghc itself (ghc) will process GHCRTS, and it is not built with the threaded runtime. So the environment variable solution cannot be used.

I think it should be possible to provide -with-rtsopts -N flag to stack script --compile, but that doesn't seem to be working, needs further investigation. This doesn't work with runghc, because it uses the interpreter.

like image 132
mgsloan Avatar answered Oct 06 '22 10:10

mgsloan