Skip to content
Snippets Groups Projects
Commit b9beeaf0 authored by Russel Arbore's avatar Russel Arbore
Browse files

unsafe matmul example, working!

parent 8fffb27e
No related branches found
No related tags found
1 merge request!24Handle arrays properly in the runtime (part 1)
...@@ -673,6 +673,7 @@ dependencies = [ ...@@ -673,6 +673,7 @@ dependencies = [
name = "hercules_matmul" name = "hercules_matmul"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-std",
"clap", "clap",
"hercules_rt", "hercules_rt",
"rand", "rand",
......
...@@ -52,6 +52,14 @@ fn generate_type_string(ty: &Type, rust_types: &Vec<String>) -> String { ...@@ -52,6 +52,14 @@ fn generate_type_string(ty: &Type, rust_types: &Vec<String>) -> String {
} }
} }
/*
* Emit dynamic constant math to calculate the runtime size of an array, in
* number of bytes.
*/
fn emit_array_size_expression(ty: TypeID, manifest: &ModuleManifest) -> String {
"16".to_string()
}
/* /*
* Generate async Rust code orchestrating partition execution. * Generate async Rust code orchestrating partition execution.
*/ */
...@@ -180,6 +188,30 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro ...@@ -180,6 +188,30 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro
)?; )?;
} }
// Declare all of the array constant memories. We declare them as Boxes
// to allocate the memories. We emit multiplications of the dynamic
// constant dimensions to allocate the whole memory as one contiguous
// range. TODO: emit only the array constants actually used in this
// function.
for (arr_cons_num, arr_cons_id) in manifest.array_cons_ids.iter().enumerate() {
let arr_ty_id = manifest
.constant_types
.iter()
.filter(|(cons_id, _)| arr_cons_id == cons_id)
.next()
.expect("PANIC: Couldn't find type of array constant in manifest.")
.1;
write!(
rust_code,
"let mut arr_cons_{}_vec = vec![0u8; {}];let mut arr_cons_{} = arr_cons_{}_vec.as_mut_ptr();arr_cons_{}_vec.leak();",
arr_cons_num,
emit_array_size_expression(arr_ty_id, manifest),
arr_cons_num,
arr_cons_num,
arr_cons_num,
)?;
}
// The core executor is a Rust loop. We literally run a "control token" // The core executor is a Rust loop. We literally run a "control token"
// as described in the original sea of nodes paper through the // as described in the original sea of nodes paper through the
// partitions to drive execution. // partitions to drive execution.
...@@ -208,7 +240,7 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro ...@@ -208,7 +240,7 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro
id.idx() id.idx()
), ),
PartitionInput::FunctionArgument(idx) => format!("param_{}", idx), PartitionInput::FunctionArgument(idx) => format!("param_{}", idx),
PartitionInput::ArrayConstant(_) => todo!(), PartitionInput::ArrayConstant(num) => format!("arr_cons_{} as _", num),
PartitionInput::DynamicConstant(idx) => format!("dyn_cons_{}", idx), PartitionInput::DynamicConstant(idx) => format!("dyn_cons_{}", idx),
} }
)?; )?;
...@@ -228,6 +260,8 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro ...@@ -228,6 +260,8 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro
"{} = Some(output.{});", "{} = Some(output.{});",
match output { match output {
PartitionOutput::DataOutput(id) => format!("node_{}", id.idx()), PartitionOutput::DataOutput(id) => format!("node_{}", id.idx()),
// TODO: handle partitions with control indicator
// outputs.
PartitionOutput::ControlIndicator => "todo!()".to_string(), PartitionOutput::ControlIndicator => "todo!()".to_string(),
}, },
output_idx output_idx
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
name = "hercules_matmul" name = "hercules_matmul"
version = "0.1.0" version = "0.1.0"
authors = ["Russel Arbore <rarbore2@illinois.edu>"] authors = ["Russel Arbore <rarbore2@illinois.edu>"]
edition = "2021"
[dependencies] [dependencies]
clap = { version = "*", features = ["derive"] } clap = { version = "*", features = ["derive"] }
hercules_rt = { path = "../../hercules_rt" } hercules_rt = { path = "../../hercules_rt" }
rand = "*" rand = "*"
async-std = "*"
extern crate async_std;
extern crate clap; extern crate clap;
extern crate hercules_rt; extern crate hercules_rt;
hercules_rt::use_hbin!("matmul.hbin"); hercules_rt::use_hbin!("matmul.hbin");
fn main() { fn main() {
//let a = [[1.0f32, 2.0f32], [3.0f32, 4.0f32]]; async_std::task::block_on(async {
//let b = [[5.0f32, 6.0f32], [7.0f32, 8.0f32]]; let mut a = vec![1.0, 2.0, 3.0, 4.0];
//let mut c = [[0.0f32, 0.0f32], [0.0f32, 0.0f32]]; let mut b = vec![5.0, 6.0, 7.0, 8.0];
//matmul(&a, &b, &mut c, 2, 2, 2); let c = matmul(a.as_mut_ptr(), b.as_mut_ptr(), 2, 2, 2).await;
//println!("{} {}\n{} {}", c[0][0], c[0][1], c[1][0], c[1][1]); unsafe {
println!(
"[[{}, {}], [{}, {}]]",
*c,
*c.offset(1),
*c.offset(2),
*c.offset(3)
);
}
});
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment