| 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 ACLKERNEL_H |
| 25 |
#define ACLKERNEL_H |
| 26 |
|
| 27 |
#include "aclExpressionContainer.h" |
| 28 |
#include "aclKernelConfiguration.h" |
| 29 |
//#include <CL/cl.hpp> |
| 30 |
// Supply "cl.hpp" with ASL, since it is not present in OpenCL 2.0 |
| 31 |
// Remove the file after switching to OpenCL 2.1 |
| 32 |
#include "acl/cl.hpp" |
| 33 |
|
| 34 |
namespace acl |
| 35 |
{ |
| 36 |
|
| 37 |
extern const KernelConfiguration KERNEL_BASIC; |
| 38 |
|
| 39 |
/// OpenCl Kernel generator |
| 40 |
/** |
| 41 |
\ingroup KernelGen |
| 42 |
\listedNote The kernel can be run without updating of parameters. |
| 43 |
This possibility can lead to some optimization. |
| 44 |
to realize this possibility the function |
| 45 |
\p computeWithoutUpdate can be added |
| 46 |
|
| 47 |
*/ |
| 48 |
class Kernel: public ExpressionContainer |
| 49 |
{ |
| 50 |
protected: |
| 51 |
static unsigned int kernelNum; |
| 52 |
unsigned int id; |
| 53 |
unsigned int groupsNumber; |
| 54 |
KernelConfiguration kernelConfig; |
| 55 |
std::string kernelSource; |
| 56 |
cl::Kernel kernel; |
| 57 |
|
| 58 |
/// detects minimal vector width of all available types of Elements |
| 59 |
cl_uint detectVectorWidth(); |
| 60 |
void generateExtensions(); |
| 61 |
void generateArguments(); |
| 62 |
void generateIndex(); |
| 63 |
void generateLocalDeclarations(); |
| 64 |
void generateExpressions(); |
| 65 |
virtual void generateKernelSource(); |
| 66 |
void updateKernelConfiguration(); |
| 67 |
void buildKernel(); |
| 68 |
void setKernelArguments(); |
| 69 |
public: |
| 70 |
explicit Kernel(const KernelConfiguration kernelConfig_ = KERNEL_BASIC); |
| 71 |
/// Prepares kernel for launch. |
| 72 |
/// Should always be called before compute() after all expressions are added. |
| 73 |
/// Generates kernel source, builds kernel and sets its arguments. |
| 74 |
void setup(); |
| 75 |
void compute(); |
| 76 |
void setGroupsNumber(unsigned int n); |
| 77 |
unsigned int getGroupsNumber(); |
| 78 |
std::string getKernelSource(); |
| 79 |
unsigned int getKernelID(); |
| 80 |
const cl::Kernel & getKernel() const; |
| 81 |
/// removes all expressions from the kernel |
| 82 |
void clear(); |
| 83 |
inline const KernelConfiguration & getConfiguration()const; |
| 84 |
|
| 85 |
friend class KernelMerger; |
| 86 |
}; |
| 87 |
|
| 88 |
/// \related Kernel |
| 89 |
typedef std::shared_ptr<Kernel> SPKernel; |
| 90 |
|
| 91 |
/// creates \p n kernels in \p vk with configuration \p kernelConfig_ |
| 92 |
/// \related Kernel |
| 93 |
void inline createKernels(std::vector<SPKernel> & vk, |
| 94 |
unsigned int n, |
| 95 |
const KernelConfiguration & kernelConfig_ = KERNEL_BASIC); |
| 96 |
|
| 97 |
|
| 98 |
//---------------------------- Implementation ----------------------------- |
| 99 |
|
| 100 |
void inline createKernels(std::vector<SPKernel> & vk, |
| 101 |
unsigned int n, |
| 102 |
const KernelConfiguration & kernelConfig_) |
| 103 |
{ |
| 104 |
vk.resize(n); |
| 105 |
for (unsigned int i(0); i < n; ++i) |
| 106 |
{ |
| 107 |
vk[i].reset(new Kernel(kernelConfig_)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
inline const KernelConfiguration & Kernel::getConfiguration()const |
| 112 |
{ |
| 113 |
return kernelConfig; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
} // namespace acl |
| 118 |
|
| 119 |
#endif // ACLKERNEL_H |