Skip to content

Commit

Permalink
Thorn.m: Move interface.ccl file generation to new CodeGenInterface.ccl
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhinder committed Sep 9, 2013
1 parent 0af21a8 commit fac9da5
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 101 deletions.
132 changes: 132 additions & 0 deletions Tools/CodeGen/CodeGenInterface.m
@@ -0,0 +1,132 @@

(* Copyright 2004-2013 Sascha Husa, Ian Hinder, Christiane Lechner
This file is part of Kranc.
Kranc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kranc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kranc; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)

BeginPackage[
"CodeGenInterface`",
{"Errors`", "Helpers`", "Kranc`", "MapLookup`", "CodeGenC`",
"CodeGen`", "CodeGenKranc`"}];

CreateInterface::usage = "Create the content of the interface.ccl file.";

Begin["`Private`"];

(* ------------------------------------------------------------------------
Interface file
------------------------------------------------------------------------ *)

(* The following "group" structure defines a Cactus group of variables
to be included in an interface.ccl file.
group:
{Name -> "", VariableType -> "", Timelevels -> 2, GridType -> "GF",
Comment -> "", Visibility -> "public", Tags -> {tag1, tag2, ...},
Variables -> {phi, h11, ...}}
A 'tag' is of the form {"tensortypealias" -> "Scalar"}
*)

(* Given the specification of a group structure, return a CodeGen
block for the interface.ccl file to define that group *)
interfaceGroupBlock[spec_] :=
{lookup[spec, Visibility], ":\n",
lookup[spec, VariableType], " ", lookup[spec, Name],
" type=", lookup[spec,GridType],
" timelevels=", lookup[spec, Timelevels],
If[mapContains[spec,Tags], {" tags='", interfaceTags[lookupDefault[spec,Tags, {}]], "'"}, ""],
If[mapContains[spec,Dim], {" dim=", lookup[spec,Dim] }, ""],
If[mapContains[spec,Size], {" size=", lookup[spec,Size] }, ""],
"\n",
SuffixedCBlock[{CommaNewlineSeparated[lookup[spec, Variables]],"\n"},
"\"" <> lookup[spec, Comment] <> "\""]};

interfaceTag[tagName_String -> tagValue_String] :=
tagName <> "=" <> "\"" <> tagValue <> "\"";

interfaceTag[tagName_String -> tagValue_?IntegerQ] :=
tagName <> "=" <> ToString[tagValue];

interfaceTag[tagName_String -> tagValue_?NumberQ] :=
tagName <> "=" <> ToString[N[tagValue, 20]];

interfaceTags[tags_] :=
SpaceSeparated[Map[interfaceTag, tags]];





(* Function aliasing *)

(* A definition of an aliased function is:
{Name -> "MoLRegisterEvolvedGroup",
Type -> "CCTK_INT",
ArgString -> "CCTK_INT IN EvolvedIndex, CCTK_INT IN RHSIndex"}
*)

