Static Translation of Stream Program to a Parallel System

Download Report

Transcript Static Translation of Stream Program to a Parallel System

New Go Programming
Language by Google
http://golang.org/
Concurrent Programming
2
Working Principle

It works by taking a stream of all the natural
numbers and introducing a sequence of filters



one for each prime to winnow the multiples of that
prime
At each step we have a sequence of filters of the
primes so far, and the next number to pop out is
the next prime
which triggers the creation of the next filter in the
chain
3
Generate Function
// Send the sequence 2, 3, 4, ... to channel 'ch'. Func
generate(ch chan int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}
4
Filter Function
func filter(in, out chan int, prime int) {
for {
i := <-in; // Receive value of new variable 'i' from 'in'.
if i % prime != 0 {
out <- i // Send 'i' to channel 'out'.
}
}
}
5
Sieve Program
func main() {
ch := make(chan int); // Create a new channel.
go generate(ch); // Start generate() as a goroutine.
for {
prime := <-ch;
fmt.Println(prime);
ch1 := make(chan int);
go filter(ch, ch1, prime);
ch = ch1
}
}
6