diff --git a/pim-isa/Cargo.lock b/pim-isa/Cargo.lock index ce66ae9..31c7f4a 100644 --- a/pim-isa/Cargo.lock +++ b/pim-isa/Cargo.lock @@ -11,36 +11,36 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.70" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", @@ -49,9 +49,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.39" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", diff --git a/pim-os/Cargo.lock b/pim-os/Cargo.lock index edd1100..e895a83 100644 --- a/pim-os/Cargo.lock +++ b/pim-os/Cargo.lock @@ -200,18 +200,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.72" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a293318316cf6478ec1ad2a21c49390a8d5b5eae9fab736467d93fbc0edc29c5" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -239,15 +239,15 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "semver" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] @@ -265,9 +265,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", @@ -303,9 +303,9 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "syn" -version = "2.0.43" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", diff --git a/pim-os/src/alloc.rs b/pim-os/src/alloc.rs new file mode 100644 index 0000000..08f94df --- /dev/null +++ b/pim-os/src/alloc.rs @@ -0,0 +1,18 @@ +extern crate alloc; + +use core::mem::MaybeUninit; +use embedded_alloc::Heap; + +#[global_allocator] +static PIM_ALLOC: Heap = Heap::empty(); + +const PIM_ARENA_SIZE: usize = 0x2000000; + +#[link_section = ".pim_data"] +static mut PIM_ARENA: [MaybeUninit; PIM_ARENA_SIZE] = [MaybeUninit::uninit(); PIM_ARENA_SIZE]; + +pub fn init() { + unsafe { + PIM_ALLOC.init(PIM_ARENA.as_ptr() as usize, PIM_ARENA_SIZE); + } +} diff --git a/pim-os/src/bin/matrix_matrix_add.rs b/pim-os/src/bin/matrix_matrix_add.rs new file mode 100644 index 0000000..fd31fc1 --- /dev/null +++ b/pim-os/src/bin/matrix_matrix_add.rs @@ -0,0 +1,78 @@ +#![no_std] +#![no_main] + +extern crate alloc; + +use aarch64_cpu::asm::barrier; +use alloc::{boxed::Box, rc::Rc}; +use core::{cell::RefCell, fmt::Write}; +use half::f16; +use nalgebra::Matrix; +use pim_isa::BankMode; +use pim_os::{ + pim::{ + self, + array::{DummyArray, PimMatrixArena, PimStorage, NUMBER_OF_BANKS}, + kernel::matrix_matrix_add, + vector::{F16x1, F16x16}, + }, + uart::Uart0, +}; + +#[no_mangle] +pub extern "C" fn main() { + pim::state::set_kernel(&matrix_matrix_add::KERNEL); + + let pim_matrix_arena0 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + let pim_matrix_arena1 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + let pim_matrix_arena2 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + + let mut matrix0 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena0, + index: 0, + }); + matrix0.fill_lower_triangle(F16x1(f16::ONE), 0); + + let mut matrix1 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena1, + index: 0, + }); + matrix1.fill_lower_triangle(F16x1(f16::ONE), 0); + + let matrix2 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena2, + index: 0, + }); + + writeln!(Uart0, "{matrix0} + {matrix1}\n=").unwrap(); + + let dummy_array = Box::new(DummyArray([F16x16::default(); NUMBER_OF_BANKS])); + + // Verify everything is correctly initialized before PIM operation + barrier::dsb(barrier::SY); + + { + let pim_matrix_arena0 = &pim_matrix_arena0.borrow(); + let pim_matrix_arena1 = &pim_matrix_arena1.borrow(); + let pim_matrix_arena2 = &mut pim_matrix_arena2.borrow_mut(); + + pim::state::set_bank_mode(BankMode::PimAllBank); + + matrix_matrix_add::execute( + pim_matrix_arena0, + pim_matrix_arena1, + pim_matrix_arena2, + dummy_array.as_ref(), + ); + + pim::state::set_bank_mode(BankMode::SingleBank); + } + + writeln!(Uart0, "{matrix2}").unwrap(); +} diff --git a/pim-os/src/bin/matrix_matrix_multiply.rs b/pim-os/src/bin/matrix_matrix_multiply.rs new file mode 100644 index 0000000..9b79f59 --- /dev/null +++ b/pim-os/src/bin/matrix_matrix_multiply.rs @@ -0,0 +1,79 @@ +#![no_std] +#![no_main] + +extern crate alloc; + +use aarch64_cpu::asm::barrier; +use alloc::{boxed::Box, rc::Rc}; +use pim_isa::BankMode; +use core::{cell::RefCell, fmt::Write}; +use half::f16; +use nalgebra::Matrix; +use pim_os::{ + pim::{ + self, + array::{DummyArray, PimMatrixArena, PimStorage, NUMBER_OF_BANKS}, + kernel::matrix_matrix_mul, + vector::{F16x1, F16x16}, + }, + uart::Uart0, +}; + +#[no_mangle] +pub extern "C" fn main() { + pim::state::set_kernel(&matrix_matrix_mul::KERNEL); + + let pim_matrix_arena0 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + let pim_matrix_arena1 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + let pim_matrix_arena2 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + + let mut matrix0 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena0, + index: 0, + }); + matrix0.fill_lower_triangle(F16x1(f16::ONE), 0); + + let mut matrix1 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena1, + index: 0, + }); + matrix1.fill_lower_triangle(F16x1(f16::ONE), 0); + + let matrix2 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena2, + index: 0, + }); + + writeln!(Uart0, "{matrix0} * {matrix1}\n=").unwrap(); + + let dummy_array = Box::new(DummyArray([F16x16::default(); NUMBER_OF_BANKS])); + + // Verify everything is correctly initialized before PIM operation + barrier::dsb(barrier::SY); + + // Execute kernel + { + let pim_matrix_arena0 = &pim_matrix_arena0.borrow(); + let pim_matrix_arena1 = &pim_matrix_arena1.borrow(); + let pim_matrix_arena2 = &mut pim_matrix_arena2.borrow_mut(); + + pim::state::set_bank_mode(BankMode::PimAllBank); + + matrix_matrix_mul::execute( + pim_matrix_arena0, + pim_matrix_arena1, + pim_matrix_arena2, + dummy_array.as_ref(), + ); + + pim::state::set_bank_mode(BankMode::SingleBank); + } + + writeln!(Uart0, "{matrix2}").unwrap(); +} diff --git a/pim-os/src/bin/matrix_scalar_multiply.rs b/pim-os/src/bin/matrix_scalar_multiply.rs new file mode 100644 index 0000000..c47d079 --- /dev/null +++ b/pim-os/src/bin/matrix_scalar_multiply.rs @@ -0,0 +1,73 @@ +#![no_std] +#![no_main] + +extern crate alloc; + +use aarch64_cpu::asm::barrier; +use alloc::{boxed::Box, rc::Rc}; +use core::{cell::RefCell, fmt::Write}; +use half::f16; +use nalgebra::Matrix; +use pim_isa::BankMode; +use pim_os::{ + pim::{ + self, + array::{DummyArray, PimMatrixArena, PimScalarArena, PimStorage, NUMBER_OF_BANKS}, + kernel::matrix_scalar_mul, + vector::{F16x1, F16x16}, + }, + uart::Uart0, +}; + +#[no_mangle] +pub extern "C" fn main() { + pim::state::set_kernel(&matrix_scalar_mul::KERNEL); + + let pim_matrix_arena0 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + let pim_matrix_arena1 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + + let mut matrix0 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena0, + index: 0, + }); + matrix0.fill_lower_triangle(F16x1(f16::ONE), 0); + + let matrix1 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena1, + index: 0, + }); + + let pim_scalar_arena = Box::new(PimScalarArena( + [F16x16([F16x1(f16::from_f32(2.0)); 16]); 32], + )); + + writeln!(Uart0, "{} * {matrix0}\n=", pim_scalar_arena.0[0].0[0]).unwrap(); + + let dummy_array = Box::new(DummyArray([F16x16::default(); NUMBER_OF_BANKS])); + + // Verify everything is correctly initialized before PIM operation + barrier::dsb(barrier::SY); + + // Execute kernel + { + let pim_matrix_arena0 = &pim_matrix_arena0.borrow(); + let pim_matrix_arena1 = &mut pim_matrix_arena1.borrow_mut(); + + pim::state::set_bank_mode(BankMode::PimAllBank); + + matrix_scalar_mul::execute( + pim_scalar_arena.as_ref(), + pim_matrix_arena0, + pim_matrix_arena1, + dummy_array.as_ref(), + ); + + pim::state::set_bank_mode(BankMode::SingleBank); + } + + writeln!(Uart0, "{matrix1}").unwrap(); +} diff --git a/pim-os/src/bin/matrix_vector_multiply.rs b/pim-os/src/bin/matrix_vector_multiply.rs new file mode 100644 index 0000000..46a09e5 --- /dev/null +++ b/pim-os/src/bin/matrix_vector_multiply.rs @@ -0,0 +1,79 @@ +#![no_std] +#![no_main] + +extern crate alloc; + +use aarch64_cpu::asm::barrier; +use alloc::{boxed::Box, rc::Rc}; +use core::{cell::RefCell, fmt::Write}; +use half::f16; +use nalgebra::Matrix; +use pim_isa::BankMode; +use pim_os::{ + pim::{ + self, + array::{DummyArray, PimMatrixArena, PimStorage, NUMBER_OF_BANKS}, + kernel::matrix_vector_mul, + vector::{F16x1, F16x16}, + }, + uart::Uart0, +}; + +#[no_mangle] +pub extern "C" fn main() { + pim::state::set_kernel(&matrix_vector_mul::KERNEL); + + let pim_matrix_arena0 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], + ))); + let pim_matrix_arena1 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 1], + ))); + let pim_matrix_arena2 = Rc::new(RefCell::new(PimMatrixArena( + [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 1], + ))); + + let mut matrix0 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena0, + index: 0, + }); + matrix0.fill_lower_triangle(F16x1(f16::ONE), 0); + + let mut matrix1 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena1, + index: 0, + }); + matrix1.fill_lower_triangle(F16x1(f16::ONE), 0); + + let matrix2 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena2, + index: 0, + }); + + writeln!(Uart0, "{matrix0} * {matrix1}\n=").unwrap(); + + let dummy_array = Box::new(DummyArray([F16x16::default(); NUMBER_OF_BANKS])); + + // Verify everything is correctly initialized before PIM operation + barrier::dsb(barrier::SY); + + // Execute kernel + { + let pim_matrix_arena0 = &pim_matrix_arena0.borrow(); + let pim_matrix_arena1 = &pim_matrix_arena1.borrow(); + let pim_matrix_arena2 = &mut pim_matrix_arena2.borrow_mut(); + + pim::state::set_bank_mode(BankMode::PimAllBank); + + matrix_vector_mul::execute( + pim_matrix_arena0, + pim_matrix_arena1, + pim_matrix_arena2, + dummy_array.as_ref(), + ); + + pim::state::set_bank_mode(BankMode::SingleBank); + } + + writeln!(Uart0, "{matrix2}").unwrap(); +} diff --git a/pim-os/src/lib.rs b/pim-os/src/lib.rs new file mode 100644 index 0000000..a048675 --- /dev/null +++ b/pim-os/src/lib.rs @@ -0,0 +1,28 @@ +#![feature(generic_const_exprs)] +#![no_std] + +use core::sync::atomic::{compiler_fence, Ordering}; + +mod alloc; +pub mod boot; +pub mod critical_section; +pub mod m5ops; +mod panic; +pub mod pim; +pub mod uart; + +extern "C" { + fn main(); +} + +#[no_mangle] +pub extern "C" fn entry() -> ! { + alloc::init(); + + unsafe { main() } + + m5ops::exit(); + loop { + compiler_fence(Ordering::SeqCst); + } +} diff --git a/pim-os/src/main.rs b/pim-os/src/main.rs index ec98a5e..c9f1d3b 100644 --- a/pim-os/src/main.rs +++ b/pim-os/src/main.rs @@ -1,4 +1,3 @@ -#![feature(generic_const_exprs)] #![no_std] #![no_main] @@ -6,51 +5,23 @@ extern crate alloc; use aarch64_cpu::asm::barrier; use alloc::{boxed::Box, rc::Rc}; -use core::{ - cell::RefCell, - fmt::Write, - mem::MaybeUninit, - panic::PanicInfo, - sync::atomic::{compiler_fence, Ordering}, -}; -use embedded_alloc::Heap; +use core::{cell::RefCell, fmt::Write}; use half::f16; use nalgebra::Matrix; -use pim::{ - array::{DummyArray, PimMatrixArena, PimScalarArena, PimStorage, NUMBER_OF_BANKS}, - kernel::{ - execute_matrix_add, execute_matrix_multiply, execute_matrix_scalar_multiply, - execute_matrix_vector_multiply, MATRIX_ADD, MATRIX_MUL, MATRIX_SCALAR_MUL, - MATRIX_VECTOR_MUL, +use pim_isa::BankMode; +use pim_os::{ + pim::{ + self, + array::{DummyArray, PimMatrixArena, PimStorage, NUMBER_OF_BANKS}, + kernel::matrix_matrix_mul, + vector::{F16x1, F16x16}, }, - state::PimState, - vector::{F16x1, F16x16}, + uart::Uart0, }; -use uart::Uart0; - -mod boot; -mod critical_section; -mod m5ops; -mod pim; -mod uart; - -#[global_allocator] -static PIM_ALLOC: Heap = Heap::empty(); - -const PIM_ARENA_SIZE: usize = 0x2000000; - -#[link_section = ".pim_data"] -static mut PIM_ARENA: [MaybeUninit; PIM_ARENA_SIZE] = [MaybeUninit::uninit(); PIM_ARENA_SIZE]; #[no_mangle] -pub extern "C" fn entry() -> ! { - unsafe { - PIM_ALLOC.init(PIM_ARENA.as_ptr() as usize, PIM_ARENA_SIZE); - } - - let mut uart = Uart0; - let mut pim_state = PimState::new(&MATRIX_SCALAR_MUL); - pim_state.set_kernel(); +pub extern "C" fn main() { + pim::state::set_kernel(&matrix_matrix_mul::KERNEL); let pim_matrix_arena0 = Rc::new(RefCell::new(PimMatrixArena( [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], @@ -61,89 +32,48 @@ pub extern "C" fn entry() -> ! { let pim_matrix_arena2 = Rc::new(RefCell::new(PimMatrixArena( [[[F16x16::default(); NUMBER_OF_BANKS]; 8]; 8], ))); - let pim_storage0 = PimStorage { + + let mut matrix0 = Matrix::from_data(PimStorage { arena: &pim_matrix_arena0, index: 0, - }; - let pim_storage1 = PimStorage { - arena: &pim_matrix_arena1, - index: 0, - }; - let pim_storage2 = PimStorage { - arena: &pim_matrix_arena2, - index: 0, - }; - - writeln!( - &mut uart, - "arena0: {:?}\narena1: {:?}\narena2: {:?}", - core::ptr::addr_of!(*pim_matrix_arena0.borrow()), - core::ptr::addr_of!(*pim_matrix_arena1.borrow()), - core::ptr::addr_of!(*pim_matrix_arena2.borrow()), - ) - .unwrap(); - - let mut matrix0 = Matrix::from_data(pim_storage0); + }); matrix0.fill_lower_triangle(F16x1(f16::ONE), 0); - let mut matrix1 = Matrix::from_data(pim_storage1); + let mut matrix1 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena1, + index: 0, + }); matrix1.fill_lower_triangle(F16x1(f16::ONE), 0); - let matrix2 = Matrix::from_data(pim_storage2); + let matrix2 = Matrix::from_data(PimStorage { + arena: &pim_matrix_arena2, + index: 0, + }); - writeln!(&mut uart, "{matrix0} * {matrix1}\n=").unwrap(); + writeln!(Uart0, "{matrix0} * {matrix1}\n=").unwrap(); - let mut dummy_array = Box::new(DummyArray([F16x16::default(); NUMBER_OF_BANKS])); + let dummy_array = Box::new(DummyArray([F16x16::default(); NUMBER_OF_BANKS])); + + // Verify everything is correctly initialized before PIM operation barrier::dsb(barrier::SY); - // execute_matrix_add( - // &mut pim_state, - // &mut pim_matrix_arena0.borrow_mut(), - // &mut pim_matrix_arena1.borrow_mut(), - // &mut pim_matrix_arena2.borrow_mut(), - // dummy_array.as_mut(), - // ); - // execute_matrix_multiply_rowwise( - // &mut pim_state, - // &mut pim_matrix_arena0.borrow_mut(), - // &mut pim_matrix_arena1.borrow_mut(), - // &mut pim_matrix_arena2.borrow_mut(), - // dummy_array.as_mut(), - // ); - // execute_matrix_vector_multiply( - // &mut pim_state, - // &mut pim_matrix_arena0.borrow_mut(), - // &mut pim_matrix_arena1.borrow_mut(), - // &mut pim_matrix_arena2.borrow_mut(), - // dummy_array.as_mut(), - // ); + // Execute kernel + { + let pim_matrix_arena0 = &pim_matrix_arena0.borrow(); + let pim_matrix_arena1 = &pim_matrix_arena1.borrow(); + let pim_matrix_arena2 = &mut pim_matrix_arena2.borrow_mut(); - // let pim_scalar_arena = Box::new(PimScalarArena( - // [F16x16([F16x1(f16::from_f32(2.0)); 16]); 32], - // )); + pim::state::set_bank_mode(BankMode::PimAllBank); - // execute_matrix_scalar_multiply( - // &mut pim_state, - // &pim_scalar_arena, - // &pim_matrix_arena0.borrow_mut(), - // &mut pim_matrix_arena1.borrow_mut(), - // dummy_array.as_mut(), - // ); + matrix_matrix_mul::execute( + pim_matrix_arena0, + pim_matrix_arena1, + pim_matrix_arena2, + dummy_array.as_ref(), + ); - writeln!(&mut uart, "{matrix2}").unwrap(); - - m5ops::exit(); - - loop { - compiler_fence(Ordering::SeqCst); + pim::state::set_bank_mode(BankMode::SingleBank); } -} -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - writeln!(Uart0, "{info}").unwrap(); - - loop { - compiler_fence(Ordering::SeqCst); - } + writeln!(Uart0, "{matrix2}").unwrap(); } diff --git a/pim-os/src/panic.rs b/pim-os/src/panic.rs new file mode 100644 index 0000000..c3795c2 --- /dev/null +++ b/pim-os/src/panic.rs @@ -0,0 +1,15 @@ +use crate::uart::Uart0; +use core::{ + fmt::Write, + panic::PanicInfo, + sync::atomic::{compiler_fence, Ordering}, +}; + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + writeln!(Uart0, "{info}").unwrap(); + + loop { + compiler_fence(Ordering::SeqCst); + } +} diff --git a/pim-os/src/pim/config.rs b/pim-os/src/pim/config.rs index 7fbd719..c82a3cd 100644 --- a/pim-os/src/pim/config.rs +++ b/pim-os/src/pim/config.rs @@ -4,34 +4,29 @@ use core::{arch::asm, ptr::write_volatile}; #[link_section = ".pim_config"] static mut PIM_CONFIG_REGION: [u8; 0x4000] = [0; 0x4000]; -#[derive(Debug)] -pub struct PimWriter; +pub fn write(s: &str) { + unsafe { + let mut index = 0; + for &byte in s.as_bytes() { + write_volatile((&mut PIM_CONFIG_REGION as *mut u8).offset(index), byte as _); + barrier::dsb(barrier::SY); + index += 1; + } + write_volatile((&mut PIM_CONFIG_REGION as *mut u8).offset(index), b'\0'); -impl PimWriter { - pub fn write(&mut self, s: &str) { - unsafe { - let mut index = 0; - for &byte in s.as_bytes() { - write_volatile((&mut PIM_CONFIG_REGION as *mut u8).offset(index), byte as _); - barrier::dsb(barrier::SY); - index += 1; + // PIM_CONFIG_REGION[..s.len()].copy_from_slice(s.as_bytes()); + // PIM_CONFIG_REGION[s.len()] = b'\0'; + + if cfg!(feature = "cacheless") { + // Be pessimistic so that config region is not optimized away + core::hint::black_box(PIM_CONFIG_REGION); + } else { + // Flush all cache lines that were affected by write operation + for element in PIM_CONFIG_REGION[..s.len()].iter() { + asm!("dc civac, {val}", val = in(reg) element); } - write_volatile((&mut PIM_CONFIG_REGION as *mut u8).offset(index), b'\0'); - // PIM_CONFIG_REGION[..s.len()].copy_from_slice(s.as_bytes()); - // PIM_CONFIG_REGION[s.len()] = b'\0'; - - if cfg!(feature = "cacheless") { - // Be pessimistic so that config region is not optimized away - core::hint::black_box(PIM_CONFIG_REGION); - } else { - // Flush all cache lines that were affected by write operation - for element in PIM_CONFIG_REGION[..s.len()].iter() { - asm!("dc civac, {val}", val = in(reg) element); - } - - barrier::dsb(barrier::SY); - } + barrier::dsb(barrier::SY); } } } diff --git a/pim-os/src/pim/kernel.rs b/pim-os/src/pim/kernel.rs index efbd3ea..eb48da6 100644 --- a/pim-os/src/pim/kernel.rs +++ b/pim-os/src/pim/kernel.rs @@ -1,533 +1,4 @@ -use super::{ - array::{DummyArray, PimMatrixArena, PimRegion, PimScalarArena}, - state::PimState, -}; -use pim_isa::{BankMode, File, Instruction, Kernel}; - -pub const MATRIX_ADD: 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::GrfA { index: 0 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 1 }, - dst: File::GrfA { index: 1 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 2 }, - dst: File::GrfA { index: 2 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 3 }, - dst: File::GrfA { index: 3 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 4 }, - dst: File::GrfA { index: 4 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 5 }, - dst: File::GrfA { index: 5 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 6 }, - dst: File::GrfA { index: 6 }, - aam: false, - }, - Instruction::ADD { - src0: File::Bank, - src1: File::GrfA { index: 7 }, - dst: File::GrfA { index: 7 }, - aam: false, - }, - Instruction::FILL { - src: File::GrfA { index: 0 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 1 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 2 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 3 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 4 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 5 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 6 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 7 }, - dst: File::Bank, - }, - Instruction::EXIT, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, -]); - -pub fn execute_matrix_add( - pim_state: &mut PimState, - pim_matrix_arena0: &PimMatrixArena, - pim_matrix_arena1: &PimMatrixArena, - pim_matrix_arena2: &mut PimMatrixArena, - dummy_array: &DummyArray, -) { - pim_state.set_bank_mode(BankMode::PimAllBank); - - for column in 0..C { - for row in 0..R { - pim_matrix_arena0.execute_instruction_read_single_bank(column * R + row); - } - - for row in 0..R { - pim_matrix_arena1.execute_instruction_read_single_bank(column * R + row); - } - - for row in 0..R { - pim_matrix_arena2.execute_instruction_write_single_bank(column * R + row); - } - - dummy_array.execute_instruction_read_single_bank(0); - } - - pim_state.set_bank_mode(BankMode::SingleBank); -} - -pub const MATRIX_MUL: 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::MAC { - src0: File::Bank, - src1: File::GrfA { index: 0 }, - src2: File::GrfB { index: 0 }, - dst: File::GrfB { index: 0 }, - aam: true, - }, - Instruction::JUMP { - offset: -1, - count: 63, - }, - 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, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, -]); - -// Vlt in der Thesis kurz erwähnen und dann zu AAM überleiten -// pub fn execute_matrix_multiply_elementwise( -// pim_state: &mut PimState, -// pim_matrix_arena0: &mut PimMatrixArena, -// pim_matrix_arena1: &mut PimMatrixArena, -// pim_matrix_arena2: &mut PimMatrixArena, -// dummy_array: &mut DummyArray, -// ) { -// pim_state.set_bank_mode(BankMode::PimAllBank); - -// for i in 0..(R * C) { -// let start_column = i % R; -// let start_row = (i / R) * R; - -// for j in 0..C { -// pim_matrix_arena0.execute_instruction_read_single_bank(start_column + R * j); -// } - -// for j in 0..R { -// pim_matrix_arena1.execute_instruction_read_single_bank(start_row + j); -// } - -// pim_matrix_arena2.execute_instruction_write_single_bank(i); - -// dummy_array.execute_instruction_read_single_bank(0); -// } - -// pim_state.set_bank_mode(BankMode::SingleBank); -// } - -pub fn execute_matrix_multiply( - pim_state: &mut PimState, - pim_matrix_arena0: &PimMatrixArena, - pim_matrix_arena1: &PimMatrixArena, - pim_matrix_arena2: &mut PimMatrixArena, - dummy_array: &DummyArray, -) { - pim_state.set_bank_mode(BankMode::PimAllBank); - - for row in 0..R { - for i in 0..C { - pim_matrix_arena0.execute_instruction_read_single_bank(row + R * i); - } - - for column in 0..C { - for i in 0..R { - pim_matrix_arena1.execute_instruction_read_single_bank(column * R + i); - } - } - - for column in 0..C { - pim_matrix_arena2.execute_instruction_write_single_bank(column * R + row); - } - - dummy_array.execute_instruction_read_single_bank(0); - } - - pim_state.set_bank_mode(BankMode::SingleBank); -} - -pub const MATRIX_VECTOR_MUL: 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::MAC { - src0: File::Bank, - src1: File::GrfA { index: 0 }, - src2: File::GrfB { index: 0 }, - dst: File::GrfB { index: 0 }, - aam: true, - }, - Instruction::JUMP { - offset: -1, - count: 7, - }, - Instruction::FILL { - src: File::GrfB { index: 0 }, - dst: File::Bank, - }, - Instruction::EXIT, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, -]); - -pub fn execute_matrix_vector_multiply( - pim_state: &mut PimState, - pim_matrix_arena0: &PimMatrixArena, - pim_matrix_arena1: &PimMatrixArena, - pim_matrix_arena2: &mut PimMatrixArena, - dummy_array: &DummyArray, -) { - pim_state.set_bank_mode(BankMode::PimAllBank); - - for row in 0..R { - for i in 0..C { - pim_matrix_arena0.execute_instruction_read_single_bank(row + R * i); - } - - for i in 0..R { - pim_matrix_arena1.execute_instruction_read_single_bank(i); - } - - pim_matrix_arena2.execute_instruction_write_single_bank(row); - - dummy_array.execute_instruction_read_single_bank(0); - } - - pim_state.set_bank_mode(BankMode::SingleBank); -} - -pub const MATRIX_SCALAR_MUL: Kernel = Kernel([ - Instruction::MOV { - src: File::Bank, - dst: File::SrfM { index: 0 }, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 0 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 1 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 2 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 3 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 4 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 5 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 6 }, - aam: false, - }, - Instruction::MUL { - src0: File::Bank, - src1: File::SrfM { index: 0 }, - dst: File::GrfA { index: 7 }, - aam: false, - }, - Instruction::FILL { - src: File::GrfA { index: 0 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 1 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 2 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 3 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 4 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 5 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 6 }, - dst: File::Bank, - }, - Instruction::FILL { - src: File::GrfA { index: 7 }, - dst: File::Bank, - }, - Instruction::EXIT, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, - Instruction::NOP, -]); - -pub fn execute_matrix_scalar_multiply( - pim_state: &mut PimState, - pim_scalar_arena: &PimScalarArena, - pim_matrix_arena0: &PimMatrixArena, - pim_matrix_arena1: &mut PimMatrixArena, - dummy_array: &DummyArray, -) { - pim_state.set_bank_mode(BankMode::PimAllBank); - - for column in 0..C { - pim_scalar_arena.execute_instruction_read_single_bank(0); - - for i in 0..R { - pim_matrix_arena0.execute_instruction_read_single_bank(column * R + i); - } - - for i in 0..R { - pim_matrix_arena1.execute_instruction_write_single_bank(column * R + i); - } - - dummy_array.execute_instruction_read_single_bank(0); - } - - pim_state.set_bank_mode(BankMode::SingleBank); -} +pub mod matrix_matrix_add; +pub mod matrix_matrix_mul; +pub mod matrix_scalar_mul; +pub mod matrix_vector_mul; diff --git a/pim-os/src/pim/kernel/matrix_matrix_add.rs b/pim-os/src/pim/kernel/matrix_matrix_add.rs new file mode 100644 index 0000000..de2a9a6 --- /dev/null +++ b/pim-os/src/pim/kernel/matrix_matrix_add.rs @@ -0,0 +1,148 @@ +use crate::pim::array::{DummyArray, PimMatrixArena, PimRegion}; +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::GrfA { index: 0 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 1 }, + dst: File::GrfA { index: 1 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 2 }, + dst: File::GrfA { index: 2 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 3 }, + dst: File::GrfA { index: 3 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 4 }, + dst: File::GrfA { index: 4 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 5 }, + dst: File::GrfA { index: 5 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 6 }, + dst: File::GrfA { index: 6 }, + aam: false, + }, + Instruction::ADD { + src0: File::Bank, + src1: File::GrfA { index: 7 }, + dst: File::GrfA { index: 7 }, + aam: false, + }, + Instruction::FILL { + src: File::GrfA { index: 0 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 1 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 2 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 3 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 4 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 5 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 6 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 7 }, + dst: File::Bank, + }, + Instruction::EXIT, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, +]); + +pub fn execute( + pim_matrix_arena0: &PimMatrixArena, + pim_matrix_arena1: &PimMatrixArena, + pim_matrix_arena2: &mut PimMatrixArena, + dummy_array: &DummyArray, +) { + for column in 0..C { + for row in 0..R { + pim_matrix_arena0.execute_instruction_read_single_bank(column * R + row); + } + + for row in 0..R { + pim_matrix_arena1.execute_instruction_read_single_bank(column * R + row); + } + + for row in 0..R { + pim_matrix_arena2.execute_instruction_write_single_bank(column * R + row); + } + + dummy_array.execute_instruction_read_single_bank(0); + } +} \ No newline at end of file diff --git a/pim-os/src/pim/kernel/matrix_matrix_mul.rs b/pim-os/src/pim/kernel/matrix_matrix_mul.rs new file mode 100644 index 0000000..b7d4106 --- /dev/null +++ b/pim-os/src/pim/kernel/matrix_matrix_mul.rs @@ -0,0 +1,153 @@ +use crate::pim::array::{DummyArray, PimMatrixArena, PimRegion}; +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::MAC { + src0: File::Bank, + src1: File::GrfA { index: 0 }, + src2: File::GrfB { index: 0 }, + dst: File::GrfB { index: 0 }, + aam: true, + }, + Instruction::JUMP { + offset: -1, + count: 63, + }, + 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, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, +]); + +// Vlt in der Thesis kurz erwähnen und dann zu AAM überleiten +// pub fn execute_matrix_multiply_elementwise( +// pim_state: &mut PimState, +// pim_matrix_arena0: &mut PimMatrixArena, +// pim_matrix_arena1: &mut PimMatrixArena, +// pim_matrix_arena2: &mut PimMatrixArena, +// dummy_array: &mut DummyArray, +// ) { +// set_bank_mode(BankMode::PimAllBank); + +// for i in 0..(R * C) { +// let start_column = i % R; +// let start_row = (i / R) * R; + +// for j in 0..C { +// pim_matrix_arena0.execute_instruction_read_single_bank(start_column + R * j); +// } + +// for j in 0..R { +// pim_matrix_arena1.execute_instruction_read_single_bank(start_row + j); +// } + +// pim_matrix_arena2.execute_instruction_write_single_bank(i); + +// dummy_array.execute_instruction_read_single_bank(0); +// } + +// set_bank_mode(BankMode::SingleBank); +// } + +const MATRIX_DIMENSION: usize = 8; + +pub fn execute( + pim_matrix_arena0: &PimMatrixArena, + pim_matrix_arena1: &PimMatrixArena, + pim_matrix_arena2: &mut PimMatrixArena, + dummy_array: &DummyArray, +) { + for row in 0..MATRIX_DIMENSION { + for i in 0..MATRIX_DIMENSION { + pim_matrix_arena0.execute_instruction_read_single_bank(row + MATRIX_DIMENSION * i); + } + + for column in 0..MATRIX_DIMENSION { + for i in 0..MATRIX_DIMENSION { + pim_matrix_arena1 + .execute_instruction_read_single_bank(column * MATRIX_DIMENSION + i); + } + } + + for column in 0..MATRIX_DIMENSION { + pim_matrix_arena2 + .execute_instruction_write_single_bank(column * MATRIX_DIMENSION + row); + } + + dummy_array.execute_instruction_read_single_bank(0); + } +} diff --git a/pim-os/src/pim/kernel/matrix_scalar_mul.rs b/pim-os/src/pim/kernel/matrix_scalar_mul.rs new file mode 100644 index 0000000..eab0b1e --- /dev/null +++ b/pim-os/src/pim/kernel/matrix_scalar_mul.rs @@ -0,0 +1,125 @@ +use crate::pim::array::{DummyArray, PimMatrixArena, PimRegion, PimScalarArena}; +use pim_isa::{File, Instruction, Kernel}; + +pub const KERNEL: Kernel = Kernel([ + Instruction::MOV { + src: File::Bank, + dst: File::SrfM { index: 0 }, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 0 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 1 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 2 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 3 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 4 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 5 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 6 }, + aam: false, + }, + Instruction::MUL { + src0: File::Bank, + src1: File::SrfM { index: 0 }, + dst: File::GrfA { index: 7 }, + aam: false, + }, + Instruction::FILL { + src: File::GrfA { index: 0 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 1 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 2 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 3 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 4 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 5 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 6 }, + dst: File::Bank, + }, + Instruction::FILL { + src: File::GrfA { index: 7 }, + dst: File::Bank, + }, + Instruction::EXIT, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, +]); + +pub fn execute( + pim_scalar_arena: &PimScalarArena, + pim_matrix_arena0: &PimMatrixArena, + pim_matrix_arena1: &mut PimMatrixArena, + dummy_array: &DummyArray, +) { + for column in 0..C { + pim_scalar_arena.execute_instruction_read_single_bank(0); + + for i in 0..R { + pim_matrix_arena0.execute_instruction_read_single_bank(column * R + i); + } + + for i in 0..R { + pim_matrix_arena1.execute_instruction_write_single_bank(column * R + i); + } + + dummy_array.execute_instruction_read_single_bank(0); + } +} \ No newline at end of file diff --git a/pim-os/src/pim/kernel/matrix_vector_mul.rs b/pim-os/src/pim/kernel/matrix_vector_mul.rs new file mode 100644 index 0000000..3313100 --- /dev/null +++ b/pim-os/src/pim/kernel/matrix_vector_mul.rs @@ -0,0 +1,94 @@ +use crate::pim::array::{DummyArray, PimMatrixArena, PimRegion}; +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::MAC { + src0: File::Bank, + src1: File::GrfA { index: 0 }, + src2: File::GrfB { index: 0 }, + dst: File::GrfB { index: 0 }, + aam: true, + }, + Instruction::JUMP { + offset: -1, + count: 7, + }, + Instruction::FILL { + src: File::GrfB { index: 0 }, + dst: File::Bank, + }, + Instruction::EXIT, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, + Instruction::NOP, +]); + +pub fn execute( + pim_matrix_arena0: &PimMatrixArena, + pim_matrix_arena1: &PimMatrixArena, + pim_matrix_arena2: &mut PimMatrixArena, + dummy_array: &DummyArray, +) { + for row in 0..R { + for i in 0..C { + pim_matrix_arena0.execute_instruction_read_single_bank(row + R * i); + } + + for i in 0..R { + pim_matrix_arena1.execute_instruction_read_single_bank(i); + } + + pim_matrix_arena2.execute_instruction_write_single_bank(row); + + dummy_array.execute_instruction_read_single_bank(0); + } +} diff --git a/pim-os/src/pim/state.rs b/pim-os/src/pim/state.rs index bc6ea2b..d347b4b 100644 --- a/pim-os/src/pim/state.rs +++ b/pim-os/src/pim/state.rs @@ -1,42 +1,25 @@ - - -use super::config::PimWriter; +use super::config; use pim_isa::{BankMode, Kernel, PimConfig}; -#[derive(Debug)] -pub struct PimState { - kernel: Kernel, - writer: PimWriter, +// TODO return token and return to singlebank when dropped +pub fn set_bank_mode(bank_mode: BankMode) { + config::write( + serde_json_core::to_string::(&PimConfig { + kernel: None, + bank_mode: Some(bank_mode), + }) + .unwrap() + .as_str(), + ); } -impl PimState { - pub fn new(kernel: &Kernel) -> Self { - Self { - kernel: kernel.clone(), - writer: PimWriter, - } - } - - // TODO return token and return to singlebank when dropped - pub fn set_bank_mode(&mut self, bank_mode: BankMode) { - self.writer.write( - serde_json_core::to_string::(&PimConfig { - kernel: None, - bank_mode: Some(bank_mode), - }) - .unwrap() - .as_str(), - ); - } - - pub fn set_kernel(&mut self) { - self.writer.write( - serde_json_core::to_string::(&PimConfig { - kernel: Some(self.kernel.clone()), - bank_mode: None, - }) - .unwrap() - .as_str(), - ); - } +pub fn set_kernel(kernel: &Kernel) { + config::write( + serde_json_core::to_string::(&PimConfig { + kernel: Some(kernel.clone()), + bank_mode: None, + }) + .unwrap() + .as_str(), + ); } diff --git a/pim-os/src/pim/vector.rs b/pim-os/src/pim/vector.rs index 90345f0..cc55721 100644 --- a/pim-os/src/pim/vector.rs +++ b/pim-os/src/pim/vector.rs @@ -63,22 +63,9 @@ impl core::ops::MulAssign for F16x1 { } #[repr(C)] -#[derive(Default, Clone, Copy, PartialEq)] +#[derive(Default, Debug, Clone, Copy, PartialEq)] pub struct F16x16(pub [F16x1; FLOATING_POINT_UNITS]); -// TODO remove -impl core::fmt::Debug for F16x16 { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - self.0[0].fmt(f) - } -} - -impl core::fmt::Display for F16x16 { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - self.0[0].fmt(f) - } -} - impl num_traits::identities::Zero for F16x16 { fn zero() -> Self { Self([F16x1::zero(); FLOATING_POINT_UNITS]) diff --git a/pim-vm/Cargo.lock b/pim-vm/Cargo.lock index 989fb38..550e4b3 100644 --- a/pim-vm/Cargo.lock +++ b/pim-vm/Cargo.lock @@ -40,9 +40,9 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[package]] name = "cxx" -version = "1.0.110" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7129e341034ecb940c9072817cd9007974ea696844fc4dd582dc1653a7fbe2e8" +checksum = "8de00f15a6fa069c99b88c5c78c4541d0e7899a33b86f7480e23df2431fce0bc" dependencies = [ "cc", "cxxbridge-flags", @@ -52,15 +52,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.110" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06fdd177fc61050d63f67f5bd6351fac6ab5526694ea8e359cd9cd3b75857f44" +checksum = "6f3fed61d56ba497c4efef9144dfdbaa25aa58f2f6b3a7cf441d4591c583745c" [[package]] name = "cxxbridge-macro" -version = "1.0.110" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "587663dd5fb3d10932c8aecfe7c844db1bcf0aee93eeab08fac13dc1212c2e7f" +checksum = "8908e380a8efd42150c017b0cfa31509fc49b6d47f7cb6b33e93ffb8f4e3661e" dependencies = [ "proc-macro2", "quote", @@ -87,7 +87,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -114,26 +114,26 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "is-terminal" -version = "0.4.9" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" dependencies = [ "hermit-abi", "rustix", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] name = "itoa" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "libc" -version = "0.2.150" +version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" [[package]] name = "link-cplusplus" @@ -158,9 +158,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.6.4" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "pim-isa" @@ -183,18 +183,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.70" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -230,37 +230,37 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rustix" -version = "0.38.26" +version = "0.38.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9470c4bf8246c8daf25f9598dca807fb6510347b1e1cfa55749113850c79d88a" +checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] name = "ryu" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", @@ -269,9 +269,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" dependencies = [ "itoa", "ryu", @@ -280,9 +280,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.39" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -335,37 +335,13 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.0", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", + "windows-targets", ] [[package]] @@ -374,93 +350,51 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - [[package]] name = "windows_aarch64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - [[package]] name = "windows_i686_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - [[package]] name = "windows_i686_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - [[package]] name = "windows_x86_64_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - [[package]] name = "windows_x86_64_msvc" version = "0.52.0"