Repository navigation
Expand file tree
/
Copy pathcapi.zig
More file actions
359 lines (327 loc) · 12.6 KB
/
Copy pathcapi.zig
File metadata and controls
359 lines (327 loc) · 12.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! C ABI: a driving adapter that exposes the engine to other languages
//! (Python via ctypes → python/sched_env). Built as libsched by
//! `zig build lib`.
//!
//! Lifecycle:
//! h = sim_create(&cfg) // NULL on bad config (message on stderr)
//! sim_dims(h, &dims) // sizes and strides of the shared buffers
//! sim_reset(h, seed)
//! loop: sim_action_mask(h, mask); sim_step(h, action, &res)
//! sim_destroy(h)
//!
//! Zero-copy: sim_machines_ptr / sim_jobs_ptr / sim_duration_ptr /
//! sim_arrival_ptr point into engine memory. They stay valid until
//! sim_destroy and always show the current state (read-only for callers).
//! Grids are laid out [grid][res][stride] with `block` elements per grid.
//! Job rows: ticks [0, n_ticks) are real. Machine rows use their own
//! `m_stride` / `m_block` (sliding window): tick t of the horizon is at
//! row[sim_machine_head() + t], and the head moves as time advances.
//!
//! Action index (rules.encode): i < max_jobs·n_machines → place(slot =
//! i / n_machines, machine = i % n_machines); i == max_jobs·n_machines → wait.
const std = @import("std");
const sched = @import("sched");
const cli = @import("cli.zig");
const registry = @import("registry.zig");
const rules = sched.engine.rules;
const Elem = sched.Elem;
pub const SimConfig = extern struct {
/// Generator spec, e.g. "deeprm:n=2" (same syntax as --machines).
machines: [*:0]const u8,
/// Arrival spec, e.g. "poisson:n=100,rate=0.5" (same as --jobs).
jobs: [*:0]const u8,
n_res: u32 = 10,
n_ticks: u32 = 100,
max_jobs: u32 = 128,
/// Reward spec, e.g. "slowdown" (same as --reward); NULL = neg_in_system.
reward: ?[*:0]const u8 = null,
/// Truncate at this tick; < 0 = never.
max_ticks: i64 = -1,
illegal_penalty: f32 = -1.0,
skip_empty: u8 = 1,
drop_impossible: u8 = 1,
};
pub const SimDims = extern struct {
n_machines: u32,
n_res: u32,
n_ticks: u32,
max_jobs: u32,
n_actions: u32,
/// Elements per row (n_ticks + padding).
stride: u32,
/// Elements per grid (n_res · stride + alignment).
block: u32,
/// Largest capacity over all machines and resources (for normalizing).
max_cap: u32,
/// Machine rows: elements per row / per grid (n_ticks + slack + padding).
m_stride: u32,
m_block: u32,
};
pub const SimStep = extern struct {
reward: f32 = 0,
terminated: u8 = 0,
truncated: u8 = 0,
placed: u8 = 0,
illegal: u8 = 0,
};
pub const SimStats = extern struct {
now: i64,
arrived: u64,
placed: u64,
rejected: u64,
illegal: u64,
steps: u64,
waiting: u64,
makespan: i64,
avg_wait: f64,
max_wait: i64,
avg_util: f64,
total_reward: f64,
};
const Sim = struct {
arena: std.heap.ArenaAllocator,
gen: sched.ports.UsageGenerator,
arr: sched.ports.ArrivalSource,
eng: sched.Engine,
started: bool = false,
/// Trace file (sim_trace_open); `tracing` says whether it is open.
threaded: std.Io.Threaded = undefined,
trace: sched.adapters.observers.Trace = .{},
tracing: bool = false,
/// Observer registered once; forwards to the trace while it is open.
pub fn onEvent(self: *Sim, ev: sched.Event, st: *const sched.State) void {
if (self.tracing) self.trace.onEvent(ev, st);
}
};
const gpa = std.heap.smp_allocator;
fn create(c: *const SimConfig) !*Sim {
const sim = try gpa.create(Sim);
errdefer gpa.destroy(sim);
sim.arena = .init(gpa);
errdefer sim.arena.deinit();
const a = sim.arena.allocator();
const cfg = sched.Config{
.n_res = c.n_res,
.n_ticks = c.n_ticks,
.max_jobs = c.max_jobs,
.max_ticks = if (c.max_ticks < 0) null else c.max_ticks,
.illegal_penalty = c.illegal_penalty,
.skip_empty = c.skip_empty != 0,
.drop_impossible = c.drop_impossible != 0,
};
if (c.max_jobs == 0) return cli.fail("sim_create: max_jobs must be > 0", .{});
sim.gen = try registry.generator(a, try cli.Spec.parse(a, "machines", std.mem.span(c.machines)));
errdefer sim.gen.deinit();
sim.arr = try registry.arrivals(a, try cli.Spec.parse(a, "jobs", std.mem.span(c.jobs)));
errdefer sim.arr.deinit();
sim.eng = try sched.Engine.init(gpa, cfg, sim.arr, sim.gen);
errdefer sim.eng.deinit();
if (c.reward) |r| sim.eng.setReward(try registry.reward(a, try cli.Spec.parse(a, "reward", std.mem.span(r))));
sim.started = false;
sim.trace = .{};
sim.tracing = false;
sim.threaded = std.Io.Threaded.init_single_threaded;
try sim.eng.addObserver(.init(sim));
return sim;
}
export fn sim_create(cfg: *const SimConfig) ?*Sim {
return create(cfg) catch |e| {
if (e != error.InvalidArgs) std.debug.print("sim_create: {s}\n", .{@errorName(e)});
return null;
};
}
export fn sim_destroy(sim: ?*Sim) void {
const s = sim orelse return;
_ = sim_trace_close(s);
s.eng.deinit();
s.arr.deinit();
s.gen.deinit();
s.arena.deinit();
gpa.destroy(s);
}
/// 0 = ok, -1 = error.
export fn sim_reset(sim: *Sim, seed: u64) c_int {
sim.eng.reset(seed) catch return -1;
sim.started = true;
return 0;
}
export fn sim_dims(sim: *const Sim, out: *SimDims) void {
const e = &sim.eng;
var max_cap: Elem = 0;
for (0..e.layout.n_res) |r| max_cap = @max(max_cap, e.machines.maxCap(r));
out.* = .{
.n_machines = @intCast(e.machines.n),
.n_res = @intCast(e.layout.n_res),
.n_ticks = @intCast(e.layout.n_ticks),
.max_jobs = @intCast(e.jobs.cap),
.n_actions = @intCast(e.jobs.cap * e.machines.n + 1),
.stride = @intCast(e.layout.stride),
.block = @intCast(e.layout.block),
.max_cap = max_cap,
.m_stride = @intCast(e.machines.ring.stride),
.m_block = @intCast(e.machines.ring.block),
};
}
/// 0 = ok, -1 = episode not running (call sim_reset), -2 = engine error.
export fn sim_step(sim: *Sim, action: u32, out: *SimStep) c_int {
if (!sim.started or sim.eng.done()) return -1;
const st = sim.eng.state();
const r = sim.eng.step(rules.decode(&st, action)) catch return -2;
out.* = .{
.reward = r.reward,
.terminated = @intFromBool(r.terminated),
.truncated = @intFromBool(r.truncated),
.placed = @intFromBool(r.placed),
.illegal = @intFromBool(r.illegal),
};
return 0;
}
/// mask must hold n_actions bytes; 1 = legal.
export fn sim_action_mask(sim: *const Sim, mask: [*]u8) void {
const st = sim.eng.state();
const n = rules.actionCount(&st);
rules.actionMask(&st, @as([*]bool, @ptrCast(mask))[0..n]);
}
/// valid must hold max_jobs bytes; 1 = slot holds a waiting job.
export fn sim_slot_valid(sim: *const Sim, valid: [*]u8) void {
const jobs = &sim.eng.jobs;
for (0..jobs.cap) |s| valid[s] = @intFromBool(jobs.isValid(s));
}
/// Free capacity, [n_machines][n_res][m_stride] u16; the horizon of each
/// row starts at sim_machine_head().
export fn sim_machines_ptr(sim: *const Sim) [*]const Elem {
return sim.eng.machines.buf.ptr;
}
/// Offset of tick 0 (now) inside every machine row. Changes with time.
export fn sim_machine_head(sim: *const Sim) u32 {
return @intCast(sim.eng.machines.head);
}
/// Capacity, [n_machines][n_res] u16.
export fn sim_capacity_ptr(sim: *const Sim) [*]const Elem {
return sim.eng.machines.cap.ptr;
}
/// Waiting-job demand, [max_jobs][n_res][stride] u16 (0 for empty slots).
export fn sim_jobs_ptr(sim: *const Sim) [*]const Elem {
return sim.eng.jobs.buf.ptr;
}
/// Duration per slot, [max_jobs] u16 (meaningful for valid slots only).
export fn sim_duration_ptr(sim: *const Sim) [*]const u16 {
return sim.eng.jobs.duration.ptr;
}
/// Arrival tick per slot, [max_jobs] i64 (meaningful for valid slots only).
export fn sim_arrival_ptr(sim: *const Sim) [*]const i64 {
return sim.eng.jobs.arrival.ptr;
}
/// Record every following event to a JSON Lines trace at `path` (created or
/// truncated; see src/adapters/observers/trace.zig for the format). Call
/// before sim_reset to capture whole episodes. 0 = ok, -1 = cannot create.
export fn sim_trace_open(sim: *Sim, path: [*:0]const u8) c_int {
_ = sim_trace_close(sim);
sim.trace = .{};
sim.trace.open(sim.threaded.io(), std.Io.Dir.cwd(), std.mem.span(path)) catch return -1;
sim.tracing = true;
return 0;
}
/// Flush and close the trace. 0 = ok (or none open), -1 = a write failed.
export fn sim_trace_close(sim: *Sim) c_int {
if (!sim.tracing) return 0;
sim.tracing = false;
sim.trace.close() catch return -1;
return 0;
}
export fn sim_now(sim: *const Sim) i64 {
return sim.eng.now;
}
export fn sim_stats(sim: *const Sim, out: *SimStats) void {
const s = sim.eng.stats;
out.* = .{
.now = sim.eng.now,
.arrived = s.arrived,
.placed = s.placed,
.rejected = s.rejected,
.illegal = s.illegal,
.steps = s.steps,
.waiting = sim.eng.jobs.count,
.makespan = s.makespan,
.avg_wait = s.avgWait(),
.max_wait = s.max_wait,
.avg_util = s.avgUtil(),
.total_reward = s.total_reward,
};
}
/// Reset with `seed` and run a whole episode with a built-in policy
/// ("fifo", "sjf", "best_fit", …) at native speed. 0 = ok, -1 = error.
export fn sim_run_policy(sim: *Sim, name: [*:0]const u8, seed: u64, out: *SimStats) c_int {
sim.eng.reset(seed) catch return -1;
sim.started = true;
var buf: [1024]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
const spec = cli.Spec.parse(fba.allocator(), "policy", std.mem.span(name)) catch return -1;
_ = registry.runPolicy(&sim.eng, spec, seed, std.math.maxInt(usize)) catch return -1;
sim_stats(sim, out);
return 0;
}
/// Action index a built-in policy would choose in the current state
/// (for stepping / visualizing heuristics from Python). -1 = unknown name.
/// Stateful policies (random) are reseeded from the clock and step count.
/// `name` is a policy spec, e.g. "tetris_srpt:weight=2".
export fn sim_policy_action(sim: *Sim, name: [*:0]const u8) i64 {
var buf: [1024]u8 = undefined; // per call: no growth when called every step
var fba = std.heap.FixedBufferAllocator.init(&buf);
const spec = cli.Spec.parse(fba.allocator(), "policy", std.mem.span(name)) catch return -1;
if (!registry.hasPolicy(spec.name)) return -1;
const seed = sim.eng.seed ^ (sim.eng.stats.steps *% 0x9e3779b97f4a7c15);
const act = registry.policyAction(&sim.eng, spec, seed) catch return -1;
const st = sim.eng.state();
return @intCast(rules.encode(&st, act));
}
test "capi: create, mask, step to the end, run_policy" {
const cfg = SimConfig{
.machines = "deeprm:n=2",
.jobs = "poisson:n=30,rate=0.5",
.n_res = 2,
.n_ticks = 20,
.max_jobs = 8,
};
const sim = sim_create(&cfg) orelse return error.CreateFailed;
defer sim_destroy(sim);
var d: SimDims = undefined;
sim_dims(sim, &d);
try std.testing.expectEqual(@as(u32, 8 * 2 + 1), d.n_actions);
var res: SimStep = .{};
try std.testing.expectEqual(@as(c_int, -1), sim_step(sim, 0, &res)); // before reset
try std.testing.expectEqual(@as(c_int, 0), sim_reset(sim, 1));
var mask: [17]u8 = undefined;
var steps: usize = 0;
while (true) : (steps += 1) {
sim_action_mask(sim, &mask);
try std.testing.expectEqual(@as(u8, 1), mask[16]); // wait always legal
var a: u32 = 16;
for (mask[0..16], 0..) |m, i| if (m == 1) {
a = @intCast(i);
break;
};
try std.testing.expectEqual(@as(c_int, 0), sim_step(sim, a, &res));
try std.testing.expectEqual(@as(u8, 0), res.illegal);
if (res.terminated == 1 or res.truncated == 1) break;
}
var st: SimStats = undefined;
sim_stats(sim, &st);
try std.testing.expectEqual(st.arrived, st.placed);
try std.testing.expectEqual(@as(c_int, -1), sim_step(sim, 16, &res)); // after end
try std.testing.expectEqual(@as(c_int, 0), sim_run_policy(sim, "sjf", 1, &st));
const want = st;
// stepping sjf one decision at a time gives the same episode
try std.testing.expectEqual(@as(c_int, 0), sim_reset(sim, 1));
while (true) {
const a = sim_policy_action(sim, "sjf");
try std.testing.expect(a >= 0);
try std.testing.expectEqual(@as(c_int, 0), sim_step(sim, @intCast(a), &res));
if (res.terminated == 1 or res.truncated == 1) break;
}
sim_stats(sim, &st);
try std.testing.expectEqual(want.avg_wait, st.avg_wait);
try std.testing.expectEqual(want.makespan, st.makespan);
try std.testing.expectEqual(@as(i64, -1), sim_policy_action(sim, "nope"));
try std.testing.expectEqual(st.arrived, st.placed);
}