Skip to content

Commit fa881f0

Browse files
committed
parallel execution using rayon
1 parent 0bc3d17 commit fa881f0

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ funty = "2.0.0"
1717
clap = { version = "4.5.1", features = ["derive"] }
1818
serde = "1.0.198"
1919
serde_json = "1.0.116"
20+
rayon = "1.10.0"
21+
features = "0.10.0"
22+
kdam = { version = "0.5.1", features = ["rayon"] }

src/aave/agents/borrow_agent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Agent for BorrowAgent {
6161
self.address,
6262
self.pool_address,
6363
self.supply_token_address,
64-
U256::from(10_u128.pow(20)),
64+
U256::from(10_u128.pow(18)),
6565
);
6666
self.has_supplied = true;
6767

@@ -79,7 +79,7 @@ impl Agent for BorrowAgent {
7979

8080
let exp = U256::from(10u128).pow(self.borrow_token_decimals - U256::from(4u128));
8181
// Agent borrows a random fraction of the available to borrow amount
82-
let u = U256::from(rng.gen_range(9000..10000));
82+
let u = U256::from(rng.gen_range(8000..9000));
8383
let available_borrow = exp * available_borrow_base * u / borrow_asset_price;
8484

8585
if available_borrow > U256::ZERO {

src/aave/agents/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub struct AgentStates {
2525
}
2626

2727
#[derive(Debug, Serialize, Deserialize)]
28-
pub struct AgentData {
28+
pub struct SimData {
29+
pub seed: u64,
2930
pub borrow_agents: Vec<Vec<U256>>,
3031
pub liquidation_agents: Vec<Vec<UserData>>,
3132
pub uniswap_price_agent: Vec<(i128, i128)>,

src/aave/mod.rs

+9-19
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ mod initialisation;
66
mod protocol;
77
pub mod types;
88

9-
use agents::AgentData;
9+
pub use agents::SimData;
1010
use verbs_rs::env::GasPriorityValidator;
1111
use verbs_rs::{agent::RecordedAgentSet, sim_runner};
1212

1313
use self::initialisation::initialise_sim;
1414

15-
use serde_json::json;
16-
1715
pub fn aave_sim_from_fork(
1816
seed: u64,
1917
n_steps: usize,
2018
params: types::ForkedSimParameters,
2119
alchemy_key: String,
22-
) -> serde_json::Value {
20+
) -> SimData {
2321
println!("Initialising Simulation");
2422

2523
let validator = GasPriorityValidator {};
@@ -30,33 +28,25 @@ pub fn aave_sim_from_fork(
3028
println!("Running");
3129

3230
sim_runner::run(&mut env, &mut agent_sets, seed, n_steps);
33-
let sim_data = AgentData {
31+
SimData {
32+
seed,
3433
borrow_agents: agent_sets.borrow_agents.take_records(),
3534
liquidation_agents: agent_sets.liquidation_agents.take_records(),
3635
uniswap_price_agent: agent_sets.uniswap_price_agent.take_records(),
3736
uniswap_noise_agents: agent_sets.uniswap_noise_agents.take_records(),
38-
};
39-
let sim_data = json!({
40-
"seed": seed,
41-
"sim_data": sim_data
42-
});
43-
sim_data
37+
}
4438
}
4539

46-
pub fn aave_sim(seed: u64, n_steps: usize, params: types::SimParameters) -> serde_json::Value {
40+
pub fn aave_sim(seed: u64, n_steps: usize, params: types::SimParameters) -> SimData {
4741
let validator = GasPriorityValidator {};
4842
let (mut env, mut agent_sets, _, _, _) = initialise_sim(params, validator);
4943

5044
sim_runner::run(&mut env, &mut agent_sets, seed, n_steps);
51-
let sim_data = AgentData {
45+
SimData {
46+
seed,
5247
borrow_agents: agent_sets.borrow_agents.take_records(),
5348
liquidation_agents: agent_sets.liquidation_agents.take_records(),
5449
uniswap_price_agent: agent_sets.uniswap_price_agent.take_records(),
5550
uniswap_noise_agents: agent_sets.uniswap_noise_agents.take_records(),
56-
};
57-
let sim_data = json!({
58-
"seed": seed,
59-
"sim_data": sim_data
60-
});
61-
sim_data
51+
}
6252
}

src/main.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
use aave::aave_sim;
1+
use aave::SimData;
22
use clap::Parser;
3+
use kdam::TqdmParallelIterator;
4+
use rayon::prelude::*;
5+
36
mod aave;
47

8+
use std::fs;
9+
510
#[derive(Parser, Debug)]
611
#[command(about, long_about = None)]
712
struct Args {
813
/// Random seed
914
#[arg(long)]
10-
seed: u64,
15+
n_seeds: u64,
1116
/// Number of simulation steps
1217
#[arg(long)]
1318
n_steps: usize,
@@ -22,10 +27,10 @@ struct Args {
2227
fn main() {
2328
let args = Args::parse();
2429

25-
let seed = args.seed;
30+
let seeds = Vec::from_iter(10..10 + args.n_seeds);
2631
let n_steps = args.n_steps;
2732

28-
let results = match args.fork {
33+
let results: Vec<SimData> = match args.fork {
2934
true => match args.key {
3035
Some(k) => {
3136
let params = aave::types::ForkedSimParameters {
@@ -40,7 +45,13 @@ fn main() {
4045
block_number: 18564279u64,
4146
};
4247

43-
aave::aave_sim_from_fork(seed, n_steps, params, k)
48+
seeds
49+
.par_iter()
50+
.map(|i| {
51+
let k = k.clone();
52+
aave::aave_sim_from_fork(*i, n_steps, params, k)
53+
})
54+
.collect()
4455
}
4556
None => panic!("Alchemy key argument required for forked simulation"),
4657
},
@@ -50,7 +61,7 @@ fn main() {
5061
n_liquidators: 1,
5162
prices_mu: 0f64,
5263
prices_dt: 0.01f64,
53-
prices_sigma: 0.4f64,
64+
prices_sigma: 0.3f64,
5465
borrow_activation_rate: 0.1f64,
5566
token_a_initial_price: 100000000000i128,
5667
token_b_initial_price: 100000000i128,
@@ -62,7 +73,13 @@ fn main() {
6273
adversarial: false,
6374
uniswap_fee: 500u32,
6475
};
65-
aave_sim(seed, n_steps, params)
76+
seeds
77+
.par_iter()
78+
.tqdm()
79+
.map(|i| aave::aave_sim(*i, n_steps, params))
80+
.collect()
6681
}
6782
};
83+
let json = serde_json::to_string(&results).expect("Could not serialise to json string");
84+
let _ = fs::write("sim_dat.txt", json);
6885
}

0 commit comments

Comments
 (0)