queuing and job node allocs

This commit is contained in:
Muaz Ahmad 2023-11-27 12:53:02 +05:00
parent 6a6da59a79
commit 98aaa390be
2 changed files with 52 additions and 1 deletions

View file

@ -1,23 +1,52 @@
const std = @import("std");
const util = @import("util.zig");
const QueueWrap = struct {
alloc: std.mem.Allocator,
queue: *util.JobQueue,
job_pool: util.JobPool,
const Self = @This();
fn init(alloc: std.mem.Allocator, job_pool: util.JobPool) !Self {
var q = try alloc.create(util.JobQueue);
q.* = util.JobQueue.init(job_pool);
return Self {
.alloc = alloc,
.queue = q,
.job_pool = job_pool,
};
}
fn deinit(self: *Self) void {
self.job_pool.deinit();
self.alloc.destroy(self.queue);
}
};
pub const ThreadManager = struct {
threads: std.ArrayList(std.Thread),
arena: std.heap.ArenaAllocator,
queue: QueueWrap,
const Self = @This();
pub fn init(alloc: std.mem.Allocator, n_quant_jobs: usize) !Self {
_ = n_quant_jobs;
var arena = std.heap.ArenaAllocator.init(alloc);
var arena_alloc = arena.allocator();
var job_pool = util.JobPool.init(alloc);
var thread_mgr = Self {
.threads = std.ArrayList(std.Thread).init(arena.allocator()),
.threads = std.ArrayList(std.Thread).init(arena_alloc),
.arena = arena,
.queue = try QueueWrap.init(arena_alloc, job_pool),
};
return thread_mgr;
}
pub fn deinit(self: *Self) void {
self.threads.deinit();
self.queue.deinit();
self.arena.deinit();
}
};

View file

@ -10,6 +10,28 @@ pub const Options = struct {
pub const Block = [64]u8;
pub const BlockQuantized = [64]i16;
pub const ThreadList = std.ArrayList(std.Thread);
pub const Job = struct {
source: *Block,
target: *BlockQuantized,
};
pub const JobQueue = struct {
mutex: std.Thread.Mutex,
queue: std.TailQueue(Job),
job_pool: JobPool,
const Self = @This();
pub fn init(pool: JobPool) Self {
return Self {
.mutex = std.Thread.Mutex{},
.queue = std.TailQueue(Job){},
.job_pool = pool,
};
}
};
pub const JobPool = std.heap.MemoryPool(Job);
pub const Buffers = struct {
arena: std.heap.ArenaAllocator,