ASL  0.1.7
Advanced Simulation Library
aclSubvector.h
Go to the documentation of this file.
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 
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;
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
virtual cl::Buffer & getBuffer()
Definition: aclSubvector.h:90
virtual string getName() const
Definition: aclSubvector.h:104
Advanced Computational Language.
Definition: acl.h:40
virtual void addToKernelSource(std::vector< Element > &arguments, std::vector< Element > &localDeclarations) const
Definition: aclSubvector.h:130
void errorMessage(cl_int status, const char *errorMessage)
Prints errorMessage and exits depending on the status.
std::string numToStr(T i)
Converts numbers or another type to string.
Definition: aslUtilities.h:48
virtual string str(const KernelConfiguration &kernelConfig) const
Definition: aclSubvector.h:110
C++ bindings for OpenCL 1.0 (rev 48), OpenCL 1.1 (rev 33) and OpenCL 1.2 (rev 15)
Subvector(shared_ptr< Array< T > > vector_, unsigned int size_, unsigned int offset_)
Definition: aclSubvector.h:66
const Block offset(const Block &bl, int a=1)
virtual void setAsArgument(cl::Kernel &kernel, unsigned int argumentIndex) const
Definition: aclSubvector.h:137
ACL Kernel configuration class.
virtual string getLocalDeclaration(const KernelConfiguration &kernelConfig) const
Definition: aclSubvector.h:122
CommandQueue queue
unsigned int size
Class interface for Buffer Memory Objects.
Definition: cl.hpp:3017
Global array.
Definition: acl.h:47
Class interface for cl_kernel.
Definition: cl.hpp:4721
useful common utilities
virtual string getTypeSignature(const KernelConfiguration &kernelConfig) const
Definition: aclSubvector.h:116
cl_int setArg(cl_uint index, const T &value)
Definition: cl.hpp:4845
const std::string INDEX
Definition: aclUtilities.h:39