| 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 testPrivateVar.cc |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "utilities/aslUValue.h" |
| 29 |
#include "acl/Kernels/aclKernel.h" |
| 30 |
#include "acl/Kernels/aclKernelConfigurationTemplates.h" |
| 31 |
#include <acl/acl.h> |
| 32 |
#include <acl/aclMath/aclVectorOfElements.h> |
| 33 |
#include <acl/aclGenerators.h> |
| 34 |
#include <utilities/aslTimer.h> |
| 35 |
|
| 36 |
const unsigned int nLength(1000000); |
| 37 |
const unsigned int nOperations(10); |
| 38 |
const unsigned int nCycles(10000); |
| 39 |
const acl::KernelConfiguration & kConf(acl::KERNEL_BASIC); |
| 40 |
//const acl::KernelConfiguration & kConf(acl::KERNEL_SIMDUA); |
| 41 |
|
| 42 |
bool testKernelUnoptimized() |
| 43 |
{ |
| 44 |
cout << "Test of \"Simple kernel\" function..." << flush; |
| 45 |
|
| 46 |
auto vec1(acl::generateVEData<float>(nLength,1u)); |
| 47 |
acl::Kernel k(kConf); |
| 48 |
|
| 49 |
auto res(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT)); |
| 50 |
k << (res = acl::generateVEConstant(0.)); |
| 51 |
for(unsigned int i(0); i<nOperations; ++i) |
| 52 |
{ |
| 53 |
auto tempRes(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT)); |
| 54 |
k << (tempRes=acl::generateVEIndex() * i+5.*i*i+7./(i+1.)); |
| 55 |
k << (res+=tempRes*tempRes+rsqrt(.2*tempRes)+ 1./tempRes); |
| 56 |
} |
| 57 |
k << (vec1=res); |
| 58 |
k.setup(); |
| 59 |
|
| 60 |
asl::Timer timer; |
| 61 |
timer.start(); |
| 62 |
for(unsigned int i(0); i<nCycles; ++i) |
| 63 |
k.compute(); |
| 64 |
timer.stop(); |
| 65 |
std::cout<<"Unoptimized: "<<timer.realTime()<<endl; |
| 66 |
|
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
bool testKernelUnoptimizedPlus() |
| 72 |
{ |
| 73 |
cout << "Test of \"Simple kernel\" function..." << flush; |
| 74 |
|
| 75 |
auto vec1(acl::generateVEData<float>(nLength,1u)); |
| 76 |
acl::Kernel k(kConf); |
| 77 |
|
| 78 |
auto res(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT)); |
| 79 |
k << (res = acl::generateVEConstant(0.)); |
| 80 |
vector<acl::VectorOfElements> tempRes(nOperations); |
| 81 |
for(unsigned int i(0); i<nOperations; ++i) |
| 82 |
{ |
| 83 |
copy(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT), tempRes[i]); |
| 84 |
k << (tempRes[i]=acl::generateVEIndex() * i+5.*i*i+7./(i+1.)); |
| 85 |
} |
| 86 |
for(unsigned int i(0); i<nOperations; ++i) |
| 87 |
k << (res+=tempRes[i]*tempRes[i]+rsqrt(.2*tempRes[i])+ 1./tempRes[i]); |
| 88 |
k << (vec1=res); |
| 89 |
k.setup(); |
| 90 |
|
| 91 |
asl::Timer timer; |
| 92 |
timer.start(); |
| 93 |
for(unsigned int i(0); i<nCycles; ++i) |
| 94 |
k.compute(); |
| 95 |
timer.stop(); |
| 96 |
std::cout<<"UnoptimizedPlus: "<<timer.realTime()<<endl; |
| 97 |
|
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
bool testKernelOptimized() |
| 103 |
{ |
| 104 |
cout << "Test of \"Simple kernel\" function..." << flush; |
| 105 |
|
| 106 |
auto vec1(acl::generateVEData<float>(nLength,1u)); |
| 107 |
acl::Kernel k(kConf); |
| 108 |
|
| 109 |
auto res(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT)); |
| 110 |
auto tempRes(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT)); |
| 111 |
k << (res = acl::generateVEConstant(0.)); |
| 112 |
for(unsigned int i(0); i<nOperations; ++i) |
| 113 |
{ |
| 114 |
k << (tempRes=acl::generateVEIndex() * i+5.*i*i+7./(i+1.)); |
| 115 |
k << (res+=tempRes*tempRes+rsqrt(.2*tempRes)+ 1./tempRes); |
| 116 |
} |
| 117 |
k << (vec1=res); |
| 118 |
k.setup(); |
| 119 |
// cout<<k.getKernelSource()<<endl; |
| 120 |
|
| 121 |
asl::Timer timer; |
| 122 |
timer.start(); |
| 123 |
for(unsigned int i(0); i<nCycles; ++i) |
| 124 |
k.compute(); |
| 125 |
timer.stop(); |
| 126 |
std::cout<<"Optimized: "<<timer.realTime()<<endl; |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
int main() |
| 134 |
{ |
| 135 |
testKernelUnoptimized(); |
| 136 |
testKernelUnoptimizedPlus(); |
| 137 |
testKernelOptimized(); |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |