AGXDynamics  2.31.1.0
Job.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_JOB_H
18 #define AGX_JOB_H
19 
20 #ifdef _MSC_VER
21 # pragma warning(push)
22 # pragma warning(disable:4355) // Disable warnings about passing the 'this' pointer to base class constructors.
23 # pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer
24 #endif
25 
26 #include <agx/agxCore_export.h>
27 #include <agx/IndexRange.h>
28 #include <agx/Delegate.h>
29 
30 #include <agx/Name.h>
31 #include <agxData/Type.h>
32 #include <agx/Event.h>
33 #include <agx/Vector.h>
34 #include <agx/Timer.h>
36 #include <agx/SyncTag.h>
37 #include <deque>
38 
39 
40 
41 
42 namespace agx
43 {
44  class Device;
45  class Task;
46  class Thread;
47 
48  typedef class Job* JobPtr;
49 
50 
52  typedef std::deque<class Job *> JobPtrQueue;
53 
59  class AGXCORE_EXPORT Job : public Callback
60  {
61  public:
63 
64  Job();
65  Job(const Job& other);
66  Job(const Delegate& callback, agx::UInt32 costEstimate = 100);
67  virtual ~Job();
68 
69  Job& operator=(const Job& other);
70 
72  void init(const Delegate& callback = Delegate(), agx::UInt32 costEstimate = 100);
73 
78  void addDependency(Job *parent);
79 
84  void removeDependency(Job *parent);
85 
90  bool hasDependency(Job *parent);
91 
96  void removeAllDependencies();
97 
102  void clearChildDependencies();
103 
108  void setNumDependencies(agx::UInt32 numDependencies);
109 
113  agx::UInt32 getNumDependencies() const;
114 
118  void incrementNumDependencies(agx::UInt32 size = 1);
119 
123  void decrementNumDependencies(agx::UInt32 size = 1);
124 
128  const JobPtrVector& getChildDependencies() const;
129 
133  const JobPtrVector& getParentDependencies() const;
134 
138  void setCallback(const Delegate& callback);
139 
143  const Delegate& getCallback() const;
144 
150  bool resolve();
151 
155  void spawn();
156 
160  Thread *getTargetThread();
161 
165  void setTargetThread(Thread *thread);
166 
170  Task *getTask();
171 
175  void setTask(Task *task);
176 
180  SyncTag *getSyncTag();
181 
185  void setSyncTag(SyncTag *tag);
186 
190  Real getExecutionTime() const;
191 
195  const Timer& getTimer() const;
196 
200  void setCostEstimate(agx::UInt32 cost);
201 
205  agx::UInt32 getCostEstimate() const;
206 
210  Thread *getAssignedThread();
211 
212 
216  agx::UInt32 getTag() const;
217 
221  void setTag(agx::UInt32 tag);
222 
226  void setName(const char *name);
227 
231  const char *getName() const;
232 
233 
237  agx::Index& index();
238  const agx::Index& index() const;
239 
240  public:
241 
245  virtual void implementation();
246 
247  private:
252  void execute(JobPtrVector& resolvedChildren, Thread* thread);
253 
254  protected:
255  friend class Thread;
256  friend class JobGroup;
257 
258  const char *m_name;
259  JobPtrVector m_children;
260  JobPtrVector m_parents;
264  std::atomic<Int32> m_dependencyCounter;
268  SyncTagRef m_syncTag;
272  };
273 
275 
276  //---------------------------------------------------------------
277 
282  class AGXCORE_EXPORT RangeJob : public Job
283  {
284  public:
286 
287  public:
288  RangeJob();
289  RangeJob(const RangeCallback& callback, const IndexRange& range, agx::UInt32 costPerElement = 1);
290  RangeJob(const RangeJob& other);
291  virtual ~RangeJob();
292 
293  void init(const RangeCallback& callback, const IndexRange& range, agx::UInt32 costPerElement = 1);
294  void init(const IndexRange& range, agx::UInt32 costPerElement = 1);
295 
296  RangeJob& operator= (const RangeJob& other);
297 
301  IndexRange& range();
302  const IndexRange& range() const;
303 
304  protected:
305  virtual void implementation() override;
306 
307  private:
308  // void dispatch();
309 
310  protected:
313  };
314 
316 
317  //---------------------------------------------------------------
318 
319  template <class T>
320  class RangeJobT : public RangeJob
321  {
322  public:
323  RangeJobT();
324  RangeJobT(const RangeCallback& callback, const IndexRange& range, agx::UInt32 costPerElement = 1);
325  RangeJobT(const RangeJobT<T>& other);
326 
327  virtual ~RangeJobT();
328 
329  protected:
330  virtual void implementation() override;
331  };
332 
333  //---------------------------------------------------------------
334 
339  class AGXCORE_EXPORT JobGroup : public Job
340  {
341  public:
342  JobGroup();
343  virtual ~JobGroup();
344 
345  void init();
346 
347  void addJob(Job *job);
348  const JobPtrVector& getSubJobs() const;
349 
350  // void execute();
351 
352  protected:
353  virtual void implementation() override;
354 
355  private:
356  JobPtrVector m_jobs;
357  };
358 
359  //---------------------------------------------------------------
360 
364  class AGXCORE_EXPORT WaitJob : public Job
365  {
366  public:
367  WaitJob(Job *job);
368  virtual ~WaitJob();
369 
373  void wait();
374 
378  void release();
379 
383  Job *getTarget();
384  const Job *getTarget() const;
385 
386  private:
387  Job *m_target;
388  Block m_block;
389  };
390 
391  //---------------------------------------------------------------
392 
396  class AGXCORE_EXPORT TaskJob : public Job
397  {
398  public:
399  TaskJob();
400  TaskJob(Task *task);
401  virtual ~TaskJob();
402 
403  void init(Task *task);
404  void init();
405 
406  void execute();
407 
408  private:
409  Task *m_target;
410  };
411 
412 
413 
414 
415 
416  /* Implementation */
417 
420  Callback(&Job::spawn, this),
421  m_name(""),
422  m_numDependencies(0),
423  m_dependencyCounter(0),
424  m_task(nullptr),
425  m_targetThread(nullptr),
426  m_assignedThread(nullptr),
427  m_cost(0),
428  m_tag(0),
429  m_index(InvalidIndex)
430  {
431  }
432 
433  AGX_FORCE_INLINE Job::Job(const Job& other) :
434  Callback(&Job::spawn, this),
435  m_name(other.m_name),
436  m_children(other.m_children),
437  m_parents(other.m_parents),
438  m_callback(other.m_callback),
439  m_timer(other.m_timer),
442  m_task(other.m_task),
445  m_syncTag(other.m_syncTag),
446  m_cost(other.m_cost),
447  m_tag(other.m_tag),
448  m_index(other.m_index)
449  {
450  }
451 
452 
453 
454  AGX_FORCE_INLINE Job::Job(const Delegate& callback, agx::UInt32 costEstimate) :
455  Callback(&Job::spawn, this),
456  m_name(""),
457  m_callback(callback),
460  m_task(nullptr),
461  m_targetThread(nullptr),
462  m_assignedThread(nullptr),
463  m_cost(costEstimate),
464  m_tag(0),
466  {
467  }
468 
470  {
471  m_name = other.m_name;
472  m_children = other.m_children;
473  m_parents = other.m_parents;
474  m_callback = other.m_callback;
475  m_timer = other.m_timer;
478  m_task = other.m_task;
481  m_syncTag = other.m_syncTag;
482  m_cost = other.m_cost;
483  m_tag = other.m_tag;
484  m_index = other.m_index;
485 
486  return *this;
487  }
488 
489  AGX_FORCE_INLINE const char *Job::getName() const { return m_name; }
490  AGX_FORCE_INLINE void Job::setName(const char *name) { m_name = name; }
491 
495  AGX_FORCE_INLINE void Job::setTask(Task *task) { m_task = task; }
497  AGX_FORCE_INLINE void Job::setSyncTag(SyncTag *tag) { m_syncTag = tag; }
499 
502 
503 
505  {
506  agxAssert(parent);
507  // agxAssert(!parent->m_children.contains(this));
508  parent->m_children.push_back(this);
510  #ifdef AGX_DEBUG
511  m_parents.push_back(parent);
512  #endif
513  }
514 
515 
517  {
518  agxAssert(parent);
519  // agxAssert(parent->m_children.contains(this));
520  if (parent->m_children.findAndErase(this)) {
522  #ifdef AGX_DEBUG
524  #endif
525  }
526  }
527 
528 
530  {
531  agxAssert(parent);
532  return parent->m_children.contains(this);
533  }
534 
535  AGX_FORCE_INLINE const JobPtrVector& Job::getParentDependencies() const { return m_parents; }
536 
538  AGX_FORCE_INLINE void Job::setNumDependencies(agx::UInt32 numDependencies) { m_numDependencies = numDependencies; }
540  {
541  m_numDependencies = 0;
542  #ifdef AGX_DEBUG
543  m_parents.clear();
544  #endif
545  }
546 
550 
551  AGX_FORCE_INLINE const JobPtrVector& Job::getChildDependencies() const { return m_children; }
552 
553  AGX_FORCE_INLINE void Job::setCallback(const Delegate& callback) { m_callback = callback; }
555 
556 
557  AGX_FORCE_INLINE void Job::init(const Delegate& callback, agx::UInt32 costEstimate)
558  {
559  this->setNumDependencies(0);
560  m_children.clear();
561 
562  #ifdef AGX_DEBUG
563  m_parents.clear();
564  #endif
565 
566  m_callback = callback;
567  m_cost = costEstimate;
568  m_targetThread = nullptr;
569  m_assignedThread = nullptr;
570  m_tag = 0;
571  m_index = 0;
572  }
573 
575  AGX_FORCE_INLINE const Timer& Job::getTimer() const { return m_timer; }
578 
580  AGX_FORCE_INLINE const agx::Index& Job::index() const { return m_index; }
581 
582 
584 
585  AGX_FORCE_INLINE JobGroup::JobGroup() // : Job(delegate(&JobGroup::execute, this))
586  {
587  }
588 
590  {
591  // Job::init(delegate(&JobGroup::execute, this));
592  Job::init(Delegate());
593  m_jobs.clear();
594  m_cost = 0;
595  }
596 
598  {
599  m_jobs.push_back(job);
600  m_cost += job->getCostEstimate();
601  }
602 
603  AGX_FORCE_INLINE const JobPtrVector& JobGroup::getSubJobs() const
604  {
605  return m_jobs;
606  }
607 
608 
609 
611  AGX_FORCE_INLINE RangeJob::RangeJob() // : Job(Callback(&RangeJob::dispatch, this))
612  {}
613 
614  AGX_FORCE_INLINE RangeJob::RangeJob(const RangeCallback& callback, const IndexRange& range, agx::UInt32 costPerElement) // : Job(Callback(&RangeJob::dispatch, this))
615  {
616  this->init(callback, range, costPerElement);
617  }
618 
619  AGX_FORCE_INLINE RangeJob::RangeJob(const RangeJob& other) : Job(other) // : Job(Callback(&RangeJob::dispatch, this))
620  {
621  *this = other;
622  }
623 
625  {
626  this->init(other.m_rangeCallback, other.m_range);
627  return *this;
628  }
629 
632 
633 
634  AGX_FORCE_INLINE void RangeJob::init(const RangeCallback& callback, const IndexRange& range, agx::UInt32 costPerElement)
635  {
636  m_range = range;
637  m_rangeCallback = callback;
638 
639  Job::init(Delegate(), (agx::UInt32)m_range.size() * costPerElement);
640  }
641 
643  {
644  m_range = range;
645  Job::init(Delegate(), (agx::UInt32)m_range.size() * costPerElement);
646  }
647 
649 
650  template <class T>
652  {}
653 
654  template <class T>
656  : RangeJob(callback, range, costPerElement)
657  {}
658 
659  template <class T>
661  {}
662 
663  template <class T>
665  {
666  }
667 
668  template <class T>
670  {
671  agxAssert(dynamic_cast<T *>(m_task));
672  static_cast<T *>(m_task)->dispatch(*this);
673  }
674 
676  AGX_FORCE_INLINE TaskJob::TaskJob() : Job(Delegate(&TaskJob::execute, this)), m_target(nullptr)
677  {
678  }
679 
680  AGX_FORCE_INLINE TaskJob::TaskJob(Task* task) : Job(Delegate(&TaskJob::execute, this)), m_target(task)
681  {
682  }
683 
685  {
687  m_target = task;
688  }
689 
691  {
693  }
694 
696 
697  AGX_FORCE_INLINE Job *WaitJob::getTarget() { return m_target; }
698  AGX_FORCE_INLINE const Job *WaitJob::getTarget() const { return m_target; }
699 
700 }
701 
703 AGX_TYPE_BINDING(agx::RangeJob, "RangeJob")
704 AGX_TYPE_BINDING(agx::JobGroup, "JobGroup")
705 AGX_TYPE_BINDING(agx::TaskJob, "TaskJob")
706 
707 
708 #ifdef _MSC_VER
709 # pragma warning(pop)
710 #endif
711 
712 #endif /* _AGX_JOB_H_ */
void AGXPHYSICS_EXPORT init()
Initialize agx.
IndexRange & range()
Definition: Job.h:630
Real64 getTime() const
Report total elapsed time since start in milliseconds, excluding intervals when the timer was suspend...
Definition: Timer.h:224
void push_back(const T &value)
Definition: Vector.h:1362
agx::Thread is a representation of an OS specific implementation of a computational thread...
Definition: Thread.h:197
void execute()
JobPtrVector m_children
Definition: Job.h:259
Job * getTarget()
Definition: Job.h:697
RangeJobT()
Definition: Job.h:651
Job()
Definition: Job.h:419
agx::Callback1< const RangeJob & > RangeDelegate
Definition: Delegate.h:46
An abstract job/workblock representation, which allows work threads to execute arbitrary tasks...
Definition: Job.h:59
void init(const Delegate &callback=Delegate(), agx::UInt32 costEstimate=100)
Initialize the job.
Definition: Job.h:557
Timer m_timer
Definition: Job.h:262
A job used as a synchronization utility when waiting for another job to complete. ...
Definition: Job.h:364
void setCostEstimate(agx::UInt32 cost)
Specify the cost estimate for this job.
Definition: Job.h:577
Generalized callback, using std::function.
Definition: Callback.h:53
void removeAllDependencies()
Remove all parent dependencies.
Definition: Job.h:539
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
agx::UInt32 getTag() const
Definition: Job.h:500
void setTargetThread(Thread *thread)
Set the target thread which the job will be executed on.
Definition: Job.h:493
virtual ~RangeJobT()
Definition: Job.h:664
bool hasDependency(Job *parent)
Definition: Job.h:529
AGXCORE_EXPORT const InvalidIndexStruct InvalidIndex
const JobPtrVector & getChildDependencies() const
Definition: Job.h:551
void setTag(agx::UInt32 tag)
Set the job tag, can be used to keep track of different job classes etc.
Definition: Job.h:501
Vector< Job > JobVector
Definition: Job.h:274
#define agxVerify(expr)
Definition: debug.h:131
A representation of a generic task.
Definition: Task.h:57
Delegate m_callback
Definition: Job.h:261
#define AGX_FORCE_INLINE
Definition: Object.h:63
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:73
const JobPtrVector & getSubJobs() const
Definition: Job.h:603
class Job * JobPtr
Definition: Job.h:48
RangeJob()
Definition: Job.h:611
Block synchronization primitive.
A range job executes a kernel implementation on a subset of the data.
Definition: Job.h:282
SyncTagRef m_syncTag
Definition: Job.h:268
JobPtrVector m_parents
Definition: Job.h:260
agx::Index m_index
Definition: Job.h:271
void removeDependency(Job *parent)
Remove a dependency from a job.
Definition: Job.h:516
const JobPtrVector & getParentDependencies() const
Definition: Job.h:535
Task * getTask()
Definition: Job.h:494
VectorPOD< class Job * > JobPtrVector
Definition: Job.h:51
Templated vector class.
Definition: Vector.h:52
void setCallback(const Delegate &callback)
Set the callback which is called when execute() runs.
Definition: Job.h:553
JobGroup()
Definition: Job.h:585
const Timer & getTimer() const
Definition: Job.h:575
Real getExecutionTime() const
Definition: Job.h:574
Thread * getAssignedThread()
Definition: Job.h:498
RangeJob & operator=(const RangeJob &other)
Definition: Job.h:624
A task job is a job which executes an agx::Task.
Definition: Job.h:396
Job & operator=(const Job &other)
Definition: Job.h:469
const char * getName() const
Definition: Job.h:489
void init()
Definition: Job.h:690
virtual void implementation() override
Job implementation, override in subclass.
Definition: Job.h:669
agx::UInt32 getNumDependencies() const
Definition: Job.h:547
A group of jobs to be executed.
Definition: Job.h:339
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
Templated callback with one argument.
Definition: Callback.h:100
void setNumDependencies(agx::UInt32 numDependencies)
Explicitly set the number of parent dependencies.
Definition: Job.h:538
IndexRange m_range
Definition: Job.h:312
void setName(const char *name)
Set the name of the job, for debugging.
Definition: Job.h:490
bool findAndErase(const T &element, bool searchMultiple=false)
Find and erase an element.
Definition: Vector.h:1478
UInt32 Index
Definition: Integer.h:52
agx::Index & index()
Definition: Job.h:579
void incrementNumDependencies(agx::UInt32 size=1)
Explicitly increment the number of parent dependencies.
Definition: Job.h:548
RangeCallback m_rangeCallback
Definition: Job.h:311
#define agxAssert(expr)
Definition: debug.h:143
bool contains(const T2 &element) const
Test if the vector contains a certain element.
Definition: Vector.h:1452
void init()
Definition: Job.h:589
void clearChildDependencies()
Remove all child dependencies.
Definition: Job.h:537
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
Definition: Vector.h:1149
SyncTag * getSyncTag()
Definition: Job.h:496
TaskJob()
Definition: Job.h:676
void addJob(Job *job)
Definition: Job.h:597
agx::UInt32 m_numDependencies
Definition: Job.h:263
void decrementNumDependencies(agx::UInt32 size=1)
Explicitly decrement the number of parent dependencies.
Definition: Job.h:549
agx::Delegate Delegate
Definition: Job.h:62
Vector< RangeJob > RangeJobVector
Definition: Job.h:315
Task * m_task
Definition: Job.h:265
std::atomic< Int32 > m_dependencyCounter
Definition: Job.h:264
T size() const
Definition: IndexRange.h:168
void setTask(Task *task)
Set the task which the job belongs to.
Definition: Job.h:495
const Delegate & getCallback() const
Return the current callback.
Definition: Job.h:554
agx::UInt32 m_cost
Definition: Job.h:269
double Real
Definition: Real.h:42
std::deque< class Job * > JobPtrQueue
Definition: Job.h:52
void addDependency(Job *parent)
Add a dependency to a job.
Definition: Job.h:504
agx::UInt32 getCostEstimate() const
Definition: Job.h:576
Thread * getTargetThread()
Definition: Job.h:492
Thread * m_assignedThread
Definition: Job.h:267
agx::Callback Delegate
Definition: Delegate.h:45
void spawn()
Schedule job for execution in thread pool.
uint32_t UInt32
Definition: Integer.h:40
const char * m_name
Definition: Job.h:258
Thread * m_targetThread
Definition: Job.h:266
#define AGXCORE_EXPORT
void setSyncTag(SyncTag *tag)
Set the synchronization tag which must enter completion status before the job is executed.
Definition: Job.h:497
void init(const RangeCallback &callback, const IndexRange &range, agx::UInt32 costPerElement=1)
Definition: Job.h:634
agx::UInt32 m_tag
Definition: Job.h:270