Repository navigation
Expand file tree
/
Copy pathtask.zig
More file actions
157 lines (143 loc) · 5.66 KB
/
Copy pathtask.zig
File metadata and controls
157 lines (143 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! `Task`: one CPU-bound job submitted with a strategy's `spawn` (commonware's
//! `Strategy::spawn`), awaited with `block_on`.
//!
//! Commonware's `spawn` returns a future; here the caller owns the task's memory instead, so
//! submitting never allocates:
//!
//! ```zig
//! var task: parallel.Task(Op) = .init(op);
//! strategy.spawn(len, &task);
//! const result = strategy.block_on(&task);
//! ```
//!
//! `op.call()` runs the job; an op that needs the strategy captures it. A task must be awaited
//! with `block_on` before it goes out of scope or its pool is destroyed. A strategy without a
//! pool (or with parallelism 1) runs the job inline before `spawn` returns. A caller waiting
//! for a job no worker has started runs it itself; one waiting for a job a worker holds runs
//! other queued jobs meanwhile.
const std = @import("std");
const assert = std.debug.assert;
const ops = @import("ops.zig");
const pool_module = @import("pool.zig");
const policy_module = @import("policy.zig");
const Clock = @import("clock.zig").Clock;
const ThreadPool = pool_module.ThreadPool;
/// Where a measured offloaded job reports its timing: an adaptive strategy's policy, keyed by
/// the spawning call site.
pub const Recorder = struct {
policy: *policy_module.Policy,
site: usize,
len: usize,
threads: usize,
plan: policy_module.SpawnPlan,
/// The clock the job and its round trip are timed on.
clock: Clock = .wall,
/// When the job was handed off, on `clock`.
start_ns: u64 = 0,
};
pub fn Task(comptime Op: type) type {
return struct {
const Self = @This();
pub const Result = ops.Return(Op, "call");
op: Op,
result: Result = undefined,
latch: pool_module.Latch = .{ .pending = .init(1) },
job: pool_module.Job = undefined,
pool: ?*ThreadPool = null,
spawned: bool = false,
/// Set for a measured offload; the job then times itself.
recorder: ?Recorder = null,
job_ns: u64 = 0,
pub fn init(op: Op) Self {
return .{ .op = op };
}
/// Whether the job has finished.
pub fn is_done(self: *const Self) bool {
return self.latch.pending.load(.acquire) == 0;
}
/// Whether `spawn` handed the job to a pool rather than running it inline.
pub fn is_offloaded(self: *const Self) bool {
assert(self.spawned);
return self.pool != null;
}
fn body(context: *const anyopaque, index: usize) void {
assert(index == 0);
const self: *Self = @ptrCast(@alignCast(@constCast(context)));
if (self.recorder == null) {
self.result = self.op.call();
return;
}
const io = self.pool.?.io;
const clock = self.recorder.?.clock;
const start = clock.now_ns(io);
self.result = self.op.call();
self.job_ns = clock.now_ns(io) -| start;
}
/// Runs the job on the calling thread before returning.
pub fn run_inline(self: *Self) void {
assert(!self.spawned);
assert(self.latch.pending.load(.monotonic) == 1);
self.spawned = true;
self.result = self.op.call();
self.latch.pending.store(0, .release);
}
/// Hands the job to `pool`.
pub fn offload(self: *Self, pool: *ThreadPool) void {
assert(!self.spawned);
assert(self.latch.pending.load(.monotonic) == 1);
self.spawned = true;
self.pool = pool;
self.job = .{
.context = self,
.call = body,
.index = 0,
.latch = &self.latch,
.queued = true,
};
pool.submit(&self.job);
}
/// Hands the job to `pool` and reports its timing to `recorder` when it is awaited:
/// the job's own wall time, and the round trip from hand-off to result.
pub fn offload_measured(self: *Self, pool: *ThreadPool, recorder: Recorder) void {
assert(recorder.plan.execution == .offload and recorder.plan.measure);
var measured = recorder;
measured.start_ns = recorder.clock.now_ns(pool.io);
self.recorder = measured;
self.offload(pool);
}
/// Waits for the job and returns its result. A measured job reports here, since every
/// task is awaited (commonware's worker reports the job even if its future is dropped).
pub fn wait(self: *Self) Result {
assert(self.spawned);
if (self.pool) |pool| pool.finish(&self.job);
assert(self.is_done());
if (self.recorder) |recorder| {
const round_trip_ns = recorder.clock.now_ns(self.pool.?.io) -| recorder.start_ns;
const policy = recorder.policy;
const job_ns = self.job_ns;
policy.record_spawn_job(
recorder.site,
recorder.len,
recorder.threads,
recorder.plan,
job_ns,
);
policy.record_spawn_round_trip(
recorder.site,
recorder.len,
recorder.threads,
recorder.plan,
round_trip_ns,
job_ns,
);
}
return self.result;
}
};
}
/// Waits for `task` (a `*Task(Op)`) and returns its result.
pub fn block_on(strategy: anytype, task: anytype) @TypeOf(task.*).Result {
_ = strategy;
comptime assert(@typeInfo(@TypeOf(task)) == .pointer);
return task.wait();
}