4 <nidx>GHC, using</nidx>
7 GHC is a command-line compiler: in order to compile a Haskell program,
8 GHC must be invoked on the source file(s) by typing a command to the
9 shell. The steps involved in compiling a program can be automated
10 using the @make@ tool (this is especially useful if the program
11 consists of multiple source files which depend on each other). This
12 section describes how to use GHC from the command-line.
14 %************************************************************************
16 <sect1> Overall command-line structure
17 <label id="command-line-structure">
19 <nidx>structure, command-line</nidx>
20 <nidx>command-line structure</nidx>
22 %************************************************************************
24 An invocation of GHC takes the following form:
30 Command-line arguments are either options or file names.
32 Command-line options begin with @-@. They may <em>not</em> be
33 grouped: @-vO@ is different from @-v -O@. Options need not
34 precede filenames: e.g., @ghc *.o -o foo@. All options are
35 processed and then applied to all files; you cannot, for example, invoke
36 @ghc -c -O1 Foo.hs -O2 Bar.hs@ to apply different optimisation
37 levels to the files @Foo.hs@ and @Bar.hs@. For conflicting
38 options, e.g., @-c -S@, we reserve the right to do anything we
39 want. (Usually, the last one applies.)
41 %************************************************************************
43 <sect1>Meaningful file suffixes
44 <label id="file-suffixes">
46 <nidx>suffixes, file</nidx>
47 <nidx>file suffixes for GHC</nidx>
49 %************************************************************************
51 File names with ``meaningful'' suffixes (e.g., @.lhs@ or @.o@)
52 cause the ``right thing'' to happen to those files.
56 <nidx>lhs suffix</nidx>
57 A ``literate Haskell'' module.
60 A not-so-literate Haskell module.
63 A Haskell interface file, probably compiler-generated.
66 Intermediate C file produced by the Haskell compiler.
69 A C~file not produced by the Haskell compiler.
72 % C code after it has be preprocessed by the C compiler (using the
76 An assembly-language source file, usually
77 produced by the compiler.
80 An object file, produced by an assembler.
83 Files with other suffixes (or without suffixes) are passed straight
86 %************************************************************************
88 <sect1>Help and verbosity options
89 <label id="options-help">
91 <nidx>help options (GHC)</nidx>
92 <nidx>verbose option (GHC)</nidx>
94 %************************************************************************
96 A good option to start with is the @-help@ (or @-?@) option.
97 <nidx>-help option</nidx>
98 <nidx>-? option</nidx>
99 GHC spews a long message to standard output and then exits.
101 The @-v@<nidx>-v option</nidx> option makes GHC <em>verbose</em>: it
102 reports its version number and shows (on stderr) exactly how it invokes each
103 phase of the compilation system. Moreover, it passes
104 the @-v@ flag to most phases; each reports
105 its version number (and possibly some other information).
107 Please, oh please, use the @-v@ option when reporting bugs!
108 Knowing that you ran the right bits in the right order is always the
109 first thing we want to verify.
111 If you're just interested in the compiler version number, the
112 @--version@<nidx>--version option</nidx> option prints out a
113 one-line string containing the requested info.
115 %************************************************************************
117 <sect1>Running the right phases in the right order
118 <label id="options-order">
120 <nidx>order of passes in GHC</nidx>
121 <nidx>pass ordering in GHC</nidx>
123 %************************************************************************
125 The basic task of the @ghc@ driver is to run each input file
126 through the right phases (compiling, linking, etc.).
128 The first phase to run is determined by the input-file suffix, and the
129 last phase is determined by a flag. If no relevant flag is present,
130 then go all the way through linking. This table summarises:
133 Phase of the | Suffix saying | Flag saying | (suffix of) @@
134 compilation system | ``start here''| ``stop after''| output file @@
136 literate pre-processor | .lhs | - | - @@
137 C pre-processor (opt.) | - | - | - @@
138 Haskell compiler | .hs | -C, -S | .hc, .s @@
139 C compiler (opt.) | .hc or .c | -S | .s @@
140 assembler | .s | -c | .o @@
141 linker | other | - | a.out @@
143 <nidx>-C option</nidx>
144 <nidx>-S option</nidx>
145 <nidx>-c option</nidx>
147 Thus, a common invocation would be: @ghc -c Foo.hs@
149 Note: What the Haskell compiler proper produces depends on whether a
150 native-code generator is used (producing assembly language) or not
153 The option @-cpp@<nidx>-cpp option</nidx> must be given for the C
154 pre-processor phase to be run, that is, the pre-processor will be run
155 over your Haskell source file before continuing.
157 The option @-E@<nidx>-E option</nidx> runs just the pre-processing
158 passes of the compiler, outputting the result on stdout before
159 stopping. If used in conjunction with -cpp, the output is the
160 code blocks of the original (literal) source after having put it
161 through the grinder that is the C pre-processor. Sans @-cpp@, the
162 output is the de-litted version of the original source.
164 The option @-optcpp-E@<nidx>-optcpp-E option</nidx> runs just the
165 pre-processing stage of the C-compiling phase, sending the result to
166 stdout. (For debugging or obfuscation contests, usually.)
168 %************************************************************************
170 <sect1>Re-directing the compilation output(s)
171 <label id="options-output">
173 <nidx>output-directing options</nidx>
175 %************************************************************************
177 GHC's compiled output normally goes into a @.hc@, @.o@, etc., file,
178 depending on the last-run compilation phase. The option @-o
179 foo@<nidx>-o option</nidx> re-directs the output of that last-run
182 Note: this ``feature'' can be counterintuitive:
183 @ghc -C -o foo.o foo.hs@ will put the intermediate C code in the
184 file @foo.o@, name notwithstanding!
186 EXOTICA: But the @-o@ option isn't of much use if you have
187 <em>several</em> input files... Non-interface output files are
188 normally put in the same directory as their corresponding input file
189 came from. You may specify that they be put in another directory
190 using the @-odir <dir>@<nidx>-odir <dir> option</nidx> (the
191 ``Oh, dear'' option). For example:
194 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
197 The output files, @Foo.o@, @Bar.o@, and @Bumble.o@ would be
198 put into a subdirectory named after the architecture of the executing
199 machine (@sun4@, @mips@, etc). The directory must already
200 exist; it won't be created.
202 Note that the @-odir@ option does <em>not</em> affect where the
203 interface files are put. In the above example, they would still be
204 put in @parse/Foo.hi@, @parse/Bar.hi@, and @gurgle/Bumble.hi@.
206 MORE EXOTICA: The @-osuf <suffix>@<nidx>-osuf <suffix>
207 option</nidx> will change the @.o@ file suffix for object files to
208 whatever you specify. (We use this in compiling the prelude.).
209 Similarly, the @-hisuf <suffix>@<nidx>-hisuf <suffix>
210 option</nidx> will change the @.hi@ file suffix for non-system
211 interface files (see Section <ref name="Other options related to
212 interface files" id="hi-options">).
214 The @-hisuf@/@-osuf@ game is useful if you want to compile a program
215 with both GHC and HBC (say) in the same directory. Let HBC use the
216 standard @.hi@/@.o@ suffixes; add @-hisuf g_hi -osuf g_o@ to your
217 @make@ rule for GHC compiling...
219 FURTHER EXOTICA: If you are doing a normal @.hs@-to-@.o@ compilation
220 but would like to hang onto the intermediate @.hc@ C file, just
221 throw in a @-keep-hc-file-too@ option<nidx>-keep-hc-file-too option</nidx>.
222 If you would like to look at the assembler output, toss in a
223 @-keep-s-file-too@,<nidx>-keep-s-file-too option</nidx> too.
225 <sect2> Saving GHC's standard error output
226 <label id="saving-ghc-stderr">
228 <nidx>standard error, saving</nidx>
230 Sometimes, you may cause GHC to be rather chatty on standard error;
231 with @-v@, for example. You can instruct GHC to <em>append</em> this
232 output to a particular log file with a @-odump <blah>@<nidx>-odump
233 <blah> option</nidx> option.
235 <sect2> Redirecting temporary files
236 <label id="temp-files">
238 <nidx>temporary files, redirecting</nidx>
240 If you have trouble because of running out of space in @/tmp@ (or
241 wherever your installation thinks temporary files should go), you may
242 use the @-tmpdir <dir>@<nidx>-tmpdir <dir> option</nidx> option
243 to specify an alternate directory. For example, @-tmpdir .@ says to
244 put temporary files in the current working directory.
246 Alternatively, use your @TMPDIR@ environment variable.<nidx>TMPDIR
247 environment variable</nidx> Set it to the name of the directory where
248 temporary files should be put. GCC and other programs will honour the
249 @TMPDIR@ variable as well.
251 Even better idea: Set the @TMPDIR@ variable when building GHC, and
252 never worry about @TMPDIR@ again. (see the build documentation).
254 %************************************************************************
256 <sect1>Warnings and sanity-checking
257 <label id="options-sanity">
259 <nidx>sanity-checking options</nidx>
260 <nidx>warnings</nidx>
262 %************************************************************************
264 GHC has a number of options that select which types of non-fatal error
265 messages, otherwise known as warnings, can be generated during
266 compilation. By default, you get a standard set of warnings which are
267 generally likely to indicate bugs in your program. These are:
268 @-fwarn-overlpapping-patterns@, @-fwarn-duplicate-exports@, and
269 @-fwarn-missing-methods@. The following flags are simple ways to
270 select standard ``packages'' of warnings:
275 <nidx>-Wnot option</nidx>
277 Turns off all warnings, including the standard ones.
280 <nidx>-w option</nidx>
285 <nidx>-W option</nidx>
287 Provides the standard warnings plus @-fwarn-incomplete-patterns@,
288 @-fwarn-unused-imports@ and @-fwarn-unused-binds@.
291 <nidx>-Wall option</nidx>
293 Turns on all warning options.
297 The full set of warning options is described below. To turn off any
298 warning, simply give the corresponding @-fno-warn-...@ option on
303 <tag>@-fwarn-name-shadowing@:</tag>
304 <nidx>-fwarn-name-shadowing option</nidx>
305 <nidx>shadowing, warning</nidx>
307 This option causes a warning to be emitted whenever an inner-scope
308 value has the same name as an outer-scope value, i.e. the inner value
309 shadows the outer one. This can catch typographical errors that turn
310 into hard-to-find bugs, e.g., in the inadvertent cyclic definition
311 @let x = ... x ... in@.
313 Consequently, this option does <em>not</em> allow cyclic recursive
316 <tag>@-fwarn-overlapping-patterns@:</tag>
317 <nidx>-fwarn-overlapping-patterns option</nidx>
318 <nidx>overlapping patterns, warning</nidx>
319 <nidx>patterns, overlapping</nidx>
321 By default, the compiler will warn you if a set of patterns are
331 where the last pattern match in @f@ won't ever be reached, as the
332 second pattern overlaps it. More often than not, redundant patterns
333 is a programmer mistake/error, so this option is enabled by default.
335 <tag>@-fwarn-incomplete-patterns@:</tag>
336 <nidx>-fwarn-incomplete-patterns option</nidx>
337 <nidx>incomplete patterns, warning</nidx>
338 <nidx>patterns, incomplete</nidx>
340 Similarly for incomplete patterns, the function @g@ below will fail
341 when applied to non-empty lists, so the compiler will emit a warning
342 about this when @-fwarn-incomplete-patterns@ is enabled.
348 This option isn't enabled be default because it can be a bit noisy,
349 and it doesn't always indicate a bug in the program. However, it's
350 generally considered good practice to cover all the cases in your
353 <tag>@-fwarn-missing-methods@:</tag>
354 <nidx>-fwarn-missing-methods option</nidx>
355 <nidx>missing methods, warning</nidx>
356 <nidx>methods, missing</nidx>
358 This option is on by default, and warns you whenever an instance
359 declaration is missing one or more methods, and the corresponding
360 class declaration has no default declaration for them.
362 <tag>@-fwarn-unused-imports@:</tag>
363 <nidx>-fwarn-unused-imports option</nidx>
364 <nidx>unused imports, warning</nidx>
365 <nidx>imports, unused</nidx>
367 Report any objects that are explicitly imported but never used.
369 <tag>@-fwarn-unused-binds@:</tag>
370 <nidx>-fwarn-unused-binds option</nidx>
371 <nidx>unused binds, warning</nidx>
372 <nidx>binds, unused</nidx>
374 Report any function definitions (and local bindings) which are unused.
375 For top-level functions, the warning is only given if the binding is
378 <tag>@-fwarn-unused-matches@:</tag>
379 <nidx>-fwarn-unused-matches option</nidx>
380 <nidx>unused matches, warning</nidx>
381 <nidx>matches, unused</nidx>
383 Report all unused variables which arise from pattern matches,
384 including patterns consisting of a single variable. For instance @f x
385 y = []@ would report @x@ and @y@ as unused. To eliminate the warning,
386 all unused variables can be replaced with wildcards.
388 <tag>@-fwarn-duplicate-exports@:</tag>
389 <nidx>-fwarn-duplicate-exports option</nidx>
390 <nidx>duplicate exports, warning</nidx>
391 <nidx>export lists, duplicates</nidx>
393 Have the compiler warn about duplicate entries in export lists. This
394 is useful information if you maintain large export lists, and want to
395 avoid the continued export of a definition after you've deleted (one)
396 mention of it in the export list.
398 This option is on by default.
400 <tag><tt>-fwarn-type-defaults</tt>:</tag>
401 <nidx>-fwarn-type-defaults option</nidx>
402 <nidx>defaulting mechanism, warning</nidx>
404 Have the compiler warn/inform you where in your source the Haskell
405 defaulting mechanism for numeric types kicks in. This is useful
406 information when converting code from a context that assumed one
407 default into one with another, e.g., the 'default default' for Haskell
408 1.4 caused the otherwise unconstrained value <tt>1</tt> to be given
409 the type <tt>Int</tt>, whereas Haskell 98 defaults it to
410 <tt>Integer</tt>. This may lead to differences in performance and
411 behaviour, hence the usefulness of being non-silent about this.
413 This warning is off by default.
415 <tag>@-fwarn-missing-signatures@:</tag>
416 <nidx>-fwarn-missing-signatures option</nidx>
417 <nidx>type signatures, missing</nidx>
419 If you would like GHC to check that every top-level function/value has
420 a type signature, use the @-fwarn-missing-signatures@ option. This
421 option is off by default.
425 If you're feeling really paranoid, the @-dcore-lint@
426 option<nidx>-dcore-lint option</nidx> is a good choice. It turns on
427 heavyweight intra-pass sanity-checking within GHC. (It checks GHC's
430 %************************************************************************
432 <sect1>Separate compilation
433 <label id="separate-compilation">
435 <nidx>separate compilation</nidx>
436 <nidx>recompilation checker</nidx>
437 <nidx>make and recompilation</nidx>
439 %************************************************************************
441 This section describes how GHC supports separate compilation.
443 <sect2>Interface files
444 <label id="hi-files">
446 <nidx>interface files</nidx>
447 <nidx>.hi files</nidx>
449 When GHC compiles a source file @F@ which contains a module @A@, say,
450 it generates an object @F.o@, <em>and</em> a companion <em>interface
451 file</em> @A.hi@. The interface file is not intended for human
452 consumption, as you'll see if you take a look at one. It's merely
453 there to help the compiler compile other modules in the same program.
455 NOTE: Having the name of the interface file follow the module name and
456 not the file name, means that working with tools such as @make(1)@
457 become harder. @make@ implicitly assumes that any output files
458 produced by processing a translation unit will have file names that
459 can be derived from the file name of the translation unit. For
460 instance, pattern rules becomes unusable. For this reason, we
461 recommend you stick to using the same file name as the module name.
463 The interface file for @A@ contains information needed by the compiler
464 when it compiles any module @B@ that imports @A@, whether directly or
465 indirectly. When compiling @B@, GHC will read @A.hi@ to find the
466 details that it needs to know about things defined in @A@.
468 Furthermore, when compiling module @C@ which imports @B@, GHC may
469 decide that it needs to know something about @A@ --- for example, @B@
470 might export a function that involves a type defined in @A@. In this
471 case, GHC will go and read @A.hi@ even though @C@ does not explicitly
474 The interface file may contain all sorts of things that aren't
475 explicitly exported from @A@ by the programmer. For example, even
476 though a data type is exported abstractly, @A.hi@ will contain the
477 full data type definition. For small function definitions, @A.hi@
478 will contain the complete definition of the function. For bigger
479 functions, @A.hi@ will contain strictness information about the
480 function. And so on. GHC puts much more information into @.hi@ files
481 when optimisation is turned on with the @-O@ flag. Without @-O@ it
482 puts in just the minimum; with @-O@ it lobs in a whole pile of stuff.
483 <nidx>optimsation, effect on .hi files</nidx>
485 @A.hi@ should really be thought of as a compiler-readable version of
486 @A.o@. If you use a @.hi@ file that wasn't generated by the same
487 compilation run that generates the @.o@ file the compiler may assume
488 all sorts of incorrect things about @A@, resulting in core dumps and
489 other unpleasant happenings.
491 %************************************************************************
493 <sect2>Finding interface files
494 <label id="options-finding-imports">
496 <nidx>interface files, finding them</nidx>
497 <nidx>finding interface files</nidx>
499 %************************************************************************
501 In your program, you import a module @Foo@ by saying
502 @import Foo@. GHC goes looking for an interface file, @Foo.hi@.
503 It has a builtin list of directories (notably including @.@) where
508 <tag>@-i<dirs>@</tag><nidx>-i<dirs> option</nidx> This flag
509 prepends a colon-separated list of @dirs@ to the ``import
512 <tag>@-i@</tag> resets the ``import directories'' list back to nothing.
514 <tag>@-fno-implicit-prelude@</tag>
515 <nidx>-fno-implicit-prelude option</nidx>
516 GHC normally imports @Prelude.hi@ files for you. If you'd rather it
517 didn't, then give it a @-fno-implicit-prelude@ option. You are
518 unlikely to get very far without a Prelude, but, hey, it's a free
521 <tag>@-syslib <lib>@</tag>
522 <nidx>-syslib <lib> option</nidx>
524 If you are using a system-supplied non-Prelude library (e.g., the
525 POSIX library), just use a @-syslib posix@ option (for example). The
526 right interface files should then be available. Section <ref
527 name="The GHC Prelude and Libraries" id="ghc-prelude"> lists the
528 libraries available by this mechanism.
531 <nidx>-I<dir> option</nidx>
533 Once a Haskell module has been compiled to C (@.hc@ file), you may
534 wish to specify where GHC tells the C compiler to look for @.h@ files.
535 (Or, if you are using the @-cpp@ option<nidx>-cpp option</nidx>, where
536 it tells the C pre-processor to look...) For this purpose, use a @-I@
537 option in the usual C-ish way.
541 %************************************************************************
543 <sect2>Other options related to interface files
544 <label id="hi-options">
546 <nidx>interface files, options</nidx>
548 %************************************************************************
550 The interface output may be directed to another file
551 @bar2/Wurble.iface@ with the option @-ohi bar2/Wurble.iface@<nidx>-ohi
552 <file> option</nidx> (not recommended).
554 To avoid generating an interface file at all, use a @-nohi@
555 option.<nidx>-nohi option</nidx>
557 The compiler does not overwrite an existing @.hi@ interface file if
558 the new one is byte-for-byte the same as the old one; this is friendly
559 to @make@. When an interface does change, it is often enlightening to
560 be informed. The @-hi-diffs@<nidx>-hi-diffs option</nidx> option will
561 make @ghc@ run @diff@ on the old and new @.hi@ files. You can also
562 record the difference in the interface file itself, the
563 @-keep-hi-diffs@<nidx>-keep-hi-diffs</nidx> option takes care of that.
565 The @.hi@ files from GHC contain ``usage'' information which changes
566 often and uninterestingly. If you really want to see these changes
567 reported, you need to use the
568 @-hi-diffs-with-usages@<nidx>-hi-diffs-with-usages option</nidx>
571 Interface files are normally jammed full of compiler-produced
572 <em>pragmas</em>, which record arities, strictness info, etc. If you
573 think these pragmas are messing you up (or you are doing some kind of
574 weird experiment), you can tell GHC to ignore them with the
575 @-fignore-interface-pragmas@<nidx>-fignore-interface-pragmas
576 option</nidx> option.
578 When compiling without optimisations on, the compiler is extra-careful
579 about not slurping in data constructors and instance declarations that
580 it will not need. If you believe it is getting it wrong and not
581 importing stuff which you think it should, this optimisation can be
582 turned off with @-fno-prune-tydecls@ and @-fno-prune-instdecls@.
583 <nidx>-fno-prune-tydecls option</nidx><nidx>-fno-prune-instdecls
586 See also Section <ref name="Linking and consistency-checking"
587 id="options-linker">, which describes how the linker finds standard
590 %************************************************************************
592 <sect2>The recompilation checker
595 <nidx>recompilation checker</nidx>
597 %************************************************************************
599 In the olden days, GHC compared the newly-generated @.hi@ file with
600 the previous version; if they were identical, it left the old one
601 alone and didn't change its modification date. In consequence,
602 importers of a module with an unchanged output @.hi@ file were not
605 This doesn't work any more. In our earlier example, module @C@ does
606 not import module @A@ directly, yet changes to @A.hi@ should force a
607 recompilation of @C@. And some changes to @A@ (changing the
608 definition of a function that appears in an inlining of a function
609 exported by @B@, say) may conceivably not change @B.hi@ one jot. So
612 GHC keeps a version number on each interface file, and on each type
613 signature within the interface file. It also keeps in every interface
614 file a list of the version numbers of everything it used when it last
615 compiled the file. If the source file's modification date is earlier
616 than the @.o@ file's date (i.e. the source hasn't changed since the
617 file was last compiled), and you give GHC the @-recomp@<nidx>-recomp
618 option</nidx> flag, then GHC will be clever. It compares the version
619 numbers on the things it needs this time with the version numbers on
620 the things it needed last time (gleaned from the interface file of the
621 module being compiled); if they are all the same it stops compiling
622 rather early in the process saying ``Compilation IS NOT required''.
623 What a beautiful sight!
625 It's still an experimental feature (that's why @-recomp@ is off by
626 default), so tell us if you think it doesn't work.
628 Patrick Sansom has a workshop paper about how all this is done. Ask
629 him (email: <htmlurl name="sansom@@dcs.gla.ac.uk"
630 url="mailto:sansom@@dcs.gla.ac.uk">) if you want a copy.
632 %************************************************************************
635 <label id="using-make">
639 %************************************************************************
641 It is reasonably straightforward to set up a @Makefile@ to use with
642 GHC, assuming you name your source files the same as your modules.
647 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
649 SRCS = Main.lhs Foo.lhs Bar.lhs
650 OBJS = Main.o Foo.o Bar.o
652 .SUFFIXES : .o .hi .lhs .hc .s
656 $(HC) -o $@ $(HC_OPTS) $(OBJS)
658 # Standard suffix rules
663 $(HC) -c $< $(HC_OPTS)
666 $(HC) -c $< $(HC_OPTS)
668 # Inter-module dependencies
669 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
670 Main.o Main.hc Main.s : Foo.hi Baz.hi # Main imports Foo and Baz
673 (Sophisticated @make@ variants may achieve some of the above more
674 elegantly. Notably, @gmake@'s pattern rules let you write the more
679 $(HC) -c $< $(HC_OPTS)
682 What we've shown should work with any @make@.)
684 Note the cheesy @.o.hi@ rule: It records the dependency of the
685 interface (@.hi@) file on the source. The rule says a @.hi@ file can
686 be made from a @.o@ file by doing... nothing. Which is true.
688 Note the inter-module dependencies at the end of the Makefile, which
692 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
695 They tell @make@ that if any of @Foo.o@, @Foo.hc@ or @Foo.s@ have an
696 earlier modification date than @Baz.hi@, then the out-of-date file
697 must be brought up to date. To bring it up to date, @make@ looks for
698 a rule to do so; one of the preceding suffix rules does the job
701 Putting inter-dependencies of the form @Foo.o : Bar.hi@ into your
702 @Makefile@ by hand is rather error-prone. Don't worry---never fear,
703 @mkdependHS@ is here! (and is distributed as part of GHC) Add the
704 following to your @Makefile@:
708 mkdependHS -- $(HC_OPTS) -- $(SRCS)
711 Now, before you start compiling, and any time you change the @imports@
712 in your program, do @make depend@ before you do @make cool_pgm@.
713 @mkdependHS@ will append the needed dependencies to your @Makefile@.
714 @mkdependHS@ is fully describe in Section <ref name="Makefile
715 dependencies in Haskell: using mkdependHS" id="mkdependHS">.
717 A few caveats about this simple scheme:
721 <item> You may need to compile some modules explicitly to create their
722 interfaces in the first place (e.g., @make Bar.o@ to create @Bar.hi@).
724 <item> You may have to type @make@ more than once for the dependencies
725 to have full effect. However, a @make@ run that does nothing
726 <em>does</em> mean ``everything's up-to-date.''
728 <item> This scheme will work with mutually-recursive modules but,
729 again, it may take multiple iterations to ``settle.''
733 %************************************************************************
735 <sect2>How to compile mutually recursive modules
736 <label id="mutual-recursion">
738 <nidx>module system, recursion</nidx>
739 <nidx>recursion, between modules</nidx>
741 %************************************************************************
743 Currently, the compiler does not have proper support for dealing with
744 mutually recursive modules:
751 newtype TA = MkTA Int
766 When compiling either module A and B, the compiler will try (in vain)
767 to look for the interface file of the other. So, to get mutually
768 recursive modules off the ground, you need to hand write an interface
769 file for A or B, so as to break the loop. These hand-written
770 interface files are called @hi-boot@ files, and are placed in a file
771 called @<module>.hi-boot@. To import from an @hi-boot@ file instead
772 of the standard @.hi@ file, use the following syntax in the importing module:
773 <nidx>hi-boot files</nidx>
774 <nidx>importing, hi-boot files</nidx>
777 import {-# SOURCE #-} A
780 The hand-written interface need only contain the bare minimum of
781 information needed to get the bootstrapping process started. For
782 example, it doesn't need to contain declarations for <em/everything/
783 that module @A@ exports, only the things required by the module that
784 imports @A@ recursively.
786 For the example at hand, the boot interface file for A would look like
790 __interface A 1 404 where
791 __export A TA{MkTA} ;
792 1 newtype TA = MkTA PrelBase.Int ;
795 The syntax is essentially the same as a normal @.hi@ file
796 (unfortunately), but you can usually tailor an existing @.hi@ file to
797 make a @.hi-boot@ file.
799 Notice that we only put the declaration for the newtype @TA@ in the
800 @hi-boot@ file, not the signature for @f@, since @f@ isn't used by
803 The number ``1'' after ``__interface A'' gives the version number of module A;
804 it is incremented whenever anything in A's interface file changes. The ``404'' is
805 the version number of the interface file <em>syntax</em>; we change it when
806 we change the syntax of interface files so that you get a better error message when
807 you try to read an old-format file with a new-format compiler.
809 The number ``1'' at the beginning of a declaration is the <em>version
810 number</em> of that declaration: for the purposes of @.hi-boot@ files
811 these can all be set to 1. All names must be fully qualified with the
812 <em/original/ module that an object comes from: for example, the
813 reference to @Int@ in the interface for @A@ comes from @PrelBase@,
814 which is a module internal to GHC's prelude. It's a pain, but that's
817 If you want an hi-boot file to export a data type, but you don't want to give its constructors
818 (because the constructors aren't used by the SOURCE-importing module), you can write simply:
821 __interface A 1 404 where
826 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
828 <bf>Note:</bf> This is all a temporary solution, a version of the
829 compiler that handles mutually recursive properly without the manual
830 construction of interface files, is (allegedly) in the works.
832 %************************************************************************
834 <sect1>Optimisation (code improvement)
835 <label id="options-optimise">
837 <nidx>optimisation (GHC)</nidx>
838 <nidx>improvement, code (GHC)</nidx>
840 %************************************************************************
842 The @-O*@ options specify convenient ``packages'' of optimisation
843 flags; the @-f*@ options described later on specify
844 <em>individual</em> optimisations to be turned on/off; the @-m*@
845 options specify <em>machine-specific</em> optimisations to be turned
848 %----------------------------------------------------------------------
849 <sect2>@-O*@: convenient ``packages'' of optimisation flags.
850 <label id="optimise-pkgs">
852 <nidx>-O options</nidx>
854 There are <em>many</em> options that affect the quality of code
855 produced by GHC. Most people only have a general goal, something like
856 ``Compile quickly'' or ``Make my program run like greased lightning.''
857 The following ``packages'' of optimisations (or lack thereof) should
860 Once you choose a @-O*@ ``package,'' stick with it---don't chop and
861 change. Modules' interfaces <em>will</em> change with a shift to a new
862 @-O*@ option, and you may have to recompile a large chunk of all
863 importing modules before your program can again be run
864 safely (see Section <ref name="The recompilation checker" id="recomp">).
867 <tag>No @-O*@-type option specified:</tag>
868 <nidx>-O* not specified</nidx>
869 This is taken to mean: ``Please compile quickly; I'm not over-bothered
870 about compiled-code quality.'' So, for example: @ghc -c Foo.hs@
872 <tag>@-O@ or @-O1@:</tag>
873 <nidx>-O option</nidx>
874 <nidx>-O1 option</nidx>
875 <nidx>optimise normally</nidx>
876 Means: ``Generate good-quality code without taking too long about it.''
877 Thus, for example: @ghc -c -O Main.lhs@
880 <nidx>-O2 option</nidx>
881 <nidx>optimise aggressively</nidx>
882 Means: ``Apply every non-dangerous optimisation, even if it means
883 significantly longer compile times.''
885 The avoided ``dangerous'' optimisations are those that can make
886 runtime or space <em>worse</em> if you're unlucky. They are
887 normally turned on or off individually.
889 At the moment, @-O2@ is <em>unlikely</em> to produce
890 better code than @-O@.
892 <tag>@-O2-for-C@:</tag>
893 <nidx>-O2-for-C option</nidx>
894 <nidx>gcc, invoking with -O2</nidx>
896 Says to run GCC with @-O2@, which may be worth a few percent in
897 execution speed. Don't forget @-fvia-C@, lest you use the native-code
898 generator and bypass GCC altogether!
901 <nidx>-Onot option</nidx>
902 <nidx>optimising, reset</nidx>
904 This option will make GHC ``forget'' any @-O@ish options it has seen so
905 far. Sometimes useful; for example: @make all EXTRA_HC_OPTS=-Onot@.
907 <tag>@-Ofile <file>@:</tag>
908 <nidx>-Ofile <file> option</nidx>
909 <nidx>optimising, customised</nidx>
911 For those who need <em>absolute</em> control over <em>exactly</em>
912 what options are used (e.g., compiler writers, sometimes :-), a list
913 of options can be put in a file and then slurped in with @-Ofile@.
915 In that file, comments are of the @#@-to-end-of-line variety; blank
916 lines and most whitespace is ignored.
918 Please ask if you are baffled and would like an example of @-Ofile@!
921 At Glasgow, we don't use a @-O*@ flag for day-to-day work. We use
922 @-O@ to get respectable speed; e.g., when we want to measure
923 something. When we want to go for broke, we tend to use @-O -fvia-C
924 -O2-for-C@ (and we go for lots of coffee breaks).
926 The easiest way to see what @-O@ (etc.) ``really mean'' is to run with
927 @-v@, then stand back in amazement. Alternatively, just look at the
928 @HsC_minus<blah>@ lists in the @ghc@ driver script.
930 %----------------------------------------------------------------------
931 <sect2>@-f*@: platform-independent flags
933 <nidx>-f* options (GHC)</nidx>
934 <nidx>-fno-* options (GHC)</nidx>
936 Flags can be turned <em>off</em> individually. (NB: I hope you have a
937 good reason for doing this....) To turn off the @-ffoo@ flag, just use
938 the @-fno-foo@ flag.<nidx>-fno-<opt> anti-option</nidx> So, for
939 example, you can say @-O2 -fno-strictness@, which will then drop out
940 any running of the strictness analyser.
942 The options you are most likely to want to turn off are:
943 @-fno-strictness@<nidx>-fno-strictness option</nidx> (strictness
944 analyser [because it is sometimes slow]),
945 @-fno-specialise@<nidx>-fno-specialise option</nidx> (automatic
946 specialisation of overloaded functions [because it makes your code
947 bigger]) [US spelling also accepted], and
948 @-fno-update-analysis@<nidx>-fno-update-analysis option</nidx> (update
949 analyser, because it sometimes takes a <em>long</em> time). This one
950 is only enabled with -O2 anyway.
952 Should you wish to turn individual flags <em>on</em>, you are advised
953 to use the @-Ofile@ option, described above. Because the order in
954 which optimisation passes are run is sometimes crucial, it's quite
955 hard to do with command-line options.
957 Here are some ``dangerous'' optimisations you <em>might</em> want to try:
959 %------------------------------------------------------------------
960 <tag>@-fvia-C@:</tag>
961 <nidx>-fvia-C option</nidx>
962 <nidx>native code generator, turning off</nidx>
964 Compile via C, and don't use the native-code generator. (There are
965 many cases when GHC does this on its own.) You might pick up a little
966 bit of speed by compiling via C. If you use @_ccall_gc_@s or
967 @_casm_@s, you probably <em>have to</em> use @-fvia-C@.
969 The lower-case incantation, @-fvia-c@, is synonymous.
971 Compiling via C will probably be slower (in compilation time) than
972 using GHC's native code generator.
974 <tag>@-funfolding-interface-threshold<n>@:</tag>
975 <nidx>-funfolding-interface-threshold option</nidx>
976 <nidx>inlining, controlling</nidx>
977 <nidx>unfolding, controlling</nidx>
978 (Default: 30) By raising or lowering this number, you can raise or
979 lower the amount of pragmatic junk that gets spewed into interface
980 files. (An unfolding has a ``size'' that reflects the cost in terms
981 of ``code bloat'' of expanding that unfolding in another module. A
982 bigger function would be assigned a bigger cost.)
984 <tag>@-funfolding-creation-threshold<n>@:</tag>
985 <nidx>-funfolding-creation-threshold option</nidx>
986 <nidx>inlining, controlling</nidx>
987 <nidx>unfolding, controlling</nidx>
988 (Default: 30) This option is similar to
989 @-funfolding-interface-threshold@, except that it governs unfoldings
990 within a single module. Increasing this figure is more likely to
991 result in longer compile times than faster code. The next option is
994 <tag>@-funfolding-use-threshold<n>@:</tag>
995 <nidx>-funfolding-use-threshold option</nidx>
996 <nidx>inlining, controlling</nidx>
997 <nidx>unfolding, controlling</nidx>
998 (Default: 8) This is the magic cut-off figure for unfolding: below
999 this size, a function definition will be unfolded at the call-site,
1000 any bigger and it won't. The size computed for a function depends on
1001 two things: the actual size of the expression minus any discounts that
1002 apply (see @-funfolding-con-discount@).
1004 <tag>@-funfolding-con-discount<n>@:</tag>
1005 <nidx>-funfolding-con-discount option</nidx>
1006 <nidx>inlining, controlling</nidx>
1007 <nidx>unfolding, controlling</nidx>
1008 (Default: 2) If the compiler decides that it can eliminate some
1009 computation by performing an unfolding, then this is a discount factor
1010 that it applies to the funciton size before deciding whether to unfold
1013 OK, folks, these magic numbers `30', `8', and '2' are mildly
1014 arbitrary; they are of the ``seem to be OK'' variety. The `8' is the
1015 more critical one; it's what determines how eager GHC is about
1016 expanding unfoldings.
1018 <tag>@-funbox-strict-fields@:</tag>
1019 <nidx>-funbox-strict-fields option</nidx>
1020 <nidx>strict constructor fields</nidx>
1021 <nidx>constructor fields, strict</nidx>
1023 This option causes all constructor fields which are marked strict
1024 (i.e. ``!'') to be unboxed or unpacked if possible. For example:
1027 data T = T !Float !Float
1030 will create a constructor @T@ containing two unboxed floats if the
1031 @-funbox-strict-fields@ flag is given. This may not always be an
1032 optimisation: if the @T@ constructor is scrutinised and the floats
1033 passed to a non-strict function for example, they will have to be
1034 reboxed (this is done automatically by the compiler).
1036 This option should only be used in conjunction with @-O@, in order to
1037 expose unfoldings to the compiler so the reboxing can be removed as
1038 often as possible. For example:
1042 f (T f1 f2) = f1 + f2
1045 The compiler will avoid reboxing @f1@ and @f2@ by inlining @+@ on
1046 floats, but only when @-O@ is on.
1048 Any single-constructor data is eligible for unpacking; for example
1051 data T = T !(Int,Int)
1054 will store the two @Int@s directly in the @T@ constructor, by flattening
1055 the pair. Multi-level unpacking is also supported:
1059 data S = S !Int !Int
1062 will store two unboxed @Int#@s directly in the @T@ constructor.
1064 <tag>@-fsemi-tagging@:</tag>
1065 This option (which <em>does not work</em> with the native-code generator)
1066 tells the compiler to add extra code to test for already-evaluated
1067 values. You win if you have lots of such values during a run of your
1068 program, you lose otherwise. (And you pay in extra code space.)
1070 We have not played with @-fsemi-tagging@ enough to recommend it.
1071 (For all we know, it doesn't even work anymore... Sigh.)
1074 %----------------------------------------------------------------------
1075 <sect2>@-m*@: platform-specific flags
1077 <nidx>-m* options (GHC)</nidx>
1078 <nidx>platform-specific options</nidx>
1079 <nidx>machine-specific options</nidx>
1081 Some flags only make sense for particular target platforms.
1085 (SPARC machines)<nidx>-mv8 option (SPARC only)</nidx>
1086 Means to pass the like-named option to GCC; it says to use the
1087 Version 8 SPARC instructions, notably integer multiply and divide.
1088 The similiar @-m*@ GCC options for SPARC also work, actually.
1090 <tag>@-mlong-calls@:</tag>
1091 (HPPA machines)<nidx>-mlong-calls option (HPPA only)</nidx>
1092 Means to pass the like-named option to GCC. Required for Very Big
1093 modules, maybe. (Probably means you're in trouble...)
1095 <tag>@-monly-[32]-regs@:</tag>
1096 (iX86 machines)<nidx>-monly-N-regs option (iX86 only)</nidx>
1097 GHC tries to ``steal'' four registers from GCC, for performance
1098 reasons; it almost always works. However, when GCC is compiling some
1099 modules with four stolen registers, it will crash, probably saying:
1101 Foo.hc:533: fixed or forbidden register was spilled.
1102 This may be due to a compiler bug or to impossible asm
1103 statements or clauses.
1105 Just give some registers back with @-monly-N-regs@. Try `3' first,
1106 then `2'. If `2' doesn't work, please report the bug to us.
1109 %----------------------------------------------------------------------
1110 <sect2>Code improvement by the C compiler.
1111 <label id="optimise-C-compiler">
1113 <nidx>optimisation by GCC</nidx>
1114 <nidx>GCC optimisation</nidx>
1116 The C~compiler (GCC) is run with @-O@ turned on. (It has
1119 If you want to run GCC with @-O2@---which may be worth a few
1120 percent in execution speed---you can give a
1121 @-O2-for-C@<nidx>-O2-for-C option</nidx> option.
1123 %************************************************************************
1125 <sect1>Options related to a particular phase
1126 <label id="options-phases">
1129 %************************************************************************
1131 <sect2> The C pre-processor
1132 <label id="c-pre-processor">
1134 <nidx>pre-processing: cpp</nidx>
1135 <nidx>C pre-processor options</nidx>
1136 <nidx>cpp, pre-processing with</nidx>
1138 The C pre-processor @cpp@ is run over your Haskell code only if the
1139 @-cpp@ option <nidx>-cpp option</nidx> is given. Unless you are
1140 building a large system with significant doses of conditional
1141 compilation, you really shouldn't need it.
1143 <tag>@-D<foo>@:</tag>
1144 <nidx>-D<name> option</nidx>
1145 Define macro @<foo>@ in the usual way. NB: does <em>not</em> affect
1146 @-D@ macros passed to the C~compiler when compiling via C! For those,
1147 use the @-optc-Dfoo@ hack... (see Section <ref name="Forcing options
1148 to a particular phase." id="forcing-options-through">).
1150 <tag>@-U<foo>@:</tag>
1151 <nidx>-U<name> option</nidx>
1152 Undefine macro @<foo>@ in the usual way.
1154 <tag>@-I<dir>@:</tag>
1155 <nidx>-I<dir> option</nidx>
1156 Specify a directory in which to look for @#include@ files, in
1160 The @ghc@ driver pre-defines several macros:
1162 <tag>@__HASKELL98__@:</tag>
1163 <nidx>__HASKELL98__</nidx>
1164 If defined, this means that GHC supports the language
1165 defined by the Haskell 98 report.
1167 NB. This macro is <em/only/ set when pre-processing Haskell source
1168 (ie. @.hs@ or @.lhs@ files).
1170 <tag>@__HASKELL1__@:</tag>
1171 <nidx>__HASKELL1__ macro</nidx>
1172 If defined to <em/n/, that means GHC supports the
1173 Haskell language defined in the Haskell report version <em/1.n/.
1176 NB. As with <tt/__HASKELL98__/, this macro is <em/only/ set when
1177 pre-processing Haskell source (ie. @.hs@ or @.lhs@ files).
1179 <tag>@__GLASGOW_HASKELL__@:</tag>
1180 <nidx>__GLASGOW_HASKELL__ macro</nidx>
1181 For version <em/n/ of the GHC system, this will be @#define@d to
1182 <em/100n/. So, for version 4.00, it is 400.
1184 With any luck, @__GLASGOW_HASKELL__@ will be undefined in all other
1185 implementations that support C-style pre-processing.
1187 (For reference: the comparable symbols for other systems are:
1188 @__HUGS__@ for Hugs and @__HBC__@ for Chalmers.)
1190 NB. This macro is set when pre-processing both Haskell source and C
1191 source, including the C source generated from a Haskell module
1192 (ie. @.hs@, @.lhs@, @.c@ and @.hc@ files).
1194 <tag>@__CONCURRENT_HASKELL__@:</tag>
1195 <nidx>__CONCURRENT_HASKELL__ macro</nidx>
1196 This symbol is defined when pre-processing Haskell (input) and
1197 pre-processing C (GHC output). Since GHC from verion 4.00 now
1198 supports concurrent haskell by default, this symbol is always defined.
1200 <tag>@__PARALLEL_HASKELL__@:</tag>
1201 <nidx>__PARALLEL_HASKELL__ macro</nidx>
1202 Only defined when @-parallel@ is in use! This symbol is defined when
1203 pre-processing Haskell (input) and pre-processing C (GHC output).
1206 Options other than the above can be forced through to the C
1207 pre-processor with the @-opt@ flags (see
1208 Section <ref name="Forcing options to a particular phase." id="forcing-options-through">).
1210 A small word of warning: @-cpp@ is not friendly to ``string
1211 gaps''.<nidx>-cpp vs string gaps</nidx><nidx>string gaps vs
1212 -cpp</nidx>. In other words, strings such as the following:
1220 don't work with @-cpp@; @/usr/bin/cpp@ elides the
1221 backslash-newline pairs.
1223 However, it appears that if you add a space at the end of the line,
1224 then @cpp@ (at least GNU @cpp@ and possibly other @cpp@s)
1225 leaves the backslash-space pairs alone and the string gap works as
1228 %************************************************************************
1230 <sect2>Options affecting the C compiler (if applicable)
1231 <label id="options-C-compiler">
1233 <nidx>include-file options</nidx>
1234 <nidx>C compiler options</nidx>
1235 <nidx>GCC options</nidx>
1237 %************************************************************************
1239 At the moment, quite a few common C-compiler options are passed on
1240 quietly to the C compilation of Haskell-compiler-generated C files.
1241 THIS MAY CHANGE. Meanwhile, options so sent are:
1244 @-ansi@ | do ANSI C (not K&R) @@
1245 @-pedantic@ | be so@@
1246 @-dgcc-lint@ | (hack) short for ``make GCC very paranoid''@@
1248 <nidx>-ansi option (for GCC)</nidx>
1249 <nidx>-pedantic option (for GCC)</nidx>
1250 <nidx>-dgcc-lint option (GCC paranoia)</nidx>
1252 If you are compiling with lots of @ccalls@, etc., you may need to
1253 tell the C~compiler about some @#include@ files. There is no real
1254 pretty way to do this, but you can use this hack from the
1258 % ghc -c '-#include <X/Xlib.h>' Xstuff.lhs
1262 %************************************************************************
1264 <sect2>Linking and consistency-checking
1265 <label id="options-linker">
1267 <nidx>linker options</nidx>
1268 <nidx>ld options</nidx>
1270 %************************************************************************
1272 GHC has to link your code with various libraries, possibly including:
1273 user-supplied, GHC-supplied, and system-supplied (@-lm@ math
1274 library, for example).
1277 <tag>@-l<FOO>@:</tag>
1278 <nidx>-l<lib> option</nidx>
1279 Link in a library named @lib<FOO>.a@ which resides somewhere on the
1280 library directories path.
1282 Because of the sad state of most UNIX linkers, the order of such
1283 options does matter. Thus: @ghc -lbar *.o@ is almost certainly
1284 wrong, because it will search @libbar.a@ <em>before</em> it has
1285 collected unresolved symbols from the @*.o@ files.
1286 @ghc *.o -lbar@ is probably better.
1288 The linker will of course be informed about some GHC-supplied
1289 libraries automatically; these are:
1292 <bf>-l equivalent</bf> | <bf>description</bf> @@
1294 @-lHSrts,-lHSclib@ | basic runtime libraries @@
1295 @-lHS@ | standard Prelude library @@
1296 @-lHS_cbits@ | C support code for standard Prelude library @@
1297 @-lgmp@ | GNU multi-precision library (for Integers)@@
1300 <nidx>-lHS library</nidx>
1301 <nidx>-lHS_cbits library</nidx>
1302 <nidx>-lHSrts library</nidx>
1303 <nidx>-lgmp library</nidx>
1305 <tag>@-syslib <name>@:</tag>
1306 <nidx>-syslib <name> option</nidx>
1308 If you are using a Haskell ``system library'' (e.g., the POSIX
1309 library), just use the @-syslib posix@ option, and the correct code
1310 should be linked in.
1312 <tag>@-L<dir>@:</tag>
1313 <nidx>-L<dir> option</nidx>
1314 Where to find user-supplied libraries... Prepend the directory
1315 @<dir>@ to the library directories path.
1317 <tag>@-static@:</tag>
1318 <nidx>-static option</nidx>
1319 Tell the linker to avoid shared libraries.
1321 <tag>@-no-link-chk@ and @-link-chk@:</tag>
1322 <nidx>-no-link-chk option</nidx>
1323 <nidx>-link-chk option</nidx>
1324 <nidx>consistency checking of executables</nidx>
1325 By default, immediately after linking an executable, GHC verifies that
1326 the pieces that went into it were compiled with compatible flags; a
1327 ``consistency check''.
1328 (This is to avoid mysterious failures caused by non-meshing of
1329 incompatibly-compiled programs; e.g., if one @.o@ file was compiled
1330 for a parallel machine and the others weren't.) You may turn off this
1331 check with @-no-link-chk@. You can turn it (back) on with
1332 @-link-chk@ (the default).
1334 <tag><tt>-no-hs-main</tt>:</tag>
1335 <nidx>-no-hs-main option</nidx>
1336 <nidx>linking Haskell libraries with foreign code</nidx>
1338 In the event you want to include ghc-compiled code as part of another
1339 (non-Haskell) program, the RTS will not be supplying its definition of
1340 <tt/main()/ at link-time, you will have to. To signal that to the
1341 driver script when linking, use <tt/-no-hs-main/.
1343 Notice that since the command-line passed to the linker is rather
1344 involved, you probably want to use the ghc driver script to do the
1345 final link of your `mixed-language' application. This is not a
1346 requirement though, just try linking once with <tt/-v/ on to see what
1347 options the driver passes through to the linker.
1351 %************************************************************************
1353 <sect1>Using Concurrent Haskell
1355 <nidx>Concurrent Haskell---use</nidx>
1357 %************************************************************************
1359 GHC (as of version 4.00) supports Concurrent Haskell by default,
1360 without requiring a special option or libraries compiled in a certain
1361 way. To get access to the support libraries for Concurrent Haskell
1362 (ie. @Concurrent@ and friends), use the @-syslib concurrent@ option.
1364 Three RTS options are provided for modifying the behaviour of the
1365 threaded runtime system. See the descriptions of @-C[<us>]@, @-q@,
1366 and @-t<num>@ in Section <ref name="RTS options for
1367 Concurrent/Parallel Haskell" id="parallel-rts-opts">.
1369 Concurrent Haskell is described in more detail in Section <ref
1370 name="Concurrent and Parallel Haskell" id="concurrent-and-parallel">.
1372 %************************************************************************
1374 <sect1>Using Parallel Haskell
1376 <nidx>Parallel Haskell---use</nidx>
1378 %************************************************************************
1380 [You won't be able to execute parallel Haskell programs unless PVM3
1381 (Parallel Virtual Machine, version 3) is installed at your site.]
1383 To compile a Haskell program for parallel execution under PVM, use the
1384 @-parallel@ option,<nidx>-parallel option</nidx> both when compiling
1385 <em>and linking</em>. You will probably want to @import Parallel@
1386 into your Haskell modules.
1388 To run your parallel program, once PVM is going, just invoke it ``as
1389 normal''. The main extra RTS option is @-N<n>@, to say how many
1390 PVM ``processors'' your program to run on. (For more details of
1391 all relevant RTS options, please see Section <ref name="RTS options for Concurrent/Parallel Haskell" id="parallel-rts-opts">.)
1393 In truth, running Parallel Haskell programs and getting information
1394 out of them (e.g., parallelism profiles) is a battle with the vagaries of
1395 PVM, detailed in the following sections.
1397 %************************************************************************
1399 <sect2>Dummy's guide to using PVM
1401 <nidx>PVM, how to use</nidx>
1402 <nidx>Parallel Haskell---PVM use</nidx>
1404 %************************************************************************
1406 Before you can run a parallel program under PVM, you must set the
1407 required environment variables (PVM's idea, not ours); something like,
1408 probably in your @.cshrc@ or equivalent:
1410 setenv PVM_ROOT /wherever/you/put/it
1411 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
1412 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
1415 Creating and/or controlling your ``parallel machine'' is a purely-PVM
1416 business; nothing specific to Parallel Haskell.
1418 You use the @pvm@<nidx>pvm command</nidx> command to start PVM on your
1419 machine. You can then do various things to control/monitor your
1420 ``parallel machine;'' the most useful being:
1423 @Control-D@ & exit @pvm@, leaving it running \\
1424 @halt@ & kill off this ``parallel machine'' \& exit \\
1425 @add <host>@ & add @<host>@ as a processor \\
1426 @delete <host>@ & delete @<host>@ \\
1427 @reset@ & kill what's going, but leave PVM up \\
1428 @conf@ & list the current configuration \\
1429 @ps@ & report processes' status \\
1430 @pstat <pid>@ & status of a particular process \\
1433 The PVM documentation can tell you much, much more about @pvm@!
1435 %************************************************************************
1437 <sect2>Parallelism profiles
1439 <nidx>parallelism profiles</nidx>
1440 <nidx>profiles, parallelism</nidx>
1441 <nidx>visualisation tools</nidx>
1443 %************************************************************************
1445 With Parallel Haskell programs, we usually don't care about the
1446 results---only with ``how parallel'' it was! We want pretty pictures.
1448 Parallelism profiles (\`a la @hbcpp@) can be generated with the
1449 @-q@<nidx>-q RTS option (concurrent, parallel)</nidx> RTS option. The
1450 per-processor profiling info is dumped into files named
1451 @<full-path><program>.gr@. These are then munged into a PostScript picture,
1452 which you can then display. For example, to run your program
1453 @a.out@ on 8 processors, then view the parallelism profile, do:
1456 % ./a.out +RTS -N8 -q
1457 % grs2gr *.???.gr > temp.gr # combine the 8 .gr files into one
1458 % gr2ps -O temp.gr # cvt to .ps; output in temp.ps
1459 % ghostview -seascape temp.ps # look at it!
1462 The scripts for processing the parallelism profiles are distributed
1463 in @ghc/utils/parallel/@.
1465 %************************************************************************
1467 <sect2>Other useful info about running parallel programs
1470 %************************************************************************
1472 The ``garbage-collection statistics'' RTS options can be useful for
1473 seeing what parallel programs are doing. If you do either
1474 @+RTS -Sstderr@<nidx>-Sstderr RTS option</nidx> or @+RTS -sstderr@, then
1475 you'll get mutator, garbage-collection, etc., times on standard
1476 error. The standard error of all PE's other than the `main thread'
1477 appears in @/tmp/pvml.nnn@, courtesy of PVM.
1479 Whether doing @+RTS -Sstderr@ or not, a handy way to watch
1480 what's happening overall is: @tail -f /tmp/pvml.nnn@.
1482 %************************************************************************
1484 <sect2>RTS options for Concurrent/Parallel Haskell
1485 <label id="parallel-rts-opts">
1487 <nidx>RTS options, concurrent</nidx>
1488 <nidx>RTS options, parallel</nidx>
1489 <nidx>Concurrent Haskell---RTS options</nidx>
1490 <nidx>Parallel Haskell---RTS options</nidx>
1492 %************************************************************************
1494 Besides the usual runtime system (RTS) options
1495 (Section <ref name="Running a compiled program" id="runtime-control">), there are a few options particularly
1496 for concurrent/parallel execution.
1500 <nidx>-N<N> RTS option (parallel)</nidx>
1501 (PARALLEL ONLY) Use @<N>@ PVM processors to run this program;
1504 <tag>@-C[<us>]@:</tag>
1505 <nidx>-C<us> RTS option</nidx>
1506 Sets the context switch interval to @<us>@ microseconds. A context
1507 switch will occur at the next heap allocation after the timer expires.
1508 With @-C0@ or @-C@, context switches will occur as often as
1509 possible (at every heap allocation). By default, context switches
1510 occur every 10 milliseconds. Note that many interval timers are only
1511 capable of 10 millisecond granularity, so the default setting may be
1512 the finest granularity possible, short of a context switch at every
1515 [NOTE: this option currently has no effect (version 4.00). Context
1516 switches happen when the current heap block is full, i.e. every 4k of
1520 <nidx>-q RTS option</nidx>
1521 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
1522 in the file @<program>.qp@. In the style of @hbcpp@, this profile
1523 records the movement of threads between the green (runnable) and red
1524 (blocked) queues. If you specify the verbose suboption (@-qv@), the
1525 green queue is split into green (for the currently running thread
1526 only) and amber (for other runnable threads). We do not recommend
1527 that you use the verbose suboption if you are planning to use the
1528 @hbcpp@ profiling tools or if you are context switching at every heap
1531 <tag>@-t<num>@:</tag>
1532 <nidx>-t<num> RTS option</nidx>
1533 (PARALLEL ONLY) Limit the number of concurrent threads per processor
1534 to @<num>@. The default is 32. Each thread requires slightly over 1K
1535 <em>words</em> in the heap for thread state and stack objects. (For
1536 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
1540 <nidx>-d RTS option (parallel)</nidx>
1541 (PARALLEL ONLY) Turn on debugging. It pops up one xterm (or GDB, or
1542 something...) per PVM processor. We use the standard @debugger@
1543 script that comes with PVM3, but we sometimes meddle with the
1544 @debugger2@ script. We include ours in the GHC distribution,
1545 in @ghc/utils/pvm/@.
1547 <tag>@-e<num>@:</tag>
1548 <nidx>-e<num> RTS option (parallel)</nidx>
1549 (PARALLEL ONLY) Limit the number of pending sparks per processor to
1550 @<num>@. The default is 100. A larger number may be appropriate if
1551 your program generates large amounts of parallelism initially.
1553 <tag>@-Q<num>@:</tag>
1554 <nidx>-Q<num> RTS option (parallel)</nidx>
1555 (PARALLEL ONLY) Set the size of packets transmitted between processors
1556 to @<num>@. The default is 1024 words. A larger number may be
1557 appropriate if your machine has a high communication cost relative to