AGXDynamics  2.35.0.3
IndexLambdaKernel.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 #pragma once
18 
19 #include <agx/Kernel.h>
20 
21 #include <stack>
22 
23 namespace agx
24 {
59  {
60  public:
65  using IndexCallback = std::function<void( agx::UInt )>;
66 
67  public:
76  template<typename CallbackT, typename... Args>
77  static IndexCallback generate( CallbackT callback, Args&&... args );
78 
79  public:
84 
92  template<typename CallbackT, typename... Args>
93  void execute( const agx::Name& name, agx::UInt numElements, CallbackT callback, Args&&... args );
94 
103  template<typename CallbackT, typename... Args>
104  void executeEx( const agx::Name& name, agx::UInt numElements, agx::UInt jobSize, CallbackT callback, Args&&... args );
105 
113  void execute( IndexCallback callback,
114  const agx::Name& name,
115  agx::UInt numElements,
116  agx::UInt jobSize = agx::InvalidIndex );
117 
118  protected:
122  virtual ~IndexLambdaKernel();
123 
124  private:
125  struct ExecData
126  {
127  ExecData( IndexCallback callback, const agx::Name& name, agx::UInt numElements )
128  : callback( callback ), name( name ), numElements( numElements )
129  {
130  }
131 
132  IndexCallback callback;
133  agx::Name name;
134  agx::UInt numElements;
135  };
136  using ExecStack = std::stack<ExecData>;
137 
138  private:
142  agx::UInt getNumElements() const;
143 
148  void executeJob( const agx::RangeJob& job );
149 
155  void executeIndexRange( agx::UInt begin, agx::UInt end );
156 
160  void resetJobSize();
161 
162  private:
163  ExecStack m_execStack;
164  Mutex m_mutex;
165  };
167 
168  template<typename CallbackT, typename... Args>
169  IndexLambdaKernel::IndexCallback IndexLambdaKernel::generate( CallbackT callback, Args&&... args )
170  {
171  // Placeholder for UInt index as first argument.
172  return std::bind( callback, std::placeholders::_1, std::forward<Args>( args )... );
173  }
174 
175  template<typename CallbackT, typename... Args>
176  void IndexLambdaKernel::execute( const agx::Name& name, agx::UInt numElements, CallbackT callback, Args&&... args )
177  {
178  this->executeEx( name, numElements, agx::InvalidIndex, std::forward<CallbackT>( callback ), std::forward<Args>( args )... );
179  }
180 
181  template<typename CallbackT, typename... Args>
182  void IndexLambdaKernel::executeEx( const agx::Name& name, agx::UInt numElements, agx::UInt jobSize, CallbackT callback, Args&&... args )
183  {
184  this->execute( generate( std::forward<CallbackT>( callback ), std::forward<Args>( args )... ), name, numElements, jobSize );
185  }
186 }
agx::IndexLambdaKernel::generate
static IndexCallback generate(CallbackT callback, Args &&... args)
Generates IndexCallback given function and arguments.
Definition: IndexLambdaKernel.h:169
agx::end
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Definition: LinearProbingHashSet.h:612
Kernel.h
agx::Task::execute
void execute()
Execute the task.
agx::LambdaKernel
Definition: Kernel.h:173
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
agx::begin
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Definition: LinearProbingHashSet.h:606
agx::ref_ptr< IndexLambdaKernel >
agx::RangeJob
A range job executes a kernel implementation on a subset of the data.
Definition: Job.h:283
agx::Mutex
std::mutex Mutex
Definition: Referenced.h:93
agx::Name
Representation of a name string.
Definition: Name.h:32
agx::IndexLambdaKernel
Utility entrance to the task system, simplifying parallelization of algorithms.
Definition: IndexLambdaKernel.h:58
agx::IndexLambdaKernel::IndexCallback
std::function< void(agx::UInt)> IndexCallback
Default callback signature with current index.
Definition: IndexLambdaKernel.h:65
agx::UInt
uint32_t UInt
Definition: Integer.h:34
agx::InvalidIndex
const AGXCORE_EXPORT InvalidIndexStruct InvalidIndex
agx::IndexLambdaKernel::executeEx
void executeEx(const agx::Name &name, agx::UInt numElements, agx::UInt jobSize, CallbackT callback, Args &&... args)
Execute callback given name, numElements and args (additional arguments to callback).
Definition: IndexLambdaKernel.h:182