Debian Codegraph @

src/acl/Operators/aclElementIfElse.cxx

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
24 #include "aclElementIfElse.h"
25 #include "../aclUtilities.h"
26
27 using namespace asl;
28
29 namespace acl
30 {
31
32 ElementIfElse::ElementIfElse(Element condition_):
33 // typeID fictitious
34 ElementBase(false, 0, TYPE_INT),
35 condition(condition_)
36 {
37 }
38
39
40 string ElementIfElse::str(const KernelConfiguration & kernelConfig) const
41 {
42 string s("");
43
44 // only if "if"'s body is not empty
45 // create if (...) statement
46 if (expressionIf.size() > 0)
47 {
48 s = "if (" + condition->str(kernelConfig) + ")\n\t{";
49
50 for (unsigned int i = 0; i < expressionIf.size(); i++)
51 {
52 s += "\n\t\t" + expressionIf[i]->str(kernelConfig) + ";";
53 }
54
55 s += "\n\t}";
56
57 // only if "if"'s and "else"'s body is not empty
58 // create else {...} statement
59 if (expressionElse.size() > 0)
60 {
61 s += "\n\telse\n\t{";
62
63 for (unsigned int i = 0; i < expressionElse.size(); i++)
64 {
65 s += "\n\t\t" + expressionElse[i]->str(kernelConfig) + ";";
66 }
67
68 s += "\n\t}";
69 }
70 }
71
72 return s;
73 }
74
75
76 void ElementIfElse::addBodyExpressionIf(Element expression_)
77 {
78 if (compatible(size, queue, expression_))
79 {
80 size = max(size, expression_->getSize());
81 if (expression_->getQueue().get() != 0)
82 queue = expression_->getQueue();
83 expressionIf.push_back(expression_);
84 }
85 else
86 {
87 errorMessage("ElementIfElse::addBodyExpressionIf - last added expression \
88 is incompatible with the previous ones. \
89 Either they reside on different devices or their sizes do not match: "
90 + numToStr(expression_->getSize()) + " and " + numToStr(size));
91 }
92 }
93
94
95 void ElementIfElse::addBodyExpressionElse(Element expression_)
96 {
97 if (compatible(size, queue, expression_))
98 {
99 size = max(size, expression_->getSize());
100 if (expression_->getQueue().get() != 0)
101 queue = expression_->getQueue();
102 expressionElse.push_back(expression_);
103 }
104 else
105 {
106 errorMessage("ElementIfElse::addBodyExpressionElse - last added expression \
107 is incompatible with the previous ones. \
108 Either they reside on different devices or their sizes do not match: "
109 + numToStr(expression_->getSize()) + " and " + numToStr(size));
110 }
111 }
112
113
114 void ElementIfElse::addToKernelSource(vector<Element> & arguments,
115 vector<Element> & localDeclarations) const
116 {
117 addElementToKernelSource(condition, arguments, localDeclarations);
118
119 for (unsigned int i = 0; i < expressionIf.size(); i++)
120 {
121 addElementToKernelSource(expressionIf[i], arguments, localDeclarations);
122 }
123
124 for (unsigned int i = 0; i < expressionElse.size(); i++)
125 {
126 addElementToKernelSource(expressionElse[i], arguments, localDeclarations);
127 }
128 }
129
130
131 void ElementIfElse::setAsArgument(cl::Kernel & kernel, unsigned int argumentIndex) const
132 {
133 }
134
135
136 string ElementIfElse::getName() const
137 {
138 return "";
139 }
140
141
142 string ElementIfElse::getTypeSignature(const KernelConfiguration & kernelConfig) const
143 {
144 return "";
145 }
146
147
148 string ElementIfElse::getLocalDeclaration(const KernelConfiguration & kernelConfig) const
149 {
150 return "";
151 }
152 } // namespace acl