SkelCL
SkelCL is a high level multi GPU skeleton library developed at the university of Münster, Germany.
 All Classes Namespaces Files Functions Variables Typedefs Groups
Map.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2011-2012 The SkelCL Team as listed in CREDITS.txt *
3  * http://skelcl.uni-muenster.de *
4  * *
5  * This file is part of SkelCL. *
6  * SkelCL is available under multiple licenses. *
7  * The different licenses are subject to terms and condition as provided *
8  * in the files specifying the license. See "LICENSE.txt" for details *
9  * *
10  *****************************************************************************
11  * *
12  * SkelCL is free software: you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation, either version 3 of the License, or *
15  * (at your option) any later version. See "LICENSE-gpl.txt" for details. *
16  * *
17  * SkelCL is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  *****************************************************************************
23  * *
24  * For non-commercial academic use see the license specified in the file *
25  * "LICENSE-academic.txt". *
26  * *
27  *****************************************************************************
28  * *
29  * If you are interested in other licensing models, including a commercial- *
30  * license, please contact the author at michel.steuwer@uni-muenster.de *
31  * *
32  *****************************************************************************/
33 
39 
40 #include <string>
41 
42 #include <pvsutil/Assert.h>
43 
44 #include "SkelCL/Map.h"
45 #include "SkelCL/Source.h"
46 
47 #include "SkelCL/detail/Program.h"
48 
49 namespace skelcl {
50 
51 // ## Map<Index, void> ################################################
52 
53 Map<void(Index)>::Map(const Source& source, const std::string& funcName)
54  : Skeleton(),
55  detail::MapHelper<void(Index)>(createAndBuildProgram(source, funcName))
56 {
57 }
58 
59 detail::Program
60  Map<void(Index)>::createAndBuildProgram(const std::string& source,
61  const std::string& funcName) const
62 {
63  ASSERT_MESSAGE(!source.empty(),
64  "Tried to create program with empty user source.");
65 
66  // create program
67  // first: device specific functions
68  std::string s(detail::CommonDefinitions::getSource());
69  s.append(R"(
70 typedef size_t Index;
71 
72 )");
73  // second: user defined source
74  s.append(source);
75  // last: append skeleton implementation source
76  s.append(R"(
77 
78 __kernel void SCL_MAP(const unsigned int SCL_ELEMENTS,
79  const unsigned int SCL_OFFSET)
80 {
81  if (get_global_id(0) < SCL_ELEMENTS) {
82  SCL_FUNC(get_global_id(0)+SCL_OFFSET);
83  }
84 }
85  )");
86  auto program = detail::Program(s, detail::util::hash(s));
87 
88  // modify program
89  if (!program.loadBinary()) {
90  // append parameters from user function to kernel
91  program.transferParameters(funcName, 1, "SCL_MAP");
92  program.transferArguments(funcName, 1, "SCL_FUNC");
93  // rename user function
94  program.renameFunction(funcName, "SCL_FUNC");
95  }
96 
97  // build program
98  program.build();
99 
100  return program;
101 }
102 
103 // ## Map<IndexPoint, void> ################################################
104 Map<void(IndexPoint)>::Map(const Source& source, const std::string& funcName)
105  : Skeleton(),
106  detail::MapHelper<void(IndexPoint)>(createAndBuildProgram(source, funcName))
107 {
108 }
109 
110 detail::Program Map<void(IndexPoint)>::createAndBuildProgram(
111  const std::string& source, const std::string& funcName) const
112 {
113  ASSERT_MESSAGE(!source.empty(),
114  "Tried to create program with empty user source.");
115 
116  // create program
117  // first: device specific functions
118  std::string s(detail::CommonDefinitions::getSource());
119  s.append(R"(
120 typedef struct {
121  size_t x;
122  size_t y;
123 } IndexPoint;
124 
125 )");
126  // second: user defined source
127  s.append(source);
128  // last: append skeleton implementation source
129  s.append(R"(
130 
131 __kernel void SCL_MAP(const unsigned int SCL_COL_COUNT,
132  const unsigned int SCL_ROW_COUNT,
133  const unsigned int SCL_ROW_OFFSET)
134 {
135  if ( get_global_id(1) < SCL_COL_COUNT && get_global_id(0) < SCL_ROW_COUNT ) {
136  // dim 1 is the columns, dim 0 the rows
137  IndexPoint p;
138  p.x = get_global_id(1);
139  p.y = get_global_id(0) + SCL_ROW_OFFSET;
140  SCL_FUNC(p);
141  }
142 }
143 )");
144  auto program = detail::Program(s, detail::util::hash(s));
145 
146  // modify program
147  if (!program.loadBinary()) {
148  // append parameters from user function to kernel
149  program.transferParameters(funcName, 1, "SCL_MAP");
150  program.transferArguments(funcName, 1, "SCL_FUNC");
151  // rename user function
152  program.renameFunction(funcName, "SCL_FUNC");
153  }
154 
155  // build program
156  program.build();
157 
158  return program;
159 }
160 
161 } // namespace skelcl
162 
This class is a unified wrapper for defining source code in SkelCL.
Definition: Source.h:61
Map(const Source &source, const std::string &funcName=std::string("func"))
Constructor taking the source code used of the user-defined function as argument. ...
Definition: Map.cpp:104
Map(const Source &source, const std::string &funcName=std::string("func"))
Constructor taking the source code used of the user-defined function as argument. ...
Definition: Map.cpp:53