1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sync: channel implementation (#6074)

This commit is contained in:
Uwe Krüger
2020-08-06 15:28:19 +02:00
committed by GitHub
parent 09f1362305
commit 863cf8af60
15 changed files with 1532 additions and 51 deletions

View File

@ -0,0 +1,68 @@
package main
import "fmt"
import "log"
import "os"
import "time"
import "strconv"
func assert_eq(a, b int64) {
if a != b {
log.Fatalf("assertion failed\nleft: %d, right: %d\n", a, b)
}
}
func do_rec(ch chan int32, resch chan int64, n int32) {
var sum int64
var i int32
for i = 0; i < n; i++ {
sum += int64(<- ch)
}
fmt.Println(sum)
resch <- sum
}
func do_send(ch chan int32, start, end int32) {
for i := start; i < end; i++ {
ch <- i
}
}
func main() {
if len(os.Args) != 5 {
log.Fatalf("usage:\n\t%s <nsend> <nrec> <buflen> <nobj>\n", os.Args[0])
}
nsend, _ := strconv.Atoi(os.Args[1])
nrec, _ := strconv.Atoi(os.Args[2])
buflen, _ := strconv.Atoi(os.Args[3])
nobj, _ := strconv.Atoi(os.Args[4])
stopwatch := time.Now()
ch := make(chan int32, buflen)
resch := make(chan int64, 0)
no := nobj
for i := 0; i < nrec; i++ {
n := no / (nrec - i)
go do_rec(ch, resch, int32(n))
no -= n
}
assert_eq(int64(no), 0)
no = nobj
for i := 0; i < nsend; i++ {
n := no / (nsend - i)
end := no
no -= n
go do_send(ch, int32(no), int32(end))
}
assert_eq(int64(no), 0)
var sum int64
for i := 0; i < nrec; i++ {
sum += <-resch
}
elapsed := time.Now().Sub(stopwatch)
rate := float64(nobj)/float64(elapsed.Nanoseconds())*1000.0
duration := 1.0e-09 * float64(elapsed.Nanoseconds())
fmt.Printf("%d objects in %g s (%.2f objs/µs)\n", nobj, duration, rate)
expected_sum := int64(nobj)*int64(nobj-1)/2
fmt.Printf("got: %d, expected: %d\n", sum, expected_sum)
assert_eq(sum, expected_sum)
}

View File

@ -0,0 +1,70 @@
// Channel Benchmark
//
// `nobj` integers are sent thru a channel with queue length`buflen`
// using `nsend` sender threads and `nrec` receiver threads.
//
// The receive threads add all received numbers and send them to the
// main thread where the total sum is compare to the expected value.
import sync
import time
import os
fn do_rec(mut ch sync.Channel, mut resch sync.Channel, n int) {
mut sum := i64(0)
for _ in 0 .. n {
mut a := 0
ch.pop(&a)
sum += a
}
println(sum)
resch.push(&sum)
}
fn do_send(mut ch sync.Channel, start, end int) {
for i in start .. end {
ch.push(&i)
}
}
fn main() {
if os.args.len != 5 {
eprintln('usage:\n\t${os.args[0]} <nsend> <nrec> <buflen> <nobj>')
exit(1)
}
nsend := os.args[1].int()
nrec := os.args[2].int()
buflen := os.args[3].int()
nobj := os.args[4].int()
stopwatch := time.new_stopwatch({})
mut ch := sync.new_channel<int>(buflen)
mut resch := sync.new_channel<i64>(0)
mut no := nobj
for i in 0 .. nrec {
n := no / (nrec - i)
go do_rec(mut ch, mut resch, n)
no -= n
}
assert no == 0
no = nobj
for i in 0 .. nsend {
n := no / (nsend - i)
end := no
no -= n
go do_send(mut ch, no, end)
}
assert no == 0
mut sum := i64(0)
for _ in 0 .. nrec {
mut r := i64(0)
resch.pop(&r)
sum += r
}
elapsed := stopwatch.elapsed()
rate := f64(nobj)/elapsed*time.microsecond
println('$nobj objects in ${f64(elapsed)/time.second} s (${rate:.2f} objs/µs)')
// use sum formula by Gauß to calculate the expected result
expected_sum := i64(nobj)*(nobj-1)/2
println('got: $sum, expected: $expected_sum')
assert sum == expected_sum
}

View File

@ -0,0 +1,23 @@
import sync
const (
num_iterations = 10000
)
fn do_send(mut ch sync.Channel) {
for i in 0 .. num_iterations {
ch.push(&i)
}
}
fn test_channel_buffered() {
mut ch := sync.new_channel<int>(1000)
go do_send(mut ch)
mut sum := i64(0)
for _ in 0 .. num_iterations {
a := 0
ch.pop(&a)
sum += a
}
assert sum == u64(num_iterations)*(num_iterations-1)/2
}

View File

@ -0,0 +1,23 @@
import sync
const (
num_iterations = 10000
)
fn do_send(mut ch sync.Channel) {
for i in 0 .. num_iterations {
ch.push(&i)
}
}
fn test_channel_unbuffered() {
mut ch := sync.new_channel<int>(0)
go do_send(mut ch)
mut sum := i64(0)
for _ in 0 .. num_iterations {
a := 0
ch.pop(&a)
sum += a
}
assert sum == u64(num_iterations)*(num_iterations-1)/2
}

View File

@ -0,0 +1,38 @@
import sync
fn do_rec(mut ch sync.Channel, mut resch sync.Channel) {
mut sum := i64(0)
for _ in 0 .. 2000 {
mut a := 0
ch.pop(&a)
sum += a
}
println(sum)
resch.push(&sum)
}
fn do_send(mut ch sync.Channel) {
for i in 0 .. 2000 {
ch.push(&i)
}
}
fn test_channel_multi_unbuffered() {
mut ch := sync.new_channel<int>(0)
mut resch := sync.new_channel<i64>(0)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
mut sum := i64(0)
for _ in 0 .. 4 {
mut r := i64(0)
resch.pop(&r)
sum += r
}
assert sum == i64(4) * 2000 * (2000 - 1) / 2
}

View File

@ -0,0 +1,38 @@
import sync
fn do_rec(mut ch sync.Channel, mut resch sync.Channel) {
mut sum := i64(0)
for _ in 0 .. 2000 {
mut a := 0
ch.pop(&a)
sum += a
}
println(sum)
resch.push(&sum)
}
fn do_send(mut ch sync.Channel) {
for i in 0 .. 2000 {
ch.push(&i)
}
}
fn test_channel_multi_buffered() {
mut ch := sync.new_channel<int>(100)
mut resch := sync.new_channel<i64>(0)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
mut sum := i64(0)
for _ in 0 .. 4 {
mut r := i64(0)
resch.pop(&r)
sum += r
}
assert sum == i64(4) * 2000 * (2000 - 1) / 2
}

View File

@ -0,0 +1,76 @@
import sync
fn do_rec_i64(mut ch sync.Channel) {
mut sum := i64(0)
for _ in 0 .. 300 {
mut a := i64(0)
ch.pop(&a)
sum += a
}
assert sum == 300 * (300 - 1) / 2
}
fn do_send_int(mut ch sync.Channel) {
for i in 0 .. 300 {
ch.push(&i)
}
}
fn do_send_byte(mut ch sync.Channel) {
for i in 0 .. 300 {
ii := byte(i)
ch.push(&ii)
}
}
fn do_send_i64(mut ch sync.Channel) {
for i in 0 .. 300 {
ii := i64(i)
ch.push(&ii)
}
}
fn test_select() {
mut chi := sync.new_channel<int>(0)
mut chl := sync.new_channel<i64>(1)
mut chb := sync.new_channel<byte>(10)
mut recch := sync.new_channel<i64>(0)
go do_rec_i64(mut recch)
go do_send_int(mut chi)
go do_send_byte(mut chb)
go do_send_i64(mut chl)
mut channels := [chi, recch, chl, chb]
directions := [false, true, false, false]
mut sum := i64(0)
mut rl := i64(0)
mut ri := int(0)
mut rb := byte(0)
mut sl := i64(0)
mut objs := [voidptr(&ri), &sl, &rl, &rb]
for _ in 0 .. 1200 {
idx := sync.channel_select(mut channels, directions, mut objs, 0)
match idx {
0 {
sum += ri
}
1 {
sl++
}
2 {
sum += rl
}
3 {
sum += rb
}
else {
println('got $idx (timeout)')
}
}
}
// Use Gauß' formula for the first 2 contributions
expected_sum := 2 * (300 * (300 - 1) / 2) +
// the 3rd contribution is `byte` and must be seen modulo 256
256 * (256 - 1) / 2 +
44 * (44 - 1) / 2
assert sum == expected_sum
}