| 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 |
/** |
| 25 |
\example testOperators.cc |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#include "acl/acl.h" |
| 30 |
#include "acl/DataTypes/aclIndex.h" |
| 31 |
#include "acl/DataTypes/aclConstant.h" |
| 32 |
#include "acl/DataTypes/aclVariable.h" |
| 33 |
#include "acl/DataTypes/aclVariableReference.h" |
| 34 |
#include "acl/DataTypes/aclPrivateVariable.h" |
| 35 |
#include "acl/DataTypes/aclArray.h" |
| 36 |
#include "acl/DataTypes/aclLocalArray.h" |
| 37 |
#include "acl/DataTypes/aclSubvector.h" |
| 38 |
#include "acl/Operators/aclElementFor.h" |
| 39 |
#include "acl/Operators/aclElementIfElse.h" |
| 40 |
#include "acl/Operators/aclElementParser.h" |
| 41 |
#include "acl/Kernels/aclKernel.h" |
| 42 |
#include "aslUtilities.h" |
| 43 |
#include <math.h> |
| 44 |
#include <initializer_list> |
| 45 |
|
| 46 |
using namespace acl; |
| 47 |
using namespace std; |
| 48 |
|
| 49 |
|
| 50 |
bool testIfElse() |
| 51 |
{ |
| 52 |
cout << "Test of If-Else..." << flush; |
| 53 |
|
| 54 |
using namespace elementOperators; |
| 55 |
shared_ptr<Variable<cl_int> > a(new Variable<cl_int> (15)); |
| 56 |
Element c0(new Constant<cl_int> (15)); |
| 57 |
Element c1(new Constant<cl_int> (3)); |
| 58 |
Element vec(new Array<cl_float> (11)); |
| 59 |
vector<cl_float> input(11, 2); |
| 60 |
vector<cl_float> output(11, 0); |
| 61 |
vector<cl_float> expected({8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}); |
| 62 |
copy(input, vec); |
| 63 |
|
| 64 |
// Test if |
| 65 |
shared_ptr<ElementIfElse> ifElse_TestIf(new ElementIfElse(isEqual(a, c0))); |
| 66 |
ifElse_TestIf->addBodyExpressionIf(operatorAssignment(vec, vec + c1)); |
| 67 |
ifElse_TestIf->addBodyExpressionElse(operatorAssignment(vec, vec - c1)); |
| 68 |
|
| 69 |
// Test else |
| 70 |
shared_ptr<ElementIfElse> ifElse_TestElse(new ElementIfElse(isEqual(a, c1))); |
| 71 |
ifElse_TestElse->addBodyExpressionIf(operatorAssignment(vec, vec - c1)); |
| 72 |
ifElse_TestElse->addBodyExpressionElse(operatorAssignment(vec, vec + c1)); |
| 73 |
|
| 74 |
|
| 75 |
Kernel k; |
| 76 |
|
| 77 |
k.addExpression(ifElse_TestIf); |
| 78 |
k.addExpression(ifElse_TestElse); |
| 79 |
k.setup(); |
| 80 |
k.compute(); |
| 81 |
copy(vec, output); |
| 82 |
|
| 83 |
bool status(output == expected); |
| 84 |
errorMessage(status); |
| 85 |
|
| 86 |
return status; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
bool testParser() |
| 91 |
{ |
| 92 |
cout << "Test of Parser..." << flush; |
| 93 |
|
| 94 |
using namespace elementOperators; |
| 95 |
shared_ptr<Variable<cl_int> > a(new Variable<cl_int> (15)); |
| 96 |
Element c0(new Constant<cl_int> (15)); |
| 97 |
Element c1(new Constant<cl_int> (3)); |
| 98 |
Element vec(new Array<cl_float> (11)); |
| 99 |
vector<cl_float> input(11, 2); |
| 100 |
vector<cl_float> output(11, 0); |
| 101 |
vector<cl_float> expected({35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35}); |
| 102 |
|
| 103 |
copy(input, vec); |
| 104 |
|
| 105 |
string statement("a + c0 + c1 + vec"); |
| 106 |
shared_ptr<ElementParser> parser(new ElementParser()); |
| 107 |
parser->addElementNamePair(a, "a"); |
| 108 |
parser->addElementNamePair(c0, "c0"); |
| 109 |
parser->addElementNamePair(c1, "c1"); |
| 110 |
parser->addElementNamePair(vec, "vec"); |
| 111 |
parser->setStatement(statement); |
| 112 |
|
| 113 |
Kernel k; |
| 114 |
|
| 115 |
k.addExpression(operatorAssignment(vec, parser)); |
| 116 |
k.setup(); |
| 117 |
k.compute(); |
| 118 |
|
| 119 |
copy(vec, output); |
| 120 |
|
| 121 |
bool status(output == expected); |
| 122 |
errorMessage(status); |
| 123 |
|
| 124 |
return status; |
| 125 |
} |
| 126 |
|
| 127 |
bool testAtomicSum() |
| 128 |
{ |
| 129 |
cout << "Test of Atomic Sum..." << flush; |
| 130 |
|
| 131 |
using namespace elementOperators; |
| 132 |
Element c(new Constant<cl_int> (6)); |
| 133 |
Element vec(new Array<cl_int> (11)); |
| 134 |
vector<cl_int> input(11, 2); |
| 135 |
vector<cl_int> output(11, 0); |
| 136 |
vector<cl_int> expected({8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}); |
| 137 |
copy(input, vec); |
| 138 |
|
| 139 |
KernelConfiguration kConf(KERNEL_BASIC); |
| 140 |
kConf.extensions.push_back("cl_khr_global_int32_base_atomics"); |
| 141 |
Kernel k(kConf); |
| 142 |
|
| 143 |
k.addExpression(atomic_add(vec, c)); |
| 144 |
k.setup(); |
| 145 |
k.compute(); |
| 146 |
copy(vec, output); |
| 147 |
|
| 148 |
bool status(output == expected); |
| 149 |
errorMessage(status); |
| 150 |
|
| 151 |
return status; |
| 152 |
} |
| 153 |
|
| 154 |
int main() |
| 155 |
{ |
| 156 |
bool allTestsPassed(true); |
| 157 |
|
| 158 |
allTestsPassed &= testIfElse(); |
| 159 |
allTestsPassed &= testParser(); |
| 160 |
allTestsPassed &= testAtomicSum(); |
| 161 |
|
| 162 |
return allTestsPassed ? EXIT_SUCCESS : EXIT_FAILURE; |
| 163 |
} |