aboutsummaryrefslogtreecommitdiff
path: root/Tools/CodeGen/OpenCL.m
blob: 7017d4c79f0989b3bdd17f680c9ae2e2799e277a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(*  Copyright 2004 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["OpenCL`", {"Errors`", "Helpers`", "Kranc`", "Calculation`", "CodeGen`",
                         "Vectorisation`"}];

OpenCLPrologue;
OpenCLEpilogue;
OpenCLProcessKernel;
OpenCLIncludeFiles;
OpenCLConfigurationCCL;
OpenCLProcessDifferencingHeader;
OpenCLLocalsToGridFunctions;

Begin["`Private`"];

DefFn[
  OpenCLPrologue[] :=
  "const char* const source =\n"];

DefFn[
  OpenCLEpilogue[cleancalc_List, imp_String, functionName_String] :=
  {
    ";\n\n",
    Module[
      {ignoreGroups, groupsNames, groupNameList},
      ignoreGroups = {"TmunuBase::stress_energy_scalar",
                      "TmunuBase::stress_energy_vector",
                      "TmunuBase::stress_energy_tensor"};
      groupNames = GroupsInCalculation[cleancalc, imp];
      groupNames = Select[groupNames, !MemberQ[ignoreGroups, #] &];
      {
        "const char* const groups[] = {\n  ",
        Riffle[Join[Map[Quote, groupNames], {"NULL"}], ",\n  "],
        "};\n\n"
      }
          ],
    "static struct OpenCLKernel *kernel = NULL;\n",
    "const char* 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"
  }];

DefFn[
  OpenCLProcessKernel[code:CodeGenBlock] :=
  Stringify[code]];

DefFn[
  OpenCLLocalsToGridFunctions[gridNames_List, localNames_List] :=
  VectorisationLocalsToGridFunctions[gridNames, localNames, {"lc_imin", "lc_imax"}]];

DefFn[
  OpenCLIncludeFiles[] :=
  {"OpenCLRunTime.h"}];

DefFn[
  OpenCLConfigurationCCL[] :=
  "REQUIRES OpenCL OpenCLRunTime\n"];

DefFn[
  OpenCLProcessDifferencingHeader[diffHeader_] :=
  "static const char* const differencing =\n" <>
  Stringify[diffHeader] <>
  ";\n"];


End[];

EndPackage[];