forked from adam-maj/tiny-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.sv
92 lines (80 loc) · 3.33 KB
/
dispatch.sv
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
`default_nettype none
`timescale 1ns/1ns
// BLOCK DISPATCH
// > The GPU has one dispatch unit at the top level
// > Manages processing of threads and marks kernel execution as done
// > Sends off batches of threads in blocks to be executed by available compute cores
module dispatch #(
parameter NUM_CORES = 2,
parameter THREADS_PER_BLOCK = 4
) (
input wire clk,
input wire reset,
input wire start,
// Kernel Metadata
input wire [7:0] thread_count,
// Core States
input reg [NUM_CORES-1:0] core_done,
output reg [NUM_CORES-1:0] core_start,
output reg [NUM_CORES-1:0] core_reset,
output reg [7:0] core_block_id [NUM_CORES-1:0],
output reg [$clog2(THREADS_PER_BLOCK):0] core_thread_count [NUM_CORES-1:0],
// Kernel Execution
output reg done
);
// Calculate the total number of blocks based on total threads & threads per block
wire [7:0] total_blocks;
assign total_blocks = (thread_count + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
// Keep track of how many blocks have been processed
reg [7:0] blocks_dispatched; // How many blocks have been sent to cores?
reg [7:0] blocks_done; // How many blocks have finished processing?
reg start_execution; // EDA: Unimportant hack used because of EDA tooling
always @(posedge clk) begin
if (reset) begin
done <= 0;
blocks_dispatched = 0;
blocks_done = 0;
start_execution <= 0;
for (int i = 0; i < NUM_CORES; i++) begin
core_start[i] <= 0;
core_reset[i] <= 1;
core_block_id[i] <= 0;
core_thread_count[i] <= THREADS_PER_BLOCK;
end
end else if (start) begin
// EDA: Indirect way to get @(posedge start) without driving from 2 different clocks
if (!start_execution) begin
start_execution <= 1;
for (int i = 0; i < NUM_CORES; i++) begin
core_reset[i] <= 1;
end
end
// If the last block has finished processing, mark this kernel as done executing
if (blocks_done == total_blocks) begin
done <= 1;
end
for (int i = 0; i < NUM_CORES; i++) begin
if (core_reset[i]) begin
core_reset[i] <= 0;
// If this core was just reset, check if there are more blocks to be dispatched
if (blocks_dispatched < total_blocks) begin
core_start[i] <= 1;
core_block_id[i] <= blocks_dispatched;
core_thread_count[i] <= (blocks_dispatched == total_blocks - 1)
? thread_count - (blocks_dispatched * THREADS_PER_BLOCK)
: THREADS_PER_BLOCK;
blocks_dispatched = blocks_dispatched + 1;
end
end
end
for (int i = 0; i < NUM_CORES; i++) begin
if (core_start[i] && core_done[i]) begin
// If a core just finished executing it's current block, reset it
core_reset[i] <= 1;
core_start[i] <= 0;
blocks_done = blocks_done + 1;
end
end
end
end
endmodule