AGXDynamics  2.35.0.2
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:
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::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 
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  virtual void rebind() override;
117 
118  protected:
119  virtual ~Kernel();
120  virtual void parameterAddedCallback(Parameter* parameter) override;
121  virtual void parameterRemovedCallback(Parameter* parameter) override;
122  void locateNumWorkItemsValue();
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_ */
AGXCORE_EXPORT
#define AGXCORE_EXPORT
agx::Parameter
A representation of a task/kernel parameter, which can be bound using a string path or explicitly to ...
Definition: Parameter.h:61
Referenced.h
Vector.h
agx::LambdaKernel::m_rangeJobs
Vector< RangeJob > m_rangeJobs
Definition: Kernel.h:195
agx::Vector
Templated vector class.
Definition: Vector.h:52
agx::Kernel
A kernel is an abstract representation of data parallel tasks.
Definition: Kernel.h:46
Task.h
agx::CpuKernel::RangeDispatch
RangeJob::RangeCallback RangeDispatch
Definition: Kernel.h:142
agx::LambdaKernel
Definition: Kernel.h:173
agx::Kernel::getNumJobsPerThread
agx::UInt getNumJobsPerThread() const
Definition: Kernel.h:209
agx::Kernel::preDispatch
virtual void preDispatch()
Explicitly set the splitting parameter.
Definition: Kernel.h:114
agx::Callback::Callback
Callback()
Definition: Callback.h:268
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::CpuKernel::getRangeDispatch
const RangeDispatch & getRangeDispatch() const
Definition: Kernel.h:222
agx::Kernel::getMinJobSize
agx::UInt getMinJobSize() const
Definition: Kernel.h:210
agx::Model
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
agxData::ValueT::get
const T & get() const
Definition: Value.h:448
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agx::CpuKernel::m_numJobs
UInt m_numJobs
Definition: Kernel.h:163
agx::Kernel::getNumWorkItems
agx::UInt getNumWorkItems() const
Definition: Kernel.h:214
agx::min
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:880
agx::RangeJob
A range job executes a kernel implementation on a subset of the data.
Definition: Job.h:283
agx::Kernel::getMaxJobSize
agx::UInt getMaxJobSize() const
Definition: Kernel.h:211
agx::ParallelTask
ParallelTask.
Definition: Task.h:515
agx::CpuKernel::m_rangeDispatch
RangeDispatch m_rangeDispatch
Definition: Kernel.h:162
agx::Name
Representation of a name string.
Definition: Name.h:32
agx::CpuKernel
A kernel which executes on the CPU using native C/C++ code.
Definition: Kernel.h:139
agx::Callback1
Templated callback with one argument.
Definition: Callback.h:100
AGX_DECLARE_POINTER_TYPES
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
agx::UInt
uint32_t UInt
Definition: Integer.h:34
agxData::ValueRefT< UInt >
agx::Kernel::locateNumWorkItemsValue
void locateNumWorkItemsValue()
agx::CpuKernel::getNumJobs
UInt getNumJobs() const
Definition: Kernel.h:224
agx::Device
An agx::Device is an abstract representation of a device on which data can be stored and processed.
Definition: Device.h:57
String.h
agx::Kernel::getCostPerElement
agx::UInt getCostPerElement() const
Definition: Kernel.h:212
agx::Kernel::m_numWorkItems
agxData::ValueRefT< UInt > m_numWorkItems
Definition: Kernel.h:123
agx::max
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874
agx::LambdaKernel::LambdaCallback
std::function< void(const RangeJob &)> LambdaCallback
Definition: Kernel.h:176
agx::LambdaKernel::DataSetSizeCallback
std::function< UInt()> DataSetSizeCallback
Definition: Kernel.h:177
Value.h
agxData::Val< UInt >
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58