tiny.simd.AutoTune
Defined in autotune.
Source
Source: lib/simd/src/autotune.zig:237
zig
pub fn AutoTune( comptime Config: type, comptime max_candidates: usize, comptime min_samples: usize,) type { comptime { if (min_samples < 2) @compileError("AutoTune requires at least two samples"); } const CandidateList = NextWithSkip(max_candidates); return struct { best_index: ?usize = null, candidate_values: [max_candidates]Config = undefined, cost_values: [max_candidates]CostDistribution = @splat(.{}), candidate_count: usize = 0, config_index: usize = 0, list: CandidateList = .{}, rounds_complete: usize = 0, skip_if_above: f64 = 0, const Self = @This(); pub const SetCandidatesError = error{ NoCandidates, TooManyCandidates, AlreadyConfigured, }; pub fn best(self: *const Self) ?*const Config { const index = self.best_index orelse return null; return &self.candidate_values[index]; } pub fn hasCandidates(self: *const Self) bool { return self.candidate_count != 0; } pub fn setCandidates( self: *Self, candidates_to_copy: []const Config, ) SetCandidatesError!void { if (self.hasCandidates()) return error.AlreadyConfigured; if (candidates_to_copy.len == 0) return error.NoCandidates; if (candidates_to_copy.len > max_candidates) return error.TooManyCandidates; @memcpy(self.candidate_values[0..candidates_to_copy.len], candidates_to_copy); for (self.cost_values[0..candidates_to_copy.len]) |*cost| cost.* = .{}; self.best_index = null; self.candidate_count = candidates_to_copy.len; self.config_index = 0; self.list = CandidateList.init(candidates_to_copy.len); self.rounds_complete = 0; self.skip_if_above = 0; } pub fn candidates(self: *const Self) []const Config { std.debug.assert(self.hasCandidates()); return self.candidate_values[0..self.candidate_count]; } pub fn costs(self: *Self) []CostDistribution { return self.cost_values[0..self.candidate_count]; } pub fn costsConst(self: *const Self) []const CostDistribution { return self.cost_values[0..self.candidate_count]; } pub fn nextConfig(self: *const Self) *const Config { std.debug.assert(self.hasCandidates()); return &self.candidate_values[self.config_index]; } pub fn currentIndex(self: *const Self) usize { std.debug.assert(self.hasCandidates()); return self.config_index; } pub fn completedRounds(self: *const Self) usize { return self.rounds_complete; } pub fn skipThreshold(self: *const Self) f64 { return self.skip_if_above; } pub fn notifyCost(self: *Self, cost: u64) void { std.debug.assert(self.best() == null); std.debug.assert(self.hasCandidates()); self.cost_values[self.config_index].notify(@floatFromInt(cost)); const measured_index = self.config_index; const measured_cost = if (self.rounds_complete >= min_samples) self.cost_values[self.config_index].estimateCost() else 0; self.config_index = self.list.next(self.config_index); if (measured_cost > self.skip_if_above) self.list.skip(measured_index); if (self.config_index <= measured_index) { self.rounds_complete += 1; if (self.rounds_complete >= min_samples) { var best_cost = std.math.inf(f64); var minimum_index: usize = 0; for (self.cost_values[0..self.candidate_count], 0..) |*distribution, index| { const estimate = distribution.estimateCost(); if (estimate < best_cost) { best_cost = estimate; minimum_index = index; } } self.skip_if_above = best_cost * 1.25; if (self.rounds_complete == 3 * min_samples / 2 + 1) { self.best_index = minimum_index; } } } } pub fn shouldPrint(self: *const Self) bool { return self.rounds_complete > min_samples; } };}Source: lib/simd/src/root.zig:563
zig
pub const AutoTune = autotune.AutoTune;Audit
| Definitions | 1 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |