MyraMath
LambdaJobGraph.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_LAMBDAJOBGRAPH_H
7 #define MYRAMATH_JOBGRAPH_LAMBDAJOBGRAPH_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
15 
16 #include <myramath/jobgraph/Job.h>
19 
20 #include <string>
21 #include <stdint.h>
22 
23 namespace myra {
24 
26 template<class Lambda> class LambdaJobGraph : public JobGraphBase
27  {
28  private:
29 
30  class Job;
31  friend class Job;
32  Lambda lambda;
33  std::string n;
34 
35  public:
36 
38  LambdaJobGraph(const Lambda& in_lambda)
39  : lambda(in_lambda), n("LambdaJob") { }
40 
42  LambdaJobGraph(const Lambda& in_lambda, const std::string& in_n)
43  : lambda(in_lambda), n(in_n) { }
44 
46  virtual JobGraphBase* clone() const
47  { return new LambdaJobGraph<Lambda>(*this); }
48 
50  virtual ~LambdaJobGraph()
51  { }
52 
53  // --------------------------- Base JobGraph overrides.
54 
56  virtual uint64_t n_work() const
57  { return 1; }
58 
60  virtual std::string name() const
61  { return n; }
62 
64  virtual void begins(JobIDs& output) const
65  { output.push_back( JobID(0) ); }
66 
68  virtual void ends(JobIDs& output) const
69  { output.push_back( JobID(0) ); }
70 
72  virtual Job* create(JobID j)
73  { return new Job(this); }
74 
76  virtual std::vector<::myra::Job*> create()
77  { return std::vector<::myra::Job*>(1,new Job(this)); }
78 
80  virtual uint64_t size() const
81  { return 1; }
82 
83  private:
84 
85  // Implementation detail, calls the Lambda when execute()'d
86  class Job : public ::myra::Job
87  {
88  private:
89 
90  // Host graph.
91  LambdaJobGraph* host;
92 
93  public:
94 
95  // Constructor, requires underlying Job* j and host graph g.
96  Job(LambdaJobGraph* in_host)
97  : host(in_host) { }
98 
99  // Returns (0).
100  virtual JobID id() const
101  { return JobID(0); }
102 
103  // Empty set.
104  virtual void parents(JobIDs& output) const
105  { }
106 
107  // Empty set.
108  virtual void children(JobIDs& output) const
109  { }
110 
111  // Returns j->execute()
112  virtual uint64_t execute()
113  { host->lambda(); return 1; }
114 
115  // Returns j->name()
116  virtual std::string name() const
117  { return host->n; }
118 
119  // Deletes j.
120  virtual ~Job()
121  { }
122 
123  };
124 
125  };
126 
128 template<class Lambda>
129 JobGraph make_LambdaJobGraph(const Lambda& lambda)
130  { return JobGraph(LambdaJobGraph<Lambda>(lambda)); }
131 
132 template<class Lambda>
133 JobGraph make_LambdaJobGraph(const Lambda& lambda, const std::string& name)
134  { return JobGraph(LambdaJobGraph<Lambda>(lambda,name)); }
135 
136 } // namespace myra
137 
138 #endif
virtual ~LambdaJobGraph()
Frees internal resources.
Definition: LambdaJobGraph.h:50
Abstraction to represent one node of a JobGraph.
Definition: Job.h:25
Abstraction to represent one node of a JobGraph.
Encapsulates a Lambda function into a JobGraph of a single Job.
Definition: LambdaJobGraph.h:26
virtual uint64_t n_work() const
Total "work" over all Job&#39;s of this JobGraph.
Definition: LambdaJobGraph.h:56
virtual std::string name() const
Returns a printable name for this JobGraph, for debugging.
Definition: LambdaJobGraph.h:60
virtual void begins(JobIDs &output) const
Enumerates the JobIDs of Job&#39;s that have no parents (where execution begins)
Definition: LambdaJobGraph.h:64
Type erasure class that wraps JobGraphBase, gives it value semantics.
Definition: JobGraph.h:64
virtual void ends(JobIDs &output) const
Enumerates the JobIDs of Job&#39;s that have no children (where execution ends)
Definition: LambdaJobGraph.h:68
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.
virtual JobGraphBase * clone() const
Virtual copy constructor.
Definition: LambdaJobGraph.h:46
virtual std::vector<::myra::Job * > create()
Constructs all Job&#39;s in an implementation-defined manner.
Definition: LambdaJobGraph.h:76
LambdaJobGraph(const Lambda &in_lambda, const std::string &in_n)
Constructs from a Lambda and a name.
Definition: LambdaJobGraph.h:42
virtual Job * create(JobID j)
Constructs the Job corresponding to the given JobID.
Definition: LambdaJobGraph.h:72
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
JobGraph make_LambdaJobGraph(const Lambda &lambda)
Given a Lambda, returns a JobGraph that calls lambda() when execute()&#39;d.
Definition: LambdaJobGraph.h:129
std::vector< JobID > JobIDs
Useful typedef.
Definition: JobID.h:118
LambdaJobGraph(const Lambda &in_lambda)
Constructs from a Lambda.
Definition: LambdaJobGraph.h:38
virtual uint64_t size() const
Returns 1.
Definition: LambdaJobGraph.h:80