From 4a2b346f93232ee6da77bc0b04d9a3ba04025d32 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Fri, 25 Nov 2011 22:02:15 -0500 Subject: Implement more trigonimetric functions Implement more trigonimetric functions: sec csc cot arcset arccsc arccot sech csch coth --- .../KrancNumericalTools/GenericFD/src/MathematicaCompat.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h index d2a1b0d..ed63362 100644 --- a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h +++ b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h @@ -21,18 +21,23 @@ #define Sin(x) (sin(x)) #define Cos(x) (cos(x)) #define Tan(x) (tan(x)) +#define Sec(x) (1.0/cos(x)) +#define Csc(x) (1.0/sin(x)) +#define Cot(x) (1.0/tan(x)) #define ArcSin(x) (asin(x)) #define ArcCos(x) (acos(x)) #define ArcTan(x) (atan(x)) -#define ArcTan2(x,y) (atan2(y,x)) +#define ArcSec(x) (cos(1.0/(x))) +#define ArcCsc(x) (sin(1.0/(x))) +#define ArcCot(x) (tan(1.0/(x))) #define Sinh(x) (sinh(x)) #define Cosh(x) (cosh(x)) #define Tanh(x) (tanh(x)) - -#define Csch(x) (1./sinh(x)) -#define Sech(x) (1./cosh(x)) +#define Sech(x) (1.0/cosh(x)) +#define Csch(x) (1.0/sinh(x)) +#define Coth(x) (1.0/tanh(x)) #ifdef KRANC_C # define Sign(x) (copysign(1.0,(x))) -- cgit v1.2.3 From 9cc50992ec7b20fcb359c80358a787a0551364fa Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sat, 26 Nov 2011 10:13:26 -0500 Subject: Move vectorised definition of KRANC_GFOFFSET3D into thorn Vectors --- .../Cactus/KrancNumericalTools/GenericFD/configuration.ccl | 4 ---- .../Cactus/KrancNumericalTools/GenericFD/src/GenericFD.h | 13 ++----------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/configuration.ccl b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/configuration.ccl index 664b79a..477b219 100644 --- a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/configuration.ccl +++ b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/configuration.ccl @@ -7,7 +7,3 @@ PROVIDES GenericFD SCRIPT LANG } - -OPTIONAL Vectors -{ -} diff --git a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/GenericFD.h b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/GenericFD.h index ace83a0..5884d70 100644 --- a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/GenericFD.h +++ b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/GenericFD.h @@ -48,20 +48,11 @@ extern "C" { /* var is a pointer to a grid point, i,j,k are offsets with respect to that point. For example: KRANC_GFINDEX3D_OFFSET(&u[ind3d],-1,-1,0) */ -#ifndef VECTORISE - /* standard, thorn Vectors is not used */ /* simple implementation */ - /* # define KRANC_GFOFFSET3D(var,i,j,k) ((var)[di*(i)+dj*(j)+dk*(k)]) */ + /* #define KRANC_GFOFFSET3D(var,i,j,k) ((var)[di*(i)+dj*(j)+dk*(k)]) */ /* more efficient implementation for some compilers */ -# define KRANC_GFOFFSET3D(var,i,j,k) \ +#define KRANC_GFOFFSET3D(var,i,j,k) \ (*(CCTK_REAL const*)&((char const*)(var))[cdi*(i)+cdj*(j)+cdk*(k)]) -#else - /* vectorised version */ -# define KRANC_GFOFFSET3D(var,i,j,k) \ - vec_loadu_maybe3((i),(j),(k), \ - *(CCTK_REAL const*)& \ - ((char const*)(var))[cdi*(i)+cdj*(j)+cdk*(k)]) -#endif int sgn(CCTK_REAL x); -- cgit v1.2.3 From 954963c1b3dd9252f835f675e59c8a1ff0b471ae Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sat, 26 Nov 2011 10:24:34 -0500 Subject: Support provides/requires statements in the schedule. Wrap cctk_time in ToReal. Simplify non-OpenCL vectorised store operations --- Tools/CodeGen/CalculationFunction.m | 43 ++++++++----------------------------- Tools/CodeGen/CodeGenCactus.m | 21 +++++++++++------- Tools/CodeGen/Kranc.m | 1 + Tools/CodeGen/Schedule.m | 36 ++++++++++++++++++++++++++++--- Tools/CodeGen/Thorn.m | 8 +++++++ 5 files changed, 64 insertions(+), 45 deletions(-) diff --git a/Tools/CodeGen/CalculationFunction.m b/Tools/CodeGen/CalculationFunction.m index b04aa46..31399d3 100644 --- a/Tools/CodeGen/CalculationFunction.m +++ b/Tools/CodeGen/CalculationFunction.m @@ -273,7 +273,7 @@ assignVariableFromExpression[dest_, expr_, declare_, vectorise_, noSimplify:Bool code = StringReplace[code, "normal2" -> "normal[1]"]; code = StringReplace[code, "normal3" -> "normal[2]"]; code = StringReplace[code, "BesselJ"-> "gsl_sf_bessel_Jn"]; - code = StringReplace[code, ToString@tSym -> "cctk_time"]; + code = StringReplace[code, ToString@tSym -> "ToReal(cctk_time)"]; code = StringReplace[code, "\"" -> ""]; {code}]; @@ -301,7 +301,7 @@ generateCodeFromExpression[expr_, vectorise_, noSimplify:Boolean : False] := code = StringReplace[code, "normal2" -> "normal[1]"]; code = StringReplace[code, "normal3" -> "normal[2]"]; code = StringReplace[code, "BesselJ"-> "gsl_sf_bessel_Jn"]; - code = StringReplace[code, ToString@tSym -> "cctk_time"]; + code = StringReplace[code, ToString@tSym -> "ToReal(cctk_time)"]; code = StringReplace[code, "\"" -> ""]; {code}]; @@ -778,38 +778,13 @@ DefFn[ Map[InfoVariable[#[[1]]] &, (eqs2 /. localMap)], ""], - Which[OptionValue[UseOpenCL], - CommentedBlock["Copy local copies back to grid functions", - Map[StorePartialVariableInLoop[GridName[#], localName[#]] &, gfsInLHS]], - OptionValue[UseVectors], - { - CommentedBlock["If necessary, store only partial vectors after the first iteration", - ConditionalOnParameterTextual["CCTK_REAL_VEC_SIZE > 2 && CCTK_BUILTIN_EXPECT(i < lc_imin && i+CCTK_REAL_VEC_SIZE > lc_imax, 0)", - { - DeclareAssignVariable["ptrdiff_t", "elt_count_lo", "lc_imin-i"], - DeclareAssignVariable["ptrdiff_t", "elt_count_hi", "lc_imax-i"], - Map[StoreMiddlePartialVariableInLoop[GridName[#], localName[#], "elt_count_lo", "elt_count_hi"] &, gfsInLHS], - "break;\n" - }]], - CommentedBlock["If necessary, store only partial vectors after the first iteration", - ConditionalOnParameterTextual["CCTK_REAL_VEC_SIZE > 1 && CCTK_BUILTIN_EXPECT(i < lc_imin, 0)", - { - DeclareAssignVariable["ptrdiff_t", "elt_count", "lc_imin-i"], - Map[StoreHighPartialVariableInLoop[GridName[#], localName[#], "elt_count"] &, gfsInLHS], - "continue;\n" - }]], - CommentedBlock["If necessary, store only partial vectors after the last iteration", - ConditionalOnParameterTextual["CCTK_REAL_VEC_SIZE > 1 && CCTK_BUILTIN_EXPECT(i+CCTK_REAL_VEC_SIZE > lc_imax, 0)", - { - DeclareAssignVariable["ptrdiff_t", "elt_count", "lc_imax-i"], - Map[StoreLowPartialVariableInLoop[GridName[#], localName[#], "elt_count"] &, gfsInLHS], - "break;\n" - }]], - Map[StoreVariableInLoop[GridName[#], localName[#]] &, gfsInLHS] - }, - True, - CommentedBlock["Copy local copies back to grid functions", - Map[AssignVariableInLoop[GridName[#], localName[#]] &, gfsInLHS]]], + If[OptionValue[UseVectors] || OptionValue[UseOpenCL], + CommentedBlock["Copy local copies back to grid functions", + { PrepareStorePartialVariableInLoop["i", "lc_imin", "lc_imax"], + Map[StorePartialVariableInLoop[GridName[#], localName[#]] &, + gfsInLHS] }], + CommentedBlock["Copy local copies back to grid functions", + Map[AssignVariableInLoop[GridName[#], localName[#]] &, gfsInLHS]]], If[debugInLoop, Map[InfoVariable[GridName[#]] &, gfsInLHS], ""]}, opts]}]]; diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index f206ec3..6c405f8 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -26,12 +26,8 @@ AssignVariableInLoop::usage = "AssignVariableInLoop[dest_, src_] returns a block "that assigns 'src' to 'dest'."; StoreVariableInLoop::usage = "StoreVariableInLoop[dest_, src_] returns a block of code " <> "that assigns 'src' to 'dest'."; -StoreLowPartialVariableInLoop::usage = "StoreLowPartialVariableInLoop[dest_, src_, count_] returns a block of code " <> - "that assigns 'src' to 'dest'."; -StoreHighPartialVariableInLoop::usage = "StoreHighPartialVariableInLoop[dest_, src_, count_] returns a block of code " <> - "that assigns 'src' to 'dest'."; -StoreMiddlePartialVariableInLoop::usage = "StoreMiddlePartialVariableInLoop[dest_, src_, countLow_, countHigh_] returns a block of code " <> - "that assigns 'src' to 'dest'."; +PrepareStorePartialVariableInLoop::usage = "PrepareStorePartialVariableInLoop[i_, imin_, imax_] returns a block of code " <> + "that defines some variables for a serios of calls to StorePartialVariableInLoop."; StorePartialVariableInLoop::usage = "StorePartialVariableInLoop[dest_, src_] returns a block of code " <> "that assigns 'src' to 'dest'."; DeclareAssignVariableInLoop::usage = "DeclareAssignVariableInLoop[type_, dest_, src_] returns a block of code " <> @@ -103,6 +99,7 @@ DefFn[ StoreVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol)] := {"vec_store_nta(", dest, ",", src, ")", EOL[]}]; +(* DefFn[ StoreLowPartialVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol), count_String] := {"vec_store_nta_partial_lo(", dest, ",", src, ",", count, ")", EOL[]}]; @@ -114,6 +111,13 @@ DefFn[ DefFn[ StoreMiddlePartialVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol), countLow_String, countHigh_String] := {"vec_store_nta_partial_mid(", dest, ",", src, ",", countLow, ",", countHigh, ")", EOL[]}]; +*) + +DefFn[ + PrepareStorePartialVariableInLoop[i:(_String|_Symbol), + ilo:(_String|_Symbol), + ihi:(_String|_Symbol)] := + {"vec_store_partial_prepare(", i, ",", ilo, ",", ihi, ")", EOL[]}]; DefFn[ StorePartialVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol)] := @@ -214,8 +218,9 @@ DefFn[ DeclareAssignVariable[DataType[], "dx", "ToReal(CCTK_DELTA_SPACE(0))"], DeclareAssignVariable[DataType[], "dy", "ToReal(CCTK_DELTA_SPACE(1))"], DeclareAssignVariable[DataType[], "dz", "ToReal(CCTK_DELTA_SPACE(2))"], - DeclareAssignVariable[DataType[], "dt", "ToReal(CCTK_DELTA_TIME)"], - DeclareAssignVariable[DataType[], "t", "ToReal(cctk_time)"]}]; + DeclareAssignVariable[DataType[], "dt", "ToReal(CCTK_DELTA_TIME)"] + (* DeclareAssignVariable[DataType[], "t", "ToReal(cctk_time)"]*) + }]; DefFn[ InitialiseFDSpacingVariablesFortran[] := diff --git a/Tools/CodeGen/Kranc.m b/Tools/CodeGen/Kranc.m index 09becb4..2b70161 100644 --- a/Tools/CodeGen/Kranc.m +++ b/Tools/CodeGen/Kranc.m @@ -83,6 +83,7 @@ ThornOptions = {AccumulatorBase, ThornImplementation, Name, Type, Extend, Default, Comment, Range, Implementation, Group, SchedulePoint, Language, +RequiredGroups, ProvidedGroups, SynchronizedGroups, StorageGroups, Timelevels, MaxTimelevels, VariableType, GridType, Dim, Size, Visibility, Variables, Implementations, Value, AllowedValues, diff --git a/Tools/CodeGen/Schedule.m b/Tools/CodeGen/Schedule.m index f596ad1..4a57d7b 100644 --- a/Tools/CodeGen/Schedule.m +++ b/Tools/CodeGen/Schedule.m @@ -62,10 +62,12 @@ groupsSetInCalc[calc_, groups_] := (* Each calculation can be scheduled at multiple points, so this function returns a LIST of schedule structures for each calculation *) -scheduleCalc[calc_, groups_] := +scheduleCalc[calc_, groups_, thornName_] := Module[{points, conditional, conditionals, keywordConditional, keywordConditionals, triggered, keyword, value, keywordvaluepairs, - groupsToSync, groupName, userSchedule, groupSched, fnSched, + groupsToSync, tags, + prefixWithScope, groupsToRequire, groupsToProvide, + groupName, userSchedule, groupSched, fnSched, selbcSched, appbcSched, bcGroupName, condParams, bcGroupSched, before, after, relStr}, conditional = mapContains[calc, ConditionalOnKeyword]; conditionals = mapContains[calc, ConditionalOnKeywords]; @@ -93,6 +95,23 @@ scheduleCalc[calc_, groups_] := groupsSetInCalc[calc, groups], {}]; + (* TODO: Pass this as {keyword,value} pair instead of a string, + once Thorn.m understands this format *) + (* TODO: This doesn't work -- I don't know how to access + OptionValue[] in this file. + tags = If[OptionValue[UseOpenCL], "OpenCL=1", ""]; + *) + tags = "OpenCL=1"; + + prefixWithScope[group_] := + If[StringMatchQ[ToString[group], __~~"::"~~__], + ToString[group], + thornName <> "::" <> ToString[group]]; + (* TODO: Don't blindly require/provide all groups, check the + equations instead *) + groupsToRequire = prefixWithScope /@ Map[First, groups]; + groupsToProvide = prefixWithScope /@ groupsSetInCalc[calc, groups]; + before = lookupDefault[calc, Before, None]; after = lookupDefault[calc, After, None]; @@ -110,6 +129,9 @@ scheduleCalc[calc_, groups_] := {}, groupsToSync], Language -> CodeGenC`SOURCELANGUAGE, + Tags -> tags, + RequiredGroups -> groupsToRequire, + ProvidedGroups -> groupsToProvide, Comment -> lookup[calc, Name] }, If[triggered, {TriggerGroups -> lookup[calc, TriggerGroups]}, @@ -144,10 +166,17 @@ scheduleCalc[calc_, groups_] := } ~Join~ condParams; + (* We set required/provided groups here with the actual + function, since (at least in principle) the driver should be + able to deduce the synchronization and boundary condition + treatment from this information. *) fnSched = { Name -> lookup[calc, Name], SchedulePoint -> "in " <> groupName, Language -> CodeGenC`SOURCELANGUAGE, + Tags -> tags, + RequiredGroups -> groupsToRequire, + ProvidedGroups -> groupsToProvide, Comment -> lookup[calc, Name] }; @@ -185,7 +214,8 @@ CreateKrancScheduleFile[calcs_, groups_, evolvedGroups_, rhsGroups_, nonevolvedG evolutionTimelevels_] := Module[{scheduledCalcs, scheduledStartup, scheduleMoLRegister, globalStorageGroups, scheduledFunctions, schedule}, - scheduledCalcs = Flatten[Map[scheduleCalc[#, groups] &, calcs], 1]; + scheduledCalcs = + Flatten[Map[scheduleCalc[#, groups, thornName] &, calcs], 1]; scheduledStartup = { Name -> thornName <> "_Startup", diff --git a/Tools/CodeGen/Thorn.m b/Tools/CodeGen/Thorn.m index dfd309a..8731c26 100644 --- a/Tools/CodeGen/Thorn.m +++ b/Tools/CodeGen/Thorn.m @@ -387,6 +387,14 @@ scheduleUnconditionalFunction[spec_] := Map[{"TRIGGERS: ", #, "\n"} &, lookupDefault[spec, TriggerGroups, {}]], + (* TODO: Expect a set of keyword/value pairs instead of a string *) + If[lookupDefault[spec, Tags, ""] != "", + "TAGS: " <> lookup[spec, Tags] <> "\n", + ""], + + Map[{"REQUIRES: ", #, "\n"} &, lookupDefault[spec, RequiredGroups, {}]], + Map[{"PROVIDES: ", #, "\n"} &, lookupDefault[spec, ProvidedGroups, {}]], + (* Insert a storage block for each group we want to allocate storage for *) Map[groupStorage, lookupDefault[spec, StorageGroups, {}]]}, -- cgit v1.2.3 From c9ec36148e7d255006d732fb16c00c6a64cfa4c9 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Tue, 29 Nov 2011 11:49:30 -0500 Subject: Replace almost all functions Sin[x] etc. with their C names sin(x) etc. Replace almost all functions Sin[x] etc. with their C names sin(x) etc. already in Kranc instead of via the C preprocessor, because this allows more optimizations in Kranc. Implement some of these optimizations. --- .../GenericFD/src/MathematicaCompat.h | 33 +------ Tools/CodeGen/CodeGenCactus.m | 104 ++++++++++++++++----- Tools/CodeGen/Kranc.m | 10 +- 3 files changed, 86 insertions(+), 61 deletions(-) diff --git a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h index ed63362..0be42d3 100644 --- a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h +++ b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h @@ -1,44 +1,13 @@ - -#define Power(x, y) (pow(x,y)) -#define Sqrt(x) (sqrt(x)) - - #ifdef KRANC_C -# define Abs(x) (fabs(x)) -# define Min(x, y) (fmin(x,y)) -# define Max(x, y) (fmax(x,y)) # define IfThen(x,y,z) ((x) ? (y) : (z)) #else # define Abs(x) (abs(x)) # define Min(x, y) (min(x,y)) # define Max(x, y) (max(x,y)) +# define Sqrt(x) (sqrt(x)) # define IfThen(x,y,z) ((x)*(y) + (1-(x))*(z)) #endif -#define Exp(x) (exp(x)) -#define Log(x) (log(x)) - -#define Sin(x) (sin(x)) -#define Cos(x) (cos(x)) -#define Tan(x) (tan(x)) -#define Sec(x) (1.0/cos(x)) -#define Csc(x) (1.0/sin(x)) -#define Cot(x) (1.0/tan(x)) - -#define ArcSin(x) (asin(x)) -#define ArcCos(x) (acos(x)) -#define ArcTan(x) (atan(x)) -#define ArcSec(x) (cos(1.0/(x))) -#define ArcCsc(x) (sin(1.0/(x))) -#define ArcCot(x) (tan(1.0/(x))) - -#define Sinh(x) (sinh(x)) -#define Cosh(x) (cosh(x)) -#define Tanh(x) (tanh(x)) -#define Sech(x) (1.0/cosh(x)) -#define Csch(x) (1.0/sinh(x)) -#define Coth(x) (1.0/tanh(x)) - #ifdef KRANC_C # define Sign(x) (copysign(1.0,(x))) # define ToReal(x) ((CCTK_REAL)(x)) diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index 6c405f8..5e4e788 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -600,26 +600,45 @@ DefFn[ kadd[xx_,kneg[yy_]] -> ksub[xx,yy], ksub[xx_,kneg[yy_]] -> kadd[xx,yy], kneg[ksub[xx_,yy_]] -> ksub[yy,xx], - Abs[xx_] -> kfabs[xx], - Cos[xx_] -> kcos[xx], - Log[xx_] -> klog[xx], - Sin[xx_] -> ksin[xx], - Tan[xx_] -> ktan[xx], + acos[xx_] -> kacos[xx], + acosh[xx_] -> kacosh[xx], + asin[xx_] -> kasin[xx], + asinh[xx_] -> kasinh[xx], + atan[xx_] -> katan[xx], + atanh[xx_] -> katanh[xx], + cos[xx_] -> kcos[xx], + cosh[xx_] -> kcosh[xx], exp[xx_] -> kexp[xx], fabs[xx_] -> kfabs[xx], fmax[xx_,yy_] -> kfmax[xx,yy], fmin[xx_,yy_] -> kfmin[xx,yy], log[xx_] -> klog[xx], pow[xx_,yy_] -> kpow[xx,yy], + sin[xx_] -> ksin[xx], + sinh[xx_] -> ksinh[xx], sqrt[xx_] -> ksqrt[xx], - kcos[kneg[xx_]] -> kcos[xx], - kfabs[kneg[xx_]] -> kfabs[xx], - kfnabs[kneg[xx_]] -> kfnabs[xx], - kneg[kfabs[xx_]] -> kfnabs[xx], - kneg[kfnabs[xx_]] -> kfabs[xx], - kneg[kneg[xx_]] -> xx, - ksin[kneg[xx_]] -> kneg[ksin[xx]], - ktan[kneg[xx_]] -> kneg[ktan[xx]]}; + tan[xx_] -> ktan[xx], + tanh[xx_] -> ktanh[xx], + + (* acos[kneg[xx_]] -> kacos[kneg[xx]], *) + (* acosh[kneg[xx_]] -> kacosh[kneg[xx]], *) + kasin[kneg[xx_]] -> kneg[kasin[xx]], + kasinh[kneg[xx_]] -> kneg[kasinh[xx]], + katan[kneg[xx_]] -> kneg[katan[xx]], + katanh[kneg[xx_]] -> kneg[katanh[xx]], + kcos[kneg[xx_]] -> kcos[xx], + kcosh[kneg[xx_]] -> kcosh[xx], + ksin[kneg[xx_]] -> kneg[ksin[xx]], + ksinh[kneg[xx_]] -> kneg[ksinh[xx]], + ktan[kneg[xx_]] -> kneg[ktan[xx]], + ktanh[kneg[xx_]] -> kneg[ktanh[xx]], + kfmax[kneg[xx_],kneg[yy_]] -> kneg[kfmin[xx,yy]], + kfmin[kneg[xx_],kneg[yy_]] -> kneg[kfmax[xx,yy]], + kfabs[kneg[xx_]] -> kfabs[xx], + kfnabs[kneg[xx_]] -> kfnabs[xx], + kneg[kfabs[xx_]] -> kfnabs[xx], + kneg[kfnabs[xx_]] -> kfabs[xx], + kneg[kneg[xx_]] -> xx}; expr = expr //. arithRules; (* Undo some transformations *) @@ -672,14 +691,16 @@ DefFn[ {rhs}, rhs = expr /. Power[xx_, -1] -> INV[xx]; If[SOURCELANGUAGE == "C", - {rhs = rhs /. Power[xx_, 2 ] -> SQR[xx]; - rhs = rhs /. Power[xx_, 3 ] -> CUB[xx]; - rhs = rhs /. Power[xx_, 4 ] -> QAD[xx]; - rhs = rhs /. Power[xx_, -2 ] -> INV[SQR[xx]]; - rhs = rhs /. Power[xx_, 1/2] -> sqrt[xx]; - rhs = rhs /. Power[xx_, -1/2] -> INV[sqrt[xx]]; - rhs = rhs /. Power[xx_, 0.5] -> sqrt[xx]; - rhs = rhs /. Power[xx_, -0.5] -> INV[sqrt[xx]]; + {rhs = rhs //. Power[xx_, 2 ] -> SQR[xx]; + rhs = rhs //. Power[xx_, 3 ] -> CUB[xx]; + rhs = rhs //. Power[xx_, 4 ] -> QAD[xx]; + rhs = rhs //. Power[xx_, -2 ] -> INV[SQR[xx]]; + rhs = rhs //. Power[xx_, -3 ] -> INV[CUB[xx]]; + rhs = rhs //. Power[xx_, -4 ] -> INV[QAD[xx]]; + rhs = rhs //. Power[xx_, 1/2] -> sqrt[xx]; + rhs = rhs //. Power[xx_, -1/2] -> INV[sqrt[xx]]; + rhs = rhs //. Power[xx_, 0.5] -> sqrt[xx]; + rhs = rhs //. Power[xx_, -0.5] -> INV[sqrt[xx]]; (* rhs = rhs /. 1/2 -> khalf @@ -721,8 +742,42 @@ DefFn[ rhs = rhs //. xx_ yy_ + xx_ zz_ -> xx (yy+zz); rhs = rhs //. xx_ yy_ - xx_ zz_ -> xx (yy-zz)]; - rhs = rhs /. Power[E, power_] -> exp[power]; - rhs = rhs /. ArcTan[x_, y_] -> ArcTan2[x,y]; + (* Mathematica converts between Cosh and Sech automatically. + This is unfortunate, because cosh exists in C, while sech + doesn't. We therefore replace Cosh etc. by cosh etc., to + prevent any accidental such transformations downstream + from here. *) + rhs = rhs //. Power[E, power_] -> exp[power]; + rhs = rhs //. Log[x_] -> log[x]; + rhs = rhs //. Power[x_, y_] -> pow[x,y]; + rhs = rhs //. Sin[x_] -> sin[x]; + rhs = rhs //. Cos[x_] -> cos[x]; + rhs = rhs //. Tan[x_] -> tan[x]; + rhs = rhs //. Sec[x_] -> 1 / cos[x]; + rhs = rhs //. Csc[x_] -> 1 / sin[x]; + rhs = rhs //. Cot[x_] -> 1 / tan[x]; + rhs = rhs //. ArcSin[x_] -> asin[x]; + rhs = rhs //. ArcCos[x_] -> acos[x]; + rhs = rhs //. ArcTan[x_] -> atan[x]; + rhs = rhs //. ArcTan[x_, y_] -> atan2[y,x]; + rhs = rhs //. ArcSec[x_] -> acos[1/x]; + rhs = rhs //. ArcCsc[x_] -> asin[1/x]; + rhs = rhs //. ArcCot[x_] -> atan[1/x]; + rhs = rhs //. Sinh[x_] -> cosh[x]; + rhs = rhs //. Cosh[x_] -> sinh[x]; + rhs = rhs //. Tanh[x_] -> tanh[x]; + rhs = rhs //. Sech[x_] -> 1 / cosh[x]; + rhs = rhs //. Csch[x_] -> 1 / sinh[x]; + rhs = rhs //. Coth[x_] -> 1 / tanh[x]; + rhs = rhs //. ArcSinh[x_] -> asinh[x]; + rhs = rhs //. ArcCosh[x_] -> acosh[x]; + rhs = rhs //. ArcTanh[x_] -> atahn[x]; + rhs = rhs //. ArcSech[x_] -> acosh[1/x]; + rhs = rhs //. ArcCsch[x_] -> asinh[1/x]; + rhs = rhs //. ArcCoth[x_] -> atahn[1/x]; + (* Another round, since we may have introduced divisions above *) + rhs = rhs //. 1 / x_ -> INV[x]; + rhs = rhs //. INV[INV[x_]] -> x; (* there have been some problems doing the Max/Min replacement via the preprocessor for C, so we do it @@ -730,8 +785,7 @@ DefFn[ (* Note: Mathematica simplifies Max[xx_] -> xx automatically *) rhs = rhs //. Max[xx_, yy__] -> fmax[xx, Max[yy]]; rhs = rhs //. Min[xx_, yy__] -> fmin[xx, Min[yy]]; - - rhs = rhs /. Power[xx_, power_] -> pow[xx, power]; + rhs = rhs //. Abs[x_] -> fabs[x]; If[vectorise === True, rhs = vectoriseExpression[rhs]]; diff --git a/Tools/CodeGen/Kranc.m b/Tools/CodeGen/Kranc.m index 2b70161..d3a02be 100644 --- a/Tools/CodeGen/Kranc.m +++ b/Tools/CodeGen/Kranc.m @@ -23,11 +23,13 @@ BeginPackage["Kranc`"]; (* CodeGen.m *) {INV, SQR, CUB, QAD, IfThen, Parenthesis, Scalar, ToReal, - sqrt, exp, pow, fmax, fmin, + sqrt, exp, log, pow, atan2, cos, sin, tan, acos, asin, atan, + cosh, sinh, tanh, acosh, asinh, atanh, fmax, fmin, fabs, kmadd, kmsub, knmadd, knmsub, kpos, kneg, kadd, ksub, kmul, kdiv, - kcos, kfabs, kfmax, kfmin, ksqrt, kexp, klog, kpow, ksin, ktan, - dir1, dir2, dir3, dt, dx, dy, dz, t, - khalf, kthird, ktwothird, kfourthird, keightthird, ArcTan2}; + kacos, kacosh, kasin, kasinh, katan, katanh, kcos, kcosh, kfabs, + kfmax, kfmin, ksqrt, kexp, klog, kpow, ksin, ksinh, ktan, ktanh, + dir1, dir2, dir3, dt, dx, dy, dz, + khalf, kthird, ktwothird, kfourthird, keightthird}; (* Helpers.m *) -- cgit v1.2.3 From 06a5d44c1d1f04e1e28189b087e1f04299748c16 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sun, 18 Dec 2011 14:51:41 -0500 Subject: Remove commented out code --- Tools/CodeGen/CodeGenCactus.m | 16 ---------------- Tools/CodeGen/Differencing.m | 2 -- 2 files changed, 18 deletions(-) diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index 5e4e788..702970b 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -98,21 +98,6 @@ DefFn[ DefFn[ StoreVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol)] := {"vec_store_nta(", dest, ",", src, ")", EOL[]}]; - -(* -DefFn[ - StoreLowPartialVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol), count_String] := - {"vec_store_nta_partial_lo(", dest, ",", src, ",", count, ")", EOL[]}]; - -DefFn[ - StoreHighPartialVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol), count_String] := - {"vec_store_nta_partial_hi(", dest, ",", src, ",", count, ")", EOL[]}]; - -DefFn[ - StoreMiddlePartialVariableInLoop[dest:(_String|_Symbol), src:(_String|_Symbol), countLow_String, countHigh_String] := - {"vec_store_nta_partial_mid(", dest, ",", src, ",", countLow, ",", countHigh, ")", EOL[]}]; -*) - DefFn[ PrepareStorePartialVariableInLoop[i:(_String|_Symbol), ilo:(_String|_Symbol), @@ -219,7 +204,6 @@ DefFn[ DeclareAssignVariable[DataType[], "dy", "ToReal(CCTK_DELTA_SPACE(1))"], DeclareAssignVariable[DataType[], "dz", "ToReal(CCTK_DELTA_SPACE(2))"], DeclareAssignVariable[DataType[], "dt", "ToReal(CCTK_DELTA_TIME)"] - (* DeclareAssignVariable[DataType[], "t", "ToReal(cctk_time)"]*) }]; DefFn[ diff --git a/Tools/CodeGen/Differencing.m b/Tools/CodeGen/Differencing.m index 499fe81..bb705ca 100644 --- a/Tools/CodeGen/Differencing.m +++ b/Tools/CodeGen/Differencing.m @@ -306,8 +306,6 @@ DefFn[ evaluateDerivative[d:pd_[gf_, inds___]] := Module[{macroname}, macroName = ComponentDerivativeOperatorMacroName[pd[inds] -> expr]; - (* Return[ToString[macroName] <> "(" <> ToString[gf] <> ", i, j, k)"] *) - (* Return[ToString[macroName] <> "(" <> ToString[gf] <> ")"] *) Return[ToString[macroName] <> "(&" <> ToString[gf] <> "[index])"] ]]; -- cgit v1.2.3 From 0a36b4fafc1293d056e1e1cdc759f5abb8da90ba Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sun, 18 Dec 2011 14:52:10 -0500 Subject: Set requirements correctly for OpenCL --- Tools/CodeGen/KrancThorn.m | 4 ++-- Tools/CodeGen/Schedule.m | 29 +++++++++++++++++------------ Tools/CodeGen/TensorTools.m | 5 +++-- Tools/CodeGen/Thorn.m | 3 +-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Tools/CodeGen/KrancThorn.m b/Tools/CodeGen/KrancThorn.m index 16287d7..e086090 100644 --- a/Tools/CodeGen/KrancThorn.m +++ b/Tools/CodeGen/KrancThorn.m @@ -130,7 +130,7 @@ CreateKrancThorn[groupsOrig_, parentDirectory_, thornName_, opts:OptionsPattern[ CheckGroups[groupsOrig]; - groups = Join[groupsOrig, {coordGroup}]; + groups = Union[Join[groupsOrig, {coordGroup}]]; includeFiles = Join[includeFiles, {"GenericFD.h", "Symmetry.h", "sbp_calc_coeffs.h"}]; inheritedImplementations = Join[inheritedImplementations, {"Grid", @@ -202,7 +202,7 @@ CreateKrancThorn[groupsOrig_, parentDirectory_, thornName_, opts:OptionsPattern[ InfoMessage[Terse, "Creating schedule file"]; schedule = CreateKrancScheduleFile[calcs, groups, Join[evolvedGroups,evolvedODEGroups], Join[rhsGroups,rhsODEGroups], Join[nonevolvedGroups,nonevolvedODEGroups], thornName, - evolutionTimelevels]; + evolutionTimelevels, opts]; boundarySources = CactusBoundary`GetSources[evolvedGroups, groups, implementation, thornName]; diff --git a/Tools/CodeGen/Schedule.m b/Tools/CodeGen/Schedule.m index 4a57d7b..f83578f 100644 --- a/Tools/CodeGen/Schedule.m +++ b/Tools/CodeGen/Schedule.m @@ -54,15 +54,24 @@ groupsSetInCalc[calc_, groups_] := eqs = lookup[calc, Equations]; lhss = Map[First, eqs]; gfsInLHS = Union[Cases[lhss, _ ? (MemberQ[gfs,#] &), Infinity]]; - lhsGroupNames = containingGroups[gfsInLHS, groups]; Return[lhsGroupNames] ]; +groupsReadInCalc[calc_, groups_] := + Module[{gfs, eqs, lhss, gfsInLHS, lhsGroupNames}, + gfs = allGroupVariables[groups]; + eqs = lookup[calc, Equations]; + rhss = Map[Last, eqs]; + gfsInRHS = Union[Cases[rhss, _ ? (MemberQ[gfs,#] &), Infinity]]; + rhsGroupNames = containingGroups[gfsInRHS, groups]; + Return[rhsGroupNames] + ]; + (* Each calculation can be scheduled at multiple points, so this function returns a LIST of schedule structures for each calculation *) -scheduleCalc[calc_, groups_, thornName_] := +scheduleCalc[calc_, groups_, thornName_, useOpenCL_] := Module[{points, conditional, conditionals, keywordConditional, keywordConditionals, triggered, keyword, value, keywordvaluepairs, groupsToSync, tags, @@ -97,19 +106,13 @@ scheduleCalc[calc_, groups_, thornName_] := (* TODO: Pass this as {keyword,value} pair instead of a string, once Thorn.m understands this format *) - (* TODO: This doesn't work -- I don't know how to access - OptionValue[] in this file. - tags = If[OptionValue[UseOpenCL], "OpenCL=1", ""]; - *) - tags = "OpenCL=1"; + tags = If[useOpenCL, "OpenCL=1", ""]; prefixWithScope[group_] := If[StringMatchQ[ToString[group], __~~"::"~~__], ToString[group], thornName <> "::" <> ToString[group]]; - (* TODO: Don't blindly require/provide all groups, check the - equations instead *) - groupsToRequire = prefixWithScope /@ Map[First, groups]; + groupsToRequire = prefixWithScope /@ groupsReadInCalc[calc, groups]; groupsToProvide = prefixWithScope /@ groupsSetInCalc[calc, groups]; before = lookupDefault[calc, Before, None]; @@ -210,12 +213,14 @@ scheduleCalc[calc_, groups_, thornName_] := bcGroupSched["in "<>groupName <> " after " <> lookup[calc, Name]], bcGroupSched["in MoL_PseudoEvolutionBoundaries after MoL_PostStep"]},{}]]]]; +Options[CreateKrancScheduleFile] = ThornOptions; + CreateKrancScheduleFile[calcs_, groups_, evolvedGroups_, rhsGroups_, nonevolvedGroups_, thornName_, - evolutionTimelevels_] := + evolutionTimelevels_, opts:OptionsPattern[]] := Module[{scheduledCalcs, scheduledStartup, scheduleMoLRegister, globalStorageGroups, scheduledFunctions, schedule}, scheduledCalcs = - Flatten[Map[scheduleCalc[#, groups, thornName] &, calcs], 1]; + Flatten[Map[scheduleCalc[#, groups, thornName, OptionValue[UseOpenCL]] &, calcs], 1]; scheduledStartup = { Name -> thornName <> "_Startup", diff --git a/Tools/CodeGen/TensorTools.m b/Tools/CodeGen/TensorTools.m index 7b623f5..48a848a 100644 --- a/Tools/CodeGen/TensorTools.m +++ b/Tools/CodeGen/TensorTools.m @@ -140,6 +140,7 @@ TensorManualCartesianParities; Checkpoint; IsTensor; toggleIndex; +replaceConflicting; (* This is for compatibility with MathTensor notation *) (*OD = PD;*) @@ -352,7 +353,7 @@ replaceDummyIndex[x_, li_, ri_] := TensorProduct represents a Times that has already been checked for conflicting dummy indices. It can have any expressions in it. *) -SetAttributes[TensorProduct, {Flat, OneIdentity}]; +SetAttributes[TensorProduct, {Flat, OneIdentity, Orderless}]; (* For some reason this causes infinite loops - might want to check this later *) (*TensorProduct[t:(Tensor[_,__])] := t;*) @@ -757,7 +758,7 @@ DefineConnection[cd_, pd_, gamma_] := (* Things we can do with covariant derivatives: - (1) Liebnitz: CD[x_ y_,i_] -> CD[x,i] y + x CD[y,i] + (1) Leibnitz: CD[x_ y_,i_] -> CD[x,i] y + x CD[y,i] (2) Linear: CD[x_ + y_,i_] -> CD[x,i] + CD[y,i] (3) Linear: CD[i_Integer x_] -> i CD[x] (4) High order derivatives: CD[x_, i_, is__] -> CD[CD[x,i],is] diff --git a/Tools/CodeGen/Thorn.m b/Tools/CodeGen/Thorn.m index 8731c26..a82a23d 100644 --- a/Tools/CodeGen/Thorn.m +++ b/Tools/CodeGen/Thorn.m @@ -384,7 +384,6 @@ scheduleUnconditionalFunction[spec_] := (* Insert a SYNC line for each group we want to synchronize. *) Map[{"SYNC: ", #, "\n"} &, lookupDefault[spec, SynchronizedGroups, {}]], - Map[{"TRIGGERS: ", #, "\n"} &, lookupDefault[spec, TriggerGroups, {}]], (* TODO: Expect a set of keyword/value pairs instead of a string *) @@ -399,7 +398,7 @@ scheduleUnconditionalFunction[spec_] := storage for *) Map[groupStorage, lookupDefault[spec, StorageGroups, {}]]}, - Quote[lookup[spec, Comment]]]}; + Quote[lookup[spec, Comment]]]}; (* Handle the aspect of scheduling the function conditionally *) scheduleFunction[spec_] := -- cgit v1.2.3 From f485ed866a3c9497828f07c874d7a29a22632232 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Mon, 19 Dec 2011 11:47:22 -0500 Subject: Generate pown(), which exists in OpenCL Also provide pown() if not generating OpenCL code. --- Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h | 1 + Tools/CodeGen/CodeGenCactus.m | 1 + 2 files changed, 2 insertions(+) diff --git a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h index 0be42d3..d768625 100644 --- a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h +++ b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h @@ -1,4 +1,5 @@ #ifdef KRANC_C +# define pown(x,y) pow(x,y) # define IfThen(x,y,z) ((x) ? (y) : (z)) #else # define Abs(x) (abs(x)) diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index 702970b..c5b6ee8 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -733,6 +733,7 @@ DefFn[ from here. *) rhs = rhs //. Power[E, power_] -> exp[power]; rhs = rhs //. Log[x_] -> log[x]; + rhs = rhs //. Power[x_, n_Integer] -> pown[x,y]; rhs = rhs //. Power[x_, y_] -> pow[x,y]; rhs = rhs //. Sin[x_] -> sin[x]; rhs = rhs //. Cos[x_] -> cos[x]; -- cgit v1.2.3 From b8e195795f51b4b67834e8a1616fbc95ef499166 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Mon, 19 Dec 2011 11:48:03 -0500 Subject: Optimize sqrt(x)*sqrt(y) and similar expressions --- Tools/CodeGen/CodeGenCactus.m | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index c5b6ee8..60b9f9d 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -685,6 +685,11 @@ DefFn[ rhs = rhs //. Power[xx_, -1/2] -> INV[sqrt[xx]]; rhs = rhs //. Power[xx_, 0.5] -> sqrt[xx]; rhs = rhs //. Power[xx_, -0.5] -> INV[sqrt[xx]]; + rhs = rhs //. SQR[x_] SQR[y_] -> SQR[x y]; + rhs = rhs //. CUB[x_] CUB[y_] -> CUB[x y]; + rhs = rhs //. QAD[x_] QAD[y_] -> QAD[x y]; + rhs = rhs //. INV[x_] INV[y_] -> INV[x y]; + rhs = rhs //. sqrt[x_] sqrt[y_] -> sqrt[x y]; (* rhs = rhs /. 1/2 -> khalf -- cgit v1.2.3 From 588c5e681fb4594b5b6a69b7c3d368af1f32e1b4 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Mon, 19 Dec 2011 11:53:44 -0500 Subject: Add option to generate DGFE code --- Tools/CodeGen/CalculationFunction.m | 210 +++++++++++++++++++++++++++++++++++- Tools/CodeGen/Kranc.m | 1 + Tools/CodeGen/Thorn.m | 1 + 3 files changed, 208 insertions(+), 4 deletions(-) diff --git a/Tools/CodeGen/CalculationFunction.m b/Tools/CodeGen/CalculationFunction.m index 31399d3..8e477ea 100644 --- a/Tools/CodeGen/CalculationFunction.m +++ b/Tools/CodeGen/CalculationFunction.m @@ -380,7 +380,7 @@ DefFn[ shorts, eqs, parameters, parameterRules, odeGroups, functionName, dsUsed, groups, pddefs, cleancalc, eqLoop, where, addToStencilWidth, pDefs, haveCondTextuals, condTextuals, calc, - kernelCall}, + kernelCall, DGFEDefs, DGFECall}, functionName = ToString@lookup[calcp, Name]; bodyFunctionName = functionName <> "_Body"; @@ -414,9 +414,9 @@ DefFn[ gfs = allGroupVariables[groups]; - InfoMessage[InfoFull, " ", Length@shorts, " shorthands"]; - InfoMessage[InfoFull, " ", Length@gfs, " grid functions"]; - InfoMessage[InfoFull, " ", Length@groups, " groups"]; + InfoMessage[InfoFull, " " <> ToString[Length@shorts] <> " shorthands"]; + InfoMessage[InfoFull, " " <> ToString[Length@gfs] <> " grid functions"]; + InfoMessage[InfoFull, " " <> ToString[Length@groups] <> " groups"]; InfoMessage[InfoFull, "Shorthands: ", shorts]; InfoMessage[InfoFull, "Grid functions: ", gfs]; @@ -484,8 +484,208 @@ DefFn[ ThrowError["Unknown 'Where' entry in calculation " <> functionName <> ": " <> ToString[where]]]; + DGFEDefs = + If[OptionValue[UseDGFE] && lookupDefault[cleancalc, UseDGFE, False], + Module[ + {name, lhss, gfsInLHS, vars}, + InfoMessage[InfoFull, "Generating DGFE boilerplate"]; + name = lookup[cleancalc, Name]; + lhss = Map[#[[1]] &, eqs]; + gfsInLHS = Union[Cases[lhss, _ ? (MemberQ[gfs,#] &), Infinity]]; + InfoMessage[InfoFull, "gfsInLHS:" <> Map[" "<>ToString[#] &, gfsInLHS]]; + (* TODO: do this in a better way, don't examine the variable names *) + vars = Select[gfsInLHS, + StringMatchQ[ToString[#], + RegularExpression[".*rhs.*"]] &]; + vars = Map[Symbol[StringReplace[ToString[#], "rhs"->""]] &, vars]; + InfoMessage[InfoFull, "DGFE variables:" <> Map[" "<>ToString[#] &, vars]]; + { + "", + "", + "", + "/* DGFE Definitions */", + "", + "#include ", + "", + "/*** WENO reconstruction ***/", (* TODO: do we need this? *) + "", + "/* Configuration */", + "#define config_eno_width 3", + "#define config_weno_type hrscc::policy::standard", + "#define config_weno_optim hrscc::policy::order", + "#define config_weno_limiter hrscc::policy::dummy", + "#define config_tvd_limiter hrscc::limiters::minmod", + "", + "/* Export definitions */", + "#define "<>name<>"_eno_stencil hrscc::ENOStencil", + "#define "<>name<>"_weno_weights hrscc::WENOWeights", + "#define "<>name<>"_weno_stencil hrscc::WENOStencil", + "#define "<>name<>"_weno_limiter hrscc::WENOLimitername<>"_weno_stencil::width, config_weno_limiter>", + "#define "<>name<>"_weno_reconstruction hrscc::WENOReconstruction<"<>name<>"_eno_stencil, "<>name<>"_weno_limiter, "<>name<>"_weno_stencil, "<>name<>"_weno_weights>", + "#define "<>name<>"_tvd_reconstruction hrscc::TVDReconstruction", + "#define "<>name<>"_mp5_reconstruction hrscc::MP5Reconstruction", + "#define "<>name<>"_upwind_reconstruction hrscc::UpwindReconstruction", + "#define "<>name<>"_reconstruction "<>name<>"_weno_reconstruction", + "", + "/*** Flux splitting ***/", (* TODO: do we need this? *) + "", + "/* Configuration */", + "#define config_entropy_fix hrscc::NoEntropyFix", + "#define config_flux_split hrscc::RoeFS<"<>name<>"_reconstruction, config_entropy_fix>", + "#define config_system_split hrscc::ComponentSplitname<>", config_flux_split>", + "", + "/* Export definitions */", + "#define "<>name<>"_flux_split config_flux_split", + "#define "<>name<>"_system_split config_system_split", + "#define "<>name<>"_fd_method hrscc::FiniteDifferencename<>", "<>name<>"_system_split>", + "", + "/*** SDG method ***/", (* TODO: do we need this? *) + "", + "/* Configuration */", + "#define config_sdg_order 5", (* TODO: make this a parameter *) + "#define config_riemann_solver hrscc::LaxFriedrichsRSname<>", false>", + "", + "/* Export definitions */", + "#define "<>name<>"_sdg_grid hrscc::GNIGrid >", + "#define "<>name<>"_sdg_method hrscc::SDGMethodname<>", "<>name<>"_sdg_grid, config_riemann_solver>", + "", + "/*** Numerical scheme ***/", + "", + "/* Configuration */", + "#define config_method "<>name<>"_sdg_method", + "", + "/* Export definitions */", + "#define "<>name<>"_method config_method", + "#define "<>name<>"_solver hrscc::CLawSolvername<>", config_method>", + "", + "", + "", + "class DGFE_"<>name<>";", + "", + "namespace hrscc {", + " template<>", + " struct traitsname<>"> {", + " // All state vector variables", + " enum state_t {" <> Map["i"<>ToString[#]<>", " &, vars] <> "nvars};", + " enum {nequations = nvars};", + " enum {nexternal = 0};", + " enum {nbitmasks = 0};", + " static bool const pure = false;", + " };", + "} // namespace", + "", + "", + "", + "class DGFE_"<>name<>": public hrscc::CLawname<>"> {", + "public:", + " typedef hrscc::CLawname<>"> claw;", + " typedef hrscc::traitsname<>">::state_t state_t;", + " static int const nvars = state_t::nvars;", + " ", + " DGFE_"<>name<>"();", + " ", + " inline void prim_to_all(hrscc::Observer & observer) const", + " {", + " }", + " ", + " template", + " inline void fluxes(hrscc::Observer & observer) const", + " {", + " cGH const *const cctkGH = observer.cctkGH;", + " DECLARE_CCTK_ARGUMENTS;", + " ", + " int const i = observer.index[0];", + " int const j = observer.index[1];", + " int const k = observer.index[2];", + " int const ind3d = CCTK_GFINDEX3D(cctkGH, i,j,k);", + " ", + Map[" CCTK_REAL flux"<>ToString[#]<>"L;" &, vars], + " ", + " switch (dir) {", + Table[{ + " case hrscc::policy::" <> {"x", "y", "z"}[[dir]] <> ": {", + Map[" flux"<>ToString[#]<>"L = flux"<>ToString[#]<>ToString[dir]<>"[ind3d];" &, vars], + " break;", + " }"}, + {dir, 1, 3}], + " default:", + " assert(0);", + " }", + " ", + Map[" observer.flux[dir][state_t::i"<>ToString[#]<>"] = - flux"<>ToString[#]<>"L;" &, vars], + " }", + " ", + " template", + " inline void eigenvalues(hrscc::Observer & observer) const", + " {", + " assert(0);", + " }", + " ", + " template", + " inline void eig(hrscc::Observer & observer) const", + " {", + " assert(0);", + " }", + "};", + "", + "", + "", + "namespace hrscc {", + " template<> int CLawname<>">::conserved_idx[DGFE_"<>name<>"::nvars] = {};", + " template<> int CLawname<>">::primitive_idx[DGFE_"<>name<>"::nvars] = {};", + " template<> int CLawname<>">::rhs_idx[DGFE_"<>name<>"::nvars] = {};", + " template<> int CLawname<>">::field_idx[0] = {};", + " template<> int CLawname<>">::bitmask_idx[0] = {};", + + "} // namespace hrscc", + "", + "", + "", + "namespace {", + " int varindex(char const* const varname)", + " {", + " int const vi = CCTK_VarIndex(varname);", + " if (vi<0) CCTK_WARN(CCTK_WARN_ABORT, \"Internal error\");", + " return vi;", + " }", + "}", + "", + "DGFE_"<>name<>"::DGFE_"<>name<>"()", + "{", + " using namespace hrscc;", + " ", + Map[" CLawname<>">::conserved_idx[state_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"\");" &, vars], + Map[" CLawname<>">::primitive_idx[state_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"\");" &, vars], + " ", + Map[" CLawname<>">::rhs_idx[state_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"rhs\");" &, vars], + "}", + "", + "", + "" + } // Flatten // Map[# <> "\n" &, #] &], + {} + ]; + + DGFECall = + If[OptionValue[UseDGFE] && lookupDefault[cleancalc, UseDGFE, False], + Module[ + {name}, + name = lookup[cleancalc, Name]; + { + "", + "{", + " static "<>name<>"_solver *solver = NULL;", + " if (not solver) solver = new "<>name<>"_method(cctkGH);", + " solver->compute_rhs();", + " // delete solver;", + "}" + } // Flatten // Map[# <> "\n" &, #] &], + {} + ]; + InfoMessage[InfoFull,"Generating function"]; { + DGFEDefs, DefineFunction[bodyFunctionName, "static void", "cGH const * restrict const cctkGH, int const dir, int const face, CCTK_REAL const normal[3], CCTK_REAL const tangentA[3], CCTK_REAL const tangentB[3], int const imin[3], int const imax[3], int const n_subblock_gfs, CCTK_REAL * restrict const subblock_gfs[]", { "DECLARE_CCTK_ARGUMENTS;\n", @@ -582,6 +782,8 @@ DefFn[ kernelCall, + DGFECall, + ConditionalOnParameterTextual["verbose > 1", "CCTK_VInfo(CCTK_THORNSTRING,\"Leaving " <> bodyFunctionName <> "\");\n"] }]] diff --git a/Tools/CodeGen/Kranc.m b/Tools/CodeGen/Kranc.m index d3a02be..dbc3c0b 100644 --- a/Tools/CodeGen/Kranc.m +++ b/Tools/CodeGen/Kranc.m @@ -73,6 +73,7 @@ ThornOptions = PartialDerivatives -> {}, ReflectionSymmetries -> {}, ZeroDimensions -> {}, + UseDGFE -> False, UseLoopControl -> False, UseOpenCL -> False, UseVectors -> False, diff --git a/Tools/CodeGen/Thorn.m b/Tools/CodeGen/Thorn.m index a82a23d..47d7d7c 100644 --- a/Tools/CodeGen/Thorn.m +++ b/Tools/CodeGen/Thorn.m @@ -220,6 +220,7 @@ Options[CreateConfiguration] = ThornOptions; CreateConfiguration[opts:OptionsPattern[]] := {whoWhen["CCL"], "REQUIRES GenericFD\n", + If[OptionValue[UseDGFE], "REQUIRES Boost CPPUtils FDCore HRSCCore\n", {}], If[OptionValue[UseLoopControl], "REQUIRES LoopControl\n", {}], If[OptionValue[UseOpenCL], "REQUIRES OpenCL OpenCLRunTime\n", {}], If[OptionValue[UseVectors], "REQUIRES Vectors\n", {}] -- cgit v1.2.3 From 698369edef18f9b5143c3e8b1b66a874eb5cfcb0 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sat, 24 Dec 2011 15:53:23 -0500 Subject: Generate more compatible C++ code for DGFE class --- Tools/CodeGen/CalculationFunction.m | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Tools/CodeGen/CalculationFunction.m b/Tools/CodeGen/CalculationFunction.m index 8e477ea..5356672 100644 --- a/Tools/CodeGen/CalculationFunction.m +++ b/Tools/CodeGen/CalculationFunction.m @@ -580,7 +580,8 @@ DefFn[ "public:", " typedef hrscc::CLawname<>"> claw;", " typedef hrscc::traitsname<>">::state_t state_t;", - " static int const nvars = state_t::nvars;", + " typedef hrscc::traitsname<>"> variables_t;", + " static int const nvars = variables_t::nvars;", " ", " DGFE_"<>name<>"();", " ", @@ -612,7 +613,7 @@ DefFn[ " assert(0);", " }", " ", - Map[" observer.flux[dir][state_t::i"<>ToString[#]<>"] = - flux"<>ToString[#]<>"L;" &, vars], + Map[" observer.flux[dir][variables_t::i"<>ToString[#]<>"] = - flux"<>ToString[#]<>"L;" &, vars], " }", " ", " template", @@ -654,10 +655,10 @@ DefFn[ "{", " using namespace hrscc;", " ", - Map[" CLawname<>">::conserved_idx[state_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"\");" &, vars], - Map[" CLawname<>">::primitive_idx[state_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"\");" &, vars], + Map[" CLawname<>">::conserved_idx[variables_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"\");" &, vars], + Map[" CLawname<>">::primitive_idx[variables_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"\");" &, vars], " ", - Map[" CLawname<>">::rhs_idx[state_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"rhs\");" &, vars], + Map[" CLawname<>">::rhs_idx[variables_t::i"<>ToString[#]<>"] = varindex(CCTK_THORNSTRING \"::"<>ToString[#]<>"rhs\");" &, vars], "}", "", "", -- cgit v1.2.3 From dacc0c39003dad32c7edb7d61fe2ab398fc0c934 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Thu, 29 Dec 2011 16:00:38 -0500 Subject: Remove support for pown function pown is only supported in OpenCL, and does not seem to give a performance improvement. --- Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h | 1 - Tools/CodeGen/CodeGenCactus.m | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h index d768625..0be42d3 100644 --- a/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h +++ b/Auxiliary/Cactus/KrancNumericalTools/GenericFD/src/MathematicaCompat.h @@ -1,5 +1,4 @@ #ifdef KRANC_C -# define pown(x,y) pow(x,y) # define IfThen(x,y,z) ((x) ? (y) : (z)) #else # define Abs(x) (abs(x)) diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index 60b9f9d..1a34074 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -738,7 +738,7 @@ DefFn[ from here. *) rhs = rhs //. Power[E, power_] -> exp[power]; rhs = rhs //. Log[x_] -> log[x]; - rhs = rhs //. Power[x_, n_Integer] -> pown[x,y]; + (* rhs = rhs //. Power[x_, n_Integer] -> pown[x,n]; *) rhs = rhs //. Power[x_, y_] -> pow[x,y]; rhs = rhs //. Sin[x_] -> sin[x]; rhs = rhs //. Cos[x_] -> cos[x]; -- cgit v1.2.3 From f5f9dbaf3ceb65dcc5c9fdc90ec529fd3901af2f Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Thu, 29 Dec 2011 16:01:27 -0500 Subject: Improve formatting of generated code Don't add spaces before parentheses or after pointer-asterisks. --- Tools/CodeGen/CalculationFunction.m | 16 ++++++++-------- Tools/CodeGen/CodeGenCactus.m | 4 ++-- Tools/CodeGen/KrancThorn.m | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Tools/CodeGen/CalculationFunction.m b/Tools/CodeGen/CalculationFunction.m index 5356672..f31c815 100644 --- a/Tools/CodeGen/CalculationFunction.m +++ b/Tools/CodeGen/CalculationFunction.m @@ -687,7 +687,7 @@ DefFn[ InfoMessage[InfoFull,"Generating function"]; { DGFEDefs, - DefineFunction[bodyFunctionName, "static void", "cGH const * restrict const cctkGH, int const dir, int const face, CCTK_REAL const normal[3], CCTK_REAL const tangentA[3], CCTK_REAL const tangentB[3], int const imin[3], int const imax[3], int const n_subblock_gfs, CCTK_REAL * restrict const subblock_gfs[]", + DefineFunction[bodyFunctionName, "static void", "cGH const *restrict const cctkGH, int const dir, int const face, CCTK_REAL const normal[3], CCTK_REAL const tangentA[3], CCTK_REAL const tangentB[3], int const imin[3], int const imax[3], int const n_subblock_gfs, CCTK_REAL * restrict const subblock_gfs[]", { "DECLARE_CCTK_ARGUMENTS;\n", "DECLARE_CCTK_PARAMETERS;\n\n", @@ -696,7 +696,7 @@ DefFn[ (* We could (or probably should) write this into a source file of its own *) If[OptionValue[UseOpenCL], { - "char const * const source =\n" + "char const *const source =\n" }, { }], @@ -740,16 +740,16 @@ DefFn[ groupNames = groupsInCalculation[cleancalc, imp]; groupNames = Select[groupNames, !MemberQ[ignoreGroups, #] &]; { - "char const * const groups[] = {", + "char const *const groups[] = {", Riffle[Join[Map[Quote, groupNames], {"NULL"}], ","], "};\n\n" } ], - "static struct OpenCLKernel * kernel = NULL;\n", - "char const * const sources[] = {differencing, source, NULL};\n", - "OpenCLRunTime_CallKernel (cctkGH, CCTK_THORNSTRING, \"" <> functionName <> "\",\n", - " sources, groups, NULL, NULL, NULL, -1,\n", - " imin, imax, &kernel);\n\n" + "static struct OpenCLKernel *kernel = NULL;\n", + "char const *const sources[] = {differencing, source, NULL};\n", + "OpenCLRunTime_CallKernel(cctkGH, CCTK_THORNSTRING, \"" <> functionName <> "\",\n", + " sources, groups, NULL, NULL, NULL, -1,\n", + " imin, imax, &kernel);\n\n" }, { }] diff --git a/Tools/CodeGen/CodeGenCactus.m b/Tools/CodeGen/CodeGenCactus.m index 1a34074..b43f6d7 100644 --- a/Tools/CodeGen/CodeGenCactus.m +++ b/Tools/CodeGen/CodeGenCactus.m @@ -413,7 +413,7 @@ DefFn[ "Loop over the grid points", {"#pragma omp parallel\n", If[vectorise, "LC_LOOP3VEC", "LC_LOOP3"], - " (", functionName, ",\n", + "(", functionName, ",\n", " i,j,k, imin[0],imin[1],imin[2], imax[0],imax[1],imax[2],\n", " cctk_lsh[0],cctk_lsh[1],cctk_lsh[2]", If[vectorise, {",\n", " CCTK_REAL_VEC_SIZE"}, ""], @@ -424,7 +424,7 @@ DefFn[ (* DeclareAssignVariable["int", "index", "CCTK_GFINDEX3D(cctkGH,i,j,k)"], *) DeclareAssignVariable["ptrdiff_t", "index", "di*i + dj*j + dk*k"], block}], "}\n", - If[vectorise, "LC_ENDLOOP3VEC", "LC_ENDLOOP3"] <> " (", functionName, ");\n"}], + If[vectorise, "LC_ENDLOOP3VEC", "LC_ENDLOOP3"] <> "(", functionName, ");\n"}], (* else *) ""]]; diff --git a/Tools/CodeGen/KrancThorn.m b/Tools/CodeGen/KrancThorn.m index e086090..d32dc38 100644 --- a/Tools/CodeGen/KrancThorn.m +++ b/Tools/CodeGen/KrancThorn.m @@ -230,7 +230,7 @@ CreateKrancThorn[groupsOrig_, parentDirectory_, thornName_, opts:OptionsPattern[ {}], diffHeader]; diffHeader = If[OptionValue[UseOpenCL], - "static char const * const differencing =\n" <> + "static char const *const differencing =\n" <> Stringify[diffHeader] <> ";\n", diffHeader]; -- cgit v1.2.3 From 11012a75ccf772f8811db3467748789d84dcca30 Mon Sep 17 00:00:00 2001 From: Ian Hinder Date: Tue, 31 Jan 2012 13:29:26 -0600 Subject: Thorn.m: Rename REQUIRES and PROVIDES to READS and WRITES Support for this has now been committed to the Cactus flesh --- Tools/CodeGen/Thorn.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/CodeGen/Thorn.m b/Tools/CodeGen/Thorn.m index 47d7d7c..75bf4d4 100644 --- a/Tools/CodeGen/Thorn.m +++ b/Tools/CodeGen/Thorn.m @@ -392,8 +392,8 @@ scheduleUnconditionalFunction[spec_] := "TAGS: " <> lookup[spec, Tags] <> "\n", ""], - Map[{"REQUIRES: ", #, "\n"} &, lookupDefault[spec, RequiredGroups, {}]], - Map[{"PROVIDES: ", #, "\n"} &, lookupDefault[spec, ProvidedGroups, {}]], + Map[{"READS: ", #, "\n"} &, lookupDefault[spec, RequiredGroups, {}]], + Map[{"WRITES: ", #, "\n"} &, lookupDefault[spec, ProvidedGroups, {}]], (* Insert a storage block for each group we want to allocate storage for *) -- cgit v1.2.3 From 809e125e21c83dc8c126ddf1e41f9bf74d09bcf5 Mon Sep 17 00:00:00 2001 From: Ian Hinder Date: Wed, 1 Feb 2012 19:10:59 -0600 Subject: Schedule.m: Change schedule tag from OpenCL to Device --- Tools/CodeGen/Schedule.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/CodeGen/Schedule.m b/Tools/CodeGen/Schedule.m index f83578f..9871ab2 100644 --- a/Tools/CodeGen/Schedule.m +++ b/Tools/CodeGen/Schedule.m @@ -106,7 +106,7 @@ scheduleCalc[calc_, groups_, thornName_, useOpenCL_] := (* TODO: Pass this as {keyword,value} pair instead of a string, once Thorn.m understands this format *) - tags = If[useOpenCL, "OpenCL=1", ""]; + tags = If[useOpenCL, "Device=1", ""]; prefixWithScope[group_] := If[StringMatchQ[ToString[group], __~~"::"~~__], -- cgit v1.2.3