| 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 testReductionFunction.cc |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "acl/Kernels/aclKernel.h" |
| 29 |
#include "acl/aclUtilities.h" |
| 30 |
#include "acl/aclMath/aclReductionAlgGenerator.h" |
| 31 |
#include "acl/aclGenerators.h" |
| 32 |
#include "aslUtilities.h" |
| 33 |
#include "acl/aclMath/aclVectorOfElements.h" |
| 34 |
|
| 35 |
using namespace acl; |
| 36 |
|
| 37 |
bool testSum() |
| 38 |
{ |
| 39 |
cout << "testSum..." << flush; |
| 40 |
unsigned int n(101); |
| 41 |
auto v(generateVEData<float>(n,1u)); |
| 42 |
initData(v, generateVEConstant(2)); |
| 43 |
auto summator(generateSumAlg<float>(v)); |
| 44 |
summator->generateAlg(); |
| 45 |
summator->compute(); |
| 46 |
bool status(asl::approxEqual(summator->res.v()[0],2.f*n)); |
| 47 |
asl::errorMessage(status); |
| 48 |
|
| 49 |
return status; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
bool testSum1() |
| 54 |
{ |
| 55 |
cout << "testSum1..." << flush; |
| 56 |
unsigned int n(100001); |
| 57 |
VectorOfElements v1(generateVEData<float>(n,1u)); |
| 58 |
VectorOfElements v2(generateVEData<float>(n,1u)); |
| 59 |
initData(v1, generateVEConstant(2)); |
| 60 |
initData(v2, generateVEConstant(3)); |
| 61 |
auto summator(generateSumAlg<float>(v1*v2)); |
| 62 |
summator->generateAlg(); |
| 63 |
summator->compute(); |
| 64 |
bool status(asl::approxEqual(summator->res.v()[0],6.f*n)); |
| 65 |
asl::errorMessage(status); |
| 66 |
|
| 67 |
return status; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
bool testMin() |
| 72 |
{ |
| 73 |
cout << "testMin..." << flush; |
| 74 |
VectorOfElements vI(generateVEIndex()); |
| 75 |
VectorOfElements v1(generateVEData<float>(101u,1u)); |
| 76 |
initData(v1, generateVEConstant(2)); |
| 77 |
auto minimizer(generateMinAlg<float>(v1*((vI-100)*(vI-100)+3))); |
| 78 |
minimizer->generateAlg(); |
| 79 |
minimizer->compute(); |
| 80 |
bool status(asl::approxEqual(minimizer->res.v()[0],6.f)); |
| 81 |
asl::errorMessage(status); |
| 82 |
|
| 83 |
return status; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
bool testMax() |
| 88 |
{ |
| 89 |
cout << "testMax..." << flush; |
| 90 |
VectorOfElements vI(generateVEIndex()); |
| 91 |
VectorOfElements v1(generateVEData<float>(100001u,1u)); |
| 92 |
initData(v1, generateVEConstant(2)); |
| 93 |
auto maximizer(generateMaxAlg<float>(v1*((1000.-vI)*(vI-1000.)-10.))); |
| 94 |
maximizer->generateAlg(); |
| 95 |
maximizer->compute(); |
| 96 |
bool status(asl::approxEqual(maximizer->res.v()[0],-20.f)); |
| 97 |
asl::errorMessage(status); |
| 98 |
|
| 99 |
return status; |
| 100 |
} |
| 101 |
|
| 102 |
bool testProduct() |
| 103 |
{ |
| 104 |
cout << "testProduct..." << flush; |
| 105 |
typedef double FT; |
| 106 |
VectorOfElements vI(generateVEIndex()); |
| 107 |
VectorOfElements v1(generateVEData<FT>(100001u,1u)); |
| 108 |
initData(v1, generateVEConstant(2)); |
| 109 |
auto alg(generateProductAlg<FT>(select(generateVEConstant(1.), |
| 110 |
v1, |
| 111 |
vI >=1000 && vI <= 1007, |
| 112 |
acl::typeToTypeID<FT>()))); |
| 113 |
alg->generateAlg(); |
| 114 |
alg->compute(); |
| 115 |
bool status(asl::approxEqual(alg->res.v()[0],256)); |
| 116 |
asl::errorMessage(status); |
| 117 |
|
| 118 |
return status; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
int main() |
| 123 |
{ |
| 124 |
bool allTestsPassed(true); |
| 125 |
|
| 126 |
allTestsPassed &= testSum(); |
| 127 |
allTestsPassed &= testSum1(); |
| 128 |
allTestsPassed &= testMin(); |
| 129 |
allTestsPassed &= testMax(); |
| 130 |
allTestsPassed &= testProduct(); |
| 131 |
|
| 132 |
return allTestsPassed ? EXIT_SUCCESS : EXIT_FAILURE; |
| 133 |
} |