Debian Codegraph @

src/acl/DataTypes/aclSubvector.h

raw
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 ACLSUBVECTOR_H
25 #define ACLSUBVECTOR_H
26
27 //#include <CL/cl.hpp>
28 // Supply "cl.hpp" with ASL, since it is not present in OpenCL 2.0
29 // Remove the file after switching to OpenCL 2.1
30 #include "acl/cl.hpp"
31 #include "aclArray.h"
32 #include "aslUtilities.h"
33
34 ///\todo{Add padding at the end?}
35
36 namespace acl
37 {
38
39 template <typename T> class Subvector: public MemBlock
40 {
41 private:
42 string name;
43 static const string prefix;
44 static unsigned int id;
45 unsigned int offset;
46 shared_ptr<Array<T> > vector;
47 cl_buffer_region buffer_create_info;
48 public:
49 Subvector(shared_ptr<Array<T> > vector_,
50 unsigned int size_,
51 unsigned int offset_);
52 virtual cl::Buffer &getBuffer();
53 virtual string str(const KernelConfiguration & kernelConfig) const;
54 virtual string getName() const;
55 virtual string getTypeSignature(const KernelConfiguration & kernelConfig) const;
56 virtual string getLocalDeclaration(const KernelConfiguration & kernelConfig) const;
57 virtual void addToKernelSource(std::vector<Element> & arguments,
58 std::vector<Element> & localDeclarations) const;
59 virtual void setAsArgument(cl::Kernel & kernel, unsigned int argumentIndex) const;
60 };
61
62
63
64 //---------------------------- Implementations------------------------
65
66 template <typename T> Subvector<T>::Subvector(shared_ptr<Array<T> > vector_,
67 unsigned int size_,
68 unsigned int offset_):
69 MemBlock(),
70 offset(offset_),
71 vector(vector_)
72 {
73 size = size_;
74 queue = vector_->getQueue();
75 if ( (offset + size) > vector->getSize() )
76 {
77 errorMessage("Subvector::Subvector() - (offset + size) > vector->getSize()");
78 }
79 else
80 {
81 buffer_create_info.origin = offset * sizeof(T);
82 buffer_create_info.size = size * sizeof(T);
83 }
84
85 ++id;
86 name = prefix + asl::numToStr(id);
87 }
88
89
90 template <typename T> cl::Buffer &Subvector<T>::getBuffer()
91 {
92 cl_int status = 0;
93 /// if original buffer is moved within GPU memory
94 *buffer = vector->getBuffer().createSubBuffer(CL_MEM_READ_WRITE,
95 CL_BUFFER_CREATE_TYPE_REGION,
96 &buffer_create_info,
97 &status);
98 errorMessage(status, "Subvector::Subvector() - createSubBuffer()");
99
100 return *buffer;
101 }
102
103
104 template <typename T> string Subvector<T>::getName() const
105 {
106 return name;
107 }
108
109
110 template <typename T> string Subvector<T>::str(const KernelConfiguration & kernelConfig) const
111 {
112 return name + "[" + INDEX + "]";
113 }
114
115
116 template <typename T> string Subvector<T>::getTypeSignature(const KernelConfiguration & kernelConfig) const
117 {
118 return "__global " + typeToStr<T>(kernelConfig.vectorWidth) + " *" + name;
119 }
120
121
122 template <typename T> string Subvector<T>::getLocalDeclaration(const KernelConfiguration & kernelConfig) const
123 {
124 return "";
125 }
126
127
128 // Must be empty. Only operators can add arguments.
129 template <typename T>
130 void Subvector<T>::addToKernelSource(std::vector<Element> & arguments,
131 std::vector<Element> & localDeclarations) const
132 {
133 }
134
135
136 template <typename T>
137 void Subvector<T>::setAsArgument(cl::Kernel & kernel, unsigned int argumentIndex) const
138 {
139 cl_int status = 0;
140 status = kernel.setArg(argumentIndex, *buffer);
141 errorMessage(status, "Kernel::setArg() - " + name + ", argument " + numToStr(argumentIndex));
142 }
143
144 } // namespace acl
145
146 #endif // ACLSUBVECTOR_H