Add Vector Add Kernel
This commit is contained in:
@@ -6,36 +6,33 @@ extern crate alloc;
|
||||
use aarch64_cpu::asm::barrier;
|
||||
use alloc::boxed::Box;
|
||||
use core::fmt::Write;
|
||||
use nalgebra::{SMatrix, SVector};
|
||||
use num_traits::{One, Zero};
|
||||
use half::f16;
|
||||
use nalgebra::SVector;
|
||||
use pim_isa::BankMode;
|
||||
use pim_os::{
|
||||
kernel::gemv,
|
||||
pim::{
|
||||
self, interleaved_array,
|
||||
vector::{F16x1, F16x16},
|
||||
},
|
||||
kernel::vadd,
|
||||
pim::{self, vector::F16x1},
|
||||
uart::Uart0,
|
||||
};
|
||||
|
||||
const ROWS: usize = 32;
|
||||
const COLUMNS: usize = 128;
|
||||
const X16_ROWS: usize = ROWS / 16;
|
||||
const X16_COLUMNS: usize = COLUMNS / 16;
|
||||
const ROWS: usize = 2048;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn main() {
|
||||
pim::state::set_kernel(&gemv::KERNEL);
|
||||
pim::state::set_kernel(&vadd::KERNEL);
|
||||
|
||||
let mut matrix = SMatrix::<_, ROWS, COLUMNS>::zeros();
|
||||
matrix.fill_lower_triangle(F16x1::one(), 0);
|
||||
let a = Box::new(pim::continuous_array::Vector(
|
||||
SVector::<F16x1, ROWS>::from_fn(|i, _| F16x1(f16::from_f32(i as _))),
|
||||
));
|
||||
let b = Box::new(pim::continuous_array::Vector(
|
||||
SVector::<F16x1, ROWS>::from_fn(|i, _| F16x1(f16::from_f32((ROWS - i) as _))),
|
||||
));
|
||||
|
||||
let pim_matrix = Box::new(pim::continuous_array::Matrix::<X16_ROWS, X16_COLUMNS>::from(matrix));
|
||||
// writeln!(Uart0, "{}+{}=", a.0, b.0).unwrap();
|
||||
|
||||
let input_vector = SVector::<_, X16_COLUMNS>::from_element(F16x16::one());
|
||||
let interleaved_input_vector = Box::new(interleaved_array::Vector::from(input_vector));
|
||||
|
||||
let mut output_partial_sum_vector = Box::new(SVector::<F16x16, ROWS>::zeros());
|
||||
let mut c = Box::new(pim::continuous_array::Vector(
|
||||
SVector::<F16x1, ROWS>::zeros(),
|
||||
));
|
||||
|
||||
let dummy = Box::new(0);
|
||||
|
||||
@@ -46,24 +43,11 @@ pub extern "C" fn main() {
|
||||
{
|
||||
pim::state::set_bank_mode(BankMode::PimAllBank);
|
||||
|
||||
gemv::execute(
|
||||
pim_matrix.as_ref(),
|
||||
interleaved_input_vector.as_ref(),
|
||||
output_partial_sum_vector.as_mut(),
|
||||
dummy.as_ref(),
|
||||
);
|
||||
vadd::execute(&a.0, &b.0, &mut c.0, dummy.as_ref());
|
||||
|
||||
pim::state::set_bank_mode(BankMode::SingleBank);
|
||||
}
|
||||
|
||||
writeln!(Uart0, "{output_partial_sum_vector}").unwrap();
|
||||
|
||||
let output_vector = SVector::<F16x1, ROWS>::from_fn(|r, _| {
|
||||
output_partial_sum_vector[r]
|
||||
.0
|
||||
.iter()
|
||||
.fold(F16x1::zero(), |acc, val| acc + *val)
|
||||
});
|
||||
|
||||
writeln!(Uart0, "{output_vector}").unwrap();
|
||||
// writeln!(Uart0, "{}", c.0).unwrap();
|
||||
writeln!(Uart0, "Done").unwrap();
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod legacy;
|
||||
pub mod gemv;
|
||||
pub mod vadd;
|
||||
|
||||
145
pim-os/src/kernel/vadd.rs
Normal file
145
pim-os/src/kernel/vadd.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
use crate::pim::{operation::PimOperand, vector::F16x1};
|
||||
use nalgebra::SVector;
|
||||
use pim_isa::{File, Instruction, Kernel};
|
||||
|
||||
pub const KERNEL: Kernel = Kernel([
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 0 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 1 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 2 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 3 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 4 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 5 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 6 },
|
||||
},
|
||||
Instruction::MOV {
|
||||
src: File::Bank,
|
||||
dst: File::GrfA { index: 7 },
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 0 },
|
||||
dst: File::GrfB { index: 0 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 1 },
|
||||
dst: File::GrfB { index: 1 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 2 },
|
||||
dst: File::GrfB { index: 2 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 3 },
|
||||
dst: File::GrfB { index: 3 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 4 },
|
||||
dst: File::GrfB { index: 4 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 5 },
|
||||
dst: File::GrfB { index: 5 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 6 },
|
||||
dst: File::GrfB { index: 6 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::ADD {
|
||||
src0: File::Bank,
|
||||
src1: File::GrfA { index: 7 },
|
||||
dst: File::GrfB { index: 7 },
|
||||
aam: false,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 0 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 1 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 2 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 3 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 4 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 5 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 6 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::FILL {
|
||||
src: File::GrfB { index: 7 },
|
||||
dst: File::Bank,
|
||||
},
|
||||
Instruction::EXIT,
|
||||
Instruction::NOP,
|
||||
Instruction::NOP,
|
||||
Instruction::NOP,
|
||||
Instruction::NOP,
|
||||
Instruction::NOP,
|
||||
Instruction::NOP,
|
||||
Instruction::NOP,
|
||||
]);
|
||||
|
||||
pub fn execute<const R: usize>(
|
||||
a: &SVector<F16x1, R>,
|
||||
b: &SVector<F16x1, R>,
|
||||
c: &mut SVector<F16x1, R>,
|
||||
dummy: &impl PimOperand,
|
||||
) {
|
||||
a.fixed_rows_with_step::<8>(0, 16 * 16)
|
||||
.iter()
|
||||
.for_each(|entry| entry.execute_read());
|
||||
b.fixed_rows_with_step::<8>(0, 16 * 16)
|
||||
.iter()
|
||||
.for_each(|entry| entry.execute_read());
|
||||
c.fixed_rows_with_step_mut::<8>(0, 16 * 16)
|
||||
.iter_mut()
|
||||
.for_each(|entry| entry.execute_write());
|
||||
|
||||
dummy.execute_read();
|
||||
}
|
||||
Reference in New Issue
Block a user