02158  Concurrent Programming - Lab : Go Concurrency
Technical University of Denmark DTU
02158 Concurrent Programming        Fall 2024
Lab : Go Concurrency
Home Plan Material  

Purpose

To get aquainted with basic concurrency constructs in the Go langauge.

Time and Place

Assistance for this programming lab will be available in Building 303A, Area EAST, Thursday November 21, 15.00-17.00

Preparations

You should download the command line development tools for Go as desribed in Minilab 5

If you want to use a particular IDE, you may import a proper Go-plugin as described here. However all problems are readily handled with an ordinary text editor and the command line tools.

You should be familiar with the Go concurrency notions as presented at the lectures and have had a look at the corresponding examples.

For further information about the Go language, see golang.org and especially the reference manual at https://golang.org/ref/spec.

Go has an elaborate module notion enabling groups of packages (modules) to interact in a controlled manner. In this lab, however, it suffices to use a more simple approach in which only a single main package with a main() function is compiled and executed using the go run <program>.go command.

Instructions

The lab has two exercises A and B. Exercise A is an introductory exercise, while Exercise B will be part of Mandatory Assignment 4.

A: Voting System

In a small state an election takes place between two candidates, A and B. In order to have transparent result, the votes will be shown in real-time while being counted. From a number of polling stations the results are sent to a broadcaster. For a single test polling station this is shown in the program voting.go. Here the goroutine station simulates a polling station and the main program acts as the broadcaster.

Download the program and run it using the command:

  go run voting.go

Now, for the real election eight polling stations are to be used. The results from these are to be collected through a binary tree of collector nodes each relaying the results from two stations/nodes towards the root node which itself should deliver the results to the broadcaster.

For eight instances of the polling station, setup a pattern of channels and (seven) collector nodes such that the results are shown as soon as the last station has sent its last result.

You may start by writing the collector node and test it using two polling stations and tree dedicated channels. Then generalize to eight stations.

Hint 1: Draw the communication structure! How many channels will you need?
Hint 2: Use a systematic numbering of the collector nodes (and their output channels). If you start giving the root # 1, what would be the numbers of the two childrens of any node # i ?
Hint 3: Declare an array of sufficiently many voting channels and distribute them to the collectors according to your numbering scheme as you go along creating the goroutines.

Option: To reduce the results streams, mitigate the broadcaster to send out the current tally only every two seconds while still collecting results. For this, a regular tick channel may be used.

B: Sieve of Eratosthenes

Given a positive integer constant N you should write two versions of a Go program that finds the first N prime numbers [starting at 2] according to the concurrent version of Eratosthenes' Sieve described in [Andrews 7.6.3]. The solution should print the resulting prime numbers in order like this (for N=5):

    The first 5 prime numbers are:
    2
    3
    5
    7
    11
    Done!

Basic Version

A skeleton program for a solution is found in eratosthenes.go which defines functions for two different goroutines: sieve and odds.

Starting from this program, you should:

When the prime numbers have settled they should be printed in order. This should be accomplished by closing the channels of the pipeline. Specifically:

Hint: Since all numbers communicated are positive, the closedness of a channel can be established if the integer type's "null" value, 0, is received rather than using the ok communication flag.

Try to increase N. Do you consider the concurrent version of Eratosthenes' Sieve an efficent way of finding prime numbers?

Describe your approach and hand in your Go program as eratosthenes1.go

Self-terminating Version

Always sending 10*N odd numbers through the pipeline will generally be a waste. On the other hand, as the density of prime numbers decreases as N grows larger, for N very large, it might not even be enough. Therefore you should now make a modified version of the basic sieve in which the generator routine stops the odd number stream as soon as it detects that the last sieve element has received its prime number.

Describe your deliberation and hand in your solutions as eratosthenes2.go

Happy sifting!

Hans Henrik Løvengreen, Nov 17, 2024