[project @ 2000-01-13 14:33:57 by hwloidl]
[ghc-hetmet.git] / ghc / rts / parallel / ParInit.c
1 /* --------------------------------------------------------------------------
2    Time-stamp: <Sat Dec 04 1999 18:26:22 Stardate: [-30]3998.84 hwloidl>
3    $Id: ParInit.c,v 1.2 2000/01/13 14:34:08 hwloidl Exp $
4
5    Initialising the parallel RTS
6
7    An extension based on Kevin Hammond's GRAPH for PVM version
8    P. Trinder, January 17th 1995.
9    Adapted for the new RTS
10    P. Trinder, July 1997.
11    H-W. Loidl, November 1999.
12
13    ------------------------------------------------------------------------ */
14
15 #ifdef PAR /* whole file */
16
17 #define NON_POSIX_SOURCE /* so says Solaris */
18
19 //@menu
20 //* Includes::                  
21 //* Global variables::          
22 //* Initialisation Routines::   
23 //@end menu
24
25 //@node Includes, Global variables
26 //@subsection Includes
27
28 #include "Rts.h"
29 #include "RtsFlags.h"
30 #include "RtsUtils.h"
31 #include "ParallelRts.h"
32 #include <setjmp.h>
33 #include "LLC.h"
34 #include "HLC.h"
35
36 //@node Global variables, Initialisation Routines, Includes
37 //@subsection Global variables
38
39 /* Global conditions defined here. */
40
41 rtsBool IAmMainThread = rtsFalse,       /* Set for the main thread      */
42         GlobalStopPending = rtsFalse;   /* Terminating                  */
43
44 /* Task identifiers for various interesting global tasks. */
45
46 GlobalTaskId IOTask = 0,                /* The IO Task Id               */
47              SysManTask = 0,            /* The System Manager Task Id   */
48              mytid = 0;                 /* This PE's Task Id            */
49
50 rtsTime         main_start_time;        /* When the program started     */
51 rtsTime         main_stop_time;         /* When the program finished    */
52 jmp_buf         exit_parallel_system;   /* How to abort from the RTS    */
53
54
55 //rtsBool fishing = rtsFalse;             /* We have no fish out in the stream */
56 rtsTime last_fish_arrived_at = 0;       /* Time of arrival of most recent fish*/
57 nat     outstandingFishes = 0;          /* Number of active fishes */ 
58
59 //@cindex spark queue
60 /* GranSim: a globally visible array of spark queues */
61 rtsSpark *pending_sparks_hd[SPARK_POOLS],  /* ptr to start of a spark pool */ 
62          *pending_sparks_tl[SPARK_POOLS],  /* ptr to end of a spark pool */ 
63          *pending_sparks_lim[SPARK_POOLS],
64          *pending_sparks_base[SPARK_POOLS]; 
65
66 //@cindex spark_limit
67 /* max number of sparks permitted on the PE; 
68    see RtsFlags.ParFlags.maxLocalSparks */
69 nat spark_limit[SPARK_POOLS];
70
71 globalAddr theGlobalFromGA, theGlobalToGA;
72 /*
73   HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACK !! see FETCH_ME_entry
74   Only used within FETCH_ME_entry as local vars, but they shouldn't
75   be defined locally in there -- that would move %esp and you'll never
76   return from STG land.
77   -- HWL
78 */
79 globalAddr *rga_GLOBAL;
80 globalAddr *lga_GLOBAL;
81 globalAddr fmbqga_GLOBAL;
82 StgClosure *p_GLOBAL;
83
84 //@cindex PendingFetches
85 /* A list of fetch reply messages not yet processed; this list is filled
86    by awaken_blocked_queue and processed by processFetches */
87 StgBlockedFetch *PendingFetches = END_BF_QUEUE;
88
89 //@cindex allPEs
90 GlobalTaskId *allPEs;
91
92 //@cindex nPEs
93 nat nPEs = 0;
94
95 //@cindex sparksIgnored
96 nat sparksIgnored = 0, sparksCreated = 0, 
97     threadsIgnored = 0, threadsCreated = 0;
98
99 //@cindex advisory_thread_count
100 nat advisory_thread_count = 0;
101
102 /* Where to write the log file 
103    This is now in Parallel.c 
104 FILE *gr_file = NULL;
105 char gr_filename[STATS_FILENAME_MAXLEN];
106 */
107
108 /* Flag handling. */
109
110 #if 0
111 /* that's now all done via RtsFlags.ParFlags... */
112 rtsBool TraceSparks =    rtsFalse;              /* Enable the spark trace mode          */
113 rtsBool SparkLocally =   rtsFalse;              /* Use local threads if possible        */
114 rtsBool DelaySparks =    rtsFalse;              /* Use delayed sparking                 */
115 rtsBool LocalSparkStrategy =   rtsFalse;        /* Either delayed threads or local threads*/
116 rtsBool GlobalSparkStrategy =  rtsFalse;        /* Export all threads                   */
117
118 rtsBool DeferGlobalUpdates =   rtsFalse;        /* Defer updating of global nodes       */
119 #endif
120
121 //@node Initialisation Routines,  , Global variables
122 //@subsection Initialisation Routines
123
124 /*
125   par_exit defines how to terminate the program.  If the exit code is
126   non-zero (i.e. an error has occurred), the PE should not halt until
127   outstanding error messages have been processed.  Otherwise, messages
128   might be sent to non-existent Task Ids.  The infinite loop will actually
129   terminate, since STG_Exception will call myexit\tr{(0)} when
130   it received a PP_FINISH from the system manager task.
131 */
132 //@cindex par_exit
133 void
134 shutdownParallelSystem(StgInt n)
135 {
136   belch("   entered shutdownParallelSystem ...");
137   ASSERT(GlobalStopPending = rtsTrue);
138   sendOp(PP_FINISH, SysManTask);
139   if (n != 0) 
140     waitForTermination();
141   else
142     waitForPEOp(PP_FINISH, SysManTask);
143   shutDownPE();
144   IF_PAR_DEBUG(verbose,
145                belch("--++ shutting down PE %lx, %ld sparks created, %ld sparks Ignored, %ld threads created, %ld threads Ignored", 
146                      (W_) mytid, sparksCreated, sparksIgnored,
147                      threadsCreated, threadsIgnored));
148   exit(n);
149 }
150
151 /* Some prototypes */
152 void srand48 (long);
153 time_t time (time_t *);
154
155 //@cindex initParallelSystem
156 void
157 initParallelSystem(void)
158 {
159   belch("entered initParallelSystem ...");
160
161   /* Don't buffer standard channels... */
162   setbuf(stdout,NULL);
163   setbuf(stderr,NULL);
164
165   srand48(time(NULL) * getpid());  /*Initialise Random-number generator seed*/
166                                    /* Used to select target of FISH message*/
167
168   theGlobalFromGA.payload.gc.gtid = 0;
169   theGlobalToGA.payload.gc.gtid = 0;
170
171   //IF_PAR_DEBUG(verbose,
172                belch("initPackBuffer ...");
173   if (!initPackBuffer())
174     barf("initPackBuffer");
175
176   // IF_PAR_DEBUG(verbose,
177                belch("initMoreBuffers ...");
178   if (!initMoreBuffers())
179     barf("initMoreBuffers");
180
181   // IF_PAR_DEBUG(verbose,
182                belch("initSparkPools ...");
183   if (!initSparkPools())
184     barf("initSparkPools");
185 }
186
187 /* 
188  * SynchroniseSystem synchronises the reduction task with the system
189  * manager, and initialises the Global address tables (LAGA & GALA)
190  */
191
192 //@cindex SynchroniseSystem
193 void
194 SynchroniseSystem(void)
195 {
196   int i;
197
198   fprintf(stderr, "SynchroniseSystem: nPEs=%d\n", nPEs); 
199
200   initEachPEHook();                  /* HWL: hook to be execed on each PE */
201
202   fprintf(stderr, "SynchroniseSystem: initParallelSystem\n");
203   initParallelSystem();
204   allPEs = startUpPE(nPEs);
205
206   /* Initialize global address tables */
207   initGAtables();
208
209   /* Record the shortened the PE identifiers for LAGA etc. tables */
210   for (i = 0; i < nPEs; ++i) {
211     fprintf(stderr, "[%x] registering %d-th PE as %x\n", mytid, i, allPEs[i]);
212     registerTask(allPEs[i]);
213   }
214 }
215
216 #endif /* PAR -- whole file */
217
218 //@index
219 //* PendingFetches::  @cindex\s-+PendingFetches
220 //* SynchroniseSystem::  @cindex\s-+SynchroniseSystem
221 //* allPEs::  @cindex\s-+allPEs
222 //* initParallelSystem::  @cindex\s-+initParallelSystem
223 //* nPEs::  @cindex\s-+nPEs
224 //* par_exit::  @cindex\s-+par_exit
225 //* spark queue::  @cindex\s-+spark queue
226 //* sparksIgnored::  @cindex\s-+sparksIgnored
227 //@end index