1 /* ************************************************************************
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * ************************************************************************/
16
17
18 #include "var.h"
19
20 using namespace clMath;
21
22 struct MemFlags {
23 cl_mem_flags flag;
24 const char* name;
25 };
26
27 static const struct MemFlags MEM_FLAGS[] = {
28 { CL_MEM_READ_WRITE, "CL_MEM_READ_WRITE" },
29 { CL_MEM_WRITE_ONLY, "CL_MEM_WRITE_ONLY" },
30 { CL_MEM_READ_ONLY, "CL_MEM_READ_ONLY" },
31 { CL_MEM_USE_HOST_PTR, "CL_MEM_USE_HOST_PTR" },
32 { CL_MEM_ALLOC_HOST_PTR,"CL_MEM_ALLOC_HOST_PTR" },
33 { CL_MEM_COPY_HOST_PTR, "CL_MEM_COPY_HOST_PTR" },
34 { 0, NULL }
35 };
36
Variable(const std::string & name,const std::string & type,const std::string & defaultValue)37 Variable::Variable(
38 const std::string& name,
39 const std::string& type,
40 const std::string& defaultValue)
41 {
42 name_ = name;
43 type_ = type;
44 defaultValue_ = defaultValue;
45 isBuffer_ = false;
46 constant_ = false;
47
48 copyOf_ = NULL;
49
50 flags_ = 0;
51 hostPtr_ = NULL;
52 }
53
Variable()54 Variable::Variable()
55 {
56 Variable("", "");
57 }
58
MatrixVariable(const std::string & name,const std::string & type,const std::string & defaultValue)59 MatrixVariable::MatrixVariable(
60 const std::string& name,
61 const std::string& type,
62 const std::string& defaultValue)
63 {
64 name_ = name;
65 type_ = type;
66 defaultValue_ = defaultValue;
67 isBuffer_ = false;
68 constant_ = false;
69
70 copyOf_ = NULL;
71
72 flags_ = 0;
73 hostPtr_ = NULL;
74
75 rows_ = NULL;
76 columns_ = NULL;
77 ld_ = NULL;
78 off_ = NULL;
79 }
80
VectorVariable(const std::string & name,const std::string & type,const std::string & defaultValue)81 VectorVariable::VectorVariable(
82 const std::string& name,
83 const std::string& type,
84 const std::string& defaultValue)
85 {
86 name_ = name;
87 type_ = type;
88 defaultValue_ = defaultValue;
89 isBuffer_ = false;
90 constant_ = false;
91
92 copyOf_ = NULL;
93
94 flags_ = 0;
95 hostPtr_ = NULL;
96
97 nElems_ = NULL;
98 inc_ = NULL;
99 off_ = NULL;
100 }
101
~Variable()102 Variable::~Variable()
103 {
104 }
105
106 void
setDefaultValue(const std::string & defaultValue)107 Variable::setDefaultValue(const std::string& defaultValue)
108 {
109 defaultValue_ = defaultValue;
110 }
111
112 void
setConstant(bool constant)113 Variable::setConstant(bool constant)
114 {
115 constant_ = constant;
116 }
117
118 void
setCopy(Variable * copy)119 Variable::setCopy(Variable *copy)
120 {
121 copyOf_ = copy;
122 }
123
124 void
setMatrixSize(Variable * rows,Variable * columns,Variable * ld,Variable * off)125 MatrixVariable::setMatrixSize(
126 Variable *rows,
127 Variable *columns,
128 Variable *ld,
129 Variable *off)
130 {
131 if ((rows == NULL) || (columns == NULL)) {
132 return;
133 }
134 rows_ = rows;
135 columns_ = columns;
136 ld_ = ld;
137 off_ = off;
138 matrixPointer_ = name_;
139 if (off != NULL) {
140 matrixPointer_ += " + " + off_->name();
141 }
142 }
143
144 void
setVectorSize(Variable * nElems,Variable * inc,Variable * off)145 VectorVariable::setVectorSize(
146 Variable *nElems,
147 Variable *inc,
148 Variable *off)
149 {
150 if (nElems == NULL) {
151 return;
152 }
153 nElems_ = nElems;
154 inc_ = inc;
155 off_ = off;
156 vectorPointer_ = name_;
157 if (off != NULL) {
158 vectorPointer_ += " + " + off_->name();
159 }
160 }
161
162 std::string
flagsStr() const163 Variable::flagsStr() const
164 {
165 std::string str;
166 size_t i;
167
168 if (type_ != "cl_mem") {
169 return "";
170 }
171 if (flags_ == 0) {
172 return "0";
173 }
174 for (i = 0; MEM_FLAGS[i].flag != 0; i++) {
175 if (flags_ & MEM_FLAGS[i].flag) {
176 if (!str.empty()) {
177 str += " | ";
178 }
179 str += MEM_FLAGS[i].name;
180 }
181 }
182 return str;
183 }
184
185 void
setFlags(cl_mem_flags flags)186 Variable::setFlags(cl_mem_flags flags)
187 {
188 if (type_ == "cl_mem") {
189 flags_ = flags;
190 }
191 }
192
193 void
setHostPtr(Variable * hostPtr)194 Variable::setHostPtr(Variable *hostPtr)
195 {
196 if (type_ == "cl_mem") {
197 hostPtr_ = hostPtr;
198 }
199 }
200