Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out state #579

Merged
merged 34 commits into from
Mar 8, 2016
Merged

Conversation

markuspf
Copy link
Member

@markuspf markuspf commented Feb 4, 2016

This pull request is a work-in-progress on the unification of GAP and HPC-GAP.

It takes the approach to remove a lot of globally defined state and put it into a structure currently called InterpreterState. Most of the code is taken from the hpcgap-default branch.

This compiles, but immediately segfaults (because of missing initialisation code which I will add next).

This pull request is to be understood as a base for discussion at the moment.

Obviously the first goal is to get GAP and HPC-GAP closer to each other: We mostly copy code from HPC-GAP, but just get rid of state stored in global variables. This is something that is desirable for GAP as well, though we might have a careful think about names (TLS as in ThreadLocalStorage might not be the ideal choice of name), and whether on the long run we want to have a state-struct per module or not.

@markuspf markuspf added topic: HPC-GAP unification do not merge PRs which are not yet ready to be merged (e.g. submitted for discussion, or test results) labels Feb 4, 2016
@markuspf markuspf self-assigned this Feb 4, 2016
@markuspf markuspf force-pushed the wip/factor-out-state branch from f521be8 to 7465104 Compare February 29, 2016 17:25
@@ -71,7 +71,7 @@
** the statement that was last interpreted (which might have been a
** return-value-statement).
*/
Obj IntrResult;
/* TL: Obj IntrResult; */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TL: prefix is rather cryptic. How about we consistently write something like:

/* Obj IntrResult;  -- definition has been moved to InterpreterState */

or so? That would help people trying to understand the code.

@markuspf
Copy link
Member Author

markuspf commented Mar 1, 2016

This still segfaults. I gather this has something to do with the garbage collector state being messed up, but I haven't found out yet where this happens. If @ChrisJefferson has a quick insight that'd be appreciated. I'll continue hacking at it for now, maybe I'll understand gasman then.

@ChrisJefferson
Copy link
Contributor

Two issues. One is easy to fix (need some InitGlobalBag), the other I'm not 100% sure what's going wrong (bags being created at wrong point) but it's easy to fix by calling InitPrintObjStack in InitKernel.

Patch follows (as I'm too lazy to push)

diff --git a/src/objects.c b/src/objects.c
index a309c6d..463bab2 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -865,17 +865,17 @@ static UInt LastPV = 0; /* This variable contains one of the values
                           PrintObj or ViewObj still open (0), or the
                           innermost such is Print (1) or View (2) */

-/* On-demand creation of the PrintObj stack */
+
 void InitPrintObjStack()
 {
-  if (!TLS(PrintObjThiss)) {
-    TLS(PrintObjThissObj) = NewBag(T_DATOBJ, MAXPRINTDEPTH*sizeof(Obj)+sizeof(Obj));
-    TLS(PrintObjThiss) = ADDR_OBJ(TLS(PrintObjThissObj))+1;
-    TLS(PrintObjIndicesObj) = NewBag(T_DATOBJ, MAXPRINTDEPTH*sizeof(Int)+sizeof(Obj));
-    TLS(PrintObjIndices) = (Int *)(ADDR_OBJ(TLS(PrintObjIndicesObj))+1);
-  }
+  InitGlobalBag(&TLS(PrintObjThissObj), "objects.c:PrintObjThissObj");
+  InitGlobalBag(&TLS(PrintObjIndicesObj), "objects.c:PrintObjIndicesObj");
+  TLS(PrintObjThissObj) = NewBag(T_DATOBJ, MAXPRINTDEPTH*sizeof(Obj)+sizeof(Obj));
+  TLS(PrintObjThiss) = ADDR_OBJ(TLS(PrintObjThissObj))+1;
+  TLS(PrintObjIndicesObj) = NewBag(T_DATOBJ, MAXPRINTDEPTH*sizeof(Int)+sizeof(Obj));
+  TLS(PrintObjIndices) = (Int *)(ADDR_OBJ(TLS(PrintObjIndicesObj))+1);
 }
-
+
 void            PrintObj (
     Obj                 obj )
 {
@@ -905,7 +905,6 @@ void            PrintObj (
     /* if <obj> is a subobject, then mark and remember the superobject
        unless ViewObj has done that job already */

-    InitPrintObjStack();
     if ( !fromview  && 0 < TLS(PrintObjDepth) ) {
         if ( IS_MARKABLE(TLS(PrintObjThis)) )  MARK( TLS(PrintObjThis) );
         TLS(PrintObjThiss)[TLS(PrintObjDepth)-1]   = TLS(PrintObjThis);
@@ -1028,7 +1027,6 @@ void            ViewObj (

     /* if <obj> is a subobject, then mark and remember the superobject     */

-    InitPrintObjStack();

     if ( 0 < TLS(PrintObjDepth) ) {
         if ( IS_MARKABLE(TLS(PrintObjThis)) )  MARK( TLS(PrintObjThis) );
@@ -1794,7 +1792,9 @@ static Int InitKernel (
     MakeImmutableObjFuncs[ T_POSOBJ ] = MakeImmutablePosObj;
     MakeImmutableObjFuncs[ T_DATOBJ ] = MakeImmutableDatObj;

-
+
+    InitPrintObjStack();
+
     /* return success                                                      */
     return 0;
 }

For the time being just call it in InitKernel. This is created on-demand in HPC-GAP, so it should go back to being created on-demand.
@markuspf
Copy link
Member Author

markuspf commented Mar 1, 2016

Mhm. There I probably tried doing 2 steps at the same time (and finding again that it's a bad idea).

Did you use some magic incantation to find this, or does your system just give you better behaviour than "haha I am going to segfault randomly"?

@markuspf
Copy link
Member Author

markuspf commented Mar 1, 2016

Also the current state passes testinstall & testbugfix for me. Running teststandard now.

@ChrisJefferson
Copy link
Contributor

Nothing magic, just guessed the problem would be the rearrangement made the GC lose track of something, then scanned through the patch.

@markuspf
Copy link
Member Author

markuspf commented Mar 1, 2016

Ah damn, I see what was wrong with my ported HPC-GAP code now I think. Thanks for the pointer, @ChrisJefferson

@markuspf
Copy link
Member Author

markuspf commented Mar 1, 2016

Workspaces seem to be broken though (witnessed by the manuals not building)

@ChrisJefferson
Copy link
Contributor

Looking more carefully, the whole PrintObjThiss and PrintObjIndices are broken. You can't store a pointer to a GAP object over a garbage collection!

The easiest option would be to just pop the C arrays you are removing into the struct, rather than replacing them with GAP objects.

This code does not work with GASMAN since it tries keeping pointers
into GASMAN bags.
@markuspf
Copy link
Member Author

markuspf commented Mar 2, 2016

Note that this is code that I (blindly and stupidly) copied over from HPC-GAP. I'll pay more attention to this stuff in the future. This now passes tests and builds the documentation on my machine. I'll have another look at the differences now.

I keep thinking that every module should be able to register its own (global)state with the kernel (obviously without using global variables). With Boehm-GC we could just malloc the state, but with GASMAN we have to me more careful.

@markuspf
Copy link
Member Author

markuspf commented Mar 2, 2016

@fingolfin @ChrisJefferson this builds now and passes tests. I am not sure how daring we are with this, but if we want the unification to happen before 4.9 we should probably merge things early and detect problems by using the code.

As I commented above, changing names to be more sensible, and rearranging things is in my opinion something we should keep a note of and do post-unification. (Even though for example changing the TLS() macro is of course a search-and-replace job, but one that one has to do on two branches as opposed to one.

@ChrisJefferson
Copy link
Contributor

While parts of this patch are a little horrible, my temptation is to merge it, and head towards complete merging. Once that happens, we can then do all kinds of cleanups.

@olexandr-konovalov
Copy link
Member

Weekly tests of the master branch are passing well today, so this could be a good moment to merge this and clean up any problems that may be discovered.

@markuspf markuspf removed the do not merge PRs which are not yet ready to be merged (e.g. submitted for discussion, or test results) label Mar 7, 2016
@markuspf markuspf added this to the GAP 4.9.0 milestone Mar 7, 2016
@olexandr-konovalov
Copy link
Member

OK, so let's now merge it and see what happens.

olexandr-konovalov pushed a commit that referenced this pull request Mar 8, 2016
@olexandr-konovalov olexandr-konovalov merged commit 35f6821 into gap-system:master Mar 8, 2016
@markuspf markuspf deleted the wip/factor-out-state branch February 5, 2017 12:31
@olexandr-konovalov olexandr-konovalov added the release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes label Jan 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: not needed PRs introducing changes that are wholly irrelevant to the release notes topic: HPC-GAP unification
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants