Nebula
Loading...
Searching...
No Matches
jobs.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
27//------------------------------------------------------------------------------
28#include "ids/id.h"
29#include "ids/idallocator.h"
30#include "threading/thread.h"
31#include "threading/event.h"
32#include "threading/safequeue.h"
33#include "util/stringatom.h"
34#include <atomic>
35
36namespace Jobs
37{
38
39// these values could be bigger if we need it
40#define JOBFUNCCONTEXT_MAXIO 8
41#define JOBFUNCCONTEXT_MAXUNIFORMS 4
42
43#define N_JOB_INPUT(ctx, slice, index) (ctx.inputs[index] + slice * ctx.inputSizes[index])
44#define N_JOB_OUTPUT(ctx, slice, index) (ctx.outputs[index] + slice * ctx.outputSizes[index])
45
46//------------------------------------------------------------------------------
68
70{
72
74 void* data[MaxNumBuffers]; // pointers to data
75 SizeT dataSize[MaxNumBuffers]; // size of entire array
76 SizeT sliceSize[MaxNumBuffers]; // size of singular work package
77};
78
80{
82
84 const void* data[MaxNumBuffers]; // pointers to data
85 SizeT dataSize[MaxNumBuffers]; // size of entire array
86 SizeT scratchSize; // scratch memory
87};
88
95
97{
99public:
100
109
111 {
113 union
114 {
115 struct // execute job
116 {
120 void(*JobFunc)(const JobFuncContext& ctx);
121 const std::function<void()>* callback;
123
124 struct // synchronize
125 {
127 std::atomic_uint* completionCounter;
128 const std::function<void()>* callback;
130 };
131 };
132
134 JobThread();
136 virtual ~JobThread();
137
139 void EmitWakeupSignal();
141 void DoWork();
143 bool HasWork();
144
146 void RunJobSlices(uint sliceIndex, uint numSlices, const JobContext ctx, void(*JobFunc)(const JobFuncContext& ctx), const std::function<void()>* callback);
148 void PushCommand(const JobThreadCommand& command);
151
152private:
153
154 static const SizeT MaxScratchSize = (64 * 1024); // 64 kB max scratch size
155
158};
159
160//------------------------------------------------------------------------------
161
165
173
177void DestroyJobPort(const JobPortId& id);
178
180bool JobPortBusy(const JobPortId& id);
181
182enum
183{
187};
188
189typedef Ids::IdAllocator<
190 Util::StringAtom, // 0 - name
191 Util::FixedArray<Ptr<JobThread>>, // 1 - threads
192 uint // 2 - next thread index
195
196//------------------------------------------------------------------------------
197
199{
200 void(*JobFunc)(const JobFuncContext& ctx);
201};
202
204JobId CreateJob(const CreateJobInfo& info);
206void DestroyJob(const JobId& id);
207
209void JobSchedule(const JobId& job, const JobPortId& port, const JobContext& ctx, const bool cycleThreads = true);
211void JobSchedule(const JobId& job, const JobPortId& port, const JobContext& ctx, const std::function<void()>& callback, const bool cycleThreads = true);
213void JobSchedule(const JobId& job, const JobPortId& port);
215void JobSchedule(const JobId& job, const JobPortId& port, void* ctx, SizeT count, SizeT groupSize, const bool cycleThreads = true);
217void JobScheduleSequence(const Util::Array<JobId>& jobs, const JobPortId& port, const Util::Array<JobContext>& contexts);
219void JobScheduleSequence(const Util::Array<JobId>& jobs, const JobPortId& port, const Util::Array<JobContext>& contexts, const std::function<void()>& callback);
221void* JobAllocateScratchMemory(const JobId& job, const Memory::HeapType heap, const SizeT size);
222
229
230enum
231{
236
237typedef Ids::IdAllocator<
238 CreateJobInfo, // 0 - job info
239 std::function<void()>, // 1 - callback
240 PrivateMemory // 4 - private buffer, destroyed when job is finished
243
244
245//------------------------------------------------------------------------------
246
248{
249 std::function<void()> callback;
250};
251
252enum
253{
259
261JobSyncId CreateJobSync(const CreateJobSyncInfo& info);
263void DestroyJobSync(const JobSyncId id);
264
266void JobSyncHostReset(const JobSyncId id);
268void JobSyncHostSignal(const JobSyncId id, bool reset = true);
270void JobSyncThreadSignal(const JobSyncId id, const JobPortId port, bool reset = true);
272void JobSyncHostWait(const JobSyncId id, bool reset = false);
274void JobSyncThreadWait(const JobSyncId id, const JobPortId port, bool reset = false);
275
277bool JobSyncSignaled(const JobSyncId id);
278
279typedef Ids::IdAllocator<
280 std::function<void()>, // 0 - callback
281 Threading::Event*, // 1 - event
282 std::atomic_uint*, // 2 - completion counter
283 bool // 3 - pending signal
286
287} // namespace Jobs
An ID allocator associates an id with a slice in an N number of arrays.
Definition idallocator.h:39
Definition jobs.h:97
bool HasWork()
returns true if thread has work
Definition jobs.cc:490
void DoWork()
this method runs in the thread context
Definition jobs.cc:421
JobThreadCommandType
Definition jobs.h:102
@ WaitAndReset
Definition jobs.h:106
@ RunJob
Definition jobs.h:103
@ Signal
Definition jobs.h:104
@ Wait
Definition jobs.h:105
static const SizeT MaxScratchSize
Definition jobs.h:154
JobThread()
constructor
Definition jobs.cc:390
virtual ~JobThread()
destructor
Definition jobs.cc:399
void PushCommands(const Util::Array< JobThreadCommand > &commands)
push command buffer work
Definition jobs.cc:564
void PushCommand(const JobThreadCommand &command)
push command buffer work
Definition jobs.cc:555
void RunJobSlices(uint sliceIndex, uint numSlices, const JobContext ctx, void(*JobFunc)(const JobFuncContext &ctx), const std::function< void()> *callback)
push a set of job slices
Definition jobs.cc:499
ubyte * scratchBuffer
Definition jobs.h:157
void EmitWakeupSignal()
called if thread needs a wakeup call before stopping
Definition jobs.cc:412
__DeclareClass(JobThread)
Threading::SafeQueue< JobThreadCommand > commands
Definition jobs.h:156
Thread-safe version of Util::Queue.
Definition safequeue.h:27
Nebula's dynamic array class.
Definition array.h:60
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
A StringAtom.
Definition stringatom.h:22
#define ID_16_TYPE(x)
Definition id.h:31
#define ID_32_TYPE(x)
Definition id.h:16
#define JOBFUNCCONTEXT_MAXUNIFORMS
Definition jobs.h:41
#define JOBFUNCCONTEXT_MAXIO
Definition jobs.h:40
Job system allows for scheduling and execution of a parallel task.
Definition jobs.cc:11
@ Job_CreateInfo
Definition jobs.h:232
@ Job_CallbackFunc
Definition jobs.h:233
@ Job_ScratchMemory
Definition jobs.h:234
@ JobPort_Threads
Definition jobs.h:185
@ JobPort_NextThreadIndex
Definition jobs.h:186
@ JobPort_Name
Definition jobs.h:184
JobSyncAllocator jobSyncAllocator(0xFFFFFFFF)
Definition jobs.h:285
void DestroyJob(const JobId &id)
delete job
Definition jobs.cc:78
JobPortAllocator jobPortAllocator(0xFFFF)
Definition jobs.h:194
void * JobAllocateScratchMemory(const JobId &job, const Memory::HeapType heap, const SizeT size)
allocate memory for job
Definition jobs.cc:238
void DestroyJobSync(const JobSyncId id)
destroy job sync
Definition jobs.cc:273
Ids::IdAllocator< Util::StringAtom, Util::FixedArray< Ptr< JobThread > >, uint > JobPortAllocator
Definition jobs.h:193
bool JobSyncSignaled(const JobSyncId id)
returns true if sync object has been signaled
Definition jobs.cc:380
JobSyncId CreateJobSync(const CreateJobSyncInfo &info)
create job sync
Definition jobs.cc:253
void JobSyncThreadWait(const JobSyncId id, const JobPortId port, bool reset)
wait for job on thread side, if reset is true, reset after waiting
Definition jobs.cc:357
@ SyncCompletionEvent
Definition jobs.h:255
@ SyncCallback
Definition jobs.h:254
@ SyncCompletionCounter
Definition jobs.h:256
@ SyncPendingSignal
Definition jobs.h:257
void JobSchedule(const JobId &job, const JobPortId &port, const JobContext &ctx, const bool cycleThreads)
schedule job to be executed
Definition jobs.cc:91
Ids::IdAllocator< std::function< void()>, Threading::Event *, std::atomic_uint *, bool > JobSyncAllocator
Definition jobs.h:284
void JobScheduleSequence(const Util::Array< JobId > &jobs, const JobPortId &port, const Util::Array< JobContext > &contexts)
schedule a sequence of jobs
Definition jobs.cc:196
void JobSyncHostSignal(const JobSyncId id, bool reset)
signal job sync on host
Definition jobs.cc:294
void DestroyJobPort(const JobPortId &id)
destroy job port
Definition jobs.cc:47
void JobSyncHostWait(const JobSyncId id, bool reset)
wait for job on host side, if reset is true, resets after waiting
Definition jobs.cc:344
JobAllocator jobAllocator(0xFFFFFFFF)
Definition jobs.h:242
bool JobPortBusy(const JobPortId &id)
check to see if port is idle
JobPortId CreateJobPort(const CreateJobPortInfo &info)
create a new job port
Definition jobs.cc:20
void JobSyncThreadSignal(const JobSyncId id, const JobPortId port, bool reset)
put job sync on port, if reset is true, reset prior to signaling
Definition jobs.cc:306
void JobSyncHostReset(const JobSyncId id)
reset job sync on host
Definition jobs.cc:284
Ids::IdAllocator< CreateJobInfo, std::function< void()>, PrivateMemory > JobAllocator
Definition jobs.h:241
JobId CreateJob(const CreateJobInfo &info)
create job
Definition jobs.cc:62
HeapType
Heap types are defined here.
Definition osxmemoryconfig.h:25
Definition jobs.h:199
void(* JobFunc)(const JobFuncContext &ctx)
Definition jobs.h:200
Definition jobs.h:167
uint affinity
Definition jobs.h:170
Util::StringAtom name
Definition jobs.h:168
SizeT numThreads
Definition jobs.h:169
uint priority
Definition jobs.h:171
Definition jobs.h:248
std::function< void()> callback
Definition jobs.h:249
Definition jobs.h:90
JobUniformData uniform
Definition jobs.h:93
JobIOData input
Definition jobs.h:91
JobIOData output
Definition jobs.h:92
This class describes the size of a workload for a single thread (can be multiple items)
Definition jobs.h:51
uint numSlices
Definition jobs.h:66
uint outputSizes[JOBFUNCCONTEXT_MAXIO]
Definition jobs.h:64
ubyte * outputs[JOBFUNCCONTEXT_MAXIO]
Definition jobs.h:63
uint uniformSizes[JOBFUNCCONTEXT_MAXUNIFORMS]
Definition jobs.h:56
uint numOutputs
Definition jobs.h:62
uint inputSizes[JOBFUNCCONTEXT_MAXIO]
Definition jobs.h:60
ubyte * scratch
Definition jobs.h:52
uint numInputs
Definition jobs.h:58
ubyte * uniforms[JOBFUNCCONTEXT_MAXUNIFORMS]
Definition jobs.h:55
ubyte * inputs[JOBFUNCCONTEXT_MAXIO]
Definition jobs.h:59
uint numUniforms
Definition jobs.h:54
Definition jobs.h:70
SizeT dataSize[MaxNumBuffers]
Definition jobs.h:75
void * data[MaxNumBuffers]
Definition jobs.h:74
static const SizeT MaxNumBuffers
Definition jobs.h:71
SizeT numBuffers
Definition jobs.h:73
SizeT sliceSize[MaxNumBuffers]
Definition jobs.h:76
Definition jobs.h:162
Definition jobs.h:163
Definition jobs.h:164
uint slice
Definition jobs.h:117
std::atomic_uint * completionCounter
Definition jobs.h:127
void(* JobFunc)(const JobFuncContext &ctx)
Definition jobs.h:120
Threading::Event * ev
Definition jobs.h:126
const std::function< void()> * callback
Definition jobs.h:121
JobContext context
Definition jobs.h:119
JobThreadCommandType ev
Definition jobs.h:112
uint numSlices
Definition jobs.h:118
struct Jobs::JobThread::JobThreadCommand::@17::@19 run
struct Jobs::JobThread::JobThreadCommand::@17::@20 sync
Definition jobs.h:80
const void * data[MaxNumBuffers]
Definition jobs.h:84
SizeT scratchSize
Definition jobs.h:86
static const SizeT MaxNumBuffers
Definition jobs.h:81
SizeT dataSize[MaxNumBuffers]
Definition jobs.h:85
SizeT numBuffers
Definition jobs.h:83
Definition jobs.h:224
SizeT size
Definition jobs.h:226
void * memory
Definition jobs.h:227
Memory::HeapType heapType
Definition jobs.h:225
unsigned char ubyte
Definition types.h:34
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31