| 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 testKernel.cc |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#include "acl/acl.h" |
| 30 |
#include "acl/DataTypes/aclIndex.h" |
| 31 |
#include "acl/DataTypes/aclGroupID.h" |
| 32 |
#include "acl/DataTypes/aclConstant.h" |
| 33 |
#include "acl/DataTypes/aclVariable.h" |
| 34 |
#include "acl/DataTypes/aclVariableReference.h" |
| 35 |
#include "acl/DataTypes/aclPrivateVariable.h" |
| 36 |
#include "acl/DataTypes/aclPrivateArray.h" |
| 37 |
#include "acl/DataTypes/aclArray.h" |
| 38 |
#include "acl/DataTypes/aclSubvector.h" |
| 39 |
#include "acl/DataTypes/aclLocalArray.h" |
| 40 |
#include "acl/Operators/aclElementFor.h" |
| 41 |
#include "acl/Operators/aclElementIfElse.h" |
| 42 |
#include "acl/Operators/aclElementExcerpt.h" |
| 43 |
#include "acl/Kernels/aclKernel.h" |
| 44 |
#include "aslUtilities.h" |
| 45 |
#include <math.h> |
| 46 |
#include <initializer_list> |
| 47 |
|
| 48 |
#include <acl/Kernels/aclKernelConfigurationTemplates.h> |
| 49 |
|
| 50 |
|
| 51 |
using namespace acl; |
| 52 |
using namespace std; |
| 53 |
|
| 54 |
bool testCopy() |
| 55 |
{ |
| 56 |
cout << "Test of \"copy\" function..." << flush; |
| 57 |
Element vec0(new Array<cl_float> (10)); |
| 58 |
|
| 59 |
vector<cl_float> input(10, 3); |
| 60 |
vector<cl_float> output(10, 1); |
| 61 |
|
| 62 |
copy(input, vec0); |
| 63 |
copy(vec0, output); |
| 64 |
|
| 65 |
bool status(output[3] == 3); |
| 66 |
errorMessage(status); |
| 67 |
|
| 68 |
return status; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
bool testKernel() |
| 73 |
{ |
| 74 |
cout << "Test of Kernel with double..." << flush; |
| 75 |
|
| 76 |
Element vec0(new Array<cl_double>(10)); |
| 77 |
Element vec1(new Array<cl_double>(10)); |
| 78 |
Element vec2(new Array<cl_double>(10)); |
| 79 |
Element c(new Constant<cl_double>(2.)); |
| 80 |
Element ind(new Index()); |
| 81 |
|
| 82 |
|
| 83 |
Kernel k; |
| 84 |
{ |
| 85 |
using namespace elementOperators; |
| 86 |
k.addExpression(operatorAssignment(vec2, c )); |
| 87 |
k.addExpression(operatorAssignment(vec0, c + powI(vec2, 3))); |
| 88 |
k.addExpression(operatorAssignment(vec1, ind)); |
| 89 |
} |
| 90 |
k.setup(); |
| 91 |
k.compute(); |
| 92 |
|
| 93 |
vector<cl_double> output0(10), output1(10); |
| 94 |
copy(vec0, output0); |
| 95 |
copy(vec1, output1); |
| 96 |
|
| 97 |
bool status(output0[9]<10.1 && output1[2]>2-1e-4 && output1[3]<3+1e-4); |
| 98 |
errorMessage(status); |
| 99 |
|
| 100 |
return status; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
bool testKernelSIMD() |
| 105 |
{ |
| 106 |
cout << "Test of KernelSIMD..." << flush; |
| 107 |
|
| 108 |
Element vec0(new Array<cl_float>(11)); |
| 109 |
Element vec1(new Array<cl_float>(11)); |
| 110 |
|
| 111 |
vector<cl_float> input0(11, 3); |
| 112 |
vector<cl_float> input1(11, 5); |
| 113 |
vector<cl_float> output(11, 0); |
| 114 |
vector<cl_float> expected({8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}); |
| 115 |
copy(input0, vec0); |
| 116 |
copy(input1, vec1); |
| 117 |
|
| 118 |
Kernel k(KERNEL_SIMD); |
| 119 |
{ |
| 120 |
using namespace elementOperators; |
| 121 |
k.addExpression(operatorAssignmentSafe(vec1, vec0 + vec1)); |
| 122 |
} |
| 123 |
|
| 124 |
k.setup(); |
| 125 |
|
| 126 |
k.compute(); |
| 127 |
copy(vec1, output); |
| 128 |
|
| 129 |
bool status(output == expected); |
| 130 |
errorMessage(status); |
| 131 |
|
| 132 |
return status; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
bool testKernelSIMDUA() |
| 137 |
{ |
| 138 |
cout << "Test of KernelSIMDUA..." << flush; |
| 139 |
|
| 140 |
Element vec0(new Array<cl_float> (11)); |
| 141 |
Element vec1(new Array<cl_float> (11)); |
| 142 |
|
| 143 |
vector<cl_float> input0(11, 3); |
| 144 |
vector<cl_float> input1(11, 5); |
| 145 |
vector<cl_float> output(11, 0); |
| 146 |
vector<cl_float> expected({8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}); |
| 147 |
copy(input0, vec0); |
| 148 |
copy(input1, vec1); |
| 149 |
|
| 150 |
KernelConfiguration kConf(KERNEL_SIMDUA); |
| 151 |
// kConf.extensions.push_back("cl_amd_printf"); |
| 152 |
Kernel k(kConf); |
| 153 |
{ |
| 154 |
using namespace elementOperators; |
| 155 |
k.addExpression(operatorAssignmentSafe(vec1, vec0 + vec1)); |
| 156 |
// k.addExpression(printfFunction("\"index: %d\\n\", index")); |
| 157 |
} |
| 158 |
|
| 159 |
k.setup(); |
| 160 |
|
| 161 |
k.compute(); |
| 162 |
copy(vec1, output); |
| 163 |
|
| 164 |
bool status(output == expected); |
| 165 |
errorMessage(status); |
| 166 |
|
| 167 |
return status; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
bool testPrivateVariable() |
| 172 |
{ |
| 173 |
cout << "Test of kernel with PrivateVariable..." << flush; |
| 174 |
|
| 175 |
Element vec0(new Array<cl_float>(10)); |
| 176 |
Element vec1(new Array<cl_float>(10)); |
| 177 |
Element loc(new PrivateVariable<cl_float>()); |
| 178 |
|
| 179 |
vector<cl_float> input1(10, 3); |
| 180 |
vector<cl_float> input2(10, 5); |
| 181 |
vector<cl_float> output(10, 1); |
| 182 |
|
| 183 |
copy(input1, vec0); |
| 184 |
copy(input2, vec1); |
| 185 |
|
| 186 |
Kernel k; |
| 187 |
{ |
| 188 |
using namespace elementOperators; |
| 189 |
k.addExpression(operatorAssignment(loc, vec0 + vec1)); |
| 190 |
k.addExpression(operatorAssignment(vec1, vec0 - vec1)); |
| 191 |
k.addExpression(operatorAssignment(vec0, loc)); |
| 192 |
} |
| 193 |
k.setup(); |
| 194 |
|
| 195 |
k.compute(); |
| 196 |
copy(vec0, output); |
| 197 |
|
| 198 |
|
| 199 |
bool status(output[2] ==8.); |
| 200 |
errorMessage(status); |
| 201 |
|
| 202 |
return status; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
bool testPrivateArray() |
| 207 |
{ |
| 208 |
cout << "Test of kernel with PrivateArray..." << flush; |
| 209 |
|
| 210 |
vector<cl_int> inputGaIn({0, 4, 5}); |
| 211 |
vector<cl_float> inputGaOut(3, 0); |
| 212 |
vector<cl_float> inputPa({-9, 2, 0, 15, 1, 3}); |
| 213 |
vector<cl_float> output(3); |
| 214 |
vector<cl_float> expected({-9, 1, 3}); |
| 215 |
|
| 216 |
Element gaIn(new Array<cl_int>(3)); |
| 217 |
Element gaOut(new Array<cl_float>(3)); |
| 218 |
Element pa(new PrivateArray<cl_float>(inputPa)); |
| 219 |
shared_ptr<ElementExcerpt> ex(new ElementExcerpt(pa, gaIn)); |
| 220 |
|
| 221 |
copy(inputGaIn, gaIn); |
| 222 |
|
| 223 |
|
| 224 |
Kernel k; |
| 225 |
{ |
| 226 |
using namespace elementOperators; |
| 227 |
k.addExpression(operatorAssignment(gaOut, ex)); |
| 228 |
} |
| 229 |
k.setup(); |
| 230 |
|
| 231 |
k.compute(); |
| 232 |
copy(gaOut, output); |
| 233 |
|
| 234 |
|
| 235 |
bool status(output == expected); |
| 236 |
errorMessage(status); |
| 237 |
|
| 238 |
return status; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
bool testVariable() |
| 243 |
{ |
| 244 |
cout << "Test of Variable functionality..." << flush; |
| 245 |
|
| 246 |
Element vec0(new Array<cl_float> (10)); |
| 247 |
shared_ptr<Variable<cl_float> > a(new Variable<cl_float> (1.)); |
| 248 |
|
| 249 |
vector<cl_float> output(10, 1); |
| 250 |
|
| 251 |
Kernel k; |
| 252 |
|
| 253 |
k.addExpression(elementOperators::operatorAssignment(vec0, a)); |
| 254 |
k.setup(); |
| 255 |
|
| 256 |
k.compute(); |
| 257 |
a->setValue(10.); |
| 258 |
k.compute(); |
| 259 |
copy(vec0, output); |
| 260 |
|
| 261 |
bool status(output[2] ==10.); |
| 262 |
errorMessage(status); |
| 263 |
|
| 264 |
return status; |
| 265 |
} |
| 266 |
|
| 267 |
bool testVariableReference() |
| 268 |
{ |
| 269 |
cout << "Test of VariableReference functionality..." << flush; |
| 270 |
|
| 271 |
Element vec0(new Array<cl_float> (10)); |
| 272 |
float v(1.); |
| 273 |
Element a(new VariableReference<cl_float> (v)); |
| 274 |
|
| 275 |
vector<cl_float> output(10, 1); |
| 276 |
|
| 277 |
Kernel k; |
| 278 |
|
| 279 |
k.addExpression(elementOperators::operatorAssignment(vec0, a)); |
| 280 |
k.setup(); |
| 281 |
|
| 282 |
k.compute(); |
| 283 |
v=10.; |
| 284 |
k.compute(); |
| 285 |
copy(vec0, output); |
| 286 |
|
| 287 |
bool status(output[2] ==10.); |
| 288 |
errorMessage(status); |
| 289 |
|
| 290 |
return status; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
bool testSelect() |
| 295 |
{ |
| 296 |
cout << "Test of select function..." << flush; |
| 297 |
|
| 298 |
Element vec0(new Array<cl_double> (10)); |
| 299 |
Element c0(new Constant<cl_double> (2.1)); |
| 300 |
|
| 301 |
vector<cl_double> input(10, 3.); |
| 302 |
vector<cl_double> output(10, 1.); |
| 303 |
|
| 304 |
copy(input,vec0); |
| 305 |
|
| 306 |
Kernel k; |
| 307 |
{ |
| 308 |
using namespace elementOperators; |
| 309 |
k.addExpression(operatorAssignment(vec0, |
| 310 |
select(vec0-c0, |
| 311 |
vec0*vec0, |
| 312 |
convert(TYPE_SELECT[TYPE_DOUBLE], |
| 313 |
vec0 > c0, |
| 314 |
false)))); |
| 315 |
} |
| 316 |
k.setup(); |
| 317 |
|
| 318 |
k.compute(); |
| 319 |
copy(vec0, output); |
| 320 |
|
| 321 |
bool status(output[2] ==9.); |
| 322 |
errorMessage(status); |
| 323 |
|
| 324 |
return status; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
bool testSubvector() |
| 329 |
{ |
| 330 |
cout << "Test of Subvector..." << flush; |
| 331 |
cl_float init[] = {16, 2, 77, 29, 23, 16, 2, 77, 29, 23}; |
| 332 |
shared_ptr<Array<cl_float> > vec0(new Array<cl_float>(10)); |
| 333 |
|
| 334 |
vector<cl_float> input(init, init + sizeof(init) / sizeof(cl_float) ); |
| 335 |
vector<cl_float> output(2); |
| 336 |
|
| 337 |
copy(input, vec0); |
| 338 |
Element subvec0(new Subvector<cl_float> (vec0, 2, 0)); |
| 339 |
copy(subvec0, output); |
| 340 |
|
| 341 |
bool status(output[0]==16); |
| 342 |
errorMessage(status); |
| 343 |
|
| 344 |
return status; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
bool testSwapBuffers() |
| 349 |
{ |
| 350 |
cout << "Test of Swap functionality..." << flush; |
| 351 |
shared_ptr<Array<cl_float> > vec0(new Array<cl_float>(10)); |
| 352 |
shared_ptr<Array<cl_float> > vec1(new Array<cl_float>(10)); |
| 353 |
|
| 354 |
vector<cl_float> input0(10, 1); |
| 355 |
vector<cl_float> input1(10, 2); |
| 356 |
vector<cl_float> output(10, 10); |
| 357 |
|
| 358 |
copy(input0, vec0); |
| 359 |
copy(input1, vec1); |
| 360 |
swapBuffers(*vec1,*vec0); |
| 361 |
copy(vec0, output); |
| 362 |
|
| 363 |
bool status(output[3] == 2); |
| 364 |
errorMessage(status); |
| 365 |
|
| 366 |
return status; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
bool testLocalArray() |
| 371 |
{ |
| 372 |
cout << "Test of LocalArray and syncCopy with barrier()..." << flush; |
| 373 |
|
| 374 |
KernelConfiguration kConf(KERNEL_BASIC); |
| 375 |
kConf.local = true; |
| 376 |
|
| 377 |
unsigned int groupsNumber = 5; |
| 378 |
unsigned int groupSize = 2; |
| 379 |
|
| 380 |
Element vec0(new Array<cl_float>(groupSize * groupsNumber)); |
| 381 |
Element vec1(new Array<cl_float>(groupSize * groupsNumber)); |
| 382 |
Element loc0(new LocalArray<cl_float>(groupSize)); |
| 383 |
Element loc1(new LocalArray<cl_float>(groupSize)); |
| 384 |
Element groupID(new GroupID()); |
| 385 |
Element cGroupSize(new Constant<cl_uint>(groupSize)); |
| 386 |
Element c0(new Constant<cl_uint>(0)); |
| 387 |
|
| 388 |
vector<cl_float> input0(groupSize * groupsNumber, 3); |
| 389 |
vector<cl_float> input1(groupSize * groupsNumber, 5); |
| 390 |
vector<cl_float> output(groupSize * groupsNumber, 0); |
| 391 |
vector<cl_float> expected({2, 2, 2, 2, 2, 2, 2, 2, 2, 2}); |
| 392 |
copy(input0, vec0); |
| 393 |
copy(input1, vec1); |
| 394 |
|
| 395 |
Kernel k(kConf); |
| 396 |
k.setGroupsNumber(groupsNumber); |
| 397 |
{ |
| 398 |
using namespace elementOperators; |
| 399 |
k.addExpression(syncCopy(vec0, loc0, cGroupSize * groupID, c0, cGroupSize)); |
| 400 |
k.addExpression(syncCopy(vec1, loc1, cGroupSize * groupID, c0, cGroupSize)); |
| 401 |
k.addExpression(barrier()); |
| 402 |
k.addExpression(operatorAssignment(loc1, loc1 - loc0)); |
| 403 |
k.addExpression(barrier("CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE")); |
| 404 |
k.addExpression(syncCopy(loc1, vec1, c0, cGroupSize * groupID, cGroupSize)); |
| 405 |
} |
| 406 |
k.setup(); |
| 407 |
|
| 408 |
|
| 409 |
k.compute(); |
| 410 |
copy(vec1, output); |
| 411 |
|
| 412 |
bool status(output == expected); |
| 413 |
errorMessage(status); |
| 414 |
|
| 415 |
return status; |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
int main() |
| 421 |
{ |
| 422 |
bool allTestsPassed(true); |
| 423 |
|
| 424 |
allTestsPassed &= testCopy(); |
| 425 |
allTestsPassed &= testKernel(); |
| 426 |
allTestsPassed &= testKernelSIMD(); |
| 427 |
allTestsPassed &= testKernelSIMDUA(); |
| 428 |
allTestsPassed &= testPrivateVariable(); |
| 429 |
allTestsPassed &= testPrivateArray(); |
| 430 |
allTestsPassed &= testVariable(); |
| 431 |
allTestsPassed &= testVariableReference(); |
| 432 |
allTestsPassed &= testSelect(); |
| 433 |
allTestsPassed &= testSwapBuffers(); |
| 434 |
allTestsPassed &= testLocalArray(); |
| 435 |
allTestsPassed &= testSubvector(); |
| 436 |
|
| 437 |
return allTestsPassed ? EXIT_SUCCESS : EXIT_FAILURE; |
| 438 |
} |