XML

kent logo

CO538 Anonymous Questions and Answers

This page lists the various questions and answers. To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

We have taken the liberty of making some minor typographical corrections to some of the questions as originally put. Although most of the questions here will have been submitted anonymously, this page also serves to answer some questions of general interest to those on the course.

When asking questions, please do not simply paste in your code and ask what is wrong with it, as these types of question are hard to answer in a public forum (resulting in either no answer, or an answer which will only be of use to the person asking the question). In many cases, a question about code can be formulated in general terms. For example, if you have an `IF' block that results in an `incorrect indentation' error, you might ask, ``what is the correct syntax for an occam `if' statement ?''. Questions of this form are easier to answer, and are meaningful to all.

Questions that are not suitable for these public pages (i.e. those that contain assignment-specific code), should be mailed to your seminar-leader.


Question 1:

Submission reference: IN1477

How many swallows does it take to change a parallel process?

Answer 1:

Twelfty.


Question 2:

Submission reference: IN1802

You refer to this out.int method and out.string as being from course.module:

    -- out.int is from "course.module"
    -- out.string is from "course.module"

Where is the documentation for course.module? I need the documentation to be able to output another data type, thanks.

Answer 2:

Both of these, and all the others, are documented in the course library documentation. You can find documentation on all the occam-pi modules here: http://occam-pi.org/occamdoc/ (also linked from the module web-page).

Keywords: course-library


Question 3:

Submission reference: IN1800

What is the gain from learning about concurrency with occam-pi?

This is from the concern that with concurrency being such a large issue today (and in the future) wouldn't it be more practical for students to be learning more mainstream concurrency models/libraries/languages/whatever? For example JCSP, OpenMP, Erlang, or just get lots of teaching about using threads.

Answer 3:

The short answer is because we want you to gain an understanding of concurrency, not just know how to use specific implementations. Moreover, we want to teach you how concurrency can be used as a fundamental design method, and not just as a means to attaining speedup on multiprocessor/multicore platforms. The majority of implementations of "concurrency" ideas suffer from crippling performance overheads, which limits the scope of these ideas to getting speedup on multicore platforms. Only a handful of languages cater specifically for concurrent/parallel programming (although this list is growing!); occam-pi and Erlang are certainly two of these. However, their approaches to concurrency are somewhat different. We believe (with some scientific foundation) that the approach embodied in occam-pi, of processes and communication, is easy, both to learn and apply. Furthermore, there are a handful of experts close by — see the Programming Languages and Systems pages.

Importantly, this module will give you an understanding of concurrency that you can take and apply with specific languages (Java/JCSP, OpenMP, MPI, Erlang, pthreads, ...), as well as put you in a position to understand their limitations.

Keywords: course


Question 4:

Submission reference: IN1803

Can you give any tips on filter for q1? I've got a filter procedure. I'm just not sure how to use this in PROC q1 without it deadlocking.

Answer 4:

You need a filter process – in this case, a forever-running process like all the others(S0, S1, alternate and printstream) in this exercise. Your filter process should have one input and one output channel that carry INTs – its job is to remove (i.e. not pass on) any integers that are exact multiples of 5. As the question says, this filter process just needs to be spliced into the existing network (i.e. it runs in parallel with the other processes).

Keywords: q1


Question 5:

Submission reference: IN1804

Can you tell use what the exam:coursework ratio for this module is this year? Is it still 80:20, or has that changed, or do you have no idea yet?

Answer 5:

There is no change – exam:coursework = 80:20.


Question 6:

Submission reference: IN1805

Hi,

Please can you specify the requirements of our modified print.stream process (the last part of Exercise 1) if we pass in '0' as the number of columns - do you want no new lines?

Answer 6:

The question says that the parameter "specifies the number of columns of output produced". So if its value is 0, no columns of output should be produced ... which could be interpreted as nothing or, possibly, an infinite number of newlines! It's also possible to interpret this as you suggest: if the output is a single infinite line of text, there will be no columns!

For your answer, you may assume that sensible values are supplied – i.e. 1 or more.

To be robust, you may deal with bad values (e.g. 0 or less) in some way that you document. For example, you could round such bad values up to 1. That would be great (but there are no extra marks for such care – for this exercise).

Keywords: q1


Question 7:

Submission reference: IN1806

I'm having a problem with my filter process. I understand that it needs an input channel of type INT and an output channel of type INT, and my code logic appears to be solid, however I keep ending up with a "Not enough parameters in top level." error. It expects 3 BYTE channels instead of 2 INT channels. Can you give me any pointers as to where I'm going wrong?

Answer 7:

In occam-pi, all user-defined things (variables, processes etc.) must be declared before they are used.

I suspect that your filter process is the last item in your file – i.e. after the declaration of the q1 process that is using it. Move the declaration of filter ahead of q1 and that should fix things.

The error message (about expecting 3 BYTE channels) is because the last process declaration in your file is the one from which execution starts and the Transterpreter implementation insists that this must have those 3 BYTE channels (for keyboard, screen and error streams). For this question, that last process must be q1.

Keywords: q1


Question 8:

Submission reference: IN1807

Not operator (within IF statement) ...

I've looked through the documentation and could only find the BOOL operator NOT (and could not successfully apply it). How do I apply a not condition within an IF statement? i.e. != in Java.

Thanks.

Answer 8:

The short answer is that the Java != is the occam-pi <>.

A longer answer is that NOT is a prefix operator for BOOLs in occam-pi. It is applied by writing on the left of the BOOL you are wanting to negate. For example:

    IF
      x <> 42
        ...  wrong answer
      TRUE
        ...  right answer

is the same as:

    IF
      NOT (x = 42)
        ...  wrong answer
      TRUE
        ...  right answer

Question 9:

Submission reference: IN1809

To move multiple lines of code right, you just highlight all the lines and press tab. How do you undo mutliple lines of tabbing - i.e. move them left?

Answer 9:

You can unindent code by de-tabbing it. Select the text and press shift-tab.

If you look at the printing on your tab key, it has two arrows, with the one pointing left and one pointing right. These indicate that you can tab inward (right) on the regular press and tab outward (left) when shifted. (Unless you're on a mac, in which case it's just an arrow and a line).


Question 10:

Submission reference: IN1808

In the lecture today, in the basics slides, this syntax was introduced:

    [max<<2]CHAN INT q:
What exactly does this code do?

Answer 10:

The operator << shifts its left argument leftwards by the number of bits indicated by its right argument – in this case, by 2 bits. As this happens, zeroes fill the lowest significant bits made vacant by the leftwards shift, which results in the original number having been multiplied by four. Since the value of max was 50, max<<2 is 200. Had max been a very large number, with 1s in the top two most significant bits, they would have been lost by the leftwards shift.

So, max<<2 is just an efficient way (from the computer's point of view) of saying max*4. It was used in the slide to make you think about these left and right bit shift operators.

What the code actually does is declare an array, called q, of 200 channels carrying integers.


Question 11:

Submission reference: IN1810

How do you compare BOOLs, say in an IF statement? For example, if I have:

    INITIAL BOOL b IS TRUE:
    SEQ
      ...  code where the value of b can change
      IF
        b = TRUE   -- This always says "expected "?", found end of line"
          ...  do something

Can you tell me what I'm doing wrong?

Answer 11:

There is nothing wrong with your code fragment about which the compiler should complain ... unless within the hidden code "where the value of b can change", b is acttually re-declared as a channel! Please mail your seminar leader the whole code if your problem has not disappeared.

However, there is something about your code that is a bit weak. The condition(s) associated with an IF are, of course, BOOL(s). In your code, b is a BOOL (unless re-declared as something else!). Assuming it is a BOOL, then the expression:

        b = TRUE

is a clumsy way of saying:

        b

So, your code would be much neater as:

    INITIAL BOOL b IS TRUE:
    SEQ
      ...  code where the value of b can change
      IF
        b
          ...  do something

Last thought: your IF sturcture had better have some more conditions in case b turns out to be FALSE. Otherwise, there will be a run-time error – see Question 2 (2006) and Question 46 (2000).

Keywords: if , bool


Question 12:

Submission reference: IN1812

I am having a difficulty to understand how to activate a resettable process. The replace process would always use the reset channel. How does it actually get activated? I have tried to send the zero value in the reset channel but it still won't work. Am I missing something here?

Answer 12:

It sounds like your implementation is on the right lines. Such a process would normally have the implementation:

  PROC replace (CHAN INT in?, out!, reset?)
    WHILE TRUE
      PRI ALT
        INT v:
        reset ? v
          PAR
            INT tmp:
            in ? tmp
            out ! v
        INT x:
        in ? x
          out ! x
  :

Thus, if something comes in on the "reset" channel, this component will respond by swallowing up the next value received on "in", and outputting the reset value on "out". Ordinarily, in the absence of any communications on "reset", this process will just copy data from "in" to "out" (the second guard in the PRI ALT).

Regarding 'activation', the process once instantiated is always active — though it may spend most of its time waiting at the PRI ALT for one of the input channels to become ready. Of course, this means you'd have to wire it up inside the relevant process network, which involves modifying that original network (in the case of "numbers", "integrate", etc.). The code for these can be found in the lecture slides, or on raptor in "/courses/co538/libsrc/demo_nets.occ".

Keywords: q3 , q4 , reset


Question 13:

Submission reference: IN1811

Any ideas on how can you send an INT (e.g. 1) to a process that accepts CHAN INT only?

Answer 13:

The short answer is communicate with it in parallel. Presumably you have a PROC definition which takes a CHAN INT parameter, in which case you need to wire in a channel and output on the other end. A simple example (and not useful for any assessment probably!) would be:

  CHAN INT c:
  PAR
    my.proc (in?, out!, c?)
    c ! 42

This runs "my.proc" in parallel with a simple process that just outputs on "c". It would be expected that "my.proc" accepts this value, and the continues doing whatever it previously did with the "in" and "out" channels.

If you're still not clear on this, please discuss with your seminar leader :-).

Keywords: q4


Question 14:

Submission reference: IN1814

Would it be possible to get a model answer for Q3?

Answer 14:

Not on paper or electronically.

However, you can ask about it in your seminar group and you will be led to finding your own solution. If your seminar group this week has passed, ask your friends – Q3 is not for assessment and we want you to learn from each other.

NB: an elegant solution for the first part of the control process take 14 lines. Two of these are the PROC header and final colon line. Another line sets up an infinite loop (WHILE TRUE). Another one sets up a prioritised alternative (PRI ALT). Somewhere, you need to declare a variable (or two). To test if the input character is an 'f', you'll need an IF (but you'll find a CASE easier for checking all the later possibilities – same also for Q4 ... so read the final paragraph of Q4!).

Any longer solution is either wrong or, probably, inelegant, :).

Keywords: q3


Question 15:

Submission reference: IN1813

Is there a version of the Transterpreter (or KRoC) that actually works on PowerPC based Macs?

Answer 15:

Sorry, there doesn't seem to be one pre-compiled (not many PowerPC Macs left these days). However, if you download the SVM source tree (see the KRoC install page (for Linux PCs and MAC OS X)), and build it with:

  ./build --with-toolchain=tvm --prefix=/usr/local/kroc

(where /usr/local/kroc is wherever you choose to install it), you should get a version of the transterpreter that might work! This won't give you the jEdit GUI/IDE, but should be runnable from the command line.

If you do this and it works, please let us know – same if it doesn't!

Keywords: transterpreter


Question 16:

Submission reference: IN1815

Hi,

Please could you repeat here the code you gave for the network in part (a) of the exam question we looked at in the seminar this morning? Thanks.

Answer 16:

This was to set up a circle of n prefix processes, each one outputting (or trying to output) a different number, form 1 to (n-1).

Well, we need n processes and, therefore, n channels in the ring:

    PROC X0 ()             -- no wires (or any other parameters)
      [n]CHAN INT c:
      ...  all the prefix processes go here (and run in parallel)
    :

For the code setting up the ring of prefix processes, it helps to label the connecting channels with individual elements from the channel array just declared. There are many ways to do this labelling. A simple way labels the channels to/from the i'th process as follows:

                ----------------
        c[i]    |              |   c[i+1]
    ------>-----|  prefix (i)  |------>-----
                |              |
                ----------------

This works for all values of i from 0 to (n-2). However, for the last one when i is (n-1), that would give:

                ----------------
       c[n-1]   |              |    c[n]
    ------>-----| prefix (n-1) |------>-----
                |              |
                ----------------

and, of course, there is no element c[n] in the array! This last one needs to connect back to the first one to form the circle - i.e.

                ----------------
       c[n-1]   |              |    c[0]
    ------>-----| prefix (n-1) |------>-----
                |              |
                ----------------

The neatest way to achive this is not to use the formula i+1 for the index for the output channel of the i'th process. Instead, use the formula (i+1)\n (i.e. the remainder when (i+1) is divided by n).

                ----------------
        c[i]    |              | c[(i+1)\n]
    ------>-----|  prefix (i)  |------>-----
                |              |
                ----------------

For all values of i from 0 to (n-2), this gives 1 to n for the output channel index. These are the same results given by the i+1 formula and are what we want. When i is (n-1), the new formula gives n\n which is 0 and is also, unlike the old formula, what we want!

So, to set up the ring of processes, we just replicate the i'th one using the above formula for its output channel index:

    PROC X0 ()             -- no wires (or any other parameters)
      [n]CHAN INT c:
      PAR i = 0 FOR n
	prefix (i, c[i]?, c[(i+1)\n]!)
    :

It's much easier to code than explain!

Instead of using the \ operator, we could have used an IF:

    PROC X0 ()             -- no wires (or any other parameters)
      [n]CHAN INT c:
      PAR i = 0 FOR n
	IF
	  i < (n-1)
	    prefix (i, c[i]?, c[i+1]!)
	  TRUE                                 -- i.e. i = (n-1)
	    prefix (i, c[i]?, c[0]!)
    :

Or we could have replicated only n-1 times and dealt with the last one as a special case:

    PROC X0 ()             -- no wires (or any other parameters)
      [n]CHAN INT c:
      PAR
        PAR i = 0 FOR n-1
	  prefix (i, c[i]?, c[i+1!)
	prefix (n-1, c[n-1?, c[0]!)
    :

But both of these are a bit longer to code than the first method!   :)


Question 17:

Submission reference: IN1816

Hello,

I am currently attempting q4. My question is this. Can the implementation of pairs2 be made in sequence? I find the idea of switching the circuit in PAR components perplexing. Should I be able to do this? Will a sequential implementation be looked upon with disregard by the mark scheme?

Many thanks.

Answer 17:

A sequential implementation for pairs2 will only gain a few marks I'm afraid. The exercise is to develop your skills at designing and modifying parallel networks.

Read this part of the question carefully again – especially the paragraph starting: "One way is to bring the new reset channel into ...". The solution does not require dynamically changing the circuit (which we can do in occam-pi, but is not part of this taught module). One solution involves modifying the behaviour of the adder process (so that it flips betwwen an adder and a subtractor when reset). A neater solution, hinted in the paragraph just referenced, just needs another process in the network somewhere and no modifications to the behaviours of any of them – it's just a circuit design problem, using existing components.

Keywords: q4


Question 18:

Submission reference: IN1817

Suppose we have a process P with state x. This state requires an initial value, and changes value based on periodic inputs from channel c. To provide the initial value there seem to be two options:

(1) wait for something to arrive on channel c before kicking off the main body of the process (see P1). This requires instantiating the process in parallel with another, which must be guaranteed to provide an initial value on channel c:

    PROC P1 (CHAN INT c?, ...)
      INT x:
      SEQ
        c ? x
        WHILE TRUE
          PRI ALT
	    c ? x
	      ...  some process that uses x
	    ...  some guard
	      ...  some process responding to the guard
    :

or (2) provide the initial value to the process as a value parameter (see P2):

    PROC P2 (VAL INT initial.x, CHAN INT c?, ...)
      INITIAL INT x IS initial.x:
      WHILE TRUE
        PRI ALT
          c ? x
	    ...  some process that uses x
          ...  some guard
	    ...  some process responding to the guard
    :

Does it matter which of these we do, and which is considered nicer? Are we looking at lost marks for doing one over the other?

Answer 18:

Either of your options will work. Both of them requires another process in parallel with this one in order to drive its input channel c.

If it is a natural part of a specification for that other process to supply the initial value for the state of this one, then P1 is the right solution for this one.

If that initialisation is not a natural part of a specification for that other process, it should not be made to do it! Instead, your P2 solution – where its initial state value is given explicitly in its instantiation – is the right solution.

Yes, you would lose some marks for not choosing the right solution for any problem. A working solution is not enough for full marks.

For interest only: for some time, we have been meaning to introduce INITIAL as a qualifier on data parameters (like VAL). The meaning is that an initial value must be given to that parameter (like VAL), but that it may be changed by the code body (unlike VAL parameters) and that its final value is not returned to the invoking process (unlike reference data parameters - i.e. those without any qualifier). With this mechanism, your P2 version would not need its local INITIAL declaration - i.e.

    PROC P3 (INITIAL INT x, CHAN INT c?, ...)
      WHILE TRUE
        PRI ALT
          c ? x
	    ...  some process that uses x
          ...  some guard
	    ...  some process responding to the guard
    :

We really must get around to putting that into the language ... !

Keywords: initial


Question 19:

Submission reference: IN1818

Hi, I have got a problem with the freeze functionality in q4. I have managed to implement it and it freezes an output when requested and resumes it after receiving next character, but the print streams then prints one extra column of ones. (I assume this is because I had connected the freeze.control with monitor and print.streams processes in order to avoid deadlock) Is there any way to get rid of this extra column being printed?

Answer 19:

Sorry, I've no idea how you can get an extra column of numbers (ones) after freezing and un-freezing!

I don't understand what you mean by connecting: "the freeze.control with monitor and print.streams processes"? Do you mean connecting the freeze.control between and print.streams? That sounds like you are introducing an extra channel into print.streams, which would account for the extra column – but that extra column would always have been there?!!

No. You should be introducing a freeze.control process into an already existing channel, between an already existing somewhere and print.streams (to which it was previously directly connected).

Keywords: q4


Question 20:

Submission reference: IN1819

"A neater way is to leave the existing subcomponents alone, but introduce an extra component into the circuit to achieve the required effect. [A process suitable for this new component has been described in the course.]"

Is there any chance of a hint on where to find this?

Answer 20:

Well, you'll need a component that's PRI ALTing to react to the reset message to your pairs2. So, look in the choice slides for examples ...

Keywords: q4

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:22 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.