lib/accy/src/kernel/library/histogram/family/model.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const choir_abi = @import("choir_abi");
 2 const accy = @import("../../../../root.zig");
 3 const binning = @import("binning.zig");
 4 
 5 const DType = choir_abi.DType;
 6 
 7 pub const HistogramBinningPolicy = binning.Policy;
 8 
 9 pub const HistogramVariant = enum {
10     direct,
11     shared_bins,
12 };
13 
14 pub const Histogram = struct {
15     bins: u64,
16     count: u64,
17     lo: f32 = 0,
18     width: f32 = 1,
19     dtype: DType = .f32,
20     binning: HistogramBinningPolicy = .lower_inclusive_upper_exclusive,
21     variant: HistogramVariant = .direct,
22     threads: u32 = 256,
23     bin_axis: []const u8 = "b",
24     element_axis: []const u8 = "n",
25 };
26 
27 pub const histogram_family_version: u32 = 2;
28 pub const histogram_shared_bins_cap: u64 = 4096;
29 pub const histogram_binning_policy_parameter = binning.policy_parameter_name;
30 
31 pub fn histogramDTypeSupported(dtype: DType) bool {
32     return dtype == .f32;
33 }
34 
35 pub fn histogramInstanceValid(instance: Histogram) bool {
36     if (!histogramDTypeSupported(instance.dtype)) return false;
37     if (instance.bins == 0 or instance.count == 0) return false;
38     if (!(instance.width > 0)) return false;
39     if (instance.variant == .shared_bins and instance.bins > histogram_shared_bins_cap) return false;
40     return instance.threads != 0;
41 }
42 
43 pub fn histogramBinningPolicyCode(policy: HistogramBinningPolicy) u64 {
44     return binning.code(policy);
45 }
46 
47 pub fn histogramBinningPolicyFromCode(value: u64) ?HistogramBinningPolicy {
48     return binning.fromCode(value);
49 }
50 
51 pub fn histogramBinForValue(instance: Histogram, value: f32) ?usize {
52     return switch (instance.binning) {
53         .lower_inclusive_upper_exclusive => {
54             const relative = (value - instance.lo) / instance.width;
55             const bins_float: f32 = @floatFromInt(instance.bins);
56             if (!(relative >= 0) or !(relative < bins_float)) return null;
57             return @intFromFloat(relative);
58         },
59     };
60 }