Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you have to use both a compiler flag and a run-time flag to get multicore-support in Haskell?

The Haskell wiki shows that you need to both set a compilation flag and a run-time flag to get multi-core support. Why isn't using the library enough to get the correct behavior at compile time? Why can't the run-time executable detect it was compiled with -threaded and use all cores on the system unless otherwise specified? I think turning these on by default would be better. Then there could be flags to turn off or modify these features.

http://www.haskell.org/haskellwiki/GHC/Concurrency#Multicore_GHC says:

  • Compile your program using the -threaded switch.
  • Run the program with +RTS -N2 to use 2 threads, for example. You should use a -N value equal to the number of CPU cores on your machine (not including Hyper-threading cores).


    It seems somewhat onerous to have flags one must set both at compile time and again at run time. Are these flags vestigial remains of the effort to add concurrency to GHC?

  • like image 471
    Tim Perry Avatar asked Sep 13 '10 21:09

    Tim Perry


    People also ask

    What is the purpose of the flag when compiling?

    Compile-time flags are boolean values provided through the compiler via a macro method. They allow to conditionally include or exclude code based on compile time conditions. There are several default flags provided by the compiler with information about compiler options and the target platform.

    What is a runtime flag?

    Runtime flags enable Blink developers the ability to control access Chromium users have to new features they implement. Features that are hidden behind a runtime flag are known as Runtime Enabled Features.


    3 Answers

    While you're developing the program the extra +RTS ... shouldn't be a big deal (though I admit it struck me as odd when I first picked up Haskell). For the final (shipped) binary you can link it with static RTS options (GHC manual) by providing a C file containing char *ghc_rts_opts = "-N";.

    EDIT: Updating this question for GHC 7.x, there is now a way to specify RTS options at compile time:

    ghc -threaded -rtsopts -with-rtsopts=-N
    

    This 1) uses the threaded runtime system 2) Enables the RTS options 3) Sets the RTS option to use as many threads as there are cores available (use -Nx where x is a number to manually control the number of OS threads).

    like image 156
    Thomas M. DuBuisson Avatar answered Sep 22 '22 06:09

    Thomas M. DuBuisson


    Why can't the run-time executable detect it was compiled with -threaded and use all cores on the system unless otherwise specified?

    That's an interesting feature request!

    You could ask for it on the GHC feature tracker: http://hackage.haskell.org/trac/ghc/wiki/ReportABug

    like image 26
    Don Stewart Avatar answered Sep 20 '22 06:09

    Don Stewart


    From GHC User guide (version 6.12.1):

    Omitting x, i.e. +RTS -N -RTS, lets the runtime choose the value of x itself based on how many processors are in your machine.

    I suppose there's no specific reason for this not to be the default, apart from authors' vision of what should defaults be. (Note that this also enables parallel GC, which maybe sometimes isn't what you wish to be by default.)

    like image 21
    Roman Cheplyaka Avatar answered Sep 19 '22 06:09

    Roman Cheplyaka