Record of some of the computer tech I deal with so that it's documented at least somewhere.

Sunday 4 September 2016

CSP in Julia - Sieve of Eratosthenes

The Sieve of Eratosthenes

Finally I got my head round Julia's CSP technique. The nomenclature is a bit different to what I'm used to. This is using producer/consumer which is single threaded. To do multi threaded uses different primitives, which is a shame. Perhaps one can adapt this setup using multiple RemoteRefs. We'll see.

This algorithm is translated from the Limbo code in Stanley Marbell's book Inferno Programming With Limbo which is still my favourite programming language although I haven't written any code in it for something like 10 years!

function sieve(prime, pipeline) function unsieved() while (u = consume(pipeline)) > 0 if mod(u, prime) != 0 produce(u) end end produce(0) # send end of candidates end println(prime) n = 0 for n in consume(pipeline) if mod(n, prime) == 0 break end end if n > 0 # not end of candiates sieve(n, Task(()->unsieved())) end end const LIMIT = 1024 sieve(2, Task(()->(for i in [3:LIMIT; 0] produce(i); end)))

1 comment:

D. G. said...
This comment has been removed by the author.