[m-rev.] diff: thread local trailing in .par grades

Julien Fischer juliensf at csse.unimelb.edu.au
Thu May 10 15:23:11 AEST 2007


Here is the final diff for thread local trailing in parallel grades.
PeterW has already reviewed the initial version of this.

Estimated hours taken: 14
Branches: main

Add support for thread local trailing in grades that support parallel
execution.  Previously the trail state for each context was copied into the
global variables defined in mercury_trail.[ch] when the context was loaded,
i.e. we had thread-local trailing in grades that did not support parallel
execution.

This change extends the runtime to support thread local trailing in the
presence of parallel execution.  We do this by adding the relevant fields to
the MercuryEngine structure and redefining the abstract registers related to
trailing to make accesses to the trail state go via the relevant engine.

This also works for parallel trailing grades with the high-level C backend
because in those grades the trail state is stored in a thread-local dummy
engine and context.  (The context, and its attached trail state, are created
when the thread is spawned.)

XXX the coupling between the high-level and low-level versions of the runtimes
w.r.t trailing is not good.  I intend to fix it as a separate change.
(It also affects thread local mutables.)

runtime/mercury_context.h:
 	Fix a comment: a context's trail_zone field should be accessed
 	via MR_eng_context, not via the an abstract machine register.

runtime/mercury_engine.h:
 	Extend engines so that in parallel trailing grades they contain the
 	information necessary to keep track of the state of the trail.

runtime/mercury_regs.h:
 	In parallel grades MR_trail_ptr, MR_ticket_counter and
 	MR_ticket_high_water need to be accessed w.r.t the engine that the
 	context is being executed on.

 	Modify the definition of MR_restore_trail_registers() so that it
 	accesses the trail state via abstract registers rather than
 	manipulating the global variables defined in mercury_trail.[ch]
 	directly.

runtime/mercury_trail.[ch]:
 	Only define global variables to hold the trail state in non-parallel
 	grades.

 	Document the difference between parallel and non-parallel trailing
 	grades.

library/benchmarking.m:
 	In .par.tr grades, ML_report_stats should print the size of the trail
 	for the current context.

NEWS:
 	Announce support for thread local trailing in parallel grades.


Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.458
diff -u -r1.458 NEWS
--- NEWS	10 May 2007 04:15:42 -0000	1.458
+++ NEWS	10 May 2007 05:12:09 -0000
@@ -154,6 +154,8 @@

  Changes to the Mercury compiler:

+* In parallel grades we now support thread-local trailing.
+
  * The compiler now issues a warning when an inst declaration is not
    consistent with any of the types in scope.

Index: library/benchmarking.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/benchmarking.m,v
retrieving revision 1.75
diff -u -r1.75 benchmarking.m
--- library/benchmarking.m	6 Feb 2007 14:48:47 -0000	1.75
+++ library/benchmarking.m	4 May 2007 07:12:56 -0000
@@ -286,10 +286,18 @@
  #endif

  #ifdef MR_USE_TRAIL
-    fprintf(stderr, "" Trail: %.3fk,"",
-        ((char *) MR_trail_ptr - (char *) MR_trail_zone->MR_zone_min) / 1024.0
-    );
-#endif
+    #ifdef MR_THREAD_SAFE
+        fprintf(stderr, "", Trail: %.3fk,"",
+            ((char *) MR_trail_ptr - 
+            (char *) MR_CONTEXT(MR_ctxt_trail_zone)->MR_zone_min) / 1024.0
+        );
+    #else
+        fprintf(stderr, "" Trail: %.3fk,"",
+            ((char *) MR_trail_ptr - 
+            (char *) MR_trail_zone->MR_zone_min) / 1024.0
+        );
+   #endif /* !MR_THREAD_SAFE */ 
+#endif /* !MR_USE_TRAIL */

      /*
      ** Print heap usage information.
Index: runtime/mercury_context.h
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_context.h,v
retrieving revision 1.42
diff -u -r1.42 mercury_context.h
--- runtime/mercury_context.h	19 Apr 2007 04:24:52 -0000	1.42
+++ runtime/mercury_context.h	7 May 2007 04:56:20 -0000
@@ -61,8 +61,10 @@

  #ifdef  MR_THREAD_SAFE
    #define MR_IF_THREAD_SAFE(x)  x
+  #define MR_IF_NOT_THREAD_SAFE(x)
  #else
    #define MR_IF_THREAD_SAFE(x)
+  #define MR_IF_NOT_THREAD_SAFE(x) x
  #endif

  /*
@@ -172,6 +174,8 @@
  **                  but also via MR_eng_this_context.)
  **
  ** trail_zone       The trail zone for this context.
+**                  (Accessed via MR_eng_context.)
+**
  ** trail_ptr        The saved MR_trail_ptr for this context.
  ** ticket_counter   The saved MR_ticket_counter for this context.
  ** ticket_highwater The saved MR_ticket_high_water for this context.
@@ -593,7 +597,13 @@
              )                                                                 \
          )                                                                     \
          MR_IF_USE_TRAIL(                                                      \
-            MR_trail_zone = load_context_c->MR_ctxt_trail_zone;               \
+            MR_IF_NOT_THREAD_SAFE(                                            \
+                MR_trail_zone = load_context_c->MR_ctxt_trail_zone;           \
+            )                                                                 \
+            MR_IF_THREAD_SAFE(                                                \
+                MR_ENGINE(MR_eng_context).MR_ctxt_trail_zone =                \
+                    load_context_c->MR_ctxt_trail_zone;                       \
+            )                                                                 \
              MR_trail_ptr = load_context_c->MR_ctxt_trail_ptr;                 \
              MR_ticket_counter = load_context_c->MR_ctxt_ticket_counter;       \
              MR_ticket_high_water = load_context_c->MR_ctxt_ticket_high_water; \
@@ -653,7 +663,13 @@
              )                                                                 \
          )                                                                     \
          MR_IF_USE_TRAIL(                                                      \
-            save_context_c->MR_ctxt_trail_zone = MR_trail_zone;               \
+            MR_IF_NOT_THREAD_SAFE(                                            \
+                save_context_c->MR_ctxt_trail_zone = MR_trail_zone;           \
+            )                                                                 \
+            MR_IF_THREAD_SAFE(                                                \
+                save_context_c->MR_ctxt_trail_zone =                          \
+                    MR_ENGINE(MR_eng_context).MR_ctxt_trail_zone;             \
+            )                                                                 \
              save_context_c->MR_ctxt_trail_ptr = MR_trail_ptr;                 \
              save_context_c->MR_ctxt_ticket_counter = MR_ticket_counter;       \
              save_context_c->MR_ctxt_ticket_high_water = MR_ticket_high_water; \
Index: runtime/mercury_engine.h
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_engine.h,v
retrieving revision 1.47
diff -u -r1.47 mercury_engine.h
--- runtime/mercury_engine.h	17 Apr 2007 05:38:08 -0000	1.47
+++ runtime/mercury_engine.h	7 May 2007 04:55:56 -0000
@@ -301,6 +301,16 @@
  **              for the engine, and then invokes MR_load_context to copy the
  **              info from there into the MR_eng_context field.
  **
+** trail_ptr 
+** ticket_counter
+** ticket_high_water
+**              These fields correspond to the similarly named global
+**              variables declared in mercury_trail.h.  They represent
+**              the state of the trail for the context that this engine
+**              is executing.  These fields only exist in parallel trailing
+**              grades; in other trailing grades we use the aforementioned
+**              globals to store the trail state.
+**
  ** main_context The context of the main computation. The owner_generator field
  **              of this context must be NULL.
  **
@@ -368,6 +378,11 @@
  #endif
      MR_Context          *MR_eng_this_context;
      MR_Context          MR_eng_context;
+#if defined(MR_THREAD_SAFE) && defined(MR_USE_TRAIL)
+    MR_TrailEntry       *MR_eng_trail_ptr;
+    MR_Unsigned         MR_eng_ticket_counter;
+    MR_Unsigned         MR_eng_ticket_high_water;
+#endif
  #ifdef  MR_USE_MINIMAL_MODEL_OWN_STACKS
      MR_Context          *MR_eng_main_context;
      MR_Dlist            *MR_eng_gen_contexts;   /* elements are MR_Context */
Index: runtime/mercury_regs.h
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_regs.h,v
retrieving revision 1.28
diff -u -r1.28 mercury_regs.h
--- runtime/mercury_regs.h	26 Sep 2006 03:53:21 -0000	1.28
+++ runtime/mercury_regs.h	7 May 2007 02:21:33 -0000
@@ -468,7 +468,7 @@
  	36, \
  }

-#endif
+#endif /* !MR_THREAD_SAFE or MR_NUM_REAL_REGS == 0 */

  /*
  ** The *_SLOT macros define the mapping from special registers and from
@@ -541,6 +541,15 @@
  					MR_min_sol_hp_rec_var)
  #define MR_global_hp		MR_count_usage(MR_GLOBAL_HP_SLOT,	\
  					MR_global_hp_var)
+#if defined(MR_THREAD_SAFE)
+
+#define MR_trail_ptr		MR_count_usage(MR_TRAIL_PTR_SLOT,	\
+					MR_ENGINE(MR_eng_trail_ptr))
+#define MR_ticket_counter	MR_count_usage(MR_TICKET_COUNTER_SLOT,	\
+					MR_ENGINE(MR_eng_ticket_counter))
+#define MR_ticket_high_water	MR_count_usage(MR_TICKET_HIGH_WATER_SLOT, \
+					MR_ENGINE(MR_eng_ticket_high_water))
+#else /* ! MR_THREAD_SAFE */

  #define MR_trail_ptr		MR_count_usage(MR_TRAIL_PTR_SLOT,	\
  					MR_trail_ptr_var)
@@ -548,6 +557,7 @@
  					MR_ticket_counter_var)
  #define MR_ticket_high_water	MR_count_usage(MR_TICKET_HIGH_WATER_SLOT,\
  					MR_ticket_high_water_var)
+#endif /* ! MR_THREAD_SAFE */

  #define MR_gen_next		MR_count_usage(MR_GEN_NEXT_SLOT,	\
  					MR_gen_next_var)
@@ -696,9 +706,9 @@
  	} while (0)
    #define MR_restore_trail_registers()					\
  	do {								\
-		MR_trail_ptr_var = MR_virtual_trail_ptr;		\
-		MR_ticket_counter_var = MR_virtual_ticket_counter;	\
-		MR_ticket_high_water_var = MR_virtual_ticket_high_water;\
+		MR_trail_ptr = MR_virtual_trail_ptr;			\
+		MR_ticket_counter = MR_virtual_ticket_counter;		\
+		MR_ticket_high_water = MR_virtual_ticket_high_water;	\
  	} while (0)
  #else
    #define MR_save_trail_registers()	((void) 0)
Index: runtime/mercury_trail.c
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_trail.c,v
retrieving revision 1.13
diff -u -r1.13 mercury_trail.c
--- runtime/mercury_trail.c	14 Nov 2006 00:15:41 -0000	1.13
+++ runtime/mercury_trail.c	7 May 2007 02:21:33 -0000
@@ -22,10 +22,12 @@

  #ifdef MR_USE_TRAIL

+#if !defined(MR_THREAD_SAFE)
  MR_MemoryZone   *MR_trail_zone;
  MR_TrailEntry   *MR_trail_ptr_var;
  MR_Unsigned     MR_ticket_counter_var = 1;
  MR_Unsigned     MR_ticket_high_water_var = 1;
+#endif

  void
  MR_untrail_to(MR_TrailEntry *old_trail_ptr, MR_untrail_reason reason)
@@ -79,6 +81,6 @@
      }
  }

-#endif
+#endif /* MR_USE_TRAIL */

  /*---------------------------------------------------------------------------*/
