Skip to content

Commit

Permalink
Declare variables only when they are first used
Browse files Browse the repository at this point in the history
This allows simultaneous declaration and assignment as per Erik's
previous commit, which seems to give a noticeable performance
increase, at least with the PGI compiler on Kraken, and also allows
multiple assignments to the same variable in a single calculation.
  • Loading branch information
ianhinder committed Mar 10, 2010
1 parent f6f5a7d commit f88dacd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
57 changes: 31 additions & 26 deletions Tools/CodeGen/CalculationFunction.m
Expand Up @@ -147,7 +147,7 @@ indentation after each line break (this will push the line length
Return[StringJoin[Riffle[words, " "]]]];

(* Return a CodeGen block which assigns dest by evaluating expr *)
assignVariableFromExpression[dest_, expr_] := Module[{tSym, type, cleanExpr, code},
assignVariableFromExpression[dest_, expr_, declare_] := Module[{tSym, type, cleanExpr, code},

tSym = Unique[];

Expand All @@ -156,7 +156,7 @@ indentation after each line break (this will push the line length
cleanExpr = ReplacePowers[expr] /. sym`t -> tSym;

If[SOURCELANGUAGE == "C",
code = type <> " " <> ToString[dest] <> " = " <> ToString[cleanExpr, CForm, PageWidth -> Infinity] <> ";\n",
code = If[declare, type <> " ", ""] <> ToString[dest] <> " = " <> ToString[cleanExpr, CForm, PageWidth -> Infinity] <> ";\n",
code = ToString@dest <> ".eq." <> ToString[cleanExpr, FortranForm, PageWidth -> 120] <> "\n"
];

Expand Down Expand Up @@ -189,21 +189,6 @@ indentation after each line break (this will push the line length

debugInLoop = False;


declareVariablesForCalculation[calc_] :=
Module[{shorthands, localGFs},
shorthands = Sort@Union@Map[ToString, Union@Flatten@calculationAllUsedShorthands@calc];
shorthands = PartitionVarList@shorthands;

localGFs = Map[localName, Sort@Union@Map[ToString, Union@Flatten@calculationUsedGFs@calc]];
localGFs = PartitionVarList@localGFs;

{CommentedBlock["Declare shorthands",
Map[DeclareVariables[#, "// CCTK_REAL"] &, shorthands]],

CommentedBlock["Declare local copies of grid functions",
Map[DeclareVariables[#, "// CCTK_REAL"] &, localGFs]]}];

(* Derivative precomputation *)

oldDerivativesUsed[x_] :=
Expand Down Expand Up @@ -525,7 +510,6 @@ pathalogical enough (e.g. {s1 -> s2, s2 -> s1} would not be
"DECLARE_CCTK_PARAMETERS;\n\n",
If[!OptionValue[UseLoopControl], DeclareGridLoopVariables[], {}],
DeclareFDVariables[],
(* declareVariablesForCalculation[cleancalc], *)
(* declarePrecomputedDerivatives[dsUsed], *)
(* DeclareDerivatives[pddefs, eqs], *)
declareSubblockGFs[subblockGFs, 0],
Expand Down Expand Up @@ -673,6 +657,14 @@ pathalogical enough (e.g. {s1 -> s2, s2 -> s1} would not be
name[f_,3,2] -> name[f,2,3]}],
{}]];

(* Given an input list l, return a list L such that L[[i]] is True
only if l[[i]] is the first occurrence of l[[i]] in l and is not in
the list "already" *)
markFirst[l_List, already_List] :=
If[l =!= {},
{!MemberQ[already, First[l]]} ~Join~ markFirst[Rest[l], already ~Join~ {First[l]}],
{}];

Options[equationLoop] = ThornOptions;

equationLoop[eqs_,
Expand All @@ -683,7 +675,7 @@ pathalogical enough (e.g. {s1 -> s2, s2 -> s1} would not be
Module[{rhss, lhss, gfsInRHS, gfsInLHS, gfsOnlyInRHS, localGFs, localMap, eqs2,
derivSwitch, actualSyncGroups, code, functionName, calcCode,
syncCode, loopFunction, gfsInBoth, gfsDifferentiated,
gfsDifferentiatedAndOnLHS},
gfsDifferentiatedAndOnLHS, declare, eqsReplaced},

rhss = Map[#[[2]] &, eqs];
lhss = Map[#[[1]] &, eqs];
Expand Down Expand Up @@ -746,11 +738,25 @@ pathalogical enough (e.g. {s1 -> s2, s2 -> s1} would not be
replaceDerivatives[replaceWithDerivativesHidden[eqs2, localMap], {}]
];
*)

(* Replace grid functions with their local forms, and replace
partial dervatives with their precomputed values *)
eqsReplaced = If[useCSE, CSE, Identity][
replaceDerivatives[replaceWithDerivativesHidden[eqs2, localMap], {}]];

(* Construct a list, corresponding to the list of equations,
marking those which need their LHS variables declared. We
declare variables at the same time as assigning to them as it
gives a performance increase over declaring them separately at
the start of the loop. The local variables for the grid
functions which appear in the RHSs have been declared and set
already (DeclareMaybeAssignVariableInLoop below), so assignments
to these do not generate declarations here. *)
declare = markFirst[First /@ eqsReplaced, Map[localName, gfsInRHS]];

calcCode =
Map[{assignVariableFromExpression[#[[1]], #[[2]]], "\n"} &,
If[useCSE, CSE, Identity][
replaceDerivatives[replaceWithDerivativesHidden[eqs2, localMap], {}]]
];
MapThread[{assignVariableFromExpression[#1[[1]], #1[[2]], #2], "\n"} &,
{eqsReplaced, declare}];

Join[
(*
Expand All @@ -762,8 +768,7 @@ pathalogical enough (e.g. {s1 -> s2, s2 -> s1} would not be
*)

GenericGridLoop[functionName,
{declareVariablesForCalculation[cleancalc],
declarePrecomputedDerivatives[dsUsed],
{declarePrecomputedDerivatives[dsUsed],
(* DeclareDerivatives[pddefs, eqs], *)
DeclareDerivatives[defsWithoutShorts, eqsOrdered],

Expand All @@ -777,7 +782,7 @@ pathalogical enough (e.g. {s1 -> s2, s2 -> s1} would not be
"CCTK_REAL", localName[#], GridName[#],
StringMatchQ[ToString[GridName[#]], "eT" ~~ _ ~~ _ ~~ "[" ~~ __ ~~ "]"],
"*stress_energy_state"] &,
gfsOnlyInRHS]],
gfsInRHS]],

(*
CommentedBlock["Check for nans",
Expand Down
2 changes: 1 addition & 1 deletion Tools/CodeGen/CodeGen.m
Expand Up @@ -241,7 +241,7 @@

DeclareVariables[names_?ListQ, type_] :=
If[SOURCELANGUAGE == "C",
{type, " ", CommaInitSeparated@names, EOL[]},
{type, " ", CommaSeparated@names, EOL[]},
{type, " :: ", CommaSeparated@names, EOL[]} (* no value init avoids implicit SAVE attribute *)
];

Expand Down

0 comments on commit f88dacd

Please sign in to comment.