| 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 testASLData.cc |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "acl/acl.h" |
| 29 |
#include "acl/aclGenerators.h" |
| 30 |
#include "acl/DataTypes/aclGroupID.h" |
| 31 |
#include "acl/DataTypes/aclConstant.h" |
| 32 |
#include "aslGenerators.h" |
| 33 |
#include "aslUtilities.h" |
| 34 |
#include "acl/Kernels/aclKernel.h" |
| 35 |
#include "data/aslDataUtilities.h" |
| 36 |
#include <acl/aclMath/aclVectorOfElements.h> |
| 37 |
#include <data/aslDataWithGhostNodes.h> |
| 38 |
|
| 39 |
using namespace acl; |
| 40 |
using namespace asl; |
| 41 |
using namespace std; |
| 42 |
|
| 43 |
bool testSimpleKernel() |
| 44 |
{ |
| 45 |
Block bl(makeAVec(10,5),0.1); |
| 46 |
auto a(generateDataContainerACL_SP<double>(bl,1,1)); |
| 47 |
auto b(generateDataContainerACL_SP<double>(bl,1,1)); |
| 48 |
|
| 49 |
Kernel k; |
| 50 |
|
| 51 |
k<<(a->getEContainer()=acl::generateVEConstant<double>(0)); |
| 52 |
k<<(b->getEContainer()=acl::generateVEConstant<double>(0)); |
| 53 |
|
| 54 |
k.setup(); |
| 55 |
cout<<k.getKernelSource ()<<endl; |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
bool testInitData() |
| 62 |
{ |
| 63 |
Block bl(makeAVec(10,5),0.1); |
| 64 |
auto a(generateDataContainerACL_SP<double>(bl, 1, 1)); |
| 65 |
|
| 66 |
initData(a->getEContainer(),acl::generateVEConstant<double>(0)); |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
bool testUploadToLocalMem() |
| 73 |
{ |
| 74 |
cout << "Test of UploadToLocalMem()..." << flush; |
| 75 |
|
| 76 |
unsigned int componentsNum = 2; |
| 77 |
unsigned int groupSize = 27; |
| 78 |
KernelConfiguration kConf(KERNEL_BASIC); |
| 79 |
kConf.local = true; |
| 80 |
Kernel kernel(kConf); |
| 81 |
Element groupID(new GroupID()); |
| 82 |
Element c0(new Constant<cl_uint>(0)); |
| 83 |
|
| 84 |
asl::AVec<int> totalDimensions(asl::makeAVec(10, 10, 10)); |
| 85 |
// dx = 1 |
| 86 |
asl::Block block(totalDimensions, 1); |
| 87 |
|
| 88 |
// boundary = 0 |
| 89 |
auto source(asl::generateDataContainerACL_SP<float>(block, |
| 90 |
componentsNum, |
| 91 |
0u)); |
| 92 |
// initialize source with value "13" |
| 93 |
acl::initData(source->getEContainer(), acl::generateVEConstantN(componentsNum, |
| 94 |
13)); |
| 95 |
|
| 96 |
// boundary = 0 |
| 97 |
auto destination(asl::generateDataContainerACL_SP<float>(block, |
| 98 |
componentsNum, |
| 99 |
0u)); |
| 100 |
// initialize source with value "27" |
| 101 |
acl::initData(destination->getEContainer(), acl::generateVEConstantN(componentsNum, |
| 102 |
27)); |
| 103 |
|
| 104 |
|
| 105 |
asl::AVec<int> portionDimensions(asl::makeAVec(5, 5, 5)); |
| 106 |
|
| 107 |
unsigned int portionSize = productOfElements(portionDimensions); |
| 108 |
unsigned int totalSize = productOfElements(totalDimensions); |
| 109 |
unsigned int groupsNumber = totalSize / portionSize; |
| 110 |
Element cPortionSize(new Constant<cl_uint>(portionSize)); |
| 111 |
kernel.setGroupsNumber(groupsNumber); |
| 112 |
|
| 113 |
VectorOfElements localSource(componentsNum); |
| 114 |
copy(uploadToLocalMem(*source, portionDimensions, groupSize, kernel), localSource); |
| 115 |
|
| 116 |
using namespace elementOperators; |
| 117 |
kernel.addExpression(barrier("CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE")); |
| 118 |
// overwrite destination with localSource |
| 119 |
for (unsigned int i = 0; i < componentsNum; ++i) |
| 120 |
kernel.addExpression(syncCopy(localSource[i], destination->getEContainer()[i], |
| 121 |
c0, cPortionSize * groupID, cPortionSize)); |
| 122 |
|
| 123 |
kernel.setup(); |
| 124 |
kernel.compute(); |
| 125 |
|
| 126 |
|
| 127 |
bool status(true); |
| 128 |
vector<cl_float> src(totalSize); |
| 129 |
vector<cl_float> dst(totalSize); |
| 130 |
for (unsigned int i = 0; i < componentsNum; ++i) |
| 131 |
{ |
| 132 |
copy(source->getEContainer()[i], src); |
| 133 |
copy(destination->getEContainer()[i], dst); |
| 134 |
|
| 135 |
cout << src << endl << endl << dst << endl; |
| 136 |
|
| 137 |
status &= (src == dst); |
| 138 |
} |
| 139 |
|
| 140 |
errorMessage(status); |
| 141 |
|
| 142 |
return status; |
| 143 |
} |
| 144 |
|
| 145 |
int main() |
| 146 |
{ |
| 147 |
bool allTestsPassed(true); |
| 148 |
|
| 149 |
allTestsPassed &= testSimpleKernel(); |
| 150 |
allTestsPassed &= testInitData(); |
| 151 |
allTestsPassed &= testUploadToLocalMem(); |
| 152 |
|
| 153 |
return allTestsPassed ? EXIT_SUCCESS : EXIT_FAILURE; |
| 154 |
} |