Skip to content

Commit

Permalink
Merge branch 'master' into mixed-derivatives
Browse files Browse the repository at this point in the history
Conflicts:
	Tools/CodeGen/Calculation.m
	Tools/CodeGen/CalculationFunction.m
  • Loading branch information
eschnett committed Apr 24, 2012
2 parents 71e1bb3 + 3e25941 commit ae9e8c7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 21 deletions.
39 changes: 30 additions & 9 deletions Tools/CodeGen/CaKernel.m
Expand Up @@ -202,16 +202,37 @@ CCTK_POINTER FUNCTION Device_GetVarI (CCTK_POINTER IN cctkGH, CCTK_INT IN vi, CC

If[lookup[calc,ExecuteOn,Automatic] === Host,
{calc},
{mapReplaceAdd[

Module[
{deviceCalc, hostCalc, newCalcs},

deviceCalc =
mapReplaceAdd[
mapReplaceAdd[
mapReplace[
calc,
Name,"DEVICE_"<>lookup[calc,Name]],
UseCaKernel, True],
ExecuteOn, Device];

hostCalc =
mapReplaceAdd[
AddConditionSuffix[calc, "Accelerator::device_process"],
UseCaKernel, True],
ExecuteOn, Device],
mapReplaceAdd[
mapReplaceAdd[
AddConditionSuffix[calc, "Accelerator::host_process"],
UseCaKernel,False],
ExecuteOn, Host]}]];
mapReplaceAdd[
mapReplace[
calc,
Name,"HOST_"<>lookup[calc,Name]],
UseCaKernel,False],
ExecuteOn, Host];

newCalcs = Map[InNewScheduleGroup[lookup[calc,Name],#] &, {hostCalc, deviceCalc}];

(* Apply the conditional to the routines, not to the group *)
newCalcs = MapThread[AddConditionSuffix,
{newCalcs,
{"Accelerator::host_process",
"Accelerator::device_process"}}];

newCalcs]]];

DefFn[
WithHostCalculations[calcs_List] :=
Expand Down
63 changes: 56 additions & 7 deletions Tools/CodeGen/Calculation.m
Expand Up @@ -25,6 +25,7 @@
OutputGridFunctions;
AllGridFunctions;
GetCalculationName;
GetCalculationScheduleName;
GetEquations;
GetCalculationParameters;
CalculationStencilSize;
Expand All @@ -34,6 +35,7 @@
SeparateDerivatives;
AddCondition;
AddConditionSuffix;
InNewScheduleGroup;

Begin["`Private`"];

Expand Down Expand Up @@ -92,6 +94,12 @@
GetCalculationName[calc_List] :=
lookup[calc,Name]];

DefFn[
GetCalculationScheduleName[calc_List] :=
If[lookup[calc, UseCaKernel] && CalculationOnDevice[calc],
"CAKERNEL_Launch_",""]
<>lookup[calc, Name]];

DefFn[
GetCalculationWhere[calc_List] :=
lookup[calc,Where, Everywhere]];
Expand Down Expand Up @@ -144,7 +152,17 @@
SplitCalculation[calc_] :=
Module[
{splitBy = lookup[calc,SplitBy, {}],
oldName = lookup[calc,Name]},
oldName = lookup[calc,Name],
oldSchedule = lookup[calc, Schedule, Automatic],
newGroup},

If[ListQ[oldSchedule] && Length[oldSchedule] > 1,
ThrowError["Cannot split a calculation which is scheduled in more than one place"]];

newGroup = {Name -> oldName,
Language -> "None", (* groups do not have a language *)
SchedulePoint -> oldSchedule,
Comment -> ""};

If[Intersection[Flatten[splitBy,1],OutputGridFunctions[calc]] === {},
{calc},
Expand All @@ -157,7 +175,16 @@
ToString[i[[1]]],
"_"<>StringReplace[ToString[var],{"["->"","]"->"",","->""}]];
splitVars = If[ListQ[var], var, {var}];
partialCalculation[calc, nameSuffix, {}, splitVars]]],

newCalc = partialCalculation[calc, nameSuffix, {}, splitVars];

newCalc =
mapReplaceAdd[
mapReplaceAdd[
newCalc,
Schedule, {"in "<>oldName}],
ScheduleGroups, Append[lookup[calc, ScheduleGroups, {}],newGroup]]]],

splitBy]]]];

DefFn[
Expand All @@ -180,15 +207,19 @@

Module[
{derivGFName, derivGFName2, derivs, sepDerivs, sepDerivs2, calc2,
replaceSymmetric, replaceMixed, derivCalcs, derivCalcs2, addAfter},
replaceSymmetric, replaceMixed, derivCalcs, derivCalcs2, addAfter,
compCalcName},

(* Removing duplicate "DPDstandardNth" in derivative variable
names *)
derivGFName[pd_[var_,inds___]] :=
Symbol[StringReplace["Global`D"<>ToString[pd]<>ToString[var]<>Apply[StringJoin,Map[ToString,{inds}]], "Global`D"<>ToString[pd]<>"D"<>ToString[pd] -> "Global`D"<>ToString[pd]]];

derivGFName2[pd_[var_,inds___]] :=
StringReplace["D"<>ToString[pd]<>ToString[var]<>"_"<>Apply[StringJoin,Map[ToString,{inds}]], "D"<>ToString[pd]<>"D"<>ToString[pd] -> "D"<>ToString[pd]];
StringReplace["D"<>ToString[pd]<>ToString[var]<>"_"<>Apply[StringJoin,Map[ToString,{inds}]],
"D"<>ToString[pd]<>"D"<>ToString[pd] -> "D"<>ToString[pd]];

compCalcName = lookup[calc,Name]<>"_NonDerivatives";

replaceSymmetric = pd_[var_,i_,j_] /; i > j :> pd[var,j,i];
(* Replace mixed derivatives with first derivatives of
Expand Down Expand Up @@ -223,7 +254,6 @@
OrderedQ[{ToString[#1[[2,1]]]<>ToString[#1[[2,2]]],
ToString[#2[[2,1]]]<>ToString[#2[[2,2]]]}] &]];

calc1 = mapReplace[calc1, Schedule, Map[#<>" before "<>lookup[calc,Name] &, lookup[calc,Schedule]]];
calc1 = mapReplace[calc1, Name,
StringReplace[lookup[calc,Name]<>"_"<>derivGFName2[derivs[[1]]]<>
If[Length[derivs]>1,"_"<>"etc",""],"PDstandardNth"->""]];
Expand All @@ -250,9 +280,16 @@
derivative calculations that require it *)
derivCalcs2 = Map[addAfter[#, derivCalcs]&, derivCalcs2];

calc2 = mapReplace[calc,
derivCalcs = Map[InNewScheduleGroup[lookup[calc,Name], #] &, derivCalcs];

calc2 = mapReplace[mapReplace[calc, Name, compCalcName],
Equations,
(GetEquations[calc]/.replaceSymmetric/.replaceMixed) /. Map[# -> derivGFName[#] &, Flatten[Join[sepDerivs,sepDerivs2],1]]];
(GetEquations[calc]/.replaceSymmetric/.replaceMixed) /.
Map[# -> derivGFName[#] &, Flatten[Join[sepDerivs,sepDerivs2],1]]];

derivCalcs = Map[mapReplace[#, Schedule, Map[#<>" before "<>GetCalculationName[calc2] &, lookup[#,Schedule]]] &, derivCalcs];

calc2 = InNewScheduleGroup[lookup[calc,Name], calc2];

Join[derivCalcs, derivCalcs2, {calc2}]]]];

Expand All @@ -264,6 +301,18 @@
AddConditionSuffix[calc_List, condition_] :=
mapReplaceAdd[calc, Schedule, Map[#<>" IF "<>condition &, lookup[calc,Schedule]]]];

InNewScheduleGroup[groupName_String, calc_List] :=
Module[
{newGroup},
newGroup = {Name -> groupName,
Language -> "None", (* groups do not have a language *)
SchedulePoint -> lookup[calc,Schedule,Automatic],
Comment -> ""};
mapReplaceAdd[
mapReplaceAdd[
calc,
Schedule, {"in "<>groupName}],
ScheduleGroups, Append[lookup[calc, ScheduleGroups, {}],newGroup]]];

End[];

Expand Down
3 changes: 2 additions & 1 deletion Tools/CodeGen/CalculationFunction.m
Expand Up @@ -146,7 +146,8 @@ indentation after each line break (this will push the line length
ConditionalOnTextuals, Where, ConditionalOnKeywords,
CollectList, AllowedSymbols, ApplyBCs, Conditional, CachedVariables, SplitBy,
SeparatedDerivatives, SeparatedDerivatives2,
LocalGroups, NoSimplify, UseDGFE, SimpleCode, UseCaKernel};
LocalGroups, NoSimplify, UseDGFE, SimpleCode, UseCaKernel,
ScheduleGroups};

usedKeys = Map[First, calc];
unknownKeys = Complement[usedKeys, allowedKeys];
Expand Down
2 changes: 1 addition & 1 deletion Tools/CodeGen/Kranc.m
Expand Up @@ -115,7 +115,7 @@
Groups, Calculation, GridFunctions, Shorthands, Equations, Parameter,
Value, UsesFunctions, ArgString, Conditional, Conditionals, NewConditional, D1, D2, D3, D11, D22,
D33, D21, D31, D32, Textual, TriggerGroups, Include, RHSGroups, Tags,
Steerable, Never, Always, Recover, Primitives, CaKernel};
Steerable, Never, Always, Recover, Primitives, CaKernel, ScheduleGroups};

{ExcisionGFs};

Expand Down
9 changes: 6 additions & 3 deletions Tools/CodeGen/Schedule.m
Expand Up @@ -132,7 +132,7 @@
Return[Map[
Join[
{
Name -> If[lookup[calc, UseCaKernel] && CalculationOnDevice[calc], "CAKERNEL_Launch_",""]<>lookup[calc, Name],
Name -> GetCalculationScheduleName[calc]<>" as "<>GetCalculationName[calc],
SchedulePoint -> # <> relStr,
SynchronizedGroups -> If[StringMatchQ[#, "*MoL_CalcRHS*", IgnoreCase -> True] || StringMatchQ[#, "*MoL_RHSBoundaries*", IgnoreCase -> True],
{},
Expand Down Expand Up @@ -223,7 +223,7 @@
Options[CreateKrancScheduleFile] = ThornOptions;
CreateKrancScheduleFile[calcs_, groups_, evolvedGroups_, rhsGroups_, nonevolvedGroups_, thornName_,
evolutionTimelevels_, opts:OptionsPattern[]] :=
Module[{scheduledCalcs, scheduledStartup, scheduleMoLRegister, globalStorageGroups, scheduledFunctions, schedule, allParams},
Module[{scheduledCalcs, scheduledStartup, scheduleMoLRegister, globalStorageGroups, scheduledFunctions, schedule, allParams, calcGroups},

scheduledCalcs = Flatten[Map[scheduleCalc[#, groups, thornName, opts] &, calcs], 1];
scheduledStartup =
Expand Down Expand Up @@ -266,6 +266,9 @@
Map[rhsGroupStruct[#, evolutionTimelevels, evolutionTimelevels] &,
rhsGroups]];

(* Schedule groups defined in calculations *)
calcGroups = Union[Flatten[Map[lookup[#, ScheduleGroups, {}] &, calcs],1]];

scheduledFunctions =
Join[{scheduledStartup, scheduleRegisterSymmetries},
scheduledCalcs, CactusBoundary`GetScheduledFunctions[thornName, evolvedGroups],
Expand All @@ -276,7 +279,7 @@

allParams = Union@@((lookup[#,Parameters] &) /@ calcs);
schedule = CreateSchedule[globalStorageGroups,
CactusBoundary`GetScheduledGroups[thornName], scheduledFunctions, allParams];
Join[CactusBoundary`GetScheduledGroups[thornName], calcGroups], scheduledFunctions, allParams];

Return[schedule]];

Expand Down

0 comments on commit ae9e8c7

Please sign in to comment.