[project @ 1998-01-30 17:01:49 by simonm]
[ghc-hetmet.git] / ghc / docs / users_guide / using.vsgml
1 <sect> Using GHC
2 <label id="using-GHC">
3 <p>
4 <nidx>GHC, using</nidx>
5 <nidx>using GHC</nidx>
6
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.
13
14 %************************************************************************
15 %*                                                                      *
16 <sect1> Overall command-line structure
17 <label id="command-line-structure">
18 <p>
19 <nidx>structure, command-line</nidx>
20 <nidx>command-line structure</nidx>
21 %*                                                                      *
22 %************************************************************************
23
24 An invocation of GHC takes the following form:
25
26 <tscreen> <verb>
27 ghc [argument...]
28 </verb> </tscreen>
29
30 Command-line arguments are either options or file names.
31
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.)
40
41 %************************************************************************
42 %*                                                                      *
43 <sect1>Meaningful file suffixes
44 <label id="file-suffixes">
45 <p>
46 <nidx>suffixes, file</nidx>
47 <nidx>file suffixes for GHC</nidx>
48 %*                                                                      *
49 %************************************************************************
50
51 File names with ``meaningful'' suffixes (e.g., @.lhs@ or @.o@)
52 cause the ``right thing'' to happen to those files.
53
54 <descrip>
55 <tag>@.lhs@:</tag>
56 <nidx>lhs suffix</nidx>
57 A ``literate Haskell'' module.
58
59 <tag>@.hs@:</tag> 
60 A not-so-literate Haskell module.
61
62 <tag>@.hi@:</tag>
63 A Haskell interface file, probably compiler-generated.
64
65 <tag>@.hc@:</tag>
66 Intermediate C file produced by the Haskell compiler.
67
68 <tag>@.c@:</tag>
69 A C~file not produced by the Haskell compiler.
70
71 % <tag>@.i@:</tag>
72 % C code after it has be preprocessed by the C compiler (using the
73 % @-E@ flag).
74
75 <tag>@.s@:</tag>
76 An assembly-language source file, usually
77 produced by the compiler.
78
79 <tag>@.o@:</tag>
80 An object file, produced by an assembler.
81 </descrip>
82
83 Files with other suffixes (or without suffixes) are passed straight
84 to the linker.
85
86 %************************************************************************
87 %*                                                                      *
88 <sect1>Help and verbosity options
89 <label id="options-help">
90 <p>
91 <nidx>help options (GHC)</nidx>
92 <nidx>verbose option (GHC)</nidx>
93 %*                                                                      *
94 %************************************************************************
95
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.
100
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).
106
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.
110
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.
114
115 %************************************************************************
116 %*                                                                      *
117 <sect1>Running the right phases in the right order
118 <label id="options-order">
119 <p>
120 <nidx>order of passes in GHC</nidx>
121 <nidx>pass ordering in GHC</nidx>
122 %*                                                                      *
123 %************************************************************************
124
125 The basic task of the @ghc@ driver is to run each input file
126 through the right phases (parsing, linking, etc.).
127
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:
131
132 <tabular ca="llll">
133 Phase of the           | Suffix saying | Flag saying   | (suffix of) @@
134 compilation system     | ``start here''| ``stop after''| output file @@
135 @@
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 @@
142 </tabular>
143 <nidx>-C option</nidx>
144 <nidx>-S option</nidx>
145 <nidx>-c option</nidx>
146
147 Thus, a common invocation would be: @ghc -c Foo.hs@
148
149 Note: What the Haskell compiler proper produces depends on whether a
150 native-code generator is used (producing assembly language) or not
151 (producing C).
152
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.
156
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.
163
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.)
167
168 %************************************************************************
169 %*                                                                      *
170 <sect1>Re-directing the compilation output(s)
171 <label id="options-output">
172 <p>
173 <nidx>output-directing options</nidx>
174 %*                                                                      *
175 %************************************************************************
176
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
180 phase to file @foo@.
181
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!
185
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 &lt;dir&gt; option</nidx> (the
191 ``Oh, dear'' option).  For example:
192
193 <tscreen><verb>
194 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
195 </verb></tscreen>
196
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.
201
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@.
205
206 MORE EXOTICA: The @-osuf <suffix>@<nidx>-osuf &lt;suffix&gt;
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 &lt;suffix&gt;
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">).
213
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...
218
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.
224
225 <sect2> Saving GHC's standard error output
226 <label id="saving-ghc-stderr">
227 <p>
228 <nidx>standard error, saving</nidx>
229
230 Sometimes, you may cause GHC to be rather chatty on standard error;
231 with @-fshow-import-specs@, for example.  You can instruct GHC to
232 <em>append</em> this output to a particular log file with a @-odump
233 <blah>@<nidx>-odump &lt;blah&gt; option</nidx> option.
234
235 <sect2> Redirecting temporary files
236 <label id="temp-files">
237 <p>
238 <nidx>temporary files, redirecting</nidx>
239
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 &lt;dir&gt; option</nidx> option
243 to specify an alternate directory.  For example, @-tmpdir .@ says to
244 put temporary files in the current working directory.
245
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.
250
251 Even better idea: Set the @TMPDIR@ variable when building GHC, and
252 never worry about @TMPDIR@ again. (see the build documentation).
253
254 %************************************************************************
255 %*                                                                      *
256 <sect1>Warnings and sanity-checking
257 <label id="options-sanity">
258 <p>
259 <nidx>sanity-checking options</nidx>
260 <nidx>warnings</nidx>
261 %*                                                                      *
262 %************************************************************************
263
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:
271
272 <descrip>
273
274 <tag>@-Wnot@:</tag>
275 <nidx>-Wnot option</nidx>
276
277 Turns off all warnings, including the standard ones.
278
279 <tag>@-w@:</tag>
280 <nidx>-w option</nidx>
281
282 Synonym for @-Wnot@.
283
284 <tag>@-W@:</tag>
285 <nidx>-W option</nidx>
286
287 Provides the standard warnings plus @-fwarn-incomplete-patterns@
288 and @-fwarn-unused-names@.
289
290 <tag>@-Wall@:</tag>
291 <nidx>-Wall option</nidx>
292
293 Turns on all warning options.
294
295 </descrip>
296
297 The full set of warning options is described below.  To turn off any
298 warning, simply give the corresponding @-fno-warn-...@ option on
299 the command line.
300
301 <descrip>
302
303 <tag>@-fwarn-name-shadowing@:</tag> 
304 <nidx>-fwarn-name-shadowing option</nidx>
305 <nidx>shadowing, warning</nidx>
306
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@.
312
313 Consequently, this option does <em>not</em> allow cyclic recursive
314 definitions.
315
316 <tag>@-fwarn-overlapping-patterns@:</tag>
317 <nidx>-fwarn-overlapping-patterns option</nidx>
318 <nidx>overlapping patterns, warning</nidx>
319 <nidx>patterns, overlapping</nidx>
320
321 By default, the compiler will warn you if a set of patterns are either
322 incomplete (i.e., you're only matching on a subset of an algebraic
323 data type's constructors), or overlapping, i.e.,
324
325 <tscreen><verb>
326 f :: String -> Int
327 f []     = 0
328 f (_:xs) = 1
329 f "2"    = 2
330
331 g [] = 2
332 </verb></tscreen>
333
334 where the last pattern match in @f@ won't ever be reached, as the
335 second pattern overlaps it. More often than not, redundant patterns
336 is a programmer mistake/error, so this option is enabled by default.
337
338 <tag>@-fwarn-incomplete-patterns@:</tag>
339 <nidx>-fwarn-incomplete-patterns option</nidx>
340 <nidx>incomplete patterns, warning</nidx>
341 <nidx>patterns, incomplete</nidx>
342
343 Similarly for incomplete patterns, the function @g@ will fail when
344 applied to non-empty lists, so the compiler will emit a warning about
345 this when this option is enabled.
346
347 <tag>@-fwarn-missing-methods@:</tag>
348 <nidx>-fwarn-missing-methods option</nidx>
349 <nidx>missing methods, warning</nidx>
350 <nidx>methods, missing</nidx>
351
352 This option is on by default, and warns you whenever an instance
353 declaration is missing one or more methods, and the corresponding
354 class declaration has no default declaration for them.
355
356 <tag>@-fwarn-unused-names@:</tag>
357 <nidx>-fwarn-unused-names option</nidx>
358 <nidx>unused names, warning</nidx>
359 <nidx>names, unused</nidx>
360
361 Have the renamer report which locally defined names are not used/exported.
362
363 <tag>@-fwarn-duplicate-exports@:</tag>
364 <nidx>-fwarn-duplicate-exports option</nidx>
365 <nidx>duplicate exports, warning</nidx>
366 <nidx>export lists, duplicates</nidx>
367
368 Have the compiler warn about duplicate entries in export lists. This
369 is useful information if you maintain large export lists, and want to
370 avoid the continued export of a definition after you've deleted (one)
371 mention of it in the export list.
372
373 This option is on by default.
374
375 </descrip>
376
377 If you would like GHC to check that every top-level value has a type
378 signature, use the @-fsignatures-required@
379 option.<nidx>-fsignatures-required option</nidx>
380
381 If you're feeling really paranoid, the @-dcore-lint@
382 option<nidx>-dcore-lint option</nidx> is a good choice.  It turns on
383 heavyweight intra-pass sanity-checking within GHC.  (It checks GHC's
384 sanity, not yours.)
385
386 %************************************************************************
387 %*                                                                      *
388 <sect1>Separate compilation
389 <label id="separate-compilation">
390 <p>
391 <nidx>separate compilation</nidx>
392 <nidx>recompilation checker</nidx>
393 <nidx>make and recompilation</nidx>
394 %*                                                                      *
395 %************************************************************************
396
397 This section describes how GHC supports separate compilation.
398
399 <sect2>Interface files
400 <label id="hi-files">
401 <p>
402 <nidx>interface files</nidx>
403 <nidx>.hi files</nidx>
404
405 When GHC compiles a source file @F@ which contains a module @A@, say,
406 it generates an object @F.o@, <em>and</em> a companion <em>interface
407 file</em> @A.hi@.  
408
409 NOTE: Having the name of the interface file follow the module name and
410 not the file name, means that working with tools such as @make(1)@
411 become harder. @make@ implicitly assumes that any output files
412 produced by processing a translation unit will have file names that
413 can be derived from the file name of the translation unit.  For
414 instance, pattern rules becomes unusable.  For this reason, we
415 recommend you stick to using the same file name as the module name.
416
417 The interface file for @A@ contains information needed by the compiler
418 when it compiles any module @B@ that imports @A@, whether directly or
419 indirectly.  When compiling @B@, GHC will read @A.hi@ to find the
420 details that it needs to know about things defined in @A@.
421
422 Furthermore, when compiling module @C@ which imports @B@, GHC may
423 decide that it needs to know something about @A@ --- for example, @B@
424 might export a function that involves a type defined in @A@.  In this
425 case, GHC will go and read @A.hi@ even though @C@ does not explicitly
426 import @A@ at all.
427
428 The interface file may contain all sorts of things that aren't
429 explicitly exported from @A@ by the programmer.  For example, even
430 though a data type is exported abstractly, @A.hi@ will contain the
431 full data type definition.  For small function definitions, @A.hi@
432 will contain the complete definition of the function.  For bigger
433 functions, @A.hi@ will contain strictness information about the
434 function.  And so on.  GHC puts much more information into @.hi@ files
435 when optimisation is turned on with the @-O@ flag.  Without @-O@ it
436 puts in just the minimum; with @-O@ it lobs in a whole pile of stuff.
437 <nidx>optimsation, effect on .hi files</nidx>
438
439 @A.hi@ should really be thought of as a compiler-readable version of
440 @A.o@.  If you use a @.hi@ file that wasn't generated by the same
441 compilation run that generates the @.o@ file the compiler may assume
442 all sorts of incorrect things about @A@, resulting in core dumps and
443 other unpleasant happenings.
444
445 %************************************************************************
446 %*                                                                      *
447 <sect2>Finding interface files
448 <label id="options-finding-imports">
449 <p>
450 <nidx>interface files, finding them</nidx>
451 <nidx>finding interface files</nidx>
452 %*                                                                      *
453 %************************************************************************
454
455 In your program, you import a module @Foo@ by saying
456 @import Foo@.  GHC goes looking for an interface file, @Foo.hi@.
457 It has a builtin list of directories (notably including @.@) where
458 it looks.
459
460 The @-i<dirs>@ option<nidx>-i&lt;dirs&gt; option</nidx> prepends a
461 colon-separated list of @dirs@ to the ``import directories'' list.
462
463 A plain @-i@ resets the ``import directories'' list back to nothing.
464
465 GHC normally imports @Prelude.hi@ files for you.  If you'd rather
466 it didn't, then give it a @-fno-implicit-prelude@
467 option<nidx>-fno-implicit-prelude option</nidx>.  You are unlikely to get
468 very far without a Prelude, but, hey, it's a free country.
469
470 If you are using a system-supplied non-Prelude library (e.g., the HBC
471 library), just use a @-syslib hbc@<nidx>-syslib &lt;lib&gt; option</nidx>
472 option (for example).  The right interface files should then be
473 available.
474
475 Once a Haskell module has been compiled to C (@.hc@ file), you may
476 wish to specify where GHC tells the C compiler to look for @.h@
477 files.  (Or, if you are using the @-cpp@ option<nidx>-cpp option</nidx>,
478 where it tells the C pre-processor to look...)  For this purpose, use
479 a @-I<dir>@<nidx>-I&lt;dir&gt; option</nidx> in the usual C-ish way.
480
481 %************************************************************************
482 %*                                                                      *
483 <sect2>Other options related to interface files
484 <label id="hi-options">
485 <p>
486 <nidx>interface files, options</nidx>
487 %*                                                                      *
488 %************************************************************************
489
490 The interface output may be directed to another file
491 @bar2/Wurble.iface@ with the option @-ohi bar2/Wurble.iface@<nidx>-ohi
492 &lt;file&gt; option</nidx> (not recommended).
493
494 To avoid generating an interface file at all, use a @-nohi@
495 option.<nidx>-nohi option</nidx>
496
497 The compiler does not overwrite an existing @.hi@ interface file if
498 the new one is byte-for-byte the same as the old one; this is friendly
499 to @make@.  When an interface does change, it is often enlightening to
500 be informed.  The @-hi-diffs@<nidx>-hi-diffs option</nidx> option will
501 make @ghc@ run @diff@ on the old and new @.hi@ files. You can also
502 record the difference in the interface file itself, the
503 @-keep-hi-diffs@<nidx>-keep-hi-diffs</nidx> option takes care of that.
504
505 The @.hi@ files from GHC contain ``usage'' information which changes
506 often and uninterestingly.  If you really want to see these changes
507 reported, you need to use the
508 @-hi-diffs-with-usages@<nidx>-hi-diffs-with-usages option</nidx>
509 option.
510
511 Interface files are normally jammed full of compiler-produced
512 <em>pragmas</em>, which record arities, strictness info, etc.  If you
513 think these pragmas are messing you up (or you are doing some kind of
514 weird experiment), you can tell GHC to ignore them with the
515 @-fignore-interface-pragmas@<nidx>-fignore-interface-pragmas
516 option</nidx> option.
517
518 When compiling without optimisations on, the compiler is extra-careful
519 about not slurping in data constructors and instance declarations that
520 it will not need. If you believe it is getting it wrong and not
521 importing stuff which you think it should, this optimisation can be
522 turned off with @-fno-prune-tydecls@ and @-fno-prune-instdecls@.
523 <nidx>-fno-prune-tydecls option</nidx><nidx>-fno-prune-instdecls
524 option</nidx>
525
526 See also Section <ref name="Linking and consistency-checking" id="options-linker">, which describes how the linker
527 finds standard Haskell libraries.
528
529 %************************************************************************
530 %*                                                                      *
531 <sect2>The recompilation checker
532 <label id="recomp">
533 <p>
534 <nidx>recompilation checker</nidx>
535 %*                                                                      *
536 %************************************************************************
537
538 In the olden days, GHC compared the newly-generated @.hi@ file with
539 the previous version; if they were identical, it left the old one
540 alone and didn't change its modification date.  In consequence,
541 importers of a module with an unchanged output @.hi@ file were not
542 recompiled.
543
544 This doesn't work any more.  In our earlier example, module @C@ does
545 not import module @A@ directly, yet changes to @A.hi@ should force a
546 recompilation of @C@.  And some changes to @A@ (changing the
547 definition of a function that appears in an inlining of a function
548 exported by @B@, say) may conceivably not change @B.hi@ one jot.  So
549 now...
550
551 GHC keeps a version number on each interface file, and on each type
552 signature within the interface file.  It also keeps in every interface
553 file a list of the version numbers of everything it used when it last
554 compiled the file.  If the source file's modification date is earlier
555 than the @.o@ file's date (i.e. the source hasn't changed since the
556 file was last compiled), and you give GHC the @-recomp@<nidx>-recomp
557 option</nidx> flag, then GHC will be clever.  It compares the version
558 numbers on the things it needs this time with the version numbers on
559 the things it needed last time (gleaned from the interface file of the
560 module being compiled); if they are all the same it stops compiling
561 rather early in the process saying ``Compilation IS NOT required''.
562 What a beautiful sight!
563
564 It's still an experimental feature (that's why @-recomp@ is off by
565 default), so tell us if you think it doesn't work.
566
567 Patrick Sansom has a workshop paper about how all this is done.  Ask
568 him (email: <htmlurl name="sansom@@dcs.gla.ac.uk"
569 url="mailto:sansom@@dcs.gla.ac.uk">) if you want a copy.
570
571 %************************************************************************
572 %*                                                                      *
573 <sect2>Using @make@
574 <label id="using-make">
575 <p>
576 <ncdx>make</ncdx>
577 %*                                                                      *
578 %************************************************************************
579
580 It is reasonably straightforward to set up a @Makefile@ to use with
581 GHC, assuming you name your source files the same as your modules.
582 Thus:
583
584 <tscreen><verb>
585 HC      = ghc
586 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
587
588 SRCS = Main.lhs Foo.lhs Bar.lhs
589 OBJS = Main.o   Foo.o   Bar.o
590
591 .SUFFIXES : .o .hi .lhs .hc .s
592
593 cool_pgm : $(OBJS)
594         rm $@
595         $(HC) -o $@ $(HC_OPTS) $(OBJS)
596
597 # Standard suffix rules
598 .o.hi:
599         @:
600
601 .lhs.o:
602         $(HC) -c $< $(HC_OPTS)
603
604 .hs.o:
605         $(HC) -c $< $(HC_OPTS)
606
607 # Inter-module dependencies
608 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
609 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
610 </verb></tscreen>
611
612 (Sophisticated @make@ variants may achieve some of the above more
613 elegantly.  Notably, @gmake@'s pattern rules let you write the more
614 comprehensible:
615
616 <tscreen><verb>
617 %.o : %.lhs
618         $(HC) -c $< $(HC_OPTS)
619 </verb></tscreen>
620
621 What we've shown should work with any @make@.)
622
623 Note the cheesy @.o.hi@ rule: It records the dependency of the
624 interface (@.hi@) file on the source.  The rule says a @.hi@ file can
625 be made from a @.o@ file by doing... nothing.  Which is true.
626
627 Note the inter-module dependencies at the end of the Makefile, which
628 take the form
629
630 <tscreen><verb>
631 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
632 </verb></tscreen>
633
634 They tell @make@ that if any of @Foo.o@, @Foo.hc@ or @Foo.s@ have an
635 earlier modification date than @Baz.hi@, then the out-of-date file
636 must be brought up to date.  To bring it up to date, @make@ looks for
637 a rule to do so; one of the preceding suffix rules does the job
638 nicely.
639
640 Putting inter-dependencies of the form @Foo.o : Bar.hi@ into your
641 @Makefile@ by hand is rather error-prone.  Don't worry---never fear,
642 @mkdependHS@ is here! (and is distributed as part of GHC) Add the
643 following to your @Makefile@:
644
645 <tscreen><verb>
646 depend :
647         mkdependHS -- $(HC_OPTS) -- $(SRCS)
648 </verb></tscreen>
649
650 Now, before you start compiling, and any time you change the @imports@
651 in your program, do @make depend@ before you do @make cool_pgm@.
652 @mkdependHS@ will append the needed dependencies to your @Makefile@.
653 @mkdependHS@ is fully describe in Section <ref name="Makefile
654 dependencies in Haskell: using mkdependHS" id="mkdependHS">.
655
656 A few caveats about this simple scheme:
657
658 <itemize>
659
660 <item> You may need to compile some modules explicitly to create their
661 interfaces in the first place (e.g., @make Bar.o@ to create @Bar.hi@).
662
663 <item> You may have to type @make@ more than once for the dependencies
664 to have full effect.  However, a @make@ run that does nothing
665 <em>does</em> mean ``everything's up-to-date.''
666
667 <item> This scheme will work with mutually-recursive modules but,
668 again, it may take multiple iterations to ``settle.''
669
670 </itemize>
671
672 %************************************************************************
673 %*                                                                      *
674 <sect1>Optimisation (code improvement)
675 <label id="options-optimise">
676 <p>
677 <nidx>optimisation (GHC)</nidx>
678 <nidx>improvement, code (GHC)</nidx>
679 %*                                                                      *
680 %************************************************************************
681
682 The @-O*@ options specify convenient ``packages'' of optimisation
683 flags; the @-f*@ options described later on specify
684 <em>individual</em> optimisations to be turned on/off; the @-m*@
685 options specify <em>machine-specific</em> optimisations to be turned
686 on/off.
687
688 %----------------------------------------------------------------------
689 <sect2>@-O*@: convenient ``packages'' of optimisation flags.
690 <label id="optimise-pkgs">
691 <p>
692 <nidx>-O options</nidx>
693
694 There are <em>many</em> options that affect the quality of code
695 produced by GHC.  Most people only have a general goal, something like
696 ``Compile quickly'' or ``Make my program run like greased lightning.''
697 The following ``packages'' of optimisations (or lack thereof) should
698 suffice.
699
700 Once you choose a @-O*@ ``package,'' stick with it---don't chop and
701 change.  Modules' interfaces <em>will</em> change with a shift to a new
702 @-O*@ option, and you may have to recompile a large chunk of all
703 importing modules before your program can again be run
704 safely (see Section <ref name="The recompilation checker" id="recomp">).
705
706 <descrip>
707 <tag>No @-O*@-type option specified:</tag>
708 <nidx>-O* not specified</nidx>
709 This is taken to mean: ``Please compile quickly; I'm not over-bothered
710 about compiled-code quality.''  So, for example: @ghc -c Foo.hs@
711
712 <tag>@-O@ or @-O1@:</tag>
713 <nidx>-O option</nidx>
714 <nidx>-O1 option</nidx>
715 <nidx>optimise normally</nidx>
716 Means: ``Generate good-quality code without taking too long about it.''
717 Thus, for example: @ghc -c -O Main.lhs@
718
719 <tag>@-O2@:</tag>
720 <nidx>-O2 option</nidx>
721 <nidx>optimise aggressively</nidx>
722 Means: ``Apply every non-dangerous optimisation, even if it means
723 significantly longer compile times.''
724
725 The avoided ``dangerous'' optimisations are those that can make
726 runtime or space <em>worse</em> if you're unlucky.  They are
727 normally turned on or off individually.
728
729 At the moment, @-O2@ is <em>unlikely</em> to produce
730 better code than @-O@.
731
732 <tag>@-O2-for-C@:</tag>
733 <nidx>-O2-for-C option</nidx>
734 <nidx>gcc, invoking with -O2</nidx>
735
736 Says to run GCC with @-O2@, which may be worth a few percent in
737 execution speed.  Don't forget @-fvia-C@, lest you use the native-code
738 generator and bypass GCC altogether!
739
740 <tag>@-Onot@:</tag>
741 <nidx>-Onot option</nidx>
742 <nidx>optimising, reset</nidx>
743
744 This option will make GHC ``forget'' any -Oish options it has seen so
745 far.  Sometimes useful; for example: @make all EXTRA_HC_OPTS=-Onot@.
746
747 <tag>@-Ofile <file>@:</tag>
748 <nidx>-Ofile &lt;file&gt; option</nidx>
749 <nidx>optimising, customised</nidx>
750
751 For those who need <em>absolute</em> control over <em>exactly</em>
752 what options are used (e.g., compiler writers, sometimes :-), a list
753 of options can be put in a file and then slurped in with @-Ofile@.
754
755 In that file, comments are of the @#@-to-end-of-line variety; blank
756 lines and most whitespace is ignored.
757
758 Please ask if you are baffled and would like an example of @-Ofile@!
759 </descrip>
760
761 At Glasgow, we don't use a @-O*@ flag for day-to-day work.  We use
762 @-O@ to get respectable speed; e.g., when we want to measure
763 something.  When we want to go for broke, we tend to use @-O -fvia-C
764 -O2-for-C@ (and we go for lots of coffee breaks).
765
766 The easiest way to see what @-O@ (etc) ``really mean'' is to run with
767 @-v@, then stand back in amazement.  Alternatively, just look at the
768 @HsC_minus<blah>@ lists in the @ghc@ driver script.
769
770 %----------------------------------------------------------------------
771 <sect2>@-f*@: platform-independent flags
772 <p>
773 <nidx>-f* options (GHC)</nidx>
774 <nidx>-fno-* options (GHC)</nidx>
775
776 Flags can be turned <em>off</em> individually.  (NB: I hope you have a
777 good reason for doing this....) To turn off the @-ffoo@ flag, just use
778 the @-fno-foo@ flag.<nidx>-fno-&lt;opt&gt; anti-option</nidx> So, for
779 example, you can say @-O2 -fno-strictness@, which will then drop out
780 any running of the strictness analyser.
781
782 The options you are most likely to want to turn off are:
783 @-fno-strictness@<nidx>-fno-strictness option</nidx> (strictness
784 analyser [because it is sometimes slow]),
785 @-fno-specialise@<nidx>-fno-specialise option</nidx> (automatic
786 specialisation of overloaded functions [because it makes your code
787 bigger]) [US spelling also accepted], and
788 @-fno-update-analyser@<nidx>-fno-update-analyser option</nidx>
789 (update analyser, because it sometimes takes a <em>long</em> time).
790
791 Should you wish to turn individual flags <em>on</em>, you are advised
792 to use the @-Ofile@ option, described above.  Because the order in
793 which optimisation passes are run is sometimes crucial, it's quite
794 hard to do with command-line options.
795
796 Here are some ``dangerous'' optimisations you <em>might</em> want to try:
797 <descrip>
798 %------------------------------------------------------------------
799 <tag>@-fvia-C@:</tag>
800 <nidx>-fvia-C option</nidx>
801 <nidx>native code generator, turning off</nidx>
802
803 Compile via C, and don't use the native-code generator.  (There are
804 many cases when GHC does this on its own.)  You might pick up a little
805 bit of speed by compiling via C.  If you use @_ccall_gc_@s or
806 @_casm_@s, you probably <em>have to</em> use @-fvia-C@.
807
808 The lower-case incantation, @-fvia-c@, is synonymous.
809
810 <tag>@-funfolding-creation-threshold<n>@:</tag>
811 <nidx>-funfolding-creation-threshold option</nidx>
812 <nidx>inlining, controlling</nidx>
813 <nidx>unfolding, controlling</nidx>
814 (Default: 30) By raising or lowering this number, you can raise or
815 lower the amount of pragmatic junk that gets spewed into interface
816 files.  (An unfolding has a ``size'' that reflects the cost in terms
817 of ``code bloat'' of expanding that unfolding in another module.  A
818 bigger Core expression would be assigned a bigger cost.)
819
820 <tag>@-funfolding-use-threshold<n>@:</tag>
821 <nidx>-funfolding-use-threshold option</nidx>
822 <nidx>inlining, controlling</nidx>
823 <nidx>unfolding, controlling</nidx>
824 (Default: 3) By raising or lowering this number, you can make the
825 compiler more or less keen to expand unfoldings.
826
827 OK, folks, these magic numbers `30' and `3' are mildly arbitrary; they
828 are of the ``seem to be OK'' variety.  The `3' is the more critical
829 one; it's what determines how eager GHC is about expanding unfoldings.
830
831 % <tag>@-funfolding-override-threshold<n>@:</tag>
832 % (Default: 8) [Pretty obscure]
833 W hen deciding what unfoldings from a module should be made available
834 % to the rest of the world (via this module's interface), the compiler
835 % normally likes ``small'' expressions.
836
837 % For example, if it sees @foo = bar@, it will decide that the very
838 % small expression @bar@ is a great unfolding for @foo@.  But if
839 % @bar@ turns out to be @(True,False,True)@, we would probably
840 % prefer <em>that</em> for the unfolding for @foo@.
841
842 % Should we ``override'' the initial small unfolding from @foo=bar@
843 % with the bigger-but-better one?  Yes, if the bigger one's ``size'' is
844 % still under the ``override threshold.''  You can use this flag to
845 % adjust this threshold (why, I'm not sure).
846
847 % <tag>@-fliberated-case-threshold<n>@:</tag>
848 % (Default: 12) [Vastly obscure: NOT IMPLEMENTED YET]
849 % ``Case liberation'' lifts evaluation out of recursive functions; it
850 % does this by duplicating code.  Done without constraint, you can get
851 % serious code bloat; so we only do it if the ``size'' of the duplicated
852 % code is smaller than some ``threshold.''  This flag can fiddle that
853 % threshold.
854
855 <tag>@-fsemi-tagging@:</tag>
856 This option (which <em>does not work</em> with the native-code generator)
857 tells the compiler to add extra code to test for already-evaluated
858 values.  You win if you have lots of such values during a run of your
859 program, you lose otherwise.  (And you pay in extra code space.)
860
861 We have not played with @-fsemi-tagging@ enough to recommend it.
862 (For all we know, it doesn't even work anymore...  Sigh.)
863 </descrip>
864
865 %----------------------------------------------------------------------
866 <sect2>@-m*@: platform-specific flags
867 <p>
868 <nidx>-m* options (GHC)</nidx>
869 <nidx>platform-specific options</nidx>
870 <nidx>machine-specific options</nidx>
871
872 Some flags only make sense for particular target platforms.
873
874 <descrip>
875 <tag>@-mv8@:</tag>
876 (SPARC machines)<nidx>-mv8 option (SPARC only)</nidx>
877 Means to pass the like-named option to GCC; it says to use the
878 Version 8 SPARC instructions, notably integer multiply and divide.
879 The similiar @-m*@ GCC options for SPARC also work, actually.
880
881 <tag>@-mlong-calls@:</tag>
882 (HPPA machines)<nidx>-mlong-calls option (HPPA only)</nidx>
883 Means to pass the like-named option to GCC.  Required for Very Big
884 modules, maybe.  (Probably means you're in trouble...)
885
886 <tag>@-monly-[32]-regs@:</tag>
887 (iX86 machines)<nidx>-monly-N-regs option (iX86 only)</nidx>
888 GHC tries to ``steal'' four registers from GCC, for performance
889 reasons; it almost always works.  However, when GCC is compiling some
890 modules with four stolen registers, it will crash, probably saying:
891 <tscreen><verb>
892 Foo.hc:533: fixed or forbidden register was spilled.
893 This may be due to a compiler bug or to impossible asm
894 statements or clauses.
895 </verb></tscreen>
896 Just give some registers back with @-monly-N-regs@.  Try `3' first,
897 then `2'.  If `2' doesn't work, please report the bug to us.
898 </descrip>
899
900 %----------------------------------------------------------------------
901 <sect2>Code improvement by the C compiler.
902 <label id="optimise-C-compiler">
903 <p>
904 <nidx>optimisation by GCC</nidx>
905 <nidx>GCC optimisation</nidx>
906
907 The C~compiler (GCC) is run with @-O@ turned on.  (It has
908 to be, actually).
909
910 If you want to run GCC with @-O2@---which may be worth a few
911 percent in execution speed---you can give a
912 @-O2-for-C@<nidx>-O2-for-C option</nidx> option.
913
914 %************************************************************************
915 %*                                                                      *
916 <sect1>Options related to a particular phase
917 <label id="options-phases">
918 <p>
919 %*                                                                      *
920 %************************************************************************
921
922 <sect2> The C pre-processor
923 <label id="c-pre-processor">
924 <p>
925 <nidx>pre-processing: cpp</nidx>
926 <nidx>C pre-processor options</nidx>
927 <nidx>cpp, pre-processing with</nidx>
928
929 The C pre-processor @cpp@ is run over your Haskell code only if the
930 @-cpp@ option <nidx>-cpp option</nidx> is given.  Unless you are
931 building a large system with significant doses of conditional
932 compilation, you really shouldn't need it.
933 <descrip>
934 <tag>@-D<foo>@:</tag>
935 <nidx>-D&lt;name&gt; option</nidx>
936 Define macro @<foo>@ in the usual way.  NB: does <em>not</em> affect
937 @-D@ macros passed to the C~compiler when compiling via C!  For
938 those, use the @-optc-Dfoo@ hack...
939
940 <tag>@-U<foo>@:</tag>
941 <nidx>-U&lt;name&gt; option</nidx>
942 Undefine macro @<foo>@ in the usual way.
943
944 <tag>@-I<dir>@:</tag>
945 <nidx>-I&lt;dir&gt; option</nidx>
946 Specify a directory in which to look for @#include@ files, in
947 the usual C way.
948 </descrip>
949
950 The @ghc@ driver pre-defines several macros:
951 <descrip>
952 <tag>@__HASKELL1__@:</tag>
953 <nidx>__HASKELL1__ macro</nidx>
954 If defined to $n$, that means GHC supports the
955 Haskell language defined in the Haskell report version $1.n$.
956 Currently 4.
957
958 NB: This macro is set both when pre-processing Haskell source and
959 when pre-processing generated C (@.hc@) files.
960
961 <tag>@__GLASGOW_HASKELL__@:</tag>
962 <nidx>__GLASGOW_HASKELL__ macro</nidx>
963 For version $n$ of the GHC system, this will be @#define@d to
964 $100 \times n$.  So, for version~3.00, it is 300.
965
966 This macro is <em>only</em> set when pre-processing Haskell source.
967 (<em>Not</em> when pre-processing generated C.)
968
969 With any luck, @__GLASGOW_HASKELL__@ will be undefined in all other
970 implementations that support C-style pre-processing.
971
972 (For reference: the comparable symbols for other systems are:
973 @__HUGS__@ for Hugs and @__HBC__@ for Chalmers.)
974
975 <tag>@__CONCURRENT_HASKELL__@:</tag>
976 <nidx>__CONCURRENT_HASKELL__ macro</nidx>
977 Only defined when @-concurrent@ is in use!
978 This symbol is defined when pre-processing Haskell (input) and
979 pre-processing C (GHC output).
980
981 <tag>@__PARALLEL_HASKELL__@:</tag>
982 <nidx>__PARALLEL_HASKELL__ macro</nidx>
983 Only defined when @-parallel@ is in use!  This symbol is defined when
984 pre-processing Haskell (input) and pre-processing C (GHC output).
985 </descrip>
986
987 Options other than the above can be forced through to the C
988 pre-processor with the @-opt@ flags (see
989 Section <ref name="Forcing options to a particular phase." id="forcing-options-through">).
990
991 A small word of warning: @-cpp@ is not friendly to ``string
992 gaps''.<nidx>-cpp vs string gaps</nidx><nidx>string gaps vs -cpp</nidx>.  In
993 other words, strings such as the following:
994
995 <tscreen><verb>
996         strmod = "\
997         \ p \
998         \ "
999 </verb></tscreen>
1000
1001 don't work with @-cpp@; @/usr/bin/cpp@ elides the
1002 backslash-newline pairs.
1003
1004 However, it appears that if you add a space at the end of the line,
1005 then @cpp@ (at least GNU @cpp@ and possibly other @cpp@s)
1006 leaves the backslash-space pairs alone and the string gap works as
1007 expected.
1008
1009 %************************************************************************
1010 %*                                                                      *
1011 <sect2>Options affecting the C compiler (if applicable)
1012 <label id="options-C-compiler">
1013 <p>
1014 <nidx>include-file options</nidx>
1015 <nidx>C compiler options</nidx>
1016 <nidx>GCC options</nidx>
1017 %*                                                                      *
1018 %************************************************************************
1019
1020 At the moment, quite a few common C-compiler options are passed on
1021 quietly to the C compilation of Haskell-compiler-generated C files.
1022 THIS MAY CHANGE.  Meanwhile, options so sent are:
1023
1024 <tabular ca="ll">
1025 @-ansi@      | do ANSI C (not K&amp;R) @@
1026 @-pedantic@  | be so@@
1027 @-dgcc-lint@ | (hack) short for ``make GCC very paranoid''@@
1028 </tabular>
1029 <nidx>-ansi option (for GCC)</nidx>
1030 <nidx>-pedantic option (for GCC)</nidx>
1031 <nidx>-dgcc-lint option (GCC paranoia)</nidx>
1032
1033 If you are compiling with lots of @ccalls@, etc., you may need to
1034 tell the C~compiler about some @#include@ files.  There is no real
1035 pretty way to do this, but you can use this hack from the
1036 command-line:
1037
1038 <tscreen><verb>
1039 % ghc -c '-#include <X/Xlib.h>' Xstuff.lhs
1040 </verb></tscreen>
1041
1042
1043 %************************************************************************
1044 %*                                                                      *
1045 <sect2>Linking and consistency-checking
1046 <label id="options-linker">
1047 <p>
1048 <nidx>linker options</nidx>
1049 <nidx>ld options</nidx>
1050 %*                                                                      *
1051 %************************************************************************
1052
1053 GHC has to link your code with various libraries, possibly including:
1054 user-supplied, GHC-supplied, and system-supplied (@-lm@ math
1055 library, for example).
1056
1057 <descrip>
1058 <tag>@-l<FOO>@:</tag>
1059 <nidx>-l&lt;lib&gt; option</nidx>
1060 Link in a library named @lib<FOO>.a@ which resides somewhere on the
1061 library directories path.
1062
1063 Because of the sad state of most UNIX linkers, the order of such
1064 options does matter.  Thus: @ghc -lbar *.o@ is almost certainly
1065 wrong, because it will search @libbar.a@ <em>before</em> it has
1066 collected unresolved symbols from the @*.o@ files.
1067 @ghc *.o -lbar@ is probably better.
1068
1069 The linker will of course be informed about some GHC-supplied
1070 libraries automatically; these are:
1071
1072 <tabular ca="ll">
1073 <bf>-l equivalent</bf> | <bf>description</bf> @@
1074 @@
1075 @-lHSrts,-lHSclib@ | basic runtime libraries @@
1076 @-lHS@         | standard Prelude library @@
1077 @-lHS_cbits@  | C support code for standard Prelude library @@
1078 @-lgmp@        | GNU multi-precision library (for Integers)@@
1079 </tabular>
1080
1081 <nidx>-lHS library</nidx>
1082 <nidx>-lHS_cbits library</nidx>
1083 <nidx>-lHSrts library</nidx>
1084 <nidx>-lgmp library</nidx>
1085
1086 <tag>@-syslib <name>@:</tag>
1087 <nidx>-syslib &lt;name&gt; option</nidx>
1088
1089 If you are using a Haskell ``system library'' (e.g., the POSIX
1090 library), just use the @-syslib posix@ option, and the correct code
1091 should be linked in.
1092
1093 <tag>@-L<dir>@:</tag>
1094 <nidx>-L&lt;dir&gt; option</nidx>
1095 Where to find user-supplied libraries...  Prepend the directory
1096 @<dir>@ to the library directories path.
1097
1098 <tag>@-static@:</tag>
1099 <nidx>-static option</nidx>
1100 Tell the linker to avoid shared libraries.
1101
1102 <tag>@-no-link-chk@ and @-link-chk@:</tag>
1103 <nidx>-no-link-chk option</nidx>
1104 <nidx>-link-chk option</nidx>
1105 <nidx>consistency checking of executables</nidx>
1106 By default, immediately after linking an executable, GHC verifies that
1107 the pieces that went into it were compiled with compatible flags; a
1108 ``consistency check''.
1109 (This is to avoid mysterious failures caused by non-meshing of
1110 incompatibly-compiled programs; e.g., if one @.o@ file was compiled
1111 for a parallel machine and the others weren't.)  You may turn off this
1112 check with @-no-link-chk@.  You can turn it (back) on with
1113 @-link-chk@ (the default).
1114 </descrip>
1115
1116 %************************************************************************
1117 %*                                                                      *
1118 <sect1>Using Concurrent Haskell
1119 <p>
1120 <nidx>Concurrent Haskell---use</nidx>
1121 %*                                                                      *
1122 %************************************************************************
1123
1124 To compile a program as Concurrent Haskell, use the @-concurrent@
1125 option,<nidx>-concurrent option</nidx> both when compiling <em>and
1126 linking</em>.  You will probably need the @-fglasgow-exts@ option, too.
1127
1128 Three RTS options are provided for modifying the behaviour of the
1129 threaded runtime system.  See the descriptions of @-C[<us>]@, @-q@,
1130 and @-t<num>@ in Section <ref name="RTS options for Concurrent/Parallel Haskell" id="parallel-rts-opts">.
1131
1132 %************************************************************************
1133 %*                                                                      *
1134 <sect2>Potential problems with Concurrent Haskell
1135 <label id="concurrent-problems">
1136 <p>
1137 <nidx>Concurrent Haskell problems</nidx>
1138 <nidx>problems, Concurrent Haskell</nidx>
1139 %*                                                                      *
1140 %************************************************************************
1141
1142 The main thread in a Concurrent Haskell program is given its own
1143 private stack space, but all other threads are given stack space from
1144 the heap.  Stack space for the main thread can be
1145 adjusted as usual with the @-K@ RTS
1146 option,<nidx>-K RTS option (concurrent, parallel)</nidx> but if this
1147 private stack space is exhausted, the main thread will switch to stack
1148 segments in the heap, just like any other thread.  Thus, problems
1149 which would normally result in stack overflow in ``sequential Haskell''
1150 can be expected to result in heap overflow when using threads.
1151
1152 The concurrent runtime system uses black holes as synchronisation
1153 points for subexpressions which are shared among multiple threads.  In
1154 ``sequential Haskell'', a black hole indicates a cyclic data
1155 dependency, which is a fatal error.  However, in concurrent execution, a
1156 black hole may simply indicate that the desired expression is being
1157 evaluated by another thread.  Therefore, when a thread encounters a
1158 black hole, it simply blocks and waits for the black hole to be
1159 updated.  Cyclic data dependencies will result in deadlock, and the
1160 program will fail to terminate.
1161
1162 Because the concurrent runtime system uses black holes as
1163 synchronisation points, it is not possible to disable black-holing
1164 with the @-N@ RTS option.<nidx>-N RTS option</nidx> Therefore, the use
1165 of signal handlers (including timeouts) with the concurrent runtime
1166 system can lead to problems if a thread attempts to enter a black hole
1167 that was created by an abandoned computation.  The use of signal
1168 handlers in conjunction with threads is strongly discouraged.
1169
1170
1171 %************************************************************************
1172 %*                                                                      *
1173 <sect1>Using Parallel Haskell
1174 <p>
1175 <nidx>Parallel Haskell---use</nidx>
1176 %*                                                                      *
1177 %************************************************************************
1178
1179 [You won't be able to execute parallel Haskell programs unless PVM3
1180 (Parallel Virtual Machine, version 3) is installed at your site.]
1181
1182 To compile a Haskell program for parallel execution under PVM, use the
1183 @-parallel@ option,<nidx>-parallel option</nidx> both when compiling
1184 <em>and linking</em>.  You will probably want to @import Parallel@
1185 into your Haskell modules.
1186
1187 To run your parallel program, once PVM is going, just invoke it ``as
1188 normal''.  The main extra RTS option is @-N<n>@, to say how many
1189 PVM ``processors'' your program to run on.  (For more details of
1190 all relevant RTS options, please see Section <ref name="RTS options for Concurrent/Parallel Haskell" id="parallel-rts-opts">.)
1191
1192 In truth, running Parallel Haskell programs and getting information
1193 out of them (e.g., parallelism profiles) is a battle with the vagaries of
1194 PVM, detailed in the following sections.
1195
1196 %************************************************************************
1197 %*                                                                      *
1198 <sect2>Dummy's guide to using PVM
1199 <p>
1200 <nidx>PVM, how to use</nidx>
1201 <nidx>Parallel Haskell---PVM use</nidx>
1202 %*                                                                      *
1203 %************************************************************************
1204
1205 Before you can run a parallel program under PVM, you must set the
1206 required environment variables (PVM's idea, not ours); something like,
1207 probably in your @.cshrc@ or equivalent:
1208 <tscreen><verb>
1209 setenv PVM_ROOT /wherever/you/put/it
1210 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
1211 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
1212 </verb></tscreen>
1213
1214 Creating and/or controlling your ``parallel machine'' is a purely-PVM
1215 business; nothing specific to Parallel Haskell.
1216
1217 You use the @pvm@<nidx>pvm command</nidx> command to start PVM on your
1218 machine.  You can then do various things to control/monitor your
1219 ``parallel machine;'' the most useful being:
1220
1221 \begin{tabular}{ll}
1222 @Control-D@ & exit @pvm@, leaving it running \\
1223 @halt@ & kill off this ``parallel machine'' \& exit \\
1224 @add <host>@ & add @<host>@ as a processor \\
1225 @delete <host>@ & delete @<host>@ \\
1226 @reset@ & kill what's going, but leave PVM up \\
1227 @conf@       & list the current configuration \\
1228 @ps@         & report processes' status \\
1229 @pstat <pid>@ & status of a particular process \\
1230 \end{tabular}
1231
1232 The PVM documentation can tell you much, much more about @pvm@!
1233
1234 %************************************************************************
1235 %*                                                                      *
1236 <sect2>Parallelism profiles
1237 <p>
1238 <nidx>parallelism profiles</nidx>
1239 <nidx>profiles, parallelism</nidx>
1240 <nidx>visualisation tools</nidx>
1241 %*                                                                      *
1242 %************************************************************************
1243
1244 With Parallel Haskell programs, we usually don't care about the
1245 results---only with ``how parallel'' it was!  We want pretty pictures.
1246
1247 Parallelism profiles (\`a la @hbcpp@) can be generated with the
1248 @-q@<nidx>-q RTS option (concurrent, parallel)</nidx> RTS option.  The
1249 per-processor profiling info is dumped into files named
1250 @<full-path><program>.gr@.  These are then munged into a PostScript picture,
1251 which you can then display.  For example, to run your program
1252 @a.out@ on 8 processors, then view the parallelism profile, do:
1253
1254 <tscreen><verb>
1255 % ./a.out +RTS -N8 -q
1256 % grs2gr *.???.gr > temp.gr     # combine the 8 .gr files into one
1257 % gr2ps -O temp.gr              # cvt to .ps; output in temp.ps
1258 % ghostview -seascape temp.ps   # look at it!
1259 </verb></tscreen>
1260
1261 The scripts for processing the parallelism profiles are distributed
1262 in @ghc/utils/parallel/@.
1263
1264 %************************************************************************
1265 %*                                                                      *
1266 <sect2>Other useful info about running parallel programs
1267 <p>
1268 %*                                                                      *
1269 %************************************************************************
1270
1271 The ``garbage-collection statistics'' RTS options can be useful for
1272 seeing what parallel programs are doing.  If you do either
1273 @+RTS -Sstderr@<nidx>-Sstderr RTS option</nidx> or @+RTS -sstderr@, then
1274 you'll get mutator, garbage-collection, etc., times on standard
1275 error. The standard error of all PE's other than the `main thread'
1276 appears in @/tmp/pvml.nnn@, courtesy of PVM.
1277
1278 Whether doing @+RTS -Sstderr@ or not, a handy way to watch
1279 what's happening overall is: @tail -f /tmp/pvml.nnn@.
1280
1281 %************************************************************************
1282 %*                                                                      *
1283 <sect2>RTS options for Concurrent/Parallel Haskell
1284 <label id="parallel-rts-opts">
1285 <p>
1286 <nidx>RTS options, concurrent</nidx>
1287 <nidx>RTS options, parallel</nidx>
1288 <nidx>Concurrent Haskell---RTS options</nidx>
1289 <nidx>Parallel Haskell---RTS options</nidx>
1290 %*                                                                      *
1291 %************************************************************************
1292
1293 Besides the usual runtime system (RTS) options
1294 (Section <ref name="Running a compiled program" id="runtime-control">), there are a few options particularly
1295 for concurrent/parallel execution.
1296
1297 <descrip>
1298 <tag>@-N<N>@:</tag>
1299 <nidx>-N&lt;N&gt; RTS option (parallel)</nidx>
1300 (PARALLEL ONLY) Use @<N>@ PVM processors to run this program;
1301 the default is 2.
1302
1303 <tag>@-C[<us>]@:</tag>
1304 <nidx>-C&lt;us&gt; RTS option</nidx>
1305 Sets the context switch interval to @<us>@ microseconds.  A context
1306 switch will occur at the next heap allocation after the timer expires.
1307 With @-C0@ or @-C@, context switches will occur as often as
1308 possible (at every heap allocation).  By default, context switches
1309 occur every 10 milliseconds.  Note that many interval timers are only
1310 capable of 10 millisecond granularity, so the default setting may be
1311 the finest granularity possible, short of a context switch at every
1312 heap allocation.
1313
1314 <tag>@-q[v]@:</tag>
1315 <nidx>-q RTS option</nidx>
1316 Produce a quasi-parallel profile of thread activity, in the file
1317 @<program>.qp@.  In the style of @hbcpp@, this profile records
1318 the movement of threads between the green (runnable) and red (blocked)
1319 queues.  If you specify the verbose suboption (@-qv@), the green
1320 queue is split into green (for the currently running thread only) and
1321 amber (for other runnable threads).  We do not recommend that you use
1322 the verbose suboption if you are planning to use the @hbcpp@
1323 profiling tools or if you are context switching at every heap check
1324 (with @-C@).
1325
1326 <tag>@-t<num>@:</tag>
1327 <nidx>-t&lt;num&gt; RTS option</nidx>
1328 Limit the number of concurrent threads per processor to @<num>@.
1329 The default is 32.  Each thread requires slightly over 1K <em>words</em>
1330 in the heap for thread state and stack objects.  (For 32-bit machines,
1331 this translates to 4K bytes, and for 64-bit machines, 8K bytes.)
1332
1333 <tag>@-d@:</tag>
1334 <nidx>-d RTS option (parallel)</nidx>
1335 (PARALLEL ONLY) Turn on debugging.  It pops up one xterm (or GDB, or
1336 something...) per PVM processor.  We use the standard @debugger@
1337 script that comes with PVM3, but we sometimes meddle with the
1338 @debugger2@ script.  We include ours in the GHC distribution,
1339 in @ghc/utils/pvm/@.
1340
1341 <tag>@-e<num>@:</tag>
1342 <nidx>-e&lt;num&gt; RTS option (parallel)</nidx>
1343 (PARALLEL ONLY) Limit the number of pending sparks per processor to
1344 @<num>@. The default is 100. A larger number may be appropriate if
1345 your program generates large amounts of parallelism initially.
1346
1347 <tag>@-Q<num>@:</tag>
1348 <nidx>-Q&lt;num&gt; RTS option (parallel)</nidx>
1349 (PARALLEL ONLY) Set the size of packets transmitted between processors
1350 to @<num>@. The default is 1024 words. A larger number may be
1351 appropriate if your machine has a high communication cost relative to
1352 computation speed.
1353 </descrip>
1354
1355 %************************************************************************
1356 %*                                                                      *
1357 <sect2>Potential problems with Parallel Haskell
1358 <label id="parallel-problems">
1359 <p>
1360 <nidx>Parallel Haskell---problems</nidx> 
1361 <nidx>problems, Parallel Haskell</nidx> 
1362 %*                                                                      *
1363 %************************************************************************
1364
1365 The ``Potential problems'' for Concurrent Haskell also apply for
1366 Parallel Haskell.  Please see Section <ref name="Potential problems with Concurrent Haskell" id="concurrent-problems">.