AGX Dynamics  2.35.0.1
Kernel.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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:
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::Callback& 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 
91 
96 
101 
106 
107 
108 
112  // void setSplittingParameter(ArrayParameter *parameter);
113 
114  virtual void preDispatch() {}
115 
116  virtual void rebind() override;
117 
118  protected:
119  virtual ~Kernel();
120  virtual void parameterAddedCallback(Parameter* parameter) override;
121  virtual void parameterRemovedCallback(Parameter* parameter) override;
124 
125  private:
126  agxData::Val<UInt> m_numJobsPerThread;
127  agxData::Val<UInt> m_minJobSize;
128  agxData::Val<UInt> m_maxJobSize;
129  agxData::Val<UInt> m_costPerElement;
130  // ArrayParameterRef m_splittingParameter;
131  };
132 
133 
134  //---------------------------------------------------------------
135 
140  {
141  public:
143 
144  public:
145  static CpuKernel *load(TiXmlElement *eKernel, Device *device);
146 
147  public:
148  CpuKernel(const agx::Name& name, RangeDispatch rangeDispatch);
149  CpuKernel(const agx::Name& name, Job::Callback serialDispatch);
150 
151  UInt getNumJobs() const;
152 
153  const RangeDispatch& getRangeDispatch() const;
154 
155  protected:
156  virtual ~CpuKernel();
157  void generateJobs();
158  virtual RangeJob *allocateRangeJobs(size_t numJobs) = 0;
159 
160  protected: // Should be private, is protected for custom job generators
161  // Vector<RangeJob> m_rangeJobs;
164  };
165 
166 
167  //---------------------------------------------------------------
168 
174  {
175  public:
176  typedef std::function<void (const RangeJob&)> LambdaCallback;
177  typedef std::function<UInt ()> DataSetSizeCallback;
178 
179  public:
187  LambdaKernel(const agx::Name& name, LambdaCallback lambdaCallback, DataSetSizeCallback sizeCallback);
188 
189  private:
190  void generateJobs();
191  virtual RangeJob *allocateRangeJobs(size_t numJobs) override;
192  void callOnJobRange(const RangeJob& job);
193 
194  protected:
196 
197  private:
198  DataSetSizeCallback m_sizeCallback;
199  LambdaCallback m_lambda;
200  };
201 
202  //---------------------------------------------------------------
203 
204 
205  /* Implementation */
206  // inline const Kernel::ImplementationTable& Kernel::getImplementations() const { return m_implementations; }
207  // inline const Kernel::Implementation& Kernel::getImplementation(const String& name) const { agxAssert(m_implementations.contains(name)); return m_implementations.find(name)->second; }
208  // inline const Kernel::Implementation& Kernel::getActiveImplementation() const { return m_activeImplementation; }
209  AGX_FORCE_INLINE UInt Kernel::getNumJobsPerThread() const { return m_numJobsPerThread; }
210  AGX_FORCE_INLINE UInt Kernel::getMinJobSize() const { return m_minJobSize; }
211  AGX_FORCE_INLINE UInt Kernel::getMaxJobSize() const { return m_maxJobSize; }
212  AGX_FORCE_INLINE UInt Kernel::getCostPerElement() const { return m_costPerElement; }
213 
215  {
216  if (!m_numWorkItems)
217  const_cast<Kernel *>(this)->locateNumWorkItemsValue();
218 
219  return m_numWorkItems->get();
220  }
221 
223 
225 }
226 
227 
228 #ifdef _MSC_VER
229 # pragma warning(pop)
230 #endif
231 
232 #endif /* _AGX_KERNEL_H_ */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXCORE_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
const T & get() const
Definition: Value.h:448
Templated callback with one argument.
Definition: Callback.h:101
A kernel which executes on the CPU using native C/C++ code.
Definition: Kernel.h:140
const RangeDispatch & getRangeDispatch() const
Definition: Kernel.h:222
UInt m_numJobs
Definition: Kernel.h:163
void generateJobs()
RangeDispatch m_rangeDispatch
Definition: Kernel.h:162
CpuKernel(const agx::Name &name, RangeDispatch rangeDispatch)
RangeJob::RangeCallback RangeDispatch
Definition: Kernel.h:142
virtual ~CpuKernel()
virtual RangeJob * allocateRangeJobs(size_t numJobs)=0
static CpuKernel * load(TiXmlElement *eKernel, Device *device)
CpuKernel(const agx::Name &name, Job::Callback serialDispatch)
UInt getNumJobs() const
Definition: Kernel.h:224
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:58
A kernel is an abstract representation of data parallel tasks.
Definition: Kernel.h:47
static agx::Model * ClassModel()
agxData::ValueRefT< UInt > m_numWorkItems
Definition: Kernel.h:123
void setCostPerElement(agx::UInt cost)
Set the per-element cost estimate.
virtual float getComputeTime()
void setNumJobsPerThread(agx::UInt numJobs)
Set the number of jobs per thread.
void setMinJobSize(agx::UInt min)
Set the minimum number of work elements per job.
virtual void parameterAddedCallback(Parameter *parameter) override
agx::UInt getMaxJobSize() const
Definition: Kernel.h:211
Kernel(const agx::Name &name, agx::Device *device, const agx::Job::Callback &dispatcher)
virtual void rebind() override
agx::UInt getNumJobsPerThread() const
Definition: Kernel.h:209
static Kernel * load(TiXmlElement *eKernel, Device *device)
agx::UInt getNumWorkItems() const
Definition: Kernel.h:214
agx::UInt getCostPerElement() const
Definition: Kernel.h:212
void setMaxJobSize(agx::UInt max)
Set the maximum number of work elements per job.
virtual void parameterRemovedCallback(Parameter *parameter) override
virtual void preDispatch()
Explicitly set the splitting parameter.
Definition: Kernel.h:114
virtual ~Kernel()
void locateNumWorkItemsValue()
agx::UInt getMinJobSize() const
Definition: Kernel.h:210
virtual void configure(TiXmlElement *eKernel) override
std::function< void(const RangeJob &)> LambdaCallback
Definition: Kernel.h:176
Vector< RangeJob > m_rangeJobs
Definition: Kernel.h:195
LambdaKernel(const agx::Name &name, LambdaCallback lambdaCallback, DataSetSizeCallback sizeCallback)
Constructor.
std::function< UInt()> DataSetSizeCallback
Definition: Kernel.h:177
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:41
Representation of a name string.
Definition: Name.h:33
ParallelTask.
Definition: Task.h:516
A representation of a task/kernel parameter, which can be bound using a string path or explicitly to ...
Definition: Parameter.h:62
A range job executes a kernel implementation on a subset of the data.
Definition: Job.h:284
Templated vector class.
Definition: agx/Vector.h:53
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:880
uint32_t UInt
Definition: Integer.h:34
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874