ASL  0.1.7
Advanced Simulation Library
testOperators.cc
/*
* Advanced Simulation Library <http://asl.org.il>
*
* Copyright 2015 Avtech Scientific <http://avtechscientific.com>
*
*
* This file is part of Advanced Simulation Library (ASL).
*
* ASL is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* ASL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ASL. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "acl/acl.h"
#include "acl/DataTypes/aclIndex.h"
#include "acl/DataTypes/aclConstant.h"
#include "acl/DataTypes/aclVariable.h"
#include "acl/DataTypes/aclVariableReference.h"
#include "acl/DataTypes/aclPrivateVariable.h"
#include "acl/DataTypes/aclArray.h"
#include "acl/DataTypes/aclLocalArray.h"
#include "acl/DataTypes/aclSubvector.h"
#include "acl/Operators/aclElementFor.h"
#include "acl/Operators/aclElementIfElse.h"
#include "acl/Operators/aclElementParser.h"
#include "acl/Kernels/aclKernel.h"
#include "aslUtilities.h"
#include <math.h>
#include <initializer_list>
using namespace acl;
using namespace std;
bool testIfElse()
{
cout << "Test of If-Else..." << flush;
using namespace elementOperators;
shared_ptr<Variable<cl_int> > a(new Variable<cl_int> (15));
Element c0(new Constant<cl_int> (15));
Element vec(new Array<cl_float> (11));
vector<cl_float> input(11, 2);
vector<cl_float> output(11, 0);
vector<cl_float> expected({8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8});
copy(input, vec);
// Test if
shared_ptr<ElementIfElse> ifElse_TestIf(new ElementIfElse(isEqual(a, c0)));
ifElse_TestIf->addBodyExpressionIf(operatorAssignment(vec, vec + c1));
ifElse_TestIf->addBodyExpressionElse(operatorAssignment(vec, vec - c1));
// Test else
shared_ptr<ElementIfElse> ifElse_TestElse(new ElementIfElse(isEqual(a, c1)));
ifElse_TestElse->addBodyExpressionIf(operatorAssignment(vec, vec - c1));
ifElse_TestElse->addBodyExpressionElse(operatorAssignment(vec, vec + c1));
Kernel k;
k.addExpression(ifElse_TestIf);
k.addExpression(ifElse_TestElse);
k.setup();
k.compute();
copy(vec, output);
bool status(output == expected);
errorMessage(status);
return status;
}
bool testParser()
{
cout << "Test of Parser..." << flush;
using namespace elementOperators;
shared_ptr<Variable<cl_int> > a(new Variable<cl_int> (15));
Element c0(new Constant<cl_int> (15));
Element vec(new Array<cl_float> (11));
vector<cl_float> input(11, 2);
vector<cl_float> output(11, 0);
vector<cl_float> expected({35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35});
copy(input, vec);
string statement("a + c0 + c1 + vec");
shared_ptr<ElementParser> parser(new ElementParser());
parser->addElementNamePair(a, "a");
parser->addElementNamePair(c0, "c0");
parser->addElementNamePair(c1, "c1");
parser->addElementNamePair(vec, "vec");
parser->setStatement(statement);
Kernel k;
k.setup();
k.compute();
copy(vec, output);
bool status(output == expected);
errorMessage(status);
return status;
}
{
cout << "Test of Atomic Sum..." << flush;
using namespace elementOperators;
Element vec(new Array<cl_int> (11));
vector<cl_int> input(11, 2);
vector<cl_int> output(11, 0);
vector<cl_int> expected({8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8});
copy(input, vec);
kConf.extensions.push_back("cl_khr_global_int32_base_atomics");
k.setup();
k.compute();
copy(vec, output);
bool status(output == expected);
errorMessage(status);
return status;
}
int main()
{
bool allTestsPassed(true);
allTestsPassed &= testIfElse();
allTestsPassed &= testParser();
allTestsPassed &= testAtomicSum();
return allTestsPassed ? EXIT_SUCCESS : EXIT_FAILURE;
}
acl::ExpressionContainer::addExpression
void addExpression(Element expression_)
acl::Variable
Definition: aclVariable.h:36
acl::Array
Global array.
Definition: acl.h:47
kConf
const acl::KernelConfiguration & kConf(acl::KERNEL_BASIC)
acl::ElementIfElse
If-Else conditional structure.
Definition: aclElementIfElse.h:34
acl::Kernel
OpenCl Kernel generator.
Definition: aclKernel.h:48
acl::elementOperators::atomic_add
Element atomic_add(Element e1, Element e2)
acl::KernelConfiguration::extensions
std::vector< std::string > extensions
Definition: aclKernelConfiguration.h:46
acl::Kernel::compute
void compute()
acl::ElementParser
Definition: aclElementParser.h:37
testIfElse
bool testIfElse()
Definition: testOperators.cc:50
acl::ElementIfElse::addBodyExpressionElse
void addBodyExpressionElse(Element expression_)
acl::Kernel::setup
void setup()
acl::ElementIfElse::addBodyExpressionIf
void addBodyExpressionIf(Element expression_)
acl::elementOperators::operatorAssignment
Element operatorAssignment(Element e1, Element e2)
cl::flush
cl_int flush(void)
Definition: cl.hpp:7041
main
int main()
Definition: testOperators.cc:154
asl::errorMessage
void errorMessage(cl_int status, const char *errorMessage)
Prints errorMessage and exits depending on the status.
acl::KernelConfiguration
ACL Kernel configuration class.
Definition: aclKernelConfiguration.h:34
acl::Element
std::shared_ptr< ElementBase > Element
Definition: acl.h:49
acl::copy
void copy(MemBlock &source, T *destination)
acl::elementOperators::isEqual
Element isEqual(Element e1, Element e2)
testParser
bool testParser()
Definition: testOperators.cc:90
acl::KERNEL_BASIC
const KernelConfiguration KERNEL_BASIC
Definition: aclElementBase.h:45
acl::Constant
Definition: aclConstant.h:33
acl
Advanced Computational Language.
Definition: acl.h:40
testAtomicSum
bool testAtomicSum()
Definition: testOperators.cc:127