faster pim-simulator

This commit is contained in:
NiccoloN
2026-04-21 18:35:51 +02:00
parent 3fa140be25
commit dafc1d15b7
2 changed files with 56 additions and 46 deletions

View File

@@ -19,19 +19,19 @@ pub mod utility;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CoreInstructionsBuilder { pub struct CoreInstructionsBuilder {
core_instructions: Vec<CoreInstruction>, core_instructions: Vec<CoreInstructions>,
} }
impl CoreInstructionsBuilder { impl CoreInstructionsBuilder {
pub fn new(size: usize) -> Self { pub fn new(size: usize) -> Self {
let mut core_instructions = Vec::with_capacity(size); let mut core_instructions = Vec::with_capacity(size);
for _ in 0..=size { for _ in 0..=size {
core_instructions.push(CoreInstruction::empty()); core_instructions.push(CoreInstructions::empty());
} }
Self { core_instructions } Self { core_instructions }
} }
pub fn build(self) -> Vec<CoreInstruction> { pub fn build(self) -> Vec<CoreInstructions> {
self.core_instructions self.core_instructions
} }
@@ -43,12 +43,12 @@ impl CoreInstructionsBuilder {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CoreInstruction { pub struct CoreInstructions {
instructions: Instructions, instructions: Instructions,
program_counter: usize, program_counter: usize,
} }
impl CoreInstruction { impl CoreInstructions {
fn new(instructions: Instructions, program_counter: usize) -> Self { fn new(instructions: Instructions, program_counter: usize) -> Self {
Self { Self {
instructions, instructions,
@@ -64,9 +64,9 @@ impl CoreInstruction {
} }
} }
impl From<Instructions> for CoreInstruction { impl From<Instructions> for CoreInstructions {
fn from(value: Instructions) -> Self { fn from(value: Instructions) -> Self {
CoreInstruction { CoreInstructions {
instructions: value, instructions: value,
program_counter: 0, program_counter: 0,
} }
@@ -76,27 +76,27 @@ impl From<Instructions> for CoreInstruction {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Executable<'a> { pub struct Executable<'a> {
cpu: CPU<'a>, cpu: CPU<'a>,
core_instructions: Vec<CoreInstruction>, core_instructions: Vec<CoreInstructions>,
send_recv: SendRecv, send_recv: SendRecv,
} }
fn print_status(core_instructions: &[CoreInstruction]) { fn print_status(core_instructions: &[CoreInstructions]) {
for (i, core_instruction) in core_instructions.iter().enumerate() { let mut tot_instructions = 0;
println!( let mut progress = 0;
"Core {} : {}% ({}/{}) ", for core_instruction in core_instructions.iter() {
i, tot_instructions += core_instruction.instructions.len();
core_instruction.program_counter as f32 / core_instruction.instructions.len() as f32 progress += core_instruction.program_counter;
* 100.0,
core_instruction.program_counter,
core_instruction.instructions.len()
)
} }
println!(
println!(); "Progress: {}% ({}/{}) ",
progress as f32 / tot_instructions as f32 * 100.0,
progress,
tot_instructions
);
} }
impl<'a> Executable<'a> { impl<'a> Executable<'a> {
pub fn new(cpu: CPU<'a>, core_instructions: Vec<CoreInstruction>) -> Executable<'a> { pub fn new(cpu: CPU<'a>, core_instructions: Vec<CoreInstructions>) -> Executable<'a> {
let num_core = cpu.num_core(); let num_core = cpu.num_core();
let send_recv = SendRecv::new(num_core); let send_recv = SendRecv::new(num_core);
assert_eq!( assert_eq!(
@@ -117,21 +117,21 @@ impl<'a> Executable<'a> {
{ {
let Self { let Self {
cpu, cpu,
core_instructions, core_instructions: cores_instructions,
send_recv, send_recv,
} = self; } = self;
let mut cpu_progressed = 0; let mut cpu_progressed = 0;
let max_core = cpu.num_core(); let max_core = cpu.num_core();
let mut index_unit = 0; let mut cpu_index = 0;
let now = SystemTime::now(); let mut now = SystemTime::now();
while (cpu_progressed > -2) { while (cpu_progressed > -2) {
let mut core_result = InstructionStatus::Completed; let mut core_result = InstructionStatus::Completed;
while core_result.is_completed() while core_result.is_completed()
&& let Some(core_instruction) = core_instructions.get_mut(index_unit) && let Some(core_instruction) = cores_instructions.get_mut(cpu_index)
{ {
core_result = InstructionStatus::NotExecuted; core_result = InstructionStatus::NotExecuted;
let CoreInstruction { let CoreInstructions {
instructions, instructions,
program_counter, program_counter,
} = core_instruction; } = core_instruction;
@@ -144,21 +144,31 @@ impl<'a> Executable<'a> {
cpu_progressed = 0; cpu_progressed = 0;
*program_counter += 1; *program_counter += 1;
} }
if (now.elapsed().unwrap() > Duration::from_secs(1)) {
print_status(&cores_instructions);
now = SystemTime::now();
}
} }
if handle_send_recv(cpu, core_instructions, send_recv, core_result) { handle_wait_sync(cpu, cores_instructions, core_result);
cpu_progressed = 0; match handle_send_recv(cpu, cores_instructions, send_recv, core_result) {
} (true, other_cpu_index) => {
handle_wait_sync(cpu, core_instructions, core_result); cpu_progressed = 0;
index_unit = if index_unit + 1 >= max_core { cpu_index = other_cpu_index;
cpu_progressed -= 1; }
0 (false, 0) => {
} else { cpu_index = if cpu_index + 1 >= cores_instructions.len() {
index_unit + 1 cpu_progressed -= 1;
}; 0
if (now.elapsed().unwrap() > Duration::from_secs(1)) { } else {
print_status(&core_instructions); cpu_index + 1
};
}
(false, other_cpu_index) => {
cpu_index = other_cpu_index;
}
} }
} }
print_status(cores_instructions);
} }
pub fn cpu(&self) -> &CPU<'a> { pub fn cpu(&self) -> &CPU<'a> {
@@ -182,7 +192,7 @@ impl<'a> Executable<'a> {
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 [CoreInstruction], core_instructions: &'c mut [CoreInstructions],
core_result: InstructionStatus, core_result: InstructionStatus,
) where ) where
'a: 'b, 'a: 'b,

View File

@@ -1,7 +1,7 @@
use anyhow::Context; use anyhow::Context;
use crate::{ use crate::{
CoreInstruction, cpu::CPU, instruction_set::InstructionStatus, tracing::TRACER, CoreInstructions, cpu::CPU, instruction_set::InstructionStatus, tracing::TRACER,
utility::add_offset_rd, utility::add_offset_rd,
}; };
@@ -43,14 +43,14 @@ impl SendRecv {
pub fn handle_send_recv<'a, 'b >( pub fn handle_send_recv<'a, 'b >(
cpu: &'b mut CPU<'a>, cpu: &'b mut CPU<'a>,
core_instructions: & mut [CoreInstruction], core_instructions: & mut [CoreInstructions],
send_recv: & mut SendRecv, send_recv: & mut SendRecv,
core_result: InstructionStatus, core_result: InstructionStatus,
) -> bool ) -> (bool, usize)
where 'a : 'b where 'a : 'b
{ {
let transfer_memory = |cpu: &'b mut CPU<'a>, let transfer_memory = |cpu: &'b mut CPU<'a>,
core_instructions: & mut [CoreInstruction], core_instructions: & mut [CoreInstructions],
sender: Option<SendRecvInfo>, sender: Option<SendRecvInfo>,
receiver: Option<SendRecvInfo>| { receiver: Option<SendRecvInfo>| {
if let Some(sender) = sender if let Some(sender) = sender
@@ -119,7 +119,7 @@ where 'a : 'b
send_recv.sending[sender] = None; send_recv.sending[sender] = None;
send_recv.receiving[receiver] = None; send_recv.receiving[receiver] = None;
} }
transfered (transfered, receiver)
} }
InstructionStatus::Reciving(instruction_data) => { InstructionStatus::Reciving(instruction_data) => {
let (core_idx, imm_core) = instruction_data.get_core_immcore(); let (core_idx, imm_core) = instruction_data.get_core_immcore();
@@ -148,8 +148,8 @@ where 'a : 'b
send_recv.sending[sender] = None; send_recv.sending[sender] = None;
send_recv.receiving[receiver] = None; send_recv.receiving[receiver] = None;
} }
transfered (transfered, sender)
} }
_ => false, _ => (false, 0),
} }
} }