lib/accy/src/kernel/library/histogram/family/runtime/artifact.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 
 3 const accy = @import("../../../../../root.zig");
 4 const artifact_product = @import("../../../../../artifact/model/root.zig");
 5 const library = @import("../../../root.zig");
 6 const body = @import("body.zig");
 7 const family = @import("../root.zig");
 8 
 9 const entry = library.entry;
10 const kernel = accy.kernel;
11 const Histogram = family.Histogram;
12 
13 pub fn createHistogramFamilyArtifact(
14     allocator: std.mem.Allocator,
15     handle: kernel.BackendHandle,
16     instance: Histogram,
17     options: entry.ArtifactOptions,
18 ) !kernel.OwnedKernelCallArtifact {
19     if (!family.histogramInstanceValid(instance)) return error.InvalidKernelLibraryEntry;
20     const target = try family.histogramFamilyTarget(allocator, instance);
21     defer allocator.free(target);
22     const entry_name = try family.histogramFamilyEntryName(allocator, instance);
23     defer allocator.free(entry_name);
24     const family_fingerprint = options.shape_family_fingerprint orelse try family.histogramFamilyFingerprint(allocator, instance);
25     const shape_profile_dimensions = family.histogramShapeProfileDimensions(instance);
26     const shape_profile = options.shape_profile orelse artifact_product.KernelCallShapeProfile{
27         .name = "histogram",
28         .fingerprint = family_fingerprint,
29         .dimensions = shape_profile_dimensions[0..],
30     };
31 
32     var graph = switch (instance.dtype) {
33         .f32 => try body.HistogramRuntimeFamilyF32.buildNamed(allocator, options.limits, entry_name, instance),
34         else => return error.UnsupportedDType,
35     };
36     defer graph.deinit();
37     return kernel.createKernelCallArtifact(allocator, handle, &graph, .{
38         .target = target,
39         .version = family.histogram_family_version,
40         .format = options.format,
41         .kernel_plan = options.kernel_plan,
42         .element_count_argument = options.element_count_argument,
43         .shape_family_fingerprint = family_fingerprint,
44         .shape_profile = shape_profile,
45         .launch = options.launch orelse try histogramDerivedLaunch(instance),
46         .runtime_scalar_argument_count = if (options.runtime_scalar_argument_count == 0) 4 else options.runtime_scalar_argument_count,
47         .static_arguments = options.static_arguments,
48     });
49 }
50 
51 fn histogramDerivedLaunch(instance: Histogram) !artifact_product.KernelCallLaunch {
52     if (instance.threads == 0) return error.KernelLibraryLaunchThreadgroupMustBeNonzero;
53     return .{ .derived = .{
54         .grid = .{
55             .{ .runtime_u32_ceil_div = .{ .argument_index = 1, .divisor = instance.threads } },
56             .{ .fixed = 1 },
57             .{ .fixed = 1 },
58         },
59         .threadgroup = .{ instance.threads, 1, 1 },
60     } };
61 }