| 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 |
#ifndef ACLARRAY_H |
| 25 |
#define ACLARRAY_H |
| 26 |
|
| 27 |
#include "../aclStdIncludes.h" |
| 28 |
#include "aclMemBlock.h" |
| 29 |
|
| 30 |
using namespace asl; |
| 31 |
|
| 32 |
namespace acl |
| 33 |
{ |
| 34 |
|
| 35 |
/// Global array |
| 36 |
template <typename T> class Array: public MemBlock |
| 37 |
{ |
| 38 |
protected: |
| 39 |
string name; |
| 40 |
static const string prefix; |
| 41 |
static unsigned int id; |
| 42 |
public: |
| 43 |
explicit Array(unsigned int size, CommandQueue queue_ = hardware.defaultQueue); |
| 44 |
Array(unsigned int size, T *initArray, CommandQueue queue_ = hardware.defaultQueue); |
| 45 |
virtual string str(const KernelConfiguration & kernelConfig) const; |
| 46 |
virtual string getName() const; |
| 47 |
virtual string getAddressSpaceQualifier() const; |
| 48 |
virtual string getTypeSignature(const KernelConfiguration & kernelConfig) const; |
| 49 |
virtual string getLocalDeclaration(const KernelConfiguration & kernelConfig) const; |
| 50 |
virtual void addToKernelSource(vector<Element> & arguments, |
| 51 |
vector<Element> & localDeclarations) const; |
| 52 |
virtual void setAsArgument(cl::Kernel & kernel, unsigned int argumentIndex) const; |
| 53 |
/// swaps buffer values of this vector and vector \p a; |
| 54 |
}; |
| 55 |
|
| 56 |
typedef shared_ptr<Array<cl_int> > ElementArrayInt; |
| 57 |
typedef shared_ptr<Array<cl_float> > ElementArrayFloat; |
| 58 |
typedef shared_ptr<Array<cl_double> > ElementArrayDouble; |
| 59 |
typedef shared_ptr<Array<cl_long> > ElementArrayLong; |
| 60 |
|
| 61 |
//------------------- Implementation ------------------------------- |
| 62 |
|
| 63 |
template <typename T> Array<T>::Array(unsigned int size_, CommandQueue queue_): |
| 64 |
MemBlock(size_, typeToTypeID<T>(), queue_) |
| 65 |
{ |
| 66 |
++id; |
| 67 |
name = prefix + asl::numToStr(id); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
template <typename T> Array<T>::Array(unsigned int size_, T *initArray, CommandQueue queue_): |
| 72 |
MemBlock(size_, typeToTypeID<T>(), queue_, (char*)initArray) |
| 73 |
{ |
| 74 |
++id; |
| 75 |
name = prefix + asl::numToStr(id); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
template <typename T> string Array<T>::getName() const |
| 81 |
{ |
| 82 |
return name; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
template <typename T> string Array<T>::str(const KernelConfiguration & kernelConfig) const |
| 87 |
{ |
| 88 |
if (kernelConfig.unaligned && kernelConfig.vectorWidth > 1) |
| 89 |
{ |
| 90 |
return "vload" + numToStr(kernelConfig.vectorWidth) + "(0, &" + name + "[" + INDEX + "])"; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
return name + "[" + INDEX + "]"; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
template <typename T> string Array<T>::getTypeSignature(const KernelConfiguration & kernelConfig) const |
| 100 |
{ |
| 101 |
return "__global " + typeToStr<T>(kernelConfig.unaligned ? 1 : kernelConfig.vectorWidth) + " *" + name; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
template <typename T> string Array<T>::getAddressSpaceQualifier() const |
| 106 |
{ |
| 107 |
return "__global"; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
template <typename T> string Array<T>::getLocalDeclaration(const KernelConfiguration & kernelConfig) const |
| 112 |
{ |
| 113 |
return ""; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
// Must be empty. Only operators can add arguments. |
| 118 |
template <typename T> |
| 119 |
void Array<T>::addToKernelSource(vector<Element> & arguments, vector<Element> & localDeclarations) const |
| 120 |
{ |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
template <typename T> |
| 125 |
void Array<T>::setAsArgument(cl::Kernel & kernel, unsigned int argumentIndex) const |
| 126 |
{ |
| 127 |
cl_int status = 0; |
| 128 |
status = kernel.setArg(argumentIndex, *buffer); |
| 129 |
errorMessage(status, "Kernel::setArg() - " + name |
| 130 |
+ ", argument " + numToStr(argumentIndex)); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
} // namespace acl |
| 135 |
|
| 136 |
#endif // ACLARRAY_H |