Home News Docs User Guide Do It Yourself Examples Media

Zoo

VIP

Ping Pong

The following code shows a first example of the BondMachine. It is a trival example yet it shows the basic capabilities of the BondMachine architecture and ecosytem. Two Goroutines send an uint8 data value back and forth through IO registers (those created with bondgo.Make), the pong goroutine also increases the value by one before sending it back.

package main

import (
        "bondgo"
)

func pong() {
        var in0 bondgo.Input
        var out0 bondgo.Output

        in0 = bondgo.Make(bondgo.Input, 3)
        out0 = bondgo.Make(bondgo.Output, 5)

        for {
                bondgo.IOWrite(out0, bondgo.IORead(in0)+1)
        }

        bondgo.Void(in0)
        bondgo.Void(out0)
}

func main() {
        var in0 bondgo.Input
        var out0 bondgo.Output

        in0 = bondgo.Make(bondgo.Input, 5)
        out0 = bondgo.Make(bondgo.Output, 3)

device_0:
        go pong()

        for {
                bondgo.IOWrite(out0, bondgo.IORead(in0))
        }

        bondgo.Void(in0)
        bondgo.Void(out0)
}

Compiling the code with the bondgo compiler:

bondgo -input-file pingpong.go -mpm

The toolchain perform the following steps:

  • Map the two goroutines to two hardware cores.
  • Creates two types of core, each one optimized to execute the assigned goroutine.
  • Creates the two binaries.
  • Connected the two core as inferred from the source code, using special IO registers.

The result is a multicore BondMachine:

Multicore Ping Pong

Changing the device_0 label with device_1 the compiler is instructed to put the two goroutines on different BondMachines. Compiling activating the etherbond protocol

bondgo -input-file pingpong.go -mpm -use-etherbond

The result will be the same as before (in term of behaviour) but using a cluster of two BondMachines connected via the etherbond protocol, each running only one goroutine.
The Multicore became a distributed system:

Clustered Ping Pong

Ping Pong Channels

The following code shows a similar behaviour but using channels.

package main

import (
	"bondgo"
)

func pong(c chan uint8) {
	var ball uint8
	for {
		ball = <-c
		ball++
		c <- ball
	}
	bondgo.Void(ball)
}

func main() {
	var ch chan uint8
	var ball uint8

	ball = 1

device_0:
	go pong(ch)

	for {
		select {
		case ch <- ball:
		case ball := <-ch:
		}
	}
	bondgo.Void(ch)
	bondgo.Void(ball)
}