94 lines
3.3 KiB
Python
94 lines
3.3 KiB
Python
"""有界并发结果按输入顺序交付;独立参考不依赖 Dever 运行库。"""
|
|
|
|
|
|
def source(language):
|
|
if language == 'dever':
|
|
return '''noop() () {}
|
|
rule pause(value: Int) ()
|
|
| value | do |
|
|
| 0 | dever.time.sleep(1) |
|
|
| other | noop() |
|
|
flow item(value: Int) (out: Int)
|
|
| step | kind | do |
|
|
| - | rule | pause(value % 4096) |
|
|
| out | calc | value |
|
|
loop jobs(n: Int) (out: List<Int>)
|
|
| each value in range(0, n) parallel 64 | result |
|
|
| out | list item(value) |
|
|
checked(value: Int, index: Int) (out: Int) {
|
|
check value == index else Invalid
|
|
out = index + 1
|
|
}
|
|
loop verify(values: List<Int>) (out: Int)
|
|
| each value in values | result |
|
|
| out | fold(index = 0, checked(value, index)) |
|
|
flow measure(n: Int) (out: Int)
|
|
| step | kind | do |
|
|
| values | loop | jobs(n) |
|
|
| out | loop | verify(values) |
|
|
cmd bench = measure
|
|
'''
|
|
if language == 'runtime':
|
|
text = source('rust')
|
|
return text.replace('let values: Vec<i64> = stream::iter(0..n).map(item).buffered(64).try_collect().await?;', """let mut values = Vec::with_capacity(n as usize);
|
|
let mut input = 0..n;
|
|
dever_runtime::ordered_loop!(&'static str, 64,
|
|
async || Ok::<_, &'static str>(input.next()),
|
|
async |value: i64| item(value).await,
|
|
|value| values.push(value)).await?;""")
|
|
if language == 'rust':
|
|
return '''use futures_util::{stream, StreamExt, TryStreamExt};
|
|
async fn item(value: i64) -> Result<i64, &'static str> {
|
|
if value % 4096 == 0 {
|
|
let deadline = tokio::time::Instant::now().checked_add(std::time::Duration::from_millis(1)).ok_or("deadline overflow")?;
|
|
tokio::time::sleep_until(deadline).await;
|
|
}
|
|
Ok(value)
|
|
}
|
|
async fn measure(n: i64) -> Result<i64, &'static str> {
|
|
let values: Vec<i64> = stream::iter(0..n).map(item).buffered(64).try_collect().await?;
|
|
let mut index = 0i64;
|
|
for value in values {
|
|
if value != index { return Err("out of order"); }
|
|
index = index.checked_add(1).expect("count overflow");
|
|
}
|
|
Ok(index)
|
|
}
|
|
fn main() {
|
|
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
|
|
let runtime = tokio::runtime::Builder::new_current_thread().enable_time().build().unwrap();
|
|
println!("{}", runtime.block_on(measure(n)).unwrap());
|
|
}
|
|
'''
|
|
return '''package main
|
|
import ("fmt"; "os"; "strconv"; "time"; "sync")
|
|
func main() {
|
|
n, err := strconv.ParseInt(os.Args[1], 10, 64); if err != nil { panic(err) }
|
|
jobs, results := make(chan int64), make(chan int64, 64)
|
|
var workers sync.WaitGroup
|
|
for i := 0; i < 64; i++ {
|
|
workers.Add(1)
|
|
go func() { defer workers.Done(); for value := range jobs {
|
|
if value % 4096 == 0 { time.Sleep(time.Millisecond) }; results <- value
|
|
} }()
|
|
}
|
|
values := make([]int64, 0, n)
|
|
var ready [64]bool
|
|
var slots [64]int64
|
|
sent, head := int64(0), int64(0)
|
|
for head < n {
|
|
var submit chan int64
|
|
if sent < n && sent-head < 64 { submit = jobs }
|
|
select {
|
|
case submit <- sent: sent++
|
|
case value := <-results:
|
|
ready[value % 64] = true; slots[value % 64] = value
|
|
for ready[head % 64] { ready[head % 64] = false; values = append(values, slots[head % 64]); head++ }
|
|
}
|
|
}
|
|
close(jobs); workers.Wait()
|
|
for index, value := range values { if value != int64(index) { panic("out of order") } }
|
|
fmt.Println(len(values))
|
|
}
|
|
'''
|