Last active
March 20, 2024 13:37
-
-
Save zommiommy/6efbb0659601ba54b912b2dd56990657 to your computer and use it in GitHub Desktop.
Bisect the stack needed to sort a vec of a given size using rayon par sort unstable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | 1 | |
---|---|---|
2 | 1 | |
3 | 1 | |
4 | 1 | |
5 | 1 | |
6 | 1 | |
7 | 1 | |
8 | 1 | |
9 | 1 | |
10 | 1 | |
11 | 1 | |
12 | 1 | |
13 | 1 | |
14 | 1 | |
15 | 1 | |
16 | 1 | |
17 | 1 | |
18 | 1 | |
19 | 1 | |
20 | 4 | |
21 | 1 | |
22 | 32768 | |
23 | 32768 | |
24 | 40898 | |
25 | 44929 | |
26 | 56547 | |
27 | 50856 | |
28 | 48624 | |
29 | 65536 | |
30 | 65536 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::hint::black_box; | |
use rayon::prelude::*; | |
use rand::SeedableRng; | |
use rand::RngCore; | |
use rand::rngs::SmallRng; | |
fn main() { | |
let mut rng = SmallRng::seed_from_u64(0xbad5eed); | |
let mut values = Vec::with_capacity(std::env::var("CAPACITY").unwrap().parse().unwrap()); | |
for _ in 0..values.capacity() { | |
values.push(rng.next_u64()); | |
} | |
values.par_sort_unstable(); | |
let _ = black_box(values); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
def oracle(stack_size, capacity): | |
try: | |
res = subprocess.check_call( | |
"./target/release/examples/bisect", | |
shell=True, | |
env={ | |
**os.environ, | |
"RUST_MIN_STACK": str(int(stack_size)), | |
"CAPACITY": str(int(capacity)) | |
}, | |
stdout=subprocess.PIPE, | |
) | |
return res == 0 | |
except subprocess.CalledProcessError: | |
return False | |
with open("data.csv", "w") as f: | |
for capacity_log in range(1, 33): | |
capacity = 1 << capacity_log | |
print(capacity) | |
guess = 1 | |
m = guess | |
while True: | |
o = True | |
for i in range(4): | |
if not oracle(guess, capacity): | |
o = False | |
break | |
if o: | |
break | |
m = guess | |
guess *= 2 | |
print(f"{o}, {guess}, {m}, {i}") | |
M = guess | |
print(f"{capacity}, {M}, {m}") | |
while M != m: | |
o = True | |
for i in range(40): | |
if not oracle(guess, capacity): | |
o = False | |
break | |
print(f"{o}, {guess}, {M}, {m}, {i}") | |
if o: | |
M = min(guess, M) | |
guess = m + ((M - m) // 2) | |
else: | |
m = max(guess, m) | |
guess = m + ((M - m) // 2) | |
f.write(f"{capacity_log},{guess}\n") | |
f.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment