AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Kernel.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_KERNEL_H
18 #define AGX_KERNEL_H
19 
20 
21 #include <agx/Referenced.h>
22 #include <agx/String.h>
23 #include <agx/Task.h>
24 #include <agxData/Value.h>
25 #include <agx/Vector.h>
26 
27 #ifdef _MSC_VER
28 # pragma warning(push)
29 # pragma warning(disable : 4251 ) // class X needs to have dll-interface to be used by clients of class Y
30 #endif
31 
32 
33 namespace agx
34 {
35  class TiXmlElement;
36 
41 
47  {
48  public:
49  static agx::Model *ClassModel();
50 
51  static Kernel *load(TiXmlElement *eKernel, Device *device);
52  virtual void configure(TiXmlElement *eKernel) override;
53 
54  public:
55  Kernel(const agx::Name& name, agx::Device* device, const agx::Job::Delegate& dispatcher);
56 
60  agx::UInt getNumWorkItems() const;
61 
65  virtual float getComputeTime();
66 
70  agx::UInt getNumJobsPerThread() const;
71 
75  agx::UInt getMinJobSize() const;
76 
80  agx::UInt getMaxJobSize() const;
81 
85  agx::UInt getCostPerElement() const;
86 
90  void setNumJobsPerThread(agx::UInt numJobs);
91 
95  void setMinJobSize(agx::UInt min);
96 
100  void setMaxJobSize(agx::UInt max);
101 
105  void setCostPerElement(agx::UInt cost);
106 
107 
108 
112  // void setSplittingParameter(ArrayParameter *parameter);
113 
114  virtual void preDispatch() {}
115 
116  protected:
117  virtual ~Kernel();
118  virtual void parameterAddedCallback(Parameter* parameter) override;
119  virtual void parameterRemovedCallback(Parameter* parameter) override;
120  void locateNumWorkItemsValue();
122 
123  private:
124  agxData::Val<UInt> m_numJobsPerThread;
125  agxData::Val<UInt> m_minJobSize;
126  agxData::Val<UInt> m_maxJobSize;
127  agxData::Val<UInt> m_costPerElement;
128  // ArrayParameterRef m_splittingParameter;
129  };
130 
131 
132  //---------------------------------------------------------------
133 
138  {
139  public:
141 
142  public:
143  static CpuKernel *load(TiXmlElement *eKernel, Device *device);
144 
145  public:
146  CpuKernel(const agx::Name& name, RangeDispatch rangeDispatch);
147  CpuKernel(const agx::Name& name, Job::Delegate serialDispatch);
148 
149  UInt getNumJobs() const;
150 
151  const RangeDispatch& getRangeDispatch() const;
152 
153  protected:
154  virtual ~CpuKernel();
155  void generateJobs();
156  virtual RangeJob *allocateRangeJobs(size_t numJobs) = 0;
157 
158  protected: // Should be private, is protected for custom job generators
159  // Vector<RangeJob> m_rangeJobs;
162  };
163 
164 
165  //---------------------------------------------------------------
166 
172  {
173  public:
174  typedef std::function<void (const RangeJob&)> RangeCallback;
175  typedef std::function<UInt ()> DataSetSizeCallback;
176 
177  public:
185  LambdaKernel(const agx::Name& name, RangeCallback rangeCallback, DataSetSizeCallback sizeCallback);
186 
187  private:
188  void generateJobs();
189  virtual RangeJob *allocateRangeJobs(size_t numJobs) override;
190  void callOnJobRange(const RangeJob& job);
191 
192  protected:
194 
195  private:
196  DataSetSizeCallback m_sizeCallback;
197  RangeCallback m_lambda;
198  };
199 
200  //---------------------------------------------------------------
201 
202 
203  /* Implementation */
204  // inline const Kernel::ImplementationTable& Kernel::getImplementations() const { return m_implementations; }
205  // inline const Kernel::Implementation& Kernel::getImplementation(const String& name) const { agxAssert(m_implementations.contains(name)); return m_implementations.find(name)->second; }
206  // inline const Kernel::Implementation& Kernel::getActiveImplementation() const { return m_activeImplementation; }
207  AGX_FORCE_INLINE UInt Kernel::getNumJobsPerThread() const { return m_numJobsPerThread; }
208  AGX_FORCE_INLINE UInt Kernel::getMinJobSize() const { return m_minJobSize; }
209  AGX_FORCE_INLINE UInt Kernel::getMaxJobSize() const { return m_maxJobSize; }
210  AGX_FORCE_INLINE UInt Kernel::getCostPerElement() const { return m_costPerElement; }
211 
213  {
214  if (!m_numWorkItems)
215  const_cast<Kernel *>(this)->locateNumWorkItemsValue();
216 
217  return m_numWorkItems->get();
218  }
219 
221 
223 }
224 
225 
226 #ifdef _MSC_VER
227 # pragma warning(pop)
228 #endif
229 
230 #endif /* _AGX_KERNEL_H_ */
ParallelTask.
Definition: Task.h:515
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:880
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
Generalized callback, using std::function.
Definition: Callback.h:53
const RangeDispatch & getRangeDispatch() const
Definition: Kernel.h:220
UInt m_numJobs
Definition: Kernel.h:161
A kernel which executes on the CPU using native C/C++ code.
Definition: Kernel.h:137
Vector< RangeJob > m_rangeJobs
Definition: Kernel.h:193
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agx::UInt getNumWorkItems() const
Definition: Kernel.h:212
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:62
RangeDispatch m_rangeDispatch
Definition: Kernel.h:160
std::function< void(const RangeJob &)> RangeCallback
Definition: Kernel.h:174
A range job executes a kernel implementation on a subset of the data.
Definition: Job.h:282
agx::UInt getNumJobsPerThread() const
Definition: Kernel.h:207
agx::UInt getMaxJobSize() const
Definition: Kernel.h:209
UInt getNumJobs() const
Definition: Kernel.h:222
Templated vector class.
Definition: Vector.h:52
RangeJob::RangeCallback RangeDispatch
Definition: Kernel.h:140
uint32_t UInt
Definition: Integer.h:34
Templated callback with one argument.
Definition: Callback.h:100
A representation of a task/kernel parameter, which can be bound using a string path or explicitly to ...
Definition: Parameter.h:61
A kernel is an abstract representation of data parallel tasks.
Definition: Kernel.h:46
agxData::ValueRefT< UInt > m_numWorkItems
Definition: Kernel.h:121
const T & get() const
Definition: Value.h:448
virtual void preDispatch()
Explicitly set the splitting parameter.
Definition: Kernel.h:114
std::function< UInt()> DataSetSizeCallback
Definition: Kernel.h:175
agx::UInt getCostPerElement() const
Definition: Kernel.h:210
An agx::Device is an abstract representation of a device on which data can be stored and processed...
Definition: Device.h:57
#define AGXCORE_EXPORT
agx::UInt getMinJobSize() const
Definition: Kernel.h:208
void locateNumWorkItemsValue()
AGX_DECLARE_VECTOR_TYPES(Clock)