Index: runtime/mercury_trail.h
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_trail.h,v
retrieving revision 1.25
diff -u -r1.25 mercury_trail.h
--- runtime/mercury_trail.h	3 May 2007 03:00:12 -0000	1.25
+++ runtime/mercury_trail.h	9 May 2007 06:52:23 -0000
@@ -330,41 +330,60 @@

  /*---------------------------------------------------------------------------*/

-/* The Mercury trail */
-extern MR_MemoryZone *MR_trail_zone;
-
-/* Pointer to the current top of the Mercury trail */
  /*
-** N.B. Use `MR_trail_ptr', defined in mercury_regs.h,
-** not `MR_trail_ptr_var'.
-*/
-
-extern MR_TrailEntry *MR_trail_ptr_var;
-
-/*
-** An integer variable that holds the current choice point identifier;
-** it is allocated a new value whenever we create a choice point (including
-** semidet choice points, e.g. in an if-then-else) and it is reset whenever
-** a choice point is backtracked over or pruned away.
-**
-** N.B.  Use `MR_ticket_counter', defined in mercury_regs.h,
-** not `MR_ticket_counter_var'.
-*/
-
-extern MR_Unsigned MR_ticket_counter_var;
-
-/*
-** An integer variable that is incremented whenever we create a choice
-** point (including semidet choice points, e.g. in an if-then-else)
-** and decremented or reset whenever we backtrack over one,
-** but which is _not_ decremented or reset when a choice point is
-** pruned away with a commit.
-**
-** N.B.  Use `MR_ticket_high_water', defined in mercury_regs.h,
-** not `MR_ticket_high_water_var'.
-*/
+** This section defines the global state needed to implement the trail.
+** The trail state is actually part of the MR_Context structure
+** (see mercury_context.h).  In grades that do not support parallelism we
+** copy the relevant fields from the context into the following global
+** variables when we load the context.  In grades that support parallelism
+** we do not use the global variables; instead each engine contains fields
+** for holding the trail state and we load the fields from the context
+** into the engine that is running it.
+** 
+** XXX the implementation for the high-level C backend is a bit of a mess.
+** It's currently all tied up with that of the low-level backend.
+** In high-level C grades each POSIX thread has a dummy engine and context
+** with associated with it.  These are used to store thread local data.
+** We store the trail state in the relevant fields of those structures.
+** These dependencies should be removed (see the commented out code
+** in mercury_wrapper.c).
+*/
+#if !defined(MR_THREAD_SAFE)
+
+    /* The Mercury trail */
+    extern MR_MemoryZone *MR_trail_zone;
+
+    /* 
+    ** A pointer to the current top of the Mercury trail.
+    ** N.B. Use `MR_trail_ptr', defined in mercury_regs.h, 
+    ** not `MR_trail_ptr_var'.
+    */
+    extern MR_TrailEntry *MR_trail_ptr_var;
+
+    /*
+    ** An integer variable that holds the current choice point identifier;
+    ** it is allocated a new value whenever we create a choice point 
+    ** (including semidet choice points, e.g. in an if-then-else) and it is
+    ** reset whenever a choice point is backtracked over or pruned away.
+    **
+    ** N.B.  Use `MR_ticket_counter', defined in mercury_regs.h,
+    ** not `MR_ticket_counter_var'.
+    */
+    extern MR_Unsigned MR_ticket_counter_var;
+
+    /*
+    ** An integer variable that is incremented whenever we create a choice
+    ** point (including semidet choice points, e.g. in an if-then-else)
+    ** and decremented or reset whenever we backtrack over one,
+    ** but which is _not_ decremented or reset when a choice point is
+    ** pruned away with a commit.
+    **
+    ** N.B.  Use `MR_ticket_high_water', defined in mercury_regs.h,
+    ** not `MR_ticket_high_water_var'.
+    */
+    extern MR_Unsigned MR_ticket_high_water_var;

-extern MR_Unsigned MR_ticket_high_water_var;
+#endif /* !defined(MR_THREAD_SAFE) */

  /*---------------------------------------------------------------------------*/
  /*

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list