From 64a3805619d16ecc0149d2c2417248be6eff9c30 Mon Sep 17 00:00:00 2001 From: NiccoloN Date: Wed, 13 May 2026 16:59:43 +0200 Subject: [PATCH] fix pim-simulator stale tests --- .../src/lib/binary_to_instruction/mod.rs | 6 +- .../src/lib/json_to_instruction/mod.rs | 2 +- .../pim/pim-simulator/tests/big_mul.rs | 25 ++-- .../pim/pim-simulator/tests/es_runner.rs | 118 +++++++++++++----- .../pim/pim-simulator/tests/placeholder.rs | 12 +- .../pim/pim-simulator/tests/simd.rs | 35 +++--- .../pim/pim-simulator/tests/sync.rs | 35 +++--- 7 files changed, 149 insertions(+), 84 deletions(-) diff --git a/backend-simulators/pim/pim-simulator/src/lib/binary_to_instruction/mod.rs b/backend-simulators/pim/pim-simulator/src/lib/binary_to_instruction/mod.rs index ecad8a1..ce14896 100644 --- a/backend-simulators/pim/pim-simulator/src/lib/binary_to_instruction/mod.rs +++ b/backend-simulators/pim/pim-simulator/src/lib/binary_to_instruction/mod.rs @@ -392,6 +392,7 @@ mod tests { HEADER_SIZE, InstructionRecord, MAGIC, RECORD_SIZE, VERSION, binary_to_instructions, }; use crate::{ + functor_to_name, instruction_set::{InstructionsBuilder, instruction_data::InstructionDataBuilder}, json_to_instruction::json_isa::json_to_instruction, }; @@ -486,7 +487,10 @@ mod tests { assert_eq!(json_instructions.len(), binary_instructions.len()); for (json_inst, binary_inst) in json_instructions.iter().zip(binary_instructions.iter()) { - assert_eq!(json_inst.functor_name(), binary_inst.functor_name()); + assert_eq!( + functor_to_name(json_inst.functor as usize), + functor_to_name(binary_inst.functor as usize) + ); assert_eq!(json_inst.data, binary_inst.data); } } diff --git a/backend-simulators/pim/pim-simulator/src/lib/json_to_instruction/mod.rs b/backend-simulators/pim/pim-simulator/src/lib/json_to_instruction/mod.rs index fbc44e6..3efbf59 100644 --- a/backend-simulators/pim/pim-simulator/src/lib/json_to_instruction/mod.rs +++ b/backend-simulators/pim/pim-simulator/src/lib/json_to_instruction/mod.rs @@ -1,2 +1,2 @@ -mod json_isa; +pub(crate) mod json_isa; pub mod json_to_executor; diff --git a/backend-simulators/pim/pim-simulator/tests/big_mul.rs b/backend-simulators/pim/pim-simulator/tests/big_mul.rs index 4f898e0..c66ec0b 100644 --- a/backend-simulators/pim/pim-simulator/tests/big_mul.rs +++ b/backend-simulators/pim/pim-simulator/tests/big_mul.rs @@ -1,6 +1,11 @@ use std::path::Path; -use pimcore::{Executable, cpu::CPU, instruction_set::{InstructionsBuilder, instruction_data::InstructionDataBuilder, isa::*}}; +use pimcore::{ + Executable, + cpu::crossbar::Crossbar, + instruction_set::{InstructionsBuilder, instruction_data::InstructionDataBuilder, isa::*}, + memory_manager::CoreMemory, +}; fn simple_read(path: &Path) -> Vec { if !path.exists() { @@ -17,14 +22,12 @@ fn simple_read(path: &Path) -> Vec { fn mvmul_f32(err: &str) where { - let mut cpu = CPU::new(0); - cpu.reserve_crossbar(1, 1024 * size_of::(), 1024); - let (memory, crossbars) = cpu.host().get_memory_crossbar(); - let matrix = simple_read(Path::new("B.txt")) ; - - - crossbars.get_mut(0).unwrap().execute_store( &matrix).unwrap(); - let vector = simple_read(Path::new("A.txt")); + let matrix = simple_read(Path::new("tests/B.txt")); + let mut crossbar = Crossbar::new(1024 * size_of::(), 1024, CoreMemory::new()); + crossbar.execute_store(&matrix).unwrap(); + let mut cpu = pimcore::cpu::CPU::new(0, vec![vec![&crossbar]]); + let (memory, _) = cpu.host().get_memory_crossbar(); + let vector = simple_read(Path::new("tests/A.txt")); memory.execute_store(0, &vector).unwrap(); let mut inst_builder = InstructionsBuilder::new(); @@ -57,7 +60,7 @@ where .cpu_mut() .host() .load::(1024 * size_of::(), 1024*size_of::()).unwrap()[0].iter().zip( - simple_read(Path::new("X.txt")) ).all(|(&a,b) : (&f32, f32)| {a-b < 0.001}), + simple_read(Path::new("tests/X.txt")) ).all(|(&a,b) : (&f32, f32)| {a-b < 0.001}), "Wrong result for {}", err ); @@ -69,5 +72,3 @@ fn mvmul_big_test() { } - - diff --git a/backend-simulators/pim/pim-simulator/tests/es_runner.rs b/backend-simulators/pim/pim-simulator/tests/es_runner.rs index 40e0e5e..8cd90a2 100644 --- a/backend-simulators/pim/pim-simulator/tests/es_runner.rs +++ b/backend-simulators/pim/pim-simulator/tests/es_runner.rs @@ -1,51 +1,103 @@ -use std::{fs, io::BufReader, path::Path}; +use std::{ + fs::{self, File}, + io::BufReader, + path::{Path, PathBuf}, +}; use anyhow::{Context, Result}; -use pimcore::json_to_instruction::json_to_executor; +use pimcore::{ + cpu::crossbar::Crossbar, + json_to_instruction::json_to_executor, + memory_manager::CoreMemory, +}; use serde_json::Value; -fn collect_json_from_subfolders>(root: P) -> Result)>> { +fn collect_examples>(root: P) -> Result> { let mut result = Vec::new(); for entry in fs::read_dir(root)? { let entry = entry.context("Root not found")?; let path = entry.path(); if path.is_dir() { - let mut cores = Vec::new(); - let mut config: Option = None; - for sub_entry in fs::read_dir(&path) - .with_context(|| format!("File {} not readable", path.display()))? - { - let sub_entry = - sub_entry.with_context(|| format!("File {} not readable", path.display()))?; - let sub_path = sub_entry.path(); - if sub_path.is_file() - && sub_path.extension().and_then(|s| s.to_str()) == Some("json") - { - let file = fs::File::open(&sub_path) - .with_context(|| format!("Subpath {} not opened", sub_path.display()))?; - let reader = BufReader::new(file); - let val: Value = serde_json::from_reader(reader).with_context(|| format!( - "Serde reader fail for subpath {}", - sub_path.display() - ))?; - if sub_path.file_name().unwrap() == "config.json" { - config = Some(val); - } else { - cores.push(val); - } - } - } - result.push((config.unwrap(), cores)); + result.push(path); } } Ok(result) } +fn core_sort_key(path: &Path) -> i32 { + let stem = path.file_stem().unwrap().to_str().unwrap(); + stem[5..].parse::().unwrap() +} + +fn crossbar_sort_key(path: &Path) -> i32 { + let stem = path.file_stem().unwrap().to_str().unwrap(); + stem[9..].parse::().unwrap() +} + +fn load_crossbars(folder: &Path, config: &Value) -> Result>> { + let xbar_size = config.get("xbar_size").unwrap().as_array().unwrap(); + let rows = xbar_size[0].as_i64().unwrap() as usize; + let cols = xbar_size[1].as_i64().unwrap() as usize; + let core_cnt = config.get("core_cnt").unwrap().as_i64().unwrap() as usize; + let mut owned_crossbars = Vec::with_capacity(core_cnt + 1); + owned_crossbars.push(Vec::new()); + + for core_idx in 0..core_cnt { + let core_folder = folder.join(format!("core_{core_idx}")); + let mut core_crossbars = Vec::new(); + if core_folder.is_dir() { + let mut paths: Vec<_> = fs::read_dir(&core_folder)? + .map(|entry| entry.map(|entry| entry.path())) + .collect::>>()?; + paths.sort_by_cached_key(|path| crossbar_sort_key(path)); + for path in paths { + if path.extension().and_then(|ext| ext.to_str()) != Some("bin") { + continue; + } + let bytes = fs::read(&path) + .with_context(|| format!("failed to read crossbar {}", path.display()))?; + let mut crossbar = Crossbar::new(cols * 4, rows, CoreMemory::new()); + crossbar.execute_store(&bytes)?; + core_crossbars.push(crossbar); + } + } + owned_crossbars.push(core_crossbars); + } + Ok(owned_crossbars) +} + #[test] fn json_folder_tester() { - let examples = collect_json_from_subfolders("data").unwrap(); - for example in examples { - let (config, cores) = example; - json_to_executor::json_to_executor(config, cores.iter()).execute(); + let examples = collect_examples("tests/data").unwrap(); + for folder in examples { + let config_path = folder.join("config.json"); + let config_file = File::open(&config_path).unwrap(); + let config: Value = serde_json::from_reader(BufReader::new(config_file)).unwrap(); + + let core_cnt = config.get("core_cnt").unwrap().as_i64().unwrap() as usize; + let mut core_paths: Vec<_> = fs::read_dir(&folder) + .unwrap() + .map(|entry| entry.unwrap().path()) + .filter(|path| path.extension().and_then(|ext| ext.to_str()) == Some("json")) + .filter(|path| path.file_name().unwrap() != "config.json") + .collect(); + core_paths.sort_by_cached_key(|path| core_sort_key(path)); + assert_eq!(core_paths.len(), core_cnt); + + let mut core_readers: Vec<_> = core_paths + .into_iter() + .map(|path| BufReader::new(File::open(path).unwrap())) + .collect(); + + let owned_crossbars = load_crossbars(&folder, &config).unwrap(); + let crossbars = owned_crossbars + .iter() + .map(|core_crossbars| core_crossbars.iter().collect()) + .collect(); + + let mut executable = json_to_executor::json_to_executor(config, &mut core_readers, crossbars); + let memory = fs::read(folder.join("memory.bin")).unwrap(); + executable.cpu_mut().host().execute_store(0, &memory).unwrap(); + executable.execute(); } } diff --git a/backend-simulators/pim/pim-simulator/tests/placeholder.rs b/backend-simulators/pim/pim-simulator/tests/placeholder.rs index 4a64c4d..9b335ad 100644 --- a/backend-simulators/pim/pim-simulator/tests/placeholder.rs +++ b/backend-simulators/pim/pim-simulator/tests/placeholder.rs @@ -1,11 +1,17 @@ +mod common; - use pimcore::{Executable, cpu::CPU, instruction_set::{InstructionType, InstructionsBuilder, instruction_data::InstructionDataBuilder, isa::*}}; +use pimcore::{ + Executable, + instruction_set::{ + InstructionType, InstructionsBuilder, instruction_data::InstructionDataBuilder, isa::*, + }, +}; #[test] #[should_panic(expected = "Function not found for the requested size") ] fn wrong_size_place_holder() { - let cpu = CPU::new(0); + let cpu = common::empty_cpu(0); let mut inst_builder = InstructionsBuilder::new(); let mut idata_build = InstructionDataBuilder::new(); idata_build.set_core_indx(0).fix_core_indx(); @@ -30,7 +36,7 @@ fn wrong_size_place_holder() { fn place_holder(inst : InstructionType) { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let mut idata_build = InstructionDataBuilder::new(); idata_build.set_core_indx(0).fix_core_indx(); inst(&mut cpu, idata_build.build()).unwrap(); diff --git a/backend-simulators/pim/pim-simulator/tests/simd.rs b/backend-simulators/pim/pim-simulator/tests/simd.rs index 0728e8e..5a3ea04 100644 --- a/backend-simulators/pim/pim-simulator/tests/simd.rs +++ b/backend-simulators/pim/pim-simulator/tests/simd.rs @@ -1,8 +1,10 @@ +mod common; + use pimcore::{ Executable, - cpu::CPU, + cpu::crossbar::Crossbar, instruction_set::{InstructionsBuilder, instruction_data::InstructionDataBuilder, isa::*}, - memory_manager::{MemoryStorable, type_traits::UpcastDestTraits}, + memory_manager::{CoreMemory, MemoryStorable, type_traits::UpcastDestTraits}, }; /// VVADD Test @@ -11,7 +13,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 1.0.into(), 2.0.into(), @@ -115,7 +117,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 1.0.into(), 2.0.into(), @@ -219,7 +221,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 1.0.into(), 2.0.into(), @@ -323,7 +325,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 1.0.into(), 2.0.into(), @@ -420,7 +422,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 9.0.into(), 2.0.into(), @@ -524,7 +526,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 9.0.into(), 2.0.into(), @@ -562,6 +564,7 @@ where vavg, idata_build .set_rdr1r2(3, 1, 1) + .set_offset_select(1) .set_imm_len(8 * size_of::() as i32) .build(), ); @@ -617,7 +620,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ (-9.0).into(), 2.0.into(), @@ -717,7 +720,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable + UpcastDestTraits, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 0.1.into(), 0.2.into(), @@ -819,7 +822,7 @@ where F: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable + UpcastDestTraits, { - let mut cpu = CPU::new(0); + let mut cpu = common::empty_cpu(0); let buff: [F; _] = [ 0.1.into(), 0.2.into(), @@ -923,9 +926,6 @@ where M: From + std::fmt::Debug + PartialEq + MemoryStorable, T: From + std::fmt::Debug + PartialEq + MemoryStorable + UpcastDestTraits, { - let mut cpu = CPU::new(0); - cpu.reserve_crossbar(1, 4 * size_of::(), 4); - let (memory, crossbars) = cpu.host().get_memory_crossbar(); let matrix: [M; _] = [ 1.0.into(), 2.0.into(), @@ -944,7 +944,10 @@ where 15.0.into(), 16.0.into(), ]; - crossbars.get_mut(0).unwrap().execute_store( &matrix).unwrap(); + let mut crossbar = Crossbar::new(4 * size_of::(), 4, CoreMemory::new()); + crossbar.execute_store(&matrix).unwrap(); + let mut cpu = pimcore::cpu::CPU::new(0, vec![vec![&crossbar]]); + let (memory, _) = cpu.host().get_memory_crossbar(); let vector: [F; _] = [ 1.0.into(), 2.0.into(), @@ -1054,5 +1057,3 @@ fn mvmul_test() { mvmul_test_generic::("mvmul",1); } - - diff --git a/backend-simulators/pim/pim-simulator/tests/sync.rs b/backend-simulators/pim/pim-simulator/tests/sync.rs index 641d381..44450e5 100644 --- a/backend-simulators/pim/pim-simulator/tests/sync.rs +++ b/backend-simulators/pim/pim-simulator/tests/sync.rs @@ -1,12 +1,13 @@ +mod common; + use pimcore::{ Executable, CoreInstructionsBuilder, - cpu::CPU, instruction_set::{InstructionsBuilder, instruction_data::InstructionDataBuilder, isa::*}, }; #[test] fn ld_test() { - let mut cpu = CPU::new(1); + let mut cpu = common::empty_cpu(1); let mut core_instruction_builder = CoreInstructionsBuilder::new(1); let buff: [f32; _] = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, @@ -41,7 +42,7 @@ fn ld_test() { #[test] fn st_test() { - let mut cpu = CPU::new(1); + let mut cpu = common::empty_cpu(1); let mut core_instruction_builder = CoreInstructionsBuilder::new(1); let buff: [f32; _] = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, @@ -76,7 +77,7 @@ fn st_test() { #[test] fn lldi_test() { - let cpu = CPU::new(1); + let cpu = common::empty_cpu(1); let mut core_instruction_builder = CoreInstructionsBuilder::new(1); let mut inst_builder = InstructionsBuilder::new(); let mut idata_build = InstructionDataBuilder::new(); @@ -106,7 +107,7 @@ fn lldi_test() { #[test] fn lmv_test() { - let mut cpu = CPU::new(1); + let mut cpu = common::empty_cpu(1); let mut core_instruction_builder = CoreInstructionsBuilder::new(1); let buff: [f32; _] = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, @@ -148,7 +149,7 @@ fn lmv_test() { #[test] fn simple_send_recv_test() { - let mut cpu = CPU::new(2); + let mut cpu = common::empty_cpu(2); let mut core_instruction_builder = CoreInstructionsBuilder::new(2); let buff: [f32; _] = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, @@ -207,7 +208,7 @@ fn simple_send_recv_test() { #[test] fn multiple_send_recv_test() { - let mut cpu = CPU::new(4); + let mut cpu = common::empty_cpu(4); let mut core_instruction_builder = CoreInstructionsBuilder::new(4); let buff: [f32; _] = [ 1.0, 1.0, 1.0, 1.0, 1.0 @@ -226,7 +227,7 @@ fn multiple_send_recv_test() { ]; cpu.core(4).execute_store(0, &buff).unwrap(); - let send_inst = |cpu :&mut CPU, core_instruction_builder: &mut CoreInstructionsBuilder, inst_builder: &mut InstructionsBuilder, from : i32, to : i32| { + let send_inst = |inst_builder: &mut InstructionsBuilder, from: i32, to: i32| { let mut idata_build = InstructionDataBuilder::new(); idata_build.set_core_indx(from).fix_core_indx(); inst_builder.make_inst(sldi, idata_build.set_rdimm(1, from*size_of::() as i32).build()); @@ -240,7 +241,7 @@ fn multiple_send_recv_test() { ); }; - let recv_inst = |cpu :&mut CPU, core_instruction_builder: &mut CoreInstructionsBuilder, mut inst_builder: &mut InstructionsBuilder, to : i32, from : i32| { + let recv_inst = |inst_builder: &mut InstructionsBuilder, to: i32, from: i32| { let mut idata_build = InstructionDataBuilder::new(); idata_build.set_core_indx(to).fix_core_indx(); inst_builder.make_inst(sldi, idata_build.set_rdimm(1, from*size_of::() as i32).build()); @@ -257,26 +258,26 @@ fn multiple_send_recv_test() { // 1 -> 3 - send_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,1, 3); + send_inst(&mut inst_builder, 1, 3); core_instruction_builder.set_core(1, inst_builder.build()); // 2 -> 3 // 2 <- 4 - send_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,2, 3); - recv_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,2, 4); + send_inst(&mut inst_builder, 2, 3); + recv_inst(&mut inst_builder, 2, 4); core_instruction_builder.set_core(2, inst_builder.build()); // 3 <- 2 // 3 <- 4 // 3 <- 1 - recv_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,3, 2); - recv_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,3, 4); - recv_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,3, 1); + recv_inst(&mut inst_builder, 3, 2); + recv_inst(&mut inst_builder, 3, 4); + recv_inst(&mut inst_builder, 3, 1); core_instruction_builder.set_core(3, inst_builder.build()); // 4 -> 2 // 4 -> 3 - send_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,4, 2); - send_inst(&mut cpu,&mut core_instruction_builder,&mut inst_builder,4, 3); + send_inst(&mut inst_builder, 4, 2); + send_inst(&mut inst_builder, 4, 3); core_instruction_builder.set_core(4, inst_builder.build()); let mut executable = Executable::new(cpu, core_instruction_builder.build());