usesFunction[f_] :=
If[lookup[f, Type] == "SUBROUTINE",
{lookup[f, Type], " ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"USES FUNCTION ", lookup[f, Name], "\n\n"},
{lookup[f, Type], " FUNCTION ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"USES FUNCTION ", lookup[f, Name], "\n\n"}
];


providesFunction[f_] :=
If[lookup[f, Type] == "SUBROUTINE",
{lookup[f, Type], " ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"PROVIDES FUNCTION ", lookup[f, Name], "\n\n"},
{lookup[f, Type], " FUNCTION ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"PROVIDES FUNCTION ", lookup[f, Name], "\n\n"}
];


(* Given the name of an implementation, a list of implementation names
that we inherit from, a list of include files to mention, and a
list of group structures as defined above, return a CodeGen block
representing the interface.ccl file. As an optional argument, one
can specify Friends -> {list of implementations we want as
friends}. Can also have UsesFunction -> {functions}*)
CreateInterface[implementation_, inheritedImplementations_, includeFiles_,
groups_, opts___] :=
{FileHeader["CCL"],
"implements: ", implementation, "\n\n",
"inherits: ", SpaceSeparated[inheritedImplementations], "\n\n",
If[mapContains[{opts}, Friends],
{"friend: ", SpaceSeparated[lookup[{opts}, Friends]]},{}],
"\n\n",
Map[{"USES INCLUDE: ", #, "\n"} &, includeFiles],
"\n",

Map[usesFunction, lookupDefault[{opts}, UsesFunctions, {}]],

Map[providesFunction, lookupDefault[{opts}, ProvidesFunctions, {}]],


NewlineSeparated[Map[FlattenBlock[interfaceGroupBlock[#]] &, groups]]};

End[];

EndPackage[];
3 changes: 2 additions & 1 deletion Tools/CodeGen/Interface.m
Expand Up @@ -19,7 +19,8 @@
*)

BeginPackage["Interface`", {"Thorn`", "KrancGroups`", "MapLookup`", "Errors`",
"Helpers`", "Kranc`", "CaKernel`", "OpenCL`"}];
"Helpers`", "Kranc`", "CaKernel`", "OpenCL`",
"CodeGenInterface`"}];

CreateKrancInterface;

Expand Down
100 changes: 0 additions & 100 deletions Tools/CodeGen/Thorn.m
Expand Up @@ -32,7 +32,6 @@
CreateSchedule::usage = "Create the content of the schedule.ccl file.";
CreateMakefile::usage = "Create the content of the Cactus make.code.defn file.";
CreateConfiguration::usage = "Create the content of the configuration.ccl file.";
CreateInterface::usage = "Create the content of the interface.ccl file.";
CreateThorn::usage = "Create a general Cactus thorn from
a thorn specification structure";
CreateSymmetriesRegistrationSource::usage = "";
Expand Down Expand Up @@ -73,105 +72,6 @@
If[OptionValue[UseCaKernel], CaKernelConfigurationCLL[], {}]
};

(* ------------------------------------------------------------------------
Interface file
------------------------------------------------------------------------ *)

(* The following "group" structure defines a Cactus group of variables
to be included in an interface.ccl file.
group:
{Name -> "", VariableType -> "", Timelevels -> 2, GridType -> "GF",
Comment -> "", Visibility -> "public", Tags -> {tag1, tag2, ...},
Variables -> {phi, h11, ...}}
A 'tag' is of the form {"tensortypealias" -> "Scalar"}
*)

(* Given the specification of a group structure, return a CodeGen
block for the interface.ccl file to define that group *)
interfaceGroupBlock[spec_] :=
{lookup[spec, Visibility], ":\n",
lookup[spec, VariableType], " ", lookup[spec, Name],
" type=", lookup[spec,GridType],
" timelevels=", lookup[spec, Timelevels],
If[mapContains[spec,Tags], {" tags='", interfaceTags[lookupDefault[spec,Tags, {}]], "'"}, ""],
If[mapContains[spec,Dim], {" dim=", lookup[spec,Dim] }, ""],
If[mapContains[spec,Size], {" size=", lookup[spec,Size] }, ""],
"\n",
SuffixedCBlock[{CommaNewlineSeparated[lookup[spec, Variables]],"\n"},
"\"" <> lookup[spec, Comment] <> "\""]};

interfaceTag[tagName_String -> tagValue_String] :=
tagName <> "=" <> "\"" <> tagValue <> "\"";

interfaceTag[tagName_String -> tagValue_?IntegerQ] :=
tagName <> "=" <> ToString[tagValue];

interfaceTag[tagName_String -> tagValue_?NumberQ] :=
tagName <> "=" <> ToString[N[tagValue, 20]];

interfaceTags[tags_] :=
SpaceSeparated[Map[interfaceTag, tags]];





(* Function aliasing *)

(* A definition of an aliased function is:
{Name -> "MoLRegisterEvolvedGroup",
Type -> "CCTK_INT",
ArgString -> "CCTK_INT IN EvolvedIndex, CCTK_INT IN RHSIndex"}
*)

usesFunction[f_] :=
If[lookup[f, Type] == "SUBROUTINE",
{lookup[f, Type], " ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"USES FUNCTION ", lookup[f, Name], "\n\n"},
{lookup[f, Type], " FUNCTION ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"USES FUNCTION ", lookup[f, Name], "\n\n"}
];


providesFunction[f_] :=
If[lookup[f, Type] == "SUBROUTINE",
{lookup[f, Type], " ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"PROVIDES FUNCTION ", lookup[f, Name], "\n\n"},
{lookup[f, Type], " FUNCTION ", lookup[f, Name], "(", lookup[f,ArgString], ")\n",
"PROVIDES FUNCTION ", lookup[f, Name], "\n\n"}
];


(* Given the name of an implementation, a list of implementation names
that we inherit from, a list of include files to mention, and a
list of group structures as defined above, return a CodeGen block
representing the interface.ccl file. As an optional argument, one
can specify Friends -> {list of implementations we want as
friends}. Can also have UsesFunction -> {functions}*)
CreateInterface[implementation_, inheritedImplementations_, includeFiles_,
groups_, opts___] :=
{FileHeader["CCL"],
"implements: ", implementation, "\n\n",
"inherits: ", SpaceSeparated[inheritedImplementations], "\n\n",
If[mapContains[{opts}, Friends],
{"friend: ", SpaceSeparated[lookup[{opts}, Friends]]},{}],
"\n\n",
Map[{"USES INCLUDE: ", #, "\n"} &, includeFiles],
"\n",

Map[usesFunction, lookupDefault[{opts}, UsesFunctions, {}]],

Map[providesFunction, lookupDefault[{opts}, ProvidesFunctions, {}]],


NewlineSeparated[Map[FlattenBlock[interfaceGroupBlock[#]] &, groups]]};

(* ------------------------------------------------------------------------
Scheduling
Expand Down

0 comments on commit fac9da5

Please sign in to comment.