MyraMath
SequentialJobGraph.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_JOBGRAPH_SEQUENTIALJOBGRAPH_H
7 #define MYRAMATH_JOBGRAPH_SEQUENTIALJOBGRAPH_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
15 
16 #include <myramath/jobgraph/Job.h>
19 #include <myramath/jobgraph/Options.h>
20 
21 #include <set>
22 #include <string>
23 #include <stdint.h>
24 
25 namespace myra {
26 
27 class SequentialJobGraph;
28 
30 class MYRAMATH_EXPORT SequentialJobGraph : public JobGraphBase
31  {
32  public:
33 
34  // --------------------------- Container API.
35 
38 
40  int push_back(const JobGraph& g);
41 
43  const JobGraph& at(int g) const;
44 
45  // --------------------------- Base JobGraph overrides.
46 
48  virtual uint64_t n_work() const;
49 
51  virtual std::string name() const;
52 
54  virtual void begins(JobIDs& output) const;
55 
57  virtual void ends(JobIDs& output) const;
58 
60  virtual ::myra::Job* create(JobID j);
61 
63  virtual std::vector<Job*> create();
64 
66  uint64_t size() const;
67 
69  virtual ::myra::JobGraphBase* clone() const;
70 
72  virtual ~SequentialJobGraph();
73 
74  private:
75 
76  // Internal contents.
77  typedef std::vector<JobGraph> Subgraphs;
78  Subgraphs subgraphs;
79 
80  // JobID offsets for each SentinelJob.
81  std::vector<uint64_t> sentinels;
82 
83  // Implementation detail, a Job that sits between the ends() of one subgraph[] and the begins() of the next.
84  class SentinelJob : public ::myra::Job
85  {
86  private:
87 
88  // Which subgraph does this SentinelJob precede.
89  int g;
90 
91  // Host graph.
92  SequentialJobGraph* host;
93 
94  public:
95 
96  // Constructor, requires g and host.
97  SentinelJob(int in_g, SequentialJobGraph* in_host);
98 
99  // Delegates to job->id(), adds offset.
100  virtual JobID id() const override;
101 
102  // Delegates to job->parents(), adds offsets to each. Appends any inter-subgraph parents.
103  virtual void parents(JobIDs& output) const;
104 
105  // Delegates to job->children(), adds offsets to each. Appends any inter-subgraph children.
106  virtual void children(JobIDs& output) const;
107 
108  // Returns job->execute()
109  virtual uint64_t execute();
110 
111  // Returns job->name()
112  virtual std::string name() const;
113 
114  // Deletes job.
115  virtual ~SentinelJob();
116 
117  };
118 
119  // Implementation detail, a Job that delegates everything to an underlying Job from one of the contained subgraphs[]
120  class SequentialJob : public ::myra::Job
121  {
122  private:
123 
124  // Underlying Job.
125  ::myra::Job* job;
126 
127  // Which subgraph it came from.
128  int g;
129 
130  // Host graph.
131  SequentialJobGraph* host;
132 
133  public:
134 
135  // Constructor, requires underlying Job* and host SequentialJobGraph*.
136  SequentialJob(::myra::Job* in_job, int in_g, SequentialJobGraph* in_host);
137 
138  // Delegates to job->id(), adds offset.
139  virtual JobID id() const override;
140 
141  // Delegates to job->parents(), adds offsets to each. Appends any inter-subgraph parents.
142  virtual void parents(JobIDs& output) const;
143 
144  // Delegates to job->children(), adds offsets to each. Appends any inter-subgraph children.
145  virtual void children(JobIDs& output) const;
146 
147  // Returns job->execute()
148  virtual uint64_t execute();
149 
150  // Returns job->name()
151  virtual std::string name() const;
152 
153  // Deletes job.
154  virtual ~SequentialJob();
155 
156  };
157 
158  // These implementation details are friends.
159  friend class SequentialJob;
160  friend class SentinelJob;
161 
162  };
163 
165 MYRAMATH_EXPORT JobGraph sequentialize(const JobGraph& g0, const JobGraph& g1);
167 MYRAMATH_EXPORT JobGraph sequentialize(const JobGraph& g0, const JobGraph& g1, const JobGraph& g2);
168 MYRAMATH_EXPORT JobGraph sequentialize(const JobGraph& g0, const JobGraph& g1, const JobGraph& g2, const JobGraph& g3);
169 MYRAMATH_EXPORT JobGraph sequentialize(const JobGraph& g0, const JobGraph& g1, const JobGraph& g2, const JobGraph& g3, const JobGraph& g4);
171 
172 } // namespace myra
173 
174 #endif
Abstraction to represent one node of a JobGraph.
Definition: Job.h:25
Abstraction to represent one node of a JobGraph.
Contains multiple JobGraph&#39;s, executes()&#39;s them in sequence.
Definition: SequentialJobGraph.h:30
Type erasure class that wraps JobGraphBase, gives it value semantics.
Definition: JobGraph.h:64
Definition: syntax.dox:1
Base/contract class for all other JobGraph&#39;s.
Definition: JobGraph.h:30
Abstraction for representing a directed acyclic graph of Job&#39;s.
Key type used to identify the Job&#39;s of a JobGraph.
Key type used to identify the Job&#39;s of a JobGraph.
Definition: JobID.h:60
std::vector< JobID > JobIDs
Useful typedef.
Definition: JobID.h:118