Broken pim-sim commit

This commit is contained in:
ilgeco
2026-05-13 13:32:09 +02:00
parent 41de3cb150
commit ad56888b0b
6 changed files with 97 additions and 42 deletions
@@ -34,3 +34,4 @@ plotly = {version="0.8", optional=true}
rayon = "1.12.0" rayon = "1.12.0"
faer = "0.24.0" faer = "0.24.0"
faer-traits = "0.24.0" faer-traits = "0.24.0"
mimalloc = "0.1.50"
@@ -1,3 +1,8 @@
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use clap::Parser; use clap::Parser;
use glob::glob; use glob::glob;
@@ -7,8 +12,8 @@ use pimcore::memory_manager::CoreMemory;
use pimcore::tracing::TRACER; use pimcore::tracing::TRACER;
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{self, read_link}; use std::fs::{self, File, read_link};
use std::io::Write; use std::io::{ BufReader, Write};
use std::path::PathBuf; use std::path::PathBuf;
/// Program to simulate core execution configuration /// Program to simulate core execution configuration
@@ -44,12 +49,12 @@ fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
let config_json = retrive_config(&args)?; let config_json = retrive_config(&args)?;
let core_jsons = retrive_cores(&args)?; let core_jsons = retrive_cores_file(&args)?;
let memory = retrive_memory(&args)?; let memory = retrive_memory(&args)?;
let global_crossbars = get_crossbars(&config_json, &args).unwrap(); let global_crossbars = get_crossbars(&config_json, &args).unwrap();
let crossbars = map_crossbars_to_cores(&config_json, &args, &global_crossbars); let crossbars = map_crossbars_to_cores(&config_json, &args, &global_crossbars);
let mut executor = let mut executor =
json_to_executor::json_to_executor(config_json, core_jsons.iter(), crossbars); json_to_executor::json_to_executor(config_json, core_jsons, crossbars);
set_memory(&mut executor, memory); set_memory(&mut executor, memory);
TRACER TRACER
.lock() .lock()
@@ -140,8 +145,7 @@ fn get_crossbars(config: &Value, args: &Args) -> anyhow::Result<HashMap<String,
} }
let bytes = std::fs::read(weight_file.path()).expect("Failed to read binary file"); let bytes = std::fs::read(weight_file.path()).expect("Failed to read binary file");
let mut crossbar = let mut crossbar = Crossbar::new(column_corssbar * 4, rows_crossbar, CoreMemory::new());
Crossbar::new(column_corssbar * 4, rows_crossbar, CoreMemory::new());
crossbar.execute_store(&bytes).unwrap(); crossbar.execute_store(&bytes).unwrap();
res.insert( res.insert(
weight_file weight_file
@@ -214,15 +218,13 @@ fn retrive_memory(args: &Args) -> Result<Vec<u8>> {
Ok(memory_vector) Ok(memory_vector)
} }
fn retrive_cores(args: &Args) -> Result<Vec<Value>, anyhow::Error> { fn retrive_cores_file(args: &Args) -> Result<Vec<BufReader<File>>, anyhow::Error> {
let mut core_jsons: Vec<Value> = Vec::new(); let mut core_file_reader: Vec<BufReader<File>> = Vec::new();
if let Some(cores_override) = &args.cores { if let Some(cores_override) = &args.cores {
for core in cores_override { for core in cores_override {
let content = fs::read_to_string(core) let file = File::open(core)?;
.with_context(|| format!("Failed to read core file: {:?}", cores_override))?; let reader = BufReader::new(file);
let json: Value = core_file_reader.push(reader);
serde_json::from_str(&content).context("Failed to parse core json override")?;
core_jsons.push(json);
} }
} else if let Some(folder) = args.folder.as_ref() { } else if let Some(folder) = args.folder.as_ref() {
let pattern = folder.join("core*.json"); let pattern = folder.join("core*.json");
@@ -242,17 +244,14 @@ fn retrive_cores(args: &Args) -> Result<Vec<Value>, anyhow::Error> {
bail!("No core*.json files found in {:?}", folder); bail!("No core*.json files found in {:?}", folder);
} }
for entry in paths { for entry in paths {
let path = entry; let file = File::open(entry)?;
let content = fs::read_to_string(&path) let reader = BufReader::new(file);
.with_context(|| format!("Failed to read core file: {:?}", path))?; core_file_reader.push(reader);
let json: Value = serde_json::from_str(&content)
.with_context(|| format!("Failed to parse JSON in {:?}", path))?;
core_jsons.push(json);
} }
} else { } else {
bail!("Either --core or --folder must be provided to find core definitions."); bail!("Either --core or --folder must be provided to find core definitions.");
} }
Ok(core_jsons) Ok(core_file_reader)
} }
fn retrive_config(args: &Args) -> Result<Value, anyhow::Error> { fn retrive_config(args: &Args) -> Result<Value, anyhow::Error> {
@@ -35,7 +35,7 @@ macro_rules! add_name_simd {
}; };
} }
static NAMES: LazyLock<HashMap<usize, &'static str>> = LazyLock::new(|| { pub static NAMES: LazyLock<HashMap<usize, &'static str>> = LazyLock::new(|| {
let mut hash = HashMap::new(); let mut hash = HashMap::new();
add_name!(hash, sldi); add_name!(hash, sldi);
add_name!(hash, sld); add_name!(hash, sld);
@@ -775,10 +775,19 @@ pub fn lmv(cores: &mut CPU, data: InstructionData) -> Result<InstructionStatus>
Ok(InstructionStatus::Completed) Ok(InstructionStatus::Completed)
} }
pub fn isa_send(functor : usize) -> bool{
(send as *const () as usize) == functor
}
pub fn send(cores: &mut CPU, data: InstructionData) -> Result<InstructionStatus> { pub fn send(cores: &mut CPU, data: InstructionData) -> Result<InstructionStatus> {
Ok(InstructionStatus::Sending(data)) Ok(InstructionStatus::Sending(data))
} }
pub fn isa_recv(functor : usize) -> bool{
println!("{} {} ", functor, (recv as *const () as usize));
(recv as *const () as usize) == functor
}
pub fn recv(cores: &mut CPU, data: InstructionData) -> Result<InstructionStatus> { pub fn recv(cores: &mut CPU, data: InstructionData) -> Result<InstructionStatus> {
Ok(InstructionStatus::Reciving(data)) Ok(InstructionStatus::Reciving(data))
} }
@@ -14,7 +14,7 @@ pub mod helper;
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Instruction { pub struct Instruction {
pub data: InstructionData, pub data: InstructionData,
functor: InstructionType, pub functor: InstructionType,
} }
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
@@ -1,11 +1,15 @@
use core::panic; use core::panic;
use std::collections::HashMap; use std::io::{Read, Write};
use std::{collections::HashMap, fs::File, io::BufReader};
use serde_json::{Map, Value}; use serde_json::{Deserializer, Map, Value};
use crate::{ use crate::{
CoreInstructionsBuilder, Executable, CoreInstructionsBuilder, Executable,
cpu::{CPU, crossbar::{self, Crossbar}}, cpu::{
CPU,
crossbar::{self, Crossbar},
},
instruction_set::{ instruction_set::{
InstructionsBuilder, InstructionsBuilder,
instruction_data::{self, InstructionData, InstructionDataBuilder}, instruction_data::{self, InstructionData, InstructionDataBuilder},
@@ -14,11 +18,10 @@ use crate::{
memory_manager::type_traits::TryToUsize, memory_manager::type_traits::TryToUsize,
}; };
pub fn json_to_executor<'a, 'b>( pub fn json_to_executor<'a, 'b>(
config: Value, config: Value,
mut cores: impl Iterator<Item = &'b Value>, mut cores: Vec<BufReader<File>>,
crossbars : Vec<Vec<&'a Crossbar>> crossbars: Vec<Vec<&'a Crossbar>>,
) -> Executable<'a> { ) -> Executable<'a> {
let cell_precision = config.get("cell_precision").unwrap().as_i64().unwrap() as i32; let cell_precision = config.get("cell_precision").unwrap().as_i64().unwrap() as i32;
let core_cnt = config.get("core_cnt").unwrap().as_i64().unwrap() as i32 - 1; let core_cnt = config.get("core_cnt").unwrap().as_i64().unwrap() as i32 - 1;
@@ -29,19 +32,18 @@ pub fn json_to_executor<'a, 'b>(
let mut cpu = CPU::new(core_cnt, crossbars); let mut cpu = CPU::new(core_cnt, crossbars);
let mut core_insts_builder = CoreInstructionsBuilder::new(core_cnt as usize); let mut core_insts_builder = CoreInstructionsBuilder::new(core_cnt as usize);
cores.next(); // Note: cores[0] is intentionally empty and discarded
for core_indx in 1..=core_cnt { for core_indx in 1..=core_cnt {
let mut insts_builder = InstructionsBuilder::new(); let mut insts_builder = InstructionsBuilder::new();
let mut inst_data_builder = InstructionDataBuilder::new(); let mut inst_data_builder = InstructionDataBuilder::new();
inst_data_builder.set_core_indx(core_indx).fix_core_indx(); inst_data_builder.set_core_indx(core_indx).fix_core_indx();
let json_core = cores let stream = Deserializer::from_reader(&mut cores[core_indx as usize]).into_iter::<Value>();
.next()
.unwrap_or_else(|| panic!("cores files less than {}", core_indx )); for (i, json_inst_result) in stream.enumerate() {
let json_core_insts = json_core let json_inst = json_inst_result.expect("Failed to parse instruction");
.as_array() // Pass the single Value to your parser
.unwrap_or_else(|| panic!("core{} has not a list of instruction", core_indx)); json_isa::json_to_instruction(&mut insts_builder, &mut inst_data_builder, &json_inst);
for json_inst in json_core_insts { drop(json_inst);
json_isa::json_to_instruction(&mut insts_builder, &mut inst_data_builder, json_inst);
} }
core_insts_builder.set_core(core_indx, insts_builder.build()); core_insts_builder.set_core(core_indx, insts_builder.build());
} }
@@ -4,7 +4,10 @@ use std::time::{Duration, SystemTime};
use crate::{ use crate::{
cpu::CPU, cpu::CPU,
instruction_set::{Instruction, InstructionStatus, Instructions, isa::functor_to_name}, instruction_set::{
Instruction, InstructionStatus, Instructions,
isa::{NAMES, functor_to_name, isa_recv, isa_send},
},
memory_manager::type_traits::TryToUsize, memory_manager::type_traits::TryToUsize,
send_recv::{SendRecv, handle_send_recv}, send_recv::{SendRecv, handle_send_recv},
tracing::TRACER, tracing::TRACER,
@@ -144,8 +147,9 @@ impl<'a> Executable<'a> {
cpu_progressed = 0; cpu_progressed = 0;
*program_counter += 1; *program_counter += 1;
} }
if (now.elapsed().unwrap() > Duration::from_secs(1)) { if (now.elapsed().unwrap() > Duration::from_secs(5)) {
print_status(&cores_instructions); print_status(cores_instructions);
check_cycle(cpu, cores_instructions, send_recv);
now = SystemTime::now(); now = SystemTime::now();
} }
} }
@@ -193,6 +197,46 @@ impl<'a> Executable<'a> {
} }
} }
fn check_cycle(cpu: &mut CPU, cores_instructions: &[CoreInstructions], send_recv: &mut SendRecv) {
//println!("PRINTAAA");
//*PRINTAAA.lock().unwrap() = true;
//(inst.functor)(cpu, inst.data);
//*PRINTAAA.lock().unwrap() = false;
//
println!("NAMES");
for (a,n) in NAMES.iter(){
println!("{} {}", a, n);
}
let mut i = 0;
for CoreInstructions {
instructions,
program_counter,
} in cores_instructions
{
if *program_counter >= instructions.len() {
continue;
}
let Instruction { data, functor } = instructions[*program_counter];
let functor_address = functor as usize;
print!("Core {} ", i);
i += 1;
match (isa_recv(functor_address), isa_send(functor_address)) {
(true, false) => {
let (receiver, sender) = data.get_core_immcore();
println!("{} <- {}", receiver, sender);
}
(false, true) => {
let (sender, receiver) = data.get_core_immcore();
println!("{} -> {}", receiver, sender);
}
_ => {}
}
}
}
fn handle_wait_sync<'a, 'b, 'c>( fn handle_wait_sync<'a, 'b, 'c>(
cpu: &'b mut CPU<'a>, cpu: &'b mut CPU<'a>,
core_instructions: &'c mut [CoreInstructions], core_instructions: &'c mut [CoreInstructions],