Volume C1 · Basic concepts · TAOCP Section 1.4.4
Input and output
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 1.4.4 of the book. The nodes and the exercises of the section.
Contents
Input and output
Overview
An input or output unit is slow. A block of a tape takes thousands of u to move, and a program that does nothing else during this time wastes most of the machine. The instructions IN and OUT only start a transfer. The processor goes on while the unit moves the block, and the program may not touch the memory area of the block until the transfer is over. This section shows how to use that freedom.
The methods come in a fixed order. The plain method starts a read and waits. The method with a second area reads the next block while the program works on the last one, but it copies the block from the second area to the work area. Buffer swapping removes the copy: the program works in the area that was read, and the read goes into the other one. The circle of buffers has any number of areas. It is built from two small actions of the program, ASSIGN and RELEASE, and from a control routine that starts the transfers. The same algorithm serves for input and for output. Only the starting value of one counter differs.
The chapter uses one model of a unit for all formulas. A transfer of one block takes u from the start. The instruction IN or OUT takes u of the processor and starts the transfer. If the unit is still busy with an earlier transfer when IN or OUT runs, the processor waits until the unit is ready, and then starts the new transfer. The instruction JBUS jumps if the unit is busy when the instruction starts, and JRED jumps if it is ready. Each takes u. Every number of time in this chapter was checked on a simulator of MIX with this model of the units.
The ideas
The wait costs a whole block time. After the pair of instructions that starts a read and waits for it, the unit is idle during the work of the program, and the processor is idle during the transfer. The time of a block is the sum of the two.
A second area lets the two overlap. If the next read is started before the work on the present block, the transfer and the work run in parallel. The time of a block is then about the longer of the two.
A copy costs time on the critical path. The method that copies the block from the second area to a work area keeps the unit idle during the copy. Swapping the roles of the two areas removes the copy, and the processor uses an index register to say which area is the present one.
A sentinel marks the end of a buffer. If the program reads one word at a time from a block, it needs a test for the end of the block. A value that cannot be data, put after the last word, makes the test one comparison per word.
ASSIGN and RELEASE are the whole interface. Between the two actions the program uses one buffer, the current one. Between RELEASE and the next ASSIGN it uses none. The program that does not touch a buffer outside this window leaves the buffering routine free to use all other buffers.
Three colours describe a buffer. A buffer is green when the program can take it by ASSIGN, yellow while the program has it, and red after RELEASE until the transfer of it is over and the unit has made it green again. Two pointers run clockwise around the circle: NEXTG at the next green buffer for the program, and NEXTR at the next red buffer for the unit.
One counter keeps the two pointers apart. The number of red buffers is enough to see that the program is not too far ahead of the unit, and that the unit is not too far ahead of the program. Input starts with and output starts with .
The unit and the program are coroutines. The control routine starts a transfer, lets the program run, and takes control back when the unit is ready. The program gives control with a conditional jump on the state of the unit, put in its code at short intervals.
Definitions, results and proofs
Definition (buffer, buffering and circle of buffers, TAOCP Section 1.4.4). A buffer is an area of memory that holds the words of one block that goes to or comes from an input or output unit. Buffering is the use of buffers so that the transfer of a block and the work of the program on other data go on at the same time. A circle of buffers is a set of buffers in a cyclic order, in which each buffer has a next buffer and the next of the last is the first.
Node: 1.4.4-def-buffer · Lean: not yet stated
Explanation. The word buffer means a memory area of the program here. It does not mean the part of a device that holds the data on the way. The circle needs no more than the next relation. A way to keep it is a word before each buffer that holds the address of the next one. Then the step from one buffer to the next is one load.
Example. Take areas at the addresses , and , each of 10 words, and put the words , and in the cells , and . Starting at , the loads of the word before the buffer give , , and so on. The set is a circle of three buffers. The program that uses it needs no multiplication and no test for the end of the memory.
Proof that the example is a circle. Each of the three areas has a next buffer, since the cells , and hold the addresses of the other areas in the order , , . The relation is a single cycle of length , since the sequence visits each area once before it returns.
Plain input (equation (1) of the section, TAOCP Section 1.4.4). The pair of instructions IN BUF(U) and JBUS *(U) reads the next block of the unit U into the area BUF and then waits, in a loop of one instruction, until the unit is ready. After the pair, the words of BUF are the block and the program may use them.
Node: 1.4.4-(1) · Lean: not yet stated
Explanation. The instruction IN only starts the transfer. The area BUF holds the block only when the transfer is over. The instruction JBUS *(U) jumps to itself while the unit is busy, so it holds the program until the unit is ready. Another I/O instruction for the same unit would also do for the wait, but then the instruction must be one that the program needs anyway.
Example. Let U be a unit with . A loop reads 6 blocks of 100 words with this pair, and then works on the block with a loop of 100 passes of two instructions, DEC2 and J2P. Between two reads the program runs ENT2 (1 u), the 100 passes ( u) and DEC3 with J3P ( u), so u. The simulator gives the time u from one IN to the next, in each of the 5 later blocks.
Proof. Let the IN start at time . It takes u, and the transfer ends at . The first JBUS starts at . Each JBUS that finds the unit busy takes u and jumps back to itself, so the JBUS starts at every time from on. The first one that starts at a time of at least finds the unit ready. Since , this one starts at exactly , takes u and falls through. The work then runs, and the next IN starts at . The time of a block is . For the numbers, .
Theorem (the limit of the gain of buffering, TAOCP Section 1.4.4). A program reads blocks from one unit, a block takes u to transfer, and the program works u for each block. With plain input, the time per block is up to a constant. For every method of buffering the time per block is at least . So buffering cannot make a program run in less than half of the time of the plain method, and the gain is exactly a factor of two only if .
Node: 1.4.4-thm-factor-two · Lean: not yet stated
Explanation. The unit must do transfers of u each, one after another, and the processor must do pieces of work of u each. Neither can be made shorter by buffering. Buffering can only let the two run in parallel. The plain method has no parallelism, and the best possible method has the full parallelism.
Example. With and , the plain method needs u per block. No method of buffering can need less than u per block, and . With the plain method needs u and the best method needs u. This is the factor two.
Proof. The unit works on one block at a time, so transfers need at least u, and the processor needs at least u. The program ends after both are done, so it needs at least u. The plain method needs up to the constant. Since , the ratio of the two is at most . It is only if .
Reading ahead with a copy (equations (2) and (3) of the section, TAOCP Section 1.4.4). The first instruction of the program, IN AHEAD(U), starts the read of the first block into a second area AHEAD before the loop. Each pass of the loop then runs ENT1 WORK, JBUS *(U), MOVE AHEAD(F) and IN AHEAD(U), in this order, where F is the number of words of a block. After the MOVE, the area WORK holds the block that the unit has read, and the unit has started to read the block after it.
Node: 1.4.4-(2) · Lean: not yet stated
Explanation. The read is started before the program has seen the block that it reads, on trust that the block will be needed. This is reading ahead. It is right only if the end of the input is known by some other sign, for example by a count or a trailer. If a block that was read for nothing must not be an error, the program must be able to throw it away. The order of the four instructions is fixed. The JBUS must come before the MOVE, since the block is not there before it. The IN must come after the MOVE, since the new read would change the second area while the MOVE copies it.
Example. Let a block have words, , and the work on a block be a loop of passes of two instructions after LDA, ADD and STA that add the first word to a sum. The simulator reads 8 blocks whose words are for the block and the word . The sum of the first words is , which is . The block took u from one IN to the next.
Proof that the sequence of the instructions is right. Let the IN of the area AHEAD start at . Until the unit is ready, the area AHEAD is changed by the unit, so the MOVE must not run. The JBUS loop ends only when the unit is ready. So the MOVE runs after the end of the transfer. The next IN comes after the MOVE and overwrites AHEAD only after the copy is complete. The first IN comes before the loop and has no earlier use of the area. By induction the pass of the loop copies the block . The sum confirms it for the run.
Node: 1.4.4-(3) · Lean: not yet stated
The time of the loop. Let be the time from the start of an IN to the start of the next JBUS, so that it counts the IN itself, the work, the loop control and the ENT1. The time from one IN to the next is
which is for . The is the JBUS that finds the unit ready, and is the time of the MOVE.
Proof of the formula. The IN starts at , and the first JBUS starts at . If , the unit is ready then, and the JBUS falls through at once. If , the JBUS loop runs until the time , and the last JBUS starts at . In both cases the JBUS that falls through starts at , takes u, and the MOVE follows and takes u. The IN comes right after.
Example of the formula. With the work of passes, the program of the example has . These are the IN (), LDA, ADD and STA (), ENT2 (), the of the passes, DEC3 and J3P () and ENT1 (). The plain method with the same work has and takes . The table gives the times of one block, measured on the simulator and computed from the two formulas, which agree.
| plain, | ahead, | ||
|---|---|---|---|
| 600 | 10 | 630 | 682 |
| 600 | 100 | 810 | 682 |
| 600 | 400 | 1410 | 893 |
| 30 | 10 | 60 | 113 |
The comparison. The method with the copy is better when the work and the transfer are both long: for and the saving is u of . It is worse when the block is short in time. For and the plain method wins, since the copy costs u and the overlap gains only the short work. The exception is not frequent, but it exists. The copy is the cost on the critical path: the unit is idle during the JBUS and the MOVE, which is u at the end of each cycle, while in the plain method it is idle during the whole work.
A routine with two swapped buffers (equation (4) of the section, TAOCP Section 1.4.4). Two areas BUFA and BUFB of words each are put one after the other, and each is followed by a word with a value that is not data, and then by a word with the address of the other area. The register I6 holds the address of the next word to read. The subroutine GETW returns the next word in rA. If the word at the address of I6 is , the area is used up. The subroutine then starts a read into this area, takes the address of the other area from the word after , and tries again. Otherwise it adds one to I6 and returns.
Node: 1.4.4-(4) · Lean: not yet stated
The routine of the example uses , the unit , and the negative word as the value , since the data of the unit are not negative. Here is the code.
GETW STJ 9F save the return address
2H LDA 0,6 the word at the pointer
JAN 1F negative: this is the sentinel
INC6 1 advance the pointer
9H JMP * return with the word in rA
1H IN -10,6(5) start to refill the used area
LD6 1,6 go to the other area
JMP 2B and try again
The program starts with ENT6 BUFA, IN BUFA(5) and IN BUFB(5), and it then calls GETW once for each word. The second IN waits in the processor until the first transfer is over, by the rule of the model. The areas have the layout BUFA: 10 words, the word , the address BUFB, and BUFB: 10 words, the word , the address BUFA.
Explanation. The refill of an area is started in the same moment as the program leaves it, and the program goes on at once in the other area. The routine has no JBUS, because the instruction IN does the wait. When the area A is used up, the unit has been given the reads for the areas A and B and the read for B may still run. The IN for the area A waits for the end of the read for B, and then it starts. So the program has the block of B complete, before it goes on. The subroutine makes a unit with blocks of words look like a unit with blocks of one word.
Example. A run reads words from blocks whose words are for and . After each word the program runs a loop of passes of two instructions, adds the word to a sum and counts. The sum is , which is the sum of the words, and the program ends. With and , the unit starts a new read every u. With the new read starts every u.
Proof of correctness. Let the blocks be and let be in the area that I6 points into. The invariant is that the unit has been told to read and , that is complete, and that goes to the other area. At the start this is true after the two IN instructions: the second waits for the first, so is complete, and I6 is at BUFA. While the words of are read, the invariant does not change. When the sentinel is found, all words of the area of are used. The IN is for into this area. It waits for the end of the read of by the rule of the model, so is complete when I6 changes to the other area. The area of is changed by the read of only after all its words were read. So the invariant holds for . The words come in the order of the blocks, and the sentinel is never returned, since the routine does not return a negative word.
Proof of the time. Let be the time that the processor spends on one block, without any wait, from the start of one IN of the routine to the start of the next one. Let be the start of the read . The read starts when the unit has finished and the processor has come to the IN, so in the steady state, and the time of a block is . For the program of the example each word costs u for the call and the return (JMP , STJ , LDA , JAN , INC6 , JMP ), u of the loop in the main program (ADD , STA , ENT1 , DEC3 , J3P ) and u of the passes, which is . A block has words, and the call that finds the sentinel has the extra instructions LDA (), JAN (), IN (), LD6 () and JMP (), which is u. So . For this is , and the time is . For it is , and the time is . The simulator gives these two values.
Output through the buffering routines (equation (5) of the section, TAOCP Section 1.4.4). A program that sends a line image to the output unit takes a free buffer with ASSIGN, puts the data of the line in the buffer, and gives the buffer back with RELEASE. For a line image of words at the address LINE, the sequence is JMP ASSIGN, ENT1 0,4, MOVE LINE(F), INC6 1 and JRED CONTROL(U). It uses the register I4 for the address of the buffer that ASSIGN gave, as the programs of the circle do.
Node: 1.4.4-(5) · Lean: not yet stated
Explanation. The first instruction takes the buffer. The second sets the destination of the MOVE to the buffer. The MOVE copies the image. The last two are the RELEASE of Program R. After RELEASE the program does not use the buffer any more, and the control routine can send it to the unit.
Example. Let LINE be the address and . The cells to hold (the numbers ). The program does the sequence three times with a circle of three buffers and a unit of . The simulator shows that the unit received three blocks, and each is the list . The time of the processor for one sequence is u: ASSIGN u as counted below, ENT1 u, MOVE u, and RELEASE u.
Proof. The sequence makes the buffer that ASSIGN gives the destination, since ENT1 puts its address in the register I1, which is the destination of MOVE. The MOVE copies the words, and after the MOVE the buffer holds the line. The RELEASE comes after the copy, so the control routine never starts the unit before the copy is complete. By the theorem of the circle below, the buffer is not used by the unit while the program fills it.
Definition (the count of red buffers, TAOCP Section 1.4.4). In a circle of buffers, is the number of the buffers that have been released and have not yet been made green by the unit. A buffer is red from its RELEASE until the end of the transfer that uses it, and the buffer on which a transfer is in progress is red. The number satisfies . At the start of an input process, , and at the start of an output process, .
Node: 1.4.4-(6) · Lean: not yet stated
Explanation. The count of red buffers is the only common variable of the two coroutines. The program increases it by RELEASE. The control routine decreases it when a transfer ends. For input, a red buffer is an empty buffer that the unit has not filled yet. At the start the unit has filled nothing, so all buffers are red. For output, a red buffer is a full buffer that the unit has not yet written. At the start nothing is full, so none is red. This one difference in the starting value is the difference between input and output. The code is otherwise the same, except that the instruction that starts the transfer is IN for input and OUT for output.
Example. The table follows a run of the program of the circle below for output with buffers, a unit with , four blocks, and a work of about u between two ASSIGN actions. Each row shows the registers after the action: is rI6, NEXTR is rI5, NEXTG is rI2 and CURRENT is rI4, and the buffers are numbered by their order in the circle. The simulator gave the table. The time of a row is the time at which the instruction of the action starts: the return JMP of ASSIGN, INC6 for RELEASE, OUT for the start of a transfer and DEC6 for the end of a transfer.
| Time | Action | NEXTR | NEXTG | CURRENT | |
|---|---|---|---|---|---|
| 18 | ASSIGN | 0 | 1 | 2 | 1 |
| 22 | RELEASE | 1 | 1 | 2 | 1 |
| 28 | start of transfer | 1 | 1 | 2 | 1 |
| 195 | ASSIGN | 1 | 1 | 3 | 2 |
| 199 | RELEASE | 2 | 1 | 3 | 2 |
| 235 | end of transfer | 1 | 2 | 3 | 2 |
| 238 | start of transfer | 1 | 2 | 3 | 2 |
| 376 | ASSIGN | 1 | 2 | 1 | 3 |
| 380 | RELEASE | 2 | 2 | 1 | 3 |
| 446 | end of transfer | 1 | 3 | 1 | 3 |
| 449 | start of transfer | 1 | 3 | 1 | 3 |
| 557 | ASSIGN | 1 | 3 | 2 | 1 |
| 561 | RELEASE | 2 | 3 | 2 | 1 |
| 657 | end of transfer | 1 | 1 | 2 | 1 |
| 660 | start of transfer | 1 | 1 | 2 | 1 |
| 867 | end of transfer | 0 | 2 | 2 | 1 |
Proof that the table agrees with the definition. The count rises by one at each RELEASE and falls by one at each end of a transfer. In the table it rises at the times , , and and falls at , , and , and the start values are . So is after the first RELEASE, after the second, and so on, as in the rows. The value never goes above , so it never gets to , and no ASSIGN has to wait. At the time all four blocks are done and . The first ASSIGN starts its return at the time . The start-up takes u of coroutine linkage (JMP, STJ, JMP) and u of ENT instructions, and the ASSIGN takes u up to its return.
Algorithms and programs
Algorithm (ASSIGN, TAOCP Section 1.4.4, Algorithm A). This algorithm gives the program the next buffer. A1. If , wait until ; the control routine runs meanwhile. A2. Set CURRENT to NEXTG. A3. Move NEXTG to the next buffer of the circle.
Node: Algorithm 1.4.4A · Lean: not yet stated
Explanation. The wait in step A1 is the case that the program has got ahead of the unit. For input it means that all the buffers are still empty or in the transfer. For output it means that all are full and waiting for the unit. The wait is only a loop that gives control to the control routine. The buffer in NEXTG is green when , as the theorem below shows. CURRENT is the buffer that the program uses until its RELEASE.
Example. In the table above, the ASSIGN at the time finds . CURRENT becomes the buffer , which is the value of NEXTG before, and NEXTG moves to the buffer . At the time the NEXTG is the buffer again, since the circle has three buffers.
Proof that the algorithm gives the buffer in the order of the circle. Step A2 takes NEXTG, and step A3 moves NEXTG to its next buffer. So the buffers that the program takes are the buffers of the circle, one after the other from the starting NEXTG. The wait at step A1 does not change NEXTG.
Algorithm (RELEASE, TAOCP Section 1.4.4, Algorithm R). This algorithm tells the buffering routine that the program has finished with the current buffer. R1. Increase by one.
Node: Algorithm 1.4.4R · Lean: not yet stated
Explanation. The one step makes the buffer red. Since the buffers are red in the order of the circle, the number is enough, and the buffer does not need a mark. For output the buffer is full, and for input it is empty.
Example. In the table, the RELEASE at the time takes from to . The buffer , which CURRENT held, is red now and NEXTR points at it.
Proof. By the theorem below, , where is the number of RELEASE actions. Step R1 is the increase of , so it keeps this formula. The buffer that is released is the one that follows the red buffers in the order of the circle, so after R1 the red buffers are the buffers from NEXTR on.
Algorithm (buffer control, TAOCP Section 1.4.4, Algorithm B). This algorithm starts the transfers. It is a coroutine of the program. B1. Let the program run for a while, until the unit is ready, then go to B2. B2. If , go to B1. B3. Start the transfer of the buffer at NEXTR. B4. Let the program run until the unit is ready again, that is, until the transfer is over. B5. Move NEXTR to the next buffer of the circle. B6. Decrease by one, and go to B2.
Node: Algorithm 1.4.4B · Lean: not yet stated
Explanation. The control routine has three states: it waits for the unit, then it checks for a red buffer, then it starts the transfer. It does only the work that the program cannot do while the unit is busy, the start of the transfer and the change of the two variables. For input the transfer fills the buffer at NEXTR, and the buffer becomes green. For output the transfer writes it, and it becomes green. In both cases the colour changes from red to green at B5 and B6, which together are the end of the transfer.
Example. In the table, the first start of a transfer is at the time . At the time the program does RELEASE, and the control routine has the chance to see the next time the unit is found ready. This happens at the time , which is about six u later. The routine starts the buffer . It is over at , and the routine notices at in the run, and it moves NEXTR to the buffer and takes to .
Proof that a transfer starts only on a red buffer and ends with a green one. Step B3 runs only after B2 has found . So there is a red buffer, and by the theorem below NEXTR points at the first one. The transfer is started only after B1 or B4 has found the unit ready, so no transfer is started on a busy unit. Steps B5 and B6 follow the end of the transfer of B4, so the buffer becomes green only after its transfer.
The programs use four registers. The register I6 holds , I5 holds NEXTR, I4 holds CURRENT and I2 holds NEXTG. The program of the user may use any other register, and it must not change I2, I4, I5 and I6. The word before each buffer holds the address of the next buffer. The unit has the code U, and is the number of the buffers. The coroutine linkage is the one of Section 1.4.2.
Program (ASSIGN, TAOCP Section 1.4.4, Program A). The subroutine ASSIGN is called by JMP ASSIGN. On the return, I4 holds the address of the buffer that the program may use, and I2 holds the address of the next one.
Node: Program 1.4.4A · Lean: not yet stated
ASSIGN STJ 9F subroutine linkage
1H CMP6 =N= A1: is n = N ?
JNE 2F
JRED CONTROL(U) the unit is ready: let the control routine run
JMP 1B ask again
2H ENT4 0,2 A2: CURRENT = NEXTG
LD2 -1,2 A3: NEXTG = the next buffer
9H JMP * return
The linkage of the coroutines, and the program of the user. The control routine and the program of the user are coroutines with the linkage of Section 1.4.2.
CONTROL STJ COMPX COMPUTE STJ CONTROLX
CONTROLX JMP CONT1 COMPX JMP COMP1
Explanation. While , the program cannot go on. It must not spin without the control routine, since the control routine is the only one that can lower . The instruction JRED CONTROL(U) gives it control in the moment when the unit is ready, which is when a transfer may have ended or a new one may start. When the control routine gives control back, the program does JMP 1B and asks the question again. The two instructions after the test, ENT4 and LD2, are the whole of A2 and A3. The load of the word before the buffer gives the address of the next one.
Time. The call JMP takes u. The path with takes STJ (), CMP6 (), JNE (), ENT4 (), LD2 () and the return JMP (), so the ASSIGN takes u with the call. Each pass of the wait takes CMP6, JNE, JRED not taken and JMP, which is u.
Example. In the test run at the time of the table, the register I2 holds the address of the buffer before the call. The program goes to 2H at the first test, since . ENT4 gives I4 the value of the buffer , and LD2 loads the word before this buffer, which is the address of the buffer .
Proof. The path to 2H is taken exactly if , and always, so it is taken exactly if , which is A1. The two instructions at 2H are A2 and A3. The only way back to 1B is after JRED, which does not change in the program, so the program can go on only when the control routine has made smaller. The return goes to the place after the call, by the linkage of Section 1.4.1.
Program (RELEASE, TAOCP Section 1.4.4, Program R). The code is put in the program of the user at each RELEASE. It is the two instructions INC6 1 and JRED CONTROL(U).
Node: Program 1.4.4R · Lean: not yet stated
Explanation. The first instruction is the step R1. The second is a point where the control routine can run, since the RELEASE is the moment when a transfer may need to start. The same JRED is put in the program at other places, at short intervals. The place of the JRED matters only for the delay of the control routine, not for the correctness.
Time. The code takes u, plus the work of the control routine if the jump is taken.
Example. In the table, the RELEASE at the time takes INC6 from to , and JRED finds the unit ready, since nothing has started yet. The control starts the transfer. The simulator shows the start at the time , which is the u of the coroutine switch and B2.
Proof. The instruction INC6 1 is the step R1. The instruction JRED does not change any variable of the buffering, so it can be put at any place.
Program (buffer control, TAOCP Section 1.4.4, Program B). The control routine is a coroutine. The instruction that starts the transfer is IN for input and OUT for output.
Node: Program 1.4.4B · Lean: not yet stated
CONT1 JMP COMPUTE B1: let the program run
1H J6Z *-1 B2: n = 0 ? back to B1
OUT 0,5(U) B3: start the transfer at NEXTR
JMP COMPUTE B4: let the program run
LD5 -1,5 B5: NEXTR = the next buffer
DEC6 1 B6: n = n - 1
JMP 1B to B2
Explanation. The instruction JMP COMPUTE leaves the control routine and comes back to the next instruction when the program does a JRED CONTROL with the unit ready. The first one is B1. The routine arrives from there when the unit is ready, and it knows that the unit is free. If , the instruction J6Z goes back to the JMP COMPUTE, which is B1, and the program runs again. Otherwise the unit starts the transfer. The second JMP COMPUTE is B4, and the routine arrives again when the unit is ready again, which is when the transfer is over. Then LD5 and DEC6 are B5 and B6, and JMP 1B goes to B2.
Time. A transfer costs the processor this time in the control: JRED taken (), STJ COMPX (), JMP at CONTROLX (), LD5 (), DEC6 (), JMP 1B (), J6Z (), OUT or IN (), JMP COMPUTE (), STJ CONTROLX () and JMP at COMPX (). The sum is u. A block that is never waited for therefore costs the processor u: ASSIGN, RELEASE and the control. The count agrees with the execution counts of a run of 10 blocks: the instructions of the two linkage entries and of CONT1 took u in all, which is rounds of u, and the th u of each round is the JRED of the program.
Example. The table above is a run of the program, with OUT in place of IN. The transfer of the buffer starts at and is over at . The control routine sees this at the first JRED of the program after . In the run, DEC6 starts at and the next transfer starts at .
Proof. The correctness is that of Algorithm B, since each instruction is one step. The instruction J6Z *-1 jumps to the instruction before it, which is JMP COMPUTE, and this is B1. The instruction after OUT is JMP COMPUTE, which is B4, and the code after it is B5, B6 and the jump to B2. The registers I5 and I6 are the registers NEXTR and .
The start of a run. For input the program sets I5 and I2 to the address of the first buffer and I6 to . For output it sets I6 to instead. The control routine starts first at CONT1. Its first instruction is JMP COMPUTE, which goes to COMP1, the first instruction of the program. The program of the test is as follows for the output case. After the start-up, it runs a loop: JMP ASSIGN, a store of the block number into the buffer at the address in I4, the RELEASE, and a loop of JRED CONTROL(U), DEC1 1 and J1P that stands for the work. At the end it does JRED CONTROL(U) and J6P until , which means that all transfers are over. For input the program does not wait at the end. It stops. One more block than needed is read.
Theorem (the circle never overwrites and never reads too early, TAOCP Section 1.4.4). Let the program alternate ASSIGN and RELEASE and start with ASSIGN. Let , and be the numbers of ASSIGN actions, RELEASE actions and ended transfers. Then , where is the starting value, the pointer NEXTG is at the buffer and NEXTR is at the buffer , counted from the starting buffer, and always. For input (), the -th ASSIGN gives a buffer whose -th transfer is over, and the -th transfer starts only after the RELEASE number . For output (), the -th transfer starts only after the RELEASE number , and the -th ASSIGN gives a buffer whose transfer number is over.
Node: 1.4.4-thm-circle · Lean: not yet stated
Explanation. The theorem is the proof that the two coroutines never get in each other's way. The first part is the account of the variables. The second part is the safety for input: the program never uses a buffer that has no data, and the unit never fills a buffer whose data the program did not release. The third part is the same for output. The two cases differ only in , which is the point of the section.
Example. Take the run of the table, an output run with and . The ASSIGN at the time is the fourth. Before it, three ASSIGN actions (at , , ) and three RELEASE actions (at , , ) are done, and two transfers have ended (at and ). So , and , which is the value that the table shows after the ASSIGN. This ASSIGN needs the transfer number to be over, and it ended at . After it, , so NEXTG is at the buffer counted from , which is the buffer number of the table, and NEXTR is at , the buffer number .
Proof. The statement holds because only R1 and B6 change , by and . NEXTG moves one step in each A3, so after actions it is at the buffer . NEXTR moves one step in each B5, which comes with B6, so it is at the buffer when transfers are over. During a transfer, NEXTR is at the buffer of the transfer.
For the bounds: , since B6 follows B3, and B3 ran after was found and only B6 lowers . And , since R1 follows an ASSIGN that passed A1 with , and nothing but R1 raises , so after R1.
For input, with : the -th ASSIGN runs when and , since the program alternates and the earlier ones have been released. It passes A1, so , which means . The transfer number is over. By the rule of NEXTG and NEXTR, the -th ASSIGN and the -th transfer both use the buffer . The -th transfer starts at B3 with and , so , which means . The buffer of the transfer was used by the ASSIGN number (if ), and its RELEASE number is done.
For output, with : the -th transfer starts with and , so , which means , and the buffer was released after it was filled. The -th ASSIGN passes A1 with , so , which is . The transfer number of the same buffer is over.
Theorem (two buffers are enough for constant work, TAOCP Section 1.4.4). Take the model without the cost of the operations, an output process that does times: ASSIGN, work , RELEASE, work , with , and a unit with the transfer time . If , the time to the end of the last transfer is if , and if . No and no method of buffering is faster, and can be slower.
Node: 1.4.4-thm-two-buffers · Lean: not yet stated
Explanation. If the work is longer than the transfer, the program has no wait, and two buffers are enough for the unit to keep up. If the work is shorter, the unit is always busy after the first RELEASE and two buffers are enough to keep it so. More buffers help only when the work between two ASSIGN actions changes much from one time to the next, so that the buffers store up the results of the short times for the long ones.
Example. Let , , and , so . The formula gives . A model program of the same events with gives . With the formula gives , and the model gives . With the model gives for and for , which are longer. The model was also run on cases with and , and it gave the formula each time.
Proof. Let , , and be the time of the -th ASSIGN, of the -th RELEASE, and of the start and the end of the -th transfer. Then , and for , since the -th ASSIGN needs the transfer to be over (the theorem above), with for and .
Let . It is shown by induction that and . If it holds for the indices before , then , so . Also , since , so . The last transfer ends at , and the program ends at .
Let . It is shown by induction that and . For and this is true since and . If it holds before , then and , since . So and . The last transfer ends at .
No method is faster. The last RELEASE comes at least after the start, and its transfer needs more. The unit needs after the first RELEASE at at the earliest, and the program itself needs . For the largest of these bounds is the first or the last one, and for it is . The formulas are equal to them.
The next table has the totals for the more irregular program that does 3 times (ASSIGN, 100, RELEASE, 100), 3 times (ASSIGN, 900, RELEASE, 700) and 2 times (ASSIGN, 100, RELEASE, 100), with , as a model of the circle with no cost for the operations. The column "wait" is the total of the time that the program waits at ASSIGN actions.
| end of the last transfer | wait of the program | |
|---|---|---|
| 1 | 7300 | 1200 |
| 2 | 6600 | 300 |
| 3 | 6300 | 0 |
| 4 | 6300 | 0 |
The work changes much in this program, and the third buffer removes all the wait of the program. The fourth buffer gives nothing more, since the program does not wait with three. This is the point of diminishing returns.
The exercises
The table has the 19 exercises of Section 1.4.4. The kind is the class of spec 06.3 that C1 proposes.
| Exercise | Rating | Markers | Kind | Lean | Solution |
|---|---|---|---|---|---|
| 1.4.4-1 | 5 | informal | not yet stated | X1, 1.4.4-1 | |
| 1.4.4-2 | 10 | K6 | not yet stated | X1, 1.4.4-2 | |
| 1.4.4-3 | 22 | K6 | not yet stated | X1, 1.4.4-3 | |
| 1.4.4-4 | 20 | M | K3 | not yet stated | X1, 1.4.4-4 |
| 1.4.4-5 | 21 | M | K3 | not yet stated | X1, 1.4.4-5 |
| 1.4.4-6 | 12 | K6 | not yet stated | X1, 1.4.4-6 | |
| 1.4.4-7 | 22 | K6 | not yet stated | X1, 1.4.4-7 | |
| 1.4.4-8 | 11 | informal | not yet stated | X1, 1.4.4-8 | |
| 1.4.4-9 | 21 | informal | not yet stated | X1, 1.4.4-9 | |
| 1.4.4-10 | 21 | informal | not yet stated | X1, 1.4.4-10 | |
| 1.4.4-11 | 21 | informal | not yet stated | X1, 1.4.4-11 | |
| 1.4.4-12 | 24 | K6 | not yet stated | X1, 1.4.4-12 | |
| 1.4.4-13 | 20 | K6 | not yet stated | X1, 1.4.4-13 | |
| 1.4.4-14 | 20 | informal | not yet stated | X1, 1.4.4-14 | |
| 1.4.4-15 | 22 | K6 | not yet stated | X1, 1.4.4-15 | |
| 1.4.4-16 | 29 | K6 | not yet stated | X1, 1.4.4-16 | |
| 1.4.4-17 | 40 | informal | not yet stated | X1, 1.4.4-17 | |
| 1.4.4-18 | 30 | K6 | not yet stated | X1, 1.4.4-18 | |
| 1.4.4-19 | 28 | M | K3 | not yet stated | X1, 1.4.4-19 |
Exercises 1 and 6 ask about the order of the instructions of the read-ahead and about the start of the swapping routine. The proofs above are the base: the order JBUS, MOVE, IN is forced, and the start needs I6 at the first area and two reads. Exercises 2, 3 and 7 are the output and the no-sentinel forms of the same routines. Exercises 4 and 5 are the theorem of the limit of the gain, for one unit and for units. Exercises 8 to 11 are time charts. The model of this chapter, with the formulas and , gives the charts for each , and the last table is an example. Exercises 12 and 13 are the end of the process: for input the control must stop reading, and for output the program must wait until , as the test program does. Exercise 14 asks what happens if ASSIGN actions come twice in a row. The proof of the theorem uses the alternation at the bound , and it shows where the proof fails. Exercises 15 to 17 are programs and an algorithm for the same buffers: three buffers for a copy, a circle that serves input and output, and pooled buffers. Exercise 18 replaces the JRED by interrupts. Exercise 19 is the case of a rotating device, in which the start time of a block is fixed modulo a period.
Lean
The Lean code of tamnd/taocp-lean at main has the model of MIX in TAOCP/MIX/. The state has no input or output units yet. The file TAOCP/MIX/Machine.lean says that the instructions IN, OUT, IOC, JRED and JBUS stop the machine with the status Fault.io until the units are added as streams of words. So the programs of this section cannot run in the model, and the model of the time of a unit that this chapter uses has no counterpart. The theorem of the circle is a statement about the counters , and and about the order of the actions of two coroutines, which a Lean proof would state on an abstract model of the two coroutines first. The theorem of two buffers is a statement on numbers and could be proved in Lean on the recurrences of the proof. No Node line of this chapter names a declaration.
Notes and history
The section of the book keeps the problem general on purpose. It says that real machines differ most in the input and output, and it chooses a typical set of methods that applies to most of them. The same is true here. The programs of this chapter are for the idealized units of MIX, which never fail. On a real machine, the program has to test for the success of a transfer before it starts the next one, and the control routine of Program B is the place for the test.
Two choices of the design are worth noting. The first is the use of one algorithm for input and for output, with counted from the red buffers. It works since a buffer is red when the unit has something to do with it, and this is an empty buffer for input and a full one for output. The second is the use of JRED in the program as the way to give control to the control routine. It makes the delay of the start of a transfer depend on how often the program tests. In the run of the table the delay is about six u after the RELEASE, but it can be long if the program has a long stretch of code without a test. Later machines solved this with interrupts, in which the unit makes the processor jump to the control routine at the end of a transfer, and exercise 18 of the section asks for this change.
The method of the circle also has an extension to several units. Each unit can have its own circle with its own , , NEXTR and NEXTG and its own control coroutine. The buffers of the same size can also be pooled between units, but then the rule that two buffers are enough no longer holds as it is, and a deadlock is possible when all buffers hold read-ahead input. These extensions are described in the section but not developed.
Section 1.4.4 asks how a program can keep a computer and an input or output unit busy at the same time. It starts with the plain read that waits for the unit, then adds a second area so that the next block is read while the program works, then swaps two areas without any copy, and ends with a circle of any number of areas that two coroutines drive. This chapter derives the time per block of each method on a model of the unit, proves that the circle never overwrites data that is still needed and never reads data that is not yet there, proves that two areas are enough when the work per block is constant, and runs each program on a simulator of MIX with a model of the devices.
The source of this page. Something is wrong? Report it.