Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark #23

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions chorus_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ ureq = "2.7.1"

[dev-dependencies]
chrono = { version = "0.4.26", features = ["serde"] }
criterion = { version = "0.5.1", features = ["html_reports"] }
clap = { version = "4.3.21", features = ["derive"] }
rand = "0.8.5"
termcolor = "1.2.0"

[[bench]]
name = "locally_benchmark"
harness = false

[[bench]]
name = "comm_benchmark"
harness = false
111 changes: 111 additions & 0 deletions chorus_lib/benches/comm_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use chorus_lib::core::{
ChoreoOp, Choreography, ChoreographyLocation, LocationSet, Projector, Transport,
};
use chorus_lib::transport::local::{LocalTransport, LocalTransportChannelBuilder};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use std::thread::spawn;

#[derive(ChoreographyLocation)]
struct Alice;

#[derive(ChoreographyLocation)]
struct Bob;

struct CommChoreography {
n: u64,
}

impl Choreography for CommChoreography {
type L = LocationSet!(Alice, Bob);

fn run(self, op: &impl ChoreoOp<Self::L>) {
for _ in 0..self.n {
op.comm(Bob, Alice, &op.locally::<f32, _, _>(Bob, |_| 1.0));
}
}
}

fn comm_choreography(n: u64) {
let channel = LocalTransportChannelBuilder::new()
.with(Alice)
.with(Bob)
.build();
let mut handles = Vec::new();
{
let channel = channel.clone();
handles.push(spawn(move || {
let transport = LocalTransport::new(Alice, channel);
let projector = Projector::new(Alice, transport);
let c = CommChoreography { n };
projector.epp_and_run(c);
}));
}
{
let channel = channel.clone();
handles.push(spawn(move || {
let transport = LocalTransport::new(Bob, channel);
let projector = Projector::new(Bob, transport);
let c = CommChoreography { n };
projector.epp_and_run(c);
}));
}
for handle in handles {
handle.join().unwrap();
}
}

fn comm_handwritten_alice(n: u64, transport: &LocalTransport<LocationSet!(Bob, Alice), Alice>) {
for _ in 0..n {
transport.receive::<f32>(Bob::name(), Alice::name());
}
}

fn comm_handwritten_bob(n: u64, transport: &LocalTransport<LocationSet!(Bob, Alice), Bob>) {
for _ in 0..n {
transport.send::<f32>(Bob::name(), Alice::name(), &1.0);
}
}

fn comm_handwritten(n: u64) {
let channel = LocalTransportChannelBuilder::new()
.with(Alice)
.with(Bob)
.build();
let mut handles = Vec::new();
{
let channel = channel.clone();
handles.push(spawn(move || {
let transport = LocalTransport::new(Alice, channel);
comm_handwritten_alice(n, &transport);
}));
}
{
let channel = channel.clone();
handles.push(spawn(move || {
let transport = LocalTransport::new(Bob, channel);
comm_handwritten_bob(n, &transport);
}));
}
for handle in handles {
handle.join().unwrap();
}
}

fn bench_comm(c: &mut Criterion) {
let mut group = c.benchmark_group("Comm");
let range = [2000, 4000, 6000, 8000, 10000];
for i in range.iter() {
group.bench_with_input(BenchmarkId::new("Handwritten", i), i, |b, i| {
b.iter(|| comm_handwritten(black_box(*i)))
});
}
for i in range.iter() {
group.bench_with_input(BenchmarkId::new("Choreographic", i), i, |b, i| {
b.iter(|| comm_choreography(black_box(*i)))
});
}
group.finish();
}

criterion_group!(benches, bench_comm);
criterion_main!(benches);
59 changes: 59 additions & 0 deletions chorus_lib/benches/locally_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, Located, LocationSet};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};

#[derive(ChoreographyLocation)]
struct Alice;

struct AddToNLocallyChoreography {
n: f64,
}
impl Choreography<Located<f64, Alice>> for AddToNLocallyChoreography {
type L = LocationSet!(Alice);

fn run(self, op: &impl ChoreoOp<Self::L>) -> Located<f64, Alice> {
let mut i = op.locally(Alice, |_| 0.0);
for _ in 0..self.n.ceil() as u64 {
i = op.locally(Alice, |un| *un.unwrap(&i) + 1.0);
}
return i;
}
}

fn add_to_n_locally_choreographic(n: f64) {
let c = AddToNLocallyChoreography { n };
let channel = chorus_lib::transport::local::LocalTransportChannelBuilder::new()
.with(Alice)
.build();
let transport = chorus_lib::transport::local::LocalTransport::new(Alice, channel);
let projector = chorus_lib::core::Projector::new(Alice, transport);
let result = projector.epp_and_run(c);
assert_eq!(projector.unwrap(result), n);
}

fn add_to_n_locally_handwritten(n: f64) {
let mut a: f64 = 0.0;
for _ in 0..n.ceil() as u64 {
a += 1.0;
}
assert_eq!(a, n);
}

fn bench_add_to_n_locally(c: &mut Criterion) {
let mut group = c.benchmark_group("Locally");
let range = [2000, 4000, 6000, 8000, 10000];
for i in range.iter() {
group.bench_with_input(BenchmarkId::new("Handwritten", i), i, |b, i| {
b.iter(|| add_to_n_locally_handwritten(black_box(*i as f64)))
});
}
for i in range.iter() {
group.bench_with_input(BenchmarkId::new("Choreographic", i), i, |b, i| {
b.iter(|| add_to_n_locally_choreographic(black_box(*i as f64)))
});
}

group.finish();
}

criterion_group!(benches, bench_add_to_n_locally);
criterion_main!(benches);
Loading