Skip to content

Commit

Permalink
In Progress: Chemora: Inline and expand differencing operations.
Browse files Browse the repository at this point in the history
Provide rules to expand the use of differencing operations
 into expressions.

Inline and expand uses of differencing operations.

ChemoraNLiteral, ChemoraNOffset: New.

Differencing.m:chemoraDifferencingMacroExpansions: New.

Move some Chemora symbols to Kranc.m.
  • Loading branch information
dmkoppelman committed Jun 19, 2015
1 parent 9f073c5 commit 5f87a03
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 19 deletions.
107 changes: 88 additions & 19 deletions Tools/CodeGen/CodeGenCalculation.m
Expand Up @@ -28,8 +28,6 @@
CreateCalculationFunction::usage = "";
CreateSetterSource::usage = "";
GridFunctionsInExpression;
chemoraQuote;
chemoraCDifferenceOps;

Begin["`Private`"];

Expand Down Expand Up @@ -320,6 +318,8 @@ General Utility Functions (could be moved outside this package)
Parameter[a_] -> a
};

chemoraCountOperations[ChemoraNLiteral[expr_]]:= chemoraQuote["Literal"];

chemoraCountOperations[expr_]:=
Module[ { opInfoRaw, opInfo },
opInfoRaw =
Expand Down Expand Up @@ -374,22 +374,64 @@ General Utility Functions (could be moved outside this package)
],
Rule[lhs_, chemoraIfElse[Parameter[cond_],rest___]] :>
lhs -> chemoraIfElse[cond,rest],
Rule[lhs_, h1_?(Not[MemberQ[{ChemoraNLiteral},#]]&)
[op1___, h2_?(NumberQ), op3___]] :>
Sequence@@Module[{lhs2, seq = chemoraExpandRulesSeqNum++},
lhs2 = Symbol[ ToString[lhs] <> "xLiteralx" <> ToString[seq] ];
{ lhs2 -> ChemoraNLiteral[h2],
lhs -> h1[op1,lhs2,op3] }],
Rule[lhs_, h1_[op1___,
h2_String?(StringMatchQ[#,RegularExpression["^KRANC.*"]]&),
op3___]] :>
Sequence@@Module[{lhs2, seq = chemoraExpandRulesSeqNum++},
lhs2 = Symbol[ ToString[lhs] <> "xDiffOpx" <> ToString[seq] ];
{ lhs2 -> ChemoraNDO["_"<>h2], lhs -> h1[op1,lhs2,op3] }],
Rule[lhs_, h1_[op1___,
h2_?(Not[MemberQ[{Parameter},#]]&)[op2__],op3___]] :>
Sequence@@Module[{lhs2, seq = chemoraExpandRulesSeqNum++},
lhs2 = Symbol[ ToString[lhs] <> "x" <>
ToString[h2] <> "x" <> ToString[seq] ];
{ lhs2 -> h2[op2], lhs -> h1[op1,lhs2,op3] }
],
{ lhs2 -> h2[op2], lhs -> h1[op1,lhs2,op3] }],
Rule[lhs_, h1_?(MemberQ[{Plus,Times},#]&)[op1_, op2_, op3__]] :>
Sequence@@Module[{lhs2, seq = chemoraExpandRulesSeqNum++},
lhs2 = Symbol[ ToString[lhs] <> "y" <>
ToString[h1] <> "y" <> ToString[seq] ];
{ lhs2 -> h1[op1,op2], lhs -> h1[lhs2,op3] } ]
}

chemoraCExpressionEQ[c_,lhs_->rhs_]:=
mapRefAppend[c,ChemoraContents,
chemoraCExpressionEQstr[lhs_->ChemoraNDO[rhs_]]:=
Module[{offsetsRaw},
offsetsRaw =
StringCases
[ rhs,
RegularExpression[
"^_KRANC_GFOFFSET3D\\(u,([^,]+),([^,]+),([^,]+)\\)"]
:> ToExpression /@ {"$1","$2","$3"} ] // Flatten;
" assign_from_offset_load("
<> chemoraQuote[lhs] <> ", "
<> "grid_function_name_placeholder,"
<> StringJoin[chemoraQuote[#] <> ", " & /@ offsetsRaw ]
<> ");\n"];

chemoraCExpressionEQstr[lhs_->ChemoraNOffset[gf_,di_,dj_,dk_]]:=
Module[{},
" assign_from_offset_load("
<> chemoraQuote[lhs] <> ", "
<> chemoraQuote[gf] <> ", "
<> chemoraQuote[di] <> ", "
<> chemoraQuote[dj] <> ", "
<> chemoraQuote[dk]
<> ");\n"];

chemoraCExpressionEQstr[lhs_->ChemoraNLiteral[val_]]:=
Module[{},
" assign_from_expr("
<> chemoraQuote[lhs] <> ", "
<> chemoraQuote["Literal"] <> ", "
<> chemoraQuote[CForm[val]] <> ", NULL);\n"];

chemoraCExpressionEQstr[lhs_->rhs_]:=
Module[{},
" assign_from_expr("
<> chemoraQuote[lhs] <> ", "
<> chemoraCountOperations[rhs] <> ", "
Expand All @@ -400,6 +442,9 @@ General Utility Functions (could be moved outside this package)
/@ chemoraCalculationExpressionVariables[rhs] ]
<> "NULL );\n"];

chemoraCExpressionEQ[c_,lhs_->rhs_]:=
mapRefAppend[c,ChemoraContents,chemoraCExpressionEQstr[lhs->rhs]]

chemoraCExpressionEQ[c_,lhs_ -> chemoraIfElse[cond_,ip_,ep_]]:=
mapRefAppend[c,ChemoraContents,
" assign_from_ifelse("
Expand All @@ -426,6 +471,7 @@ General Utility Functions (could be moved outside this package)
<> StringJoin[","<>ToString[#]& /@ Flatten[offsets]]
<> ");\n"];

(* Called from KrancThorn.m:CreateKrancThorn *)
chemoraCDifferenceOps[] :=
StringJoin[
chemoraCDifferenceOp[#] & /@ ( Flatten @@ $DifferencingMacroExpansions ) ];
Expand All @@ -435,12 +481,22 @@ General Utility Functions (could be moved outside this package)
{diffOp,gf} =
First[ StringCases[
rhs, RegularExpression["^([^()]+)\\(([^()]+)\\)"] -> {"$1","$2"} ]];

expandedDiffOp =
{ localName -> diffOp[gf] } /. chemoraDifferencingMacroExpansions;

expandedDiffOp = expandedDiffOp //. chemoraExpandRules;

mapRefAppend[c,ChemoraContents,
" assign_from_gf_load("
StringJoin[chemoraCExpressionEQstr /@ expandedDiffOp]];

mapRefAppend[c,ChemoraContents,
" assign_from_gf_load_switch("
<> chemoraQuote[localName] <> ", "
<> chemoraQuote[gf] <> ", "
<> chemoraQuote[diffOp]
<> ");\n"
<> chemoraQuote["False"]
<> ", 0, "
<> chemoraQuote[localName] <> ", 0, NULL);\n"
<> " gf_store(" <> chemoraQuote[lhs] <> ", "
<> chemoraQuote[localName] <> ");\n" ] ];

Expand Down Expand Up @@ -875,22 +931,35 @@ it is only possible to do this outside all if(){} statements. *)
[chemoraPrecomputeDerivative[#]] ) & /@ sortedgfds ] ];
];

mapRefAppend[cleancalc,ChemoraContents,
Function[{ localName, gfname, param, cases },
" assign_from_gf_load_switch(" <> chemoraQuote[localName] <> ", "
<> chemoraQuote[gfname] <> ", "
<> chemoraQuote[param] <> ", "
<> StringJoin
[ ToString[#[[1]]] <> ", "
<> chemoraQuote[#[[2]]] <> ", " & /@ cases ]
<> "0, NULL);\n" ] @@ # ] &
/@ chemoraPrecomputeDerivatives[
Module[{ precomputedD, dfRules },
precomputedD =
chemoraPrecomputeDerivatives[
defsWithoutShorts,
eqsOrdered,
lookup[{opts}, IntParameters, {}],
OptionValue[ZeroDimensions],
lookup[cleancalc, MacroPointer]];

dfRules = Function[{ localName, gfname, param, cases },
StringJoin[#[[2]],ToString[gfname]] -> #[[2]][gfname] & /@ cases ]
@@ # & /@ precomputedD;
dfRules = Flatten[dfRules];
dfRules = dfRules /. chemoraDifferencingMacroExpansions;
dfRules = dfRules //. chemoraExpandRules;
mapRefAppend[cleancalc,ChemoraContents,
StringJoin[chemoraCExpressionEQstr /@ Flatten[dfRules]]];

mapRefAppend[cleancalc,ChemoraContents,
Function[{ localName, gfname, param, cases },
" assign_from_gf_load_switch(" <> chemoraQuote[localName] <> ", "
<> chemoraQuote[gfname] <> ", "
<> chemoraQuote[param] <> ", "
<> StringJoin
[ ToString[#[[1]]] <> ", "
<> chemoraQuote[#[[2]]<>ToString[gfname]] <> ", " & /@ cases ]
<> "0, NULL);\n" ] @@ # ] & /@ precomputedD];


chemoraCExpression[cleancalc,eqsReplaced];

mapRefAppend[cleancalc,ChemoraContents,
Expand Down
18 changes: 18 additions & 0 deletions Tools/CodeGen/Differencing.m
Expand Up @@ -156,6 +156,7 @@ A GridFunctionDerivative (GFD) is an expression of the form

chemoraPrecomputeDerivative;
chemoraPrecomputeDerivatives;
chemoraDifferencingMacroExpansions;

Begin["`Private`"];

Expand Down Expand Up @@ -877,6 +878,23 @@ A GridFunctionDerivative (GFD) is an expression of the form
"\n"},
{}],
diffHeader];

chemoraDifferencingMacroExpansions =
Flatten[$DifferencingMacroExpansions]
/. { ( opNameBase_[usless_,ind___] -> rhs_ ) :>
Rule[ ( ToString[opNameBase]
<> StringJoin[ ToString /@ {ind} ]) [u_],
rhs /. { ks_String/;StringMatchQ
[ks,RegularExpression["^KRANC.*"]]
:> Flatten
[StringCases
[ ks,
RegularExpression
["^KRANC_GFOFFSET3D\\(u,([^,]+),([^,]+),([^,]+)\\)"]
:> ChemoraNOffset[u, "$1","$2","$3"] ]]} ]};
chemoraDifferencingMacroExpansions =
chemoraDifferencingMacroExpansions /. { ( lhs_->{rhs_} ) -> lhs->rhs };

(* TODO: fix circular dependency which stops us from importing OpenCL in this package *)
If[OptionValue[UseOpenCL], diffHeader = OpenCL`OpenCLProcessDifferencingHeader[diffHeader]];
AppendObjectField[
Expand Down
10 changes: 10 additions & 0 deletions Tools/CodeGen/Kranc.m
Expand Up @@ -168,4 +168,14 @@
CArray;
ConditionExpression;

(* Chemora Symobls *)

chemoraQuote;
chemoraCDifferenceOps;

ChemoraNLiteral;
ChemoraNDO;
ChemoraNOffset;


EndPackage[];

0 comments on commit 5f87a03

Please sign in to comment.