Restructure
This commit is contained in:
150
kernel/vx_os/vx_back/vx_back.c
Normal file
150
kernel/vx_os/vx_back/vx_back.c
Normal file
@@ -0,0 +1,150 @@
|
||||
|
||||
#include "vx_back.h"
|
||||
#include "../vx_io/vx_io.h"
|
||||
|
||||
|
||||
void vx_before_main()
|
||||
{
|
||||
// unsigned num_available_warps = vx_available_warps();
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
queue_initialize(q + i);
|
||||
}
|
||||
}
|
||||
|
||||
void vx_reschedule_warps()
|
||||
{
|
||||
|
||||
|
||||
register unsigned curr_warp asm("s10");
|
||||
// vx_printf("Reschedule: ", curr_warp);
|
||||
|
||||
if (queue_isEmpty(q+curr_warp))
|
||||
{
|
||||
// vx_printf("Done: ", curr_warp);
|
||||
done[curr_warp] = 1;
|
||||
if (curr_warp == 0)
|
||||
{
|
||||
vx_load_context();
|
||||
return;
|
||||
}
|
||||
ECALL;
|
||||
}
|
||||
|
||||
Job j;
|
||||
queue_dequeue(q+curr_warp,&j);
|
||||
|
||||
// vx_printf("Reschedule -> ", j.wid);
|
||||
asm __volatile__("mv sp,%0"::"r" (j.base_sp):);
|
||||
vx_createThreads(j.n_threads, j.wid, j.func_ptr, j.args, j.assigned_warp);
|
||||
|
||||
ECALL;
|
||||
|
||||
}
|
||||
|
||||
void vx_schedule_warps()
|
||||
{
|
||||
|
||||
unsigned num_available_warps = vx_available_warps();
|
||||
|
||||
asm __volatile__("mv s3, sp");
|
||||
|
||||
for (int curr_warp = 1; curr_warp < num_available_warps; ++curr_warp)
|
||||
{
|
||||
if (!queue_isEmpty(q+curr_warp))
|
||||
{
|
||||
Job j;
|
||||
queue_dequeue(q+curr_warp,&j);
|
||||
asm __volatile__("mv sp,%0"::"r" (j.base_sp):);
|
||||
vx_wspawn(j.n_threads, j.wid, j.func_ptr, j.args, j.assigned_warp);
|
||||
}
|
||||
}
|
||||
|
||||
asm __volatile__("mv sp, s3");
|
||||
|
||||
|
||||
vx_save_context();
|
||||
|
||||
// vx_print_str("saved context\n");
|
||||
|
||||
register unsigned val asm("tp");
|
||||
if (val)
|
||||
{
|
||||
if (!queue_isEmpty(q))
|
||||
{
|
||||
// vx_print_str("found something for w0\n");
|
||||
Job j;
|
||||
queue_dequeue(q,&j);
|
||||
// vx_printf("num_threads: ", j.n_threads);
|
||||
asm __volatile__("mv sp,%0"::"r" (j.base_sp):);
|
||||
vx_createThreads(j.n_threads, j.wid, j.func_ptr, j.args, j.assigned_warp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void vx_spawnWarps(unsigned num_Warps, unsigned num_threads, FUNC, void * args)
|
||||
{
|
||||
vx_before_main();
|
||||
|
||||
unsigned num_available_warps = vx_available_warps();
|
||||
// vx_printf("Num available warps: ", num_available_warps);
|
||||
|
||||
asm __volatile__("addi s2, sp, 0");
|
||||
int warp = 0;
|
||||
for (unsigned i = 0; i < num_Warps; i++)
|
||||
{
|
||||
asm __volatile__("lui s3, 0xFFFF0");
|
||||
asm __volatile__("add sp, sp, s3");
|
||||
register unsigned stack_ptr asm("sp");
|
||||
|
||||
Job j;
|
||||
j.wid = i;
|
||||
j.n_threads = num_threads;
|
||||
j.base_sp = stack_ptr;
|
||||
j.func_ptr = (unsigned) func;
|
||||
j.args = args;
|
||||
j.assigned_warp = warp;
|
||||
|
||||
queue_enqueue(q + warp,&j);
|
||||
++warp;
|
||||
if (warp >= num_available_warps) warp = 0;
|
||||
}
|
||||
asm __volatile__("addi sp, s2, 0");
|
||||
|
||||
|
||||
vx_schedule_warps();
|
||||
|
||||
}
|
||||
|
||||
void vx_wait_for_warps(unsigned num_wait)
|
||||
{
|
||||
// vx_printf("wait for: ", num_wait);
|
||||
unsigned num_available_warps = vx_available_warps();
|
||||
unsigned num = 0;
|
||||
while (num != num_wait)
|
||||
{
|
||||
num = 0;
|
||||
for (int i = 0; i < num_available_warps; i++)
|
||||
{
|
||||
if (done[i] == 1)
|
||||
{
|
||||
num += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// vx_printf("num found: ", num);
|
||||
for (int i = 0; i < num_available_warps; i++) done[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
void * vx_get_arg_struct(void)
|
||||
{
|
||||
register void *ret asm("s7");
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
55
kernel/vx_os/vx_back/vx_back.h
Normal file
55
kernel/vx_os/vx_back/vx_back.h
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "../vx_util/queue.h"
|
||||
|
||||
#define WSPAWN asm __volatile__(".word 0x3006b"::);
|
||||
#define CLONE asm __volatile__(".word 0x3506b":::);
|
||||
#define JALRS asm __volatile__(".word 0x1bfe0eb":::"s10");
|
||||
#define ECALL asm __volatile__(".word 0x00000073");
|
||||
#define JMPRT asm __volatile__(".word 0x5406b");
|
||||
#define SPLIT asm __volatile__(".word 0xf206b");
|
||||
#define P_JUMP asm __volatile__(".word 0x1ff707b");
|
||||
#define JOIN asm __volatile__(".word 0x306b");
|
||||
|
||||
|
||||
#define __if(val) bool temp = !val; \
|
||||
register unsigned p asm("t5") = temp; \
|
||||
register void * e asm("t6") = &&ELSE; \
|
||||
SPLIT; \
|
||||
P_JUMP; \
|
||||
|
||||
|
||||
#define __else register void * w asm("t3") = &&AFTER; \
|
||||
asm __volatile__("jr t3"); \
|
||||
ELSE: asm __volatile__("nop");
|
||||
|
||||
#define __end_if AFTER:\
|
||||
JOIN;
|
||||
|
||||
static int done[] = {0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
static int main_sp[1];
|
||||
|
||||
unsigned context[32];
|
||||
void vx_save_context(void);
|
||||
void vx_load_context(void);
|
||||
|
||||
|
||||
#define FUNC void (func)(unsigned, unsigned)
|
||||
|
||||
unsigned vx_available_warps(void);
|
||||
unsigned vx_available_threads(void);
|
||||
|
||||
|
||||
void vx_createThreads(unsigned, unsigned, unsigned, void *, unsigned);
|
||||
void vx_wspawn(unsigned, unsigned, unsigned, void *, unsigned);
|
||||
void vx_spawnWarps(unsigned num_Warps, unsigned num_threads, FUNC, void *);
|
||||
void vx_schedule_warps(void);
|
||||
void vx_reschedule_warps(void);
|
||||
void vx_wait_for_warps(unsigned);
|
||||
void * vx_get_arg_struct(void);
|
||||
|
||||
|
||||
151
kernel/vx_os/vx_back/vx_back.s
Normal file
151
kernel/vx_os/vx_back/vx_back.s
Normal file
@@ -0,0 +1,151 @@
|
||||
|
||||
|
||||
|
||||
.section .text
|
||||
|
||||
.type _start, @function
|
||||
.global _start
|
||||
_start:
|
||||
li a0, 4 # Num Warps
|
||||
csrw 0x20, a0 # Setting the number of available warps
|
||||
li a0, 8 # Num Threads
|
||||
csrw 0x21, a0 # Setting the number of available threads
|
||||
csrw mhartid,zero
|
||||
csrw misa,zero
|
||||
lui sp, 0x7ffff
|
||||
jal vx_before_main
|
||||
jal main
|
||||
ecall
|
||||
|
||||
.type vx_createThreads, @function
|
||||
.global vx_createThreads
|
||||
vx_createThreads:
|
||||
mv s7 ,a3 # Moving args to s7
|
||||
mv s10,a4 # Moving assigned_warp to s10
|
||||
mv t5 ,sp # Saving the current stack pointer to t5
|
||||
mv t2 , a0 # t2 = num_threads
|
||||
loop_init:
|
||||
li a0,1 # i = 0
|
||||
loop_cond:
|
||||
bge a0, t2, loop_done # i < num_threads
|
||||
loop_body:
|
||||
addi sp,sp,-2048 # Allocate 2k stack for new thread
|
||||
mv t1, a0 # #lane = i
|
||||
.word 0x3506b # clone register state
|
||||
loop_inc:
|
||||
addi a0, a0, 1
|
||||
j loop_cond
|
||||
loop_done:
|
||||
mv sp,t5 # Restoring the stack
|
||||
li a0,0 # setting tid = 0 for main thread
|
||||
mv t6,a2 # setting func_addr
|
||||
mv s11,t2 # setting num_threads to spawn
|
||||
.word 0x1bfe0eb
|
||||
la a0, vx_reschedule_warps
|
||||
.word 0x5406b
|
||||
|
||||
|
||||
.type vx_wspawn, @function
|
||||
.global vx_wspawn
|
||||
vx_wspawn:
|
||||
la t1, vx_createThreads
|
||||
.word 0x3006b # WSPAWN instruction
|
||||
ret
|
||||
|
||||
.global context
|
||||
|
||||
.type vx_save_context, @function
|
||||
.global vx_save_context
|
||||
vx_save_context:
|
||||
la tp, context
|
||||
sw x0 , 0 (tp)
|
||||
sw x1 , 4 (tp)
|
||||
sw x2 , 8 (tp)
|
||||
sw x3 , 12(tp)
|
||||
sw x4 , 16(tp)
|
||||
sw x5 , 20(tp)
|
||||
sw x6 , 24(tp)
|
||||
sw x7 , 28(tp)
|
||||
sw x8 , 32(tp)
|
||||
sw x9 , 36(tp)
|
||||
sw x10, 40(tp)
|
||||
sw x11, 44(tp)
|
||||
sw x12, 48(tp)
|
||||
sw x13, 52(tp)
|
||||
sw x14, 56(tp)
|
||||
sw x15, 60(tp)
|
||||
sw x16, 64(tp)
|
||||
sw x17, 68(tp)
|
||||
sw x18, 72(tp)
|
||||
sw x19, 76(tp)
|
||||
sw x20, 80(tp)
|
||||
sw x21, 84(tp)
|
||||
sw x22, 88(tp)
|
||||
sw x23, 92(tp)
|
||||
sw x24, 96(tp)
|
||||
sw x25, 100(tp)
|
||||
sw x26, 104(tp)
|
||||
sw x27, 108(tp)
|
||||
sw x28, 112(tp)
|
||||
sw x29, 116(tp)
|
||||
sw x30, 120(tp)
|
||||
sw x31, 124(tp)
|
||||
li tp, 1
|
||||
ret
|
||||
|
||||
|
||||
.type vx_load_context, @function
|
||||
.global vx_load_context
|
||||
vx_load_context:
|
||||
la tp, context
|
||||
lw x0 , 0 (tp)
|
||||
lw x1 , 4 (tp)
|
||||
lw x2 , 8 (tp)
|
||||
lw x3 , 12(tp)
|
||||
lw x4 , 16(tp)
|
||||
lw x5 , 20(tp)
|
||||
lw x6 , 24(tp)
|
||||
lw x7 , 28(tp)
|
||||
lw x8 , 32(tp)
|
||||
lw x9 , 36(tp)
|
||||
lw x10, 40(tp)
|
||||
lw x11, 44(tp)
|
||||
lw x12, 48(tp)
|
||||
lw x13, 52(tp)
|
||||
lw x14, 56(tp)
|
||||
lw x15, 60(tp)
|
||||
lw x16, 64(tp)
|
||||
lw x17, 68(tp)
|
||||
lw x18, 72(tp)
|
||||
lw x19, 76(tp)
|
||||
lw x20, 80(tp)
|
||||
lw x21, 84(tp)
|
||||
lw x22, 88(tp)
|
||||
lw x23, 92(tp)
|
||||
lw x24, 96(tp)
|
||||
lw x25, 100(tp)
|
||||
lw x26, 104(tp)
|
||||
lw x27, 108(tp)
|
||||
lw x28, 112(tp)
|
||||
lw x29, 116(tp)
|
||||
lw x30, 120(tp)
|
||||
lw x31, 124(tp)
|
||||
li tp, 0
|
||||
ret
|
||||
|
||||
.type vx_available_warps, @function
|
||||
.global vx_available_warps
|
||||
vx_available_warps:
|
||||
csrr a0, 0x20
|
||||
ret
|
||||
|
||||
.type vx_available_threads, @function
|
||||
.global vx_available_threads
|
||||
vx_available_threads:
|
||||
csrr a0, 0x21
|
||||
ret
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user