CSP examples in Julia?

Golang’s concurrency primitives were originally inspired by C. A. R. Hoare’s famous 1978 paper “Communicating sequential processes”, (paper, book).

Here’s a blog post where they talk about it:


This git repo re-implements all of the examples in that original paper in Go, so you can directly compare the ideas to Go’s implementation. It’s really nice!

And here’s the fancy godocs documentation page:
https://godoc.org/github.com/thomas11/csp


Does anyone know if there’s a similar such repo in Julia?

Julia’s concurrency primitives derive from the same inspiration, and I’d love to see a side-by-side comparison of good julia implementations of these examples!

Some of these could, I think, be expressed more easily in Julia than in Go. For example, I was working on A Tour of Go, and as far as I can tell in Go there’s no great way to alternate reading from two Channels, one then the other:

I eventually settled on this:

func Same(t1, t2 *tree.Tree) bool {
	ch1 := make(chan int)
	ch2 := make(chan int)
	go Walk(t1, ch1)
	go Walk(t2, ch2)
	for ok1,ok2 := true, true; ok1 && ok2; {
		v1, _ok1 := <-ch1
		v2, _ok2 := <-ch2
		if v1 != v2 {return false }
		ok1,ok2 = _ok1,_ok2
	}
	return true
}

But in Julia, of course, you can just zip them!:

function same(t1::Tree, t2::Tree)
    c1 = Channel(c->walk(t1, c))
    c2 = Channel(c->walk(t2, c))
    for (v1,v2) in zip(c1,c2)
        v1 != v2 && return false
    end
    return true
end

Or, even better,

function same(t1::Tree, t2::Tree)
    c1 = Channel(c->walk(t1, c))
    c2 = Channel(c->walk(t2, c))
    return all(isequal.(c1, c2))
end

If no one knows of an existing CSP examples repo in julia, let’s make one!

2 Likes

If no one knows of an existing CSP examples repo in julia, let’s make one!

I’ve started this empty repo where we can collect the examples if no one else has done this yet:

3 Likes

Fun times, I’ve just enabled multithreading in the CSPExamples repo:
https://github.com/NHDaly/CspExamples.jl/pull/6

It was as easy as setting the parameter to allow all the Tasks I create to be scheduled on multiple threads, and everything else just worked without modifications! :slight_smile:

I didn’t have to change any of the implementations at all, which is the point that’s always emphasized by proponents of this CSP / coroutines+channels architecture: The code structure doesn’t need to change at all to scale your parallelism. You just change the number of threads you’re scheduling on, and that’s it!

2 Likes