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

Assorted interpreter fixes #3430

Merged
merged 7 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion M2/Macaulay2/c/cprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static void cprintdefine(node t,bool definitions) {
threadLocalDeclarationFlag = 1;
cprint(t);
threadLocalDeclarationFlag = 0;
put("_id;\n");
put("_id");
// put("__thread ");
}
if (flags & const_F) put("const ");
Expand Down
2 changes: 1 addition & 1 deletion M2/Macaulay2/d/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ set(DLIST
# tasks.d
xmlactors.d # removed unless WITH_XML
actors5.d
actors6.dd
chrono.dd
threads.dd
python.d # removed unless WITH_PYTHON
interface.dd interface2.d
Expand Down
2 changes: 1 addition & 1 deletion M2/Macaulay2/d/Makefile.files.in
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ else
M2_SRCFILES += xmlactors.d
endif
M2_DFILES += actors5.d
M2_DFILES += actors6.dd
M2_DFILES += chrono.dd
M2_DFILES += threads.dd

ifeq (@PYTHON@,yes)
Expand Down
18 changes: 0 additions & 18 deletions M2/Macaulay2/d/actors2.dd
Original file line number Diff line number Diff line change
Expand Up @@ -666,24 +666,6 @@ unSingleton(e:Expr):Expr := (
else e);
setupfun("unsequence",unSingleton);

cpuTime(e:Expr):Expr := (
when e
is s:Sequence do if length(s) == 0 then toExpr(cpuTime())
else WrongNumArgs(0)
else WrongNumArgs(0));
setupfun("cpuTime",cpuTime);

-- TODO: should "timing" return thread and gc times like "time"?
-- (moved to actors5.d for gc time)
timefun(a:Code):Expr := (
v := cpuTime();
ret := eval(a);
x := cpuTime();
when ret
is Error do ret
else list(timeClass,Sequence(toExpr(x-v),ret)));
setupop(timingS,timefun);

exponent(e:Expr):Expr := (
when e
is x:ZZcell do toExpr(exponent(x.v)) -- # typical value: size2, ZZ, ZZ
Expand Down
8 changes: 4 additions & 4 deletions M2/Macaulay2/d/actors3.d
Original file line number Diff line number Diff line change
Expand Up @@ -1866,14 +1866,14 @@ map(e1:Expr,e2:Expr,f:Expr):Expr := (
)
is b2:List do (
mutable := b1.Mutable;
class := b1.Class;
if class != b2.Class then (
b1class := b1.Class;
if b1class != b2.Class then (
mutable = false;
class = listClass;
b1class = listClass;
);
c := map(b1.v,b2.v,f);
when c is err:Error do if err.message == breakMessage then if err.value == dummyExpr then nullE else err.value else c
is v:Sequence do list(class,v,mutable)
is v:Sequence do list(b1class,v,mutable)
else nullE -- will not happen
)
is s2:stringCell do map(b1.v, strtoseq(s2), f)
Expand Down
15 changes: 0 additions & 15 deletions M2/Macaulay2/d/actors5.d
Original file line number Diff line number Diff line change
Expand Up @@ -2136,21 +2136,6 @@ GCstats(e:Expr):Expr := (
else WrongNumArgs(0));
setupfun("GCstats",GCstats);

showtimefun(a:Code):Expr := (
cpuStart := cpuTime();
threadStart := threadTime();
gcStart := gcTime();
ret := eval(a);
cpuEnd := cpuTime();
threadEnd := threadTime();
gcEnd := gcTime();
stdError << " -- used "
<< cpuEnd - cpuStart << "s (cpu); "
<< threadEnd - threadStart << "s (thread); "
<< gcEnd - gcStart << "s (gc)" << endl;
ret);
setupop(timeS,showtimefun);

header "extern void set_gftable_dir(char *); /* defined in library factory, as patched by us */";
setFactoryGFtableDirectory(e:Expr):Expr := (
when e is d:stringCell do (
Expand Down
36 changes: 0 additions & 36 deletions M2/Macaulay2/d/actors6.dd

This file was deleted.

78 changes: 78 additions & 0 deletions M2/Macaulay2/d/chrono.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- TODO: get core time: number of cpu clock ticks spent in the core/libraries
-- TODO: define time and elapsedTime in the top level instead, and print in the AfterPrint
-- TODO: auto-calibrate spin based on the wall time
Comment on lines +1 to +3
Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I should say that I wrote this commit a long time ago. I don't really agree with the second item anymore.


use arithmetic; -- for types
use binding; -- for symbols
use evaluate; -- for eval
use expr; -- for timeClass
use struct; -- for Sequence
use system; -- for cpuTime
use stdio; -- for tostringRR
use actors5; -- for gcTime

declarations "#include <chrono>";

-- The typedef from this type declaration is put into the include file chrono-exports.h,
-- so that means we can write "use chrono;" only in a *.dd file, not in a *.d file.
-- That's why we are renaming interp.d to interp.dd. Eventually all *.d files should
-- be renamed.
Chrono := Type "std::chrono::steady_clock::time_point" ;

-----------------------------------------------------------------------------
-- Top level timing functions
-----------------------------------------------------------------------------

cpuTime(e:Expr):Expr := (
when e is s:Sequence do
if length(s) == 0 then toExpr(cpuTime())
else WrongNumArgs(0)
else WrongNumArgs(0));
setupfun("cpuTime", cpuTime);

cpuTimer(c:Code):Sequence := (
T0 := cpuTime();
ret := eval(c);
T1 := cpuTime();
Sequence(toExpr(T1 - T0), ret));

wallTimer(c:Code):Sequence := (
T0 := Ccode(Chrono, "std::chrono::steady_clock::now()");
ret := eval(c);
T1 := Ccode(Chrono, "std::chrono::steady_clock::now()");
dT := double(Ccode(uint64_t, "(", T1, " - ", T0, ").count()"));
dT = dT/1000000000.;
Sequence(toExpr(dT), ret));

showtimefun(a:Code):Expr := (
cpuStart := cpuTime();
threadStart := threadTime();
gcStart := gcTime();
ret := eval(a);
cpuEnd := cpuTime();
threadEnd := threadTime();
gcEnd := gcTime();
stdError << " -- used "
<< cpuEnd - cpuStart << "s (cpu); "
<< threadEnd - threadStart << "s (thread); "
<< gcEnd - gcStart << "s (gc)" << endl;
ret);
setupop(timeS,showtimefun);

-- TODO: should "timing" return thread and gc times like "time"?
timingfun(c:Code):Expr := (
s := cpuTimer(c);
when s.1 is Error do s.1 else list(timeClass, s));
setupop(timingS, timingfun);

elapsedTimefun(c:Code):Expr := (
s := wallTimer(c);
when s.0 is dT:RRcell do (
stdError << " -- " << tostringRR(dT.v) << "s elapsed" << endl;)
else nothing; s.1);
setupop(elapsedTimeS,elapsedTimefun);

elapsedTimingfun(c:Code):Expr := (
s := wallTimer(c);
when s.1 is Error do s.1 else list(timeClass, s));
setupop(elapsedTimingS,elapsedTimingfun);
3 changes: 1 addition & 2 deletions M2/Macaulay2/d/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export codePosition(c:Code):Position := ( -- TODO retire
is f:multaryCode do f.position
is f:newCode do f.position
is f:newFromCode do f.position
is f:newLocalFrameCode do codePosition(f.body)
is f:newOfCode do f.position
is f:newOfFromCode do f.position
is f:nullCode do dummyPosition
Expand Down Expand Up @@ -119,7 +118,7 @@ export setupfun(name:string,fun:unop):Symbol := (
entry.unary = fun;
entry.Protected = true;
entry);
export setupfun(name:string,value:fun):Symbol := (
export setupfun(name:string, value:function(Expr):Expr):Symbol := (
word := makeUniqueWord(name,parseWORD);
entry := makeSymbol(word,dummyPosition,globalDictionary);
globalFrame.values.(entry.frameindex) = Expr(newCompiledFunction(value));
Expand Down
2 changes: 0 additions & 2 deletions M2/Macaulay2/d/debugging.dd
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ export tostring(c:Code):string := (
is x:newFromCode do concatenate(array(string)("(new ", tostring(x.newClause), " from: ", tostring(x.fromClause), ")"))
is x:newOfCode do concatenate(array(string)("(new ", tostring(x.newClause), " of: ", tostring(x.ofClause), ")"))
is x:newCode do concatenate(array(string)("(new ", tostring(x.newClause), ")"))
--
is x:newLocalFrameCode do concatenate(array(string)()) -- soon obsolete
);

export toExpr(c:Code):Expr := Pseudocode(c);
Expand Down
5 changes: 0 additions & 5 deletions M2/Macaulay2/d/evaluate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1508,11 +1508,6 @@ export evalraw(c:Code):Expr := (
return Expr(SymbolClosure(f,r.symbol)))
is b:ternaryCode do b.f(b.arg1,b.arg2,b.arg3)
is b:multaryCode do b.f(b.args)
is n:newLocalFrameCode do (
localFrame = Frame(localFrame,n.frameID,n.framesize,false, new Sequence len n.framesize do provide nullE);
x := eval(n.body);
localFrame = localFrame.outerFrame;
x)
is c:evaluatedCode do return c.expr
is c:forCode do return evalForCode(c)
is c:whileListDoCode do evalWhileListDoCode(c)
Expand Down
2 changes: 1 addition & 1 deletion M2/Macaulay2/d/expr.d
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export newHashTableWithHash(Class:HashTable,parent:HashTable):HashTable := (
ht.hash=nextHash();
ht);

export newCompiledFunction(fn:fun):CompiledFunction := (
export newCompiledFunction(fn:function(Expr):Expr):CompiledFunction := (
cf := CompiledFunction(fn, hash_t(0));
cf.hash = hashFromAddress(Expr(cf));
cf);
Expand Down
4 changes: 2 additions & 2 deletions M2/Macaulay2/d/hashtables.dd
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,10 @@ export installMethod(meth:Expr,lhs:HashTable,rhs:HashTable,value:Expr):Expr := (
Expr(Sequence(meth,Expr(lhs),Expr(rhs))),
value));

export installMethod(s:SymbolClosure,X:HashTable,f:fun):Expr := (
export installMethod(s:SymbolClosure, X:HashTable, f:function(Expr):Expr):Expr := (
installMethod(Expr(s),X,Expr(newCompiledFunction(f)))
);
export installMethod(s:SymbolClosure,X:HashTable,Y:HashTable,f:fun):Expr := (
export installMethod(s:SymbolClosure, X:HashTable, Y:HashTable, f:function(Expr):Expr):Expr := (
installMethod(Expr(s),X,Y,Expr(newCompiledFunction(f)))
);

Expand Down
30 changes: 4 additions & 26 deletions M2/Macaulay2/d/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ export anywhereAbort(s:string) ::= Ccode(exits,"err_abort(",s,")");
-- Typedefs for functions of various numbers of arguments
export unop := function(Code):Expr;
export binop := function(Code,Code):Expr;
export binopExpr := function(Expr,Expr):Expr;
export ternop := function(Code,Code,Code):Expr;
export multop := function(CodeSequence):Expr;


export TokenFile := {+
posFile:PosFile,
nexttoken:(null or Token)
Expand Down Expand Up @@ -244,11 +242,6 @@ export semiCode := {+w:CodeSequence, position:Position};
export multaryCode := {+f:multop, args:CodeSequence, position:Position};
export forCode := {+inClause:Code, fromClause:Code, toClause:Code, whenClause:Code, listClause:Code, doClause:Code, frameID:int, framesize:int, position:Position} ;

export newLocalFrameCode := {+
frameID:int,
framesize:int,
body:Code
};
export functionDescription := {
frameID:int, -- seqno of dictionary
framesize:int,
Expand All @@ -275,27 +268,13 @@ export Code := (
or whileDoCode or whileListCode or whileListDoCode
or ifCode or tryCode or adjacentCode or functionCode or catchCode
or Error -- for tail recursion
or newLocalFrameCode -- soon obsolete
);
export PseudocodeClosure := {+ frame:Frame, code:Code };
export Pseudocode := {+ code:Code };



--misc


export CompiledFunction := {+fn:fun,hash:hash_t};
export CompiledFunctionClosure := {+
fn:function(Expr,Sequence):Expr,
hash:hash_t,
env:Sequence
};
export CompiledFunctionBody := {+
fn:function(Expr,Sequence):Expr -- it's hard to make hash codes for these things!
};


export CompiledFunction := {+ fn:function(Expr):Expr, hash:hash_t };
export CompiledFunctionBody := {+ fn:function(Expr,Sequence):Expr }; -- TODO: compute hash
export CompiledFunctionClosure := {+ fn:function(Expr,Sequence):Expr, hash:hash_t, env:Sequence };

-- Expr

Expand Down Expand Up @@ -435,7 +414,6 @@ export Expr := (
pointerCell or
atomicIntCell
);
export fun := function(Expr):Expr;

--Unique True expression
export True := Expr(Boolean(true)); -- don't make new ones!
Expand Down Expand Up @@ -482,7 +460,7 @@ export HashTable := {+
mutex:ThreadRWLockPtr
};

--This unfortunately needs to be here as it references Hash Table which needs expr.
--This unfortunately needs to be here as it references HashTable which needs expr.

export m2cfile := Pointer "struct M2File*";

Expand Down
20 changes: 10 additions & 10 deletions M2/Macaulay2/d/scclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,32 +543,32 @@ int system_unlink(M2_string name) {
}

int system_link(M2_string oldfilename,M2_string newfilename) {
char *old = M2_tocharstar(oldfilename);
char *new = M2_tocharstar(newfilename);
char *oldf = M2_tocharstar(oldfilename);
char *newf = M2_tocharstar(newfilename);
int r =
#ifdef HAVE_LINK
link(old,new)
link(oldf, newf)
#else
-1
#endif
;
freemem(old);
freemem(new);
freemem(oldf);
freemem(newf);
return r;
}

int system_symlink(M2_string oldfilename,M2_string newfilename) {
char *old = M2_tocharstar(oldfilename);
char *new = M2_tocharstar(newfilename);
char *oldf = M2_tocharstar(oldfilename);
char *newf = M2_tocharstar(newfilename);
int r =
#ifdef HAVE_SYMLINK
symlink(old,new)
symlink(oldf, newf)
#else
-1
#endif
;
freemem(old);
freemem(new);
freemem(oldf);
freemem(newf);
return r;
}

Expand Down
Loading