Midterm Project: Multi-thread program


The aim of the project is to design and write your multi-thread program to run under MT-MOS (a modified version of sim21).
MT-MOS will provide a fixed 4 threads that will run round-robin and one "sync" instruction that will coordinate all threads.

Your programming exercise is to design one program that runs on multi-thread.  The simple case is that you divide your task into four parts that cooperate.  You can communicate them with shared variables (but be mindful of mutex if necessary).  Four parts can synchronise with "sync" instruction.

Example

I will show you one simple example of making 4 threads that cooperate to sum one big vector (array).  Let assume array is 1000 elements of integer.  I divide the task of 4 independent threads, each sum 250 elements. 

bigarray[1000]
sum1, sum2, sum3, sum4

p1()
  sum1 = 0
  for i = 0; i < 250; i++
    sum1 += bigarray[i]

p2()
  sum2 = 0
  for i = 250; i < 500; i++
    sum2 += bigarray[i]

....


each p1(), p2(), p3(), p4() will run on a separate thread.  When they finish the sum, the last job is to add sum1..sum4.  How to do it? 

I will let thread p1 handle to this last addition and let other thread idle.  But before this, all threads must be synchronized.

p1()
  sum1 = 0
  for i = 0; i < 250; i++
    sum1 += bigarray[i]
  sync()
  sum = sum1+sum2+sum3+sum4
  print(sum)

p2()
  sum2 = 0
  for i = 250; i < 500; i++
    sum2 += bigarray[i]
  sync()
  while( 1 )
    a = 0     // idle loop

....

How MT-MOS work

It provides a fix 4 threads.  So, what it has to do is to have four copies of register bank (r0..r31) and their PC.  MT-MOS runs for a fixed interval (may be 1000 instructions) then save current state and restore the state of the next thread.  Your program do not see any interruption.

Special binding of the names p1().. p4() is done at compiler (rz36) to bind p1.pc ... p4.pc properly. You do not have to do any extra work.  However, the names of the threads: p1...p4 are fixed. 

Your work

1  Design and write a multi-thread program in Rz
2  (optional) run it with MT-MOS to validate it.  You can modify your own version of MT-MOS.
3  Write a report how your program work and make some analysis what kind of performance to be expected when MT-MOS is run on multi-core processors.
4  (optional) if you make any "extra" feature on MT-MOS please describe it in your write up.

last update 1 March 2026