taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

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
  1. Input and output

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 NN 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 TT u from the start. The instruction IN or OUT takes 11 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 11 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 nn 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 n=Nn = N and output starts with n=0n = 0.

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 NN 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 N=3N = 3 areas at the addresses 10011001, 10121012 and 10231023, each of 10 words, and put the words 10121012, 10231023 and 10011001 in the cells 10001000, 10111011 and 10221022. Starting at 10011001, the loads of the word before the buffer give 10121012, 10231023, 10011001 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 10001000, 10111011 and 10221022 hold the addresses of the other areas in the order 10121012, 10231023, 10011001. The relation is a single cycle of length 33, since the sequence 1001→1012→1023→10011001 \to 1012 \to 1023 \to 1001 visits each area once before it returns. □\square

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 T=1000T = 1000. 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 (200200 u) and DEC3 with J3P (22 u), so C0=203C_0 = 203 u. The simulator gives the time 12041204 u from one IN to the next, in each of the 5 later blocks.

Proof. Let the IN start at time ss. It takes 11 u, and the transfer ends at s+Ts + T. The first JBUS starts at s+1s + 1. Each JBUS that finds the unit busy takes 11 u and jumps back to itself, so the JBUS starts at every time from s+1s + 1 on. The first one that starts at a time of at least s+Ts + T finds the unit ready. Since s+1≤s+Ts + 1 \le s + T, this one starts at exactly s+Ts + T, takes 11 u and falls through. The work C0C_0 then runs, and the next IN starts at s+T+1+C0s + T + 1 + C_0. The time of a block is T+1+C0T + 1 + C_0. For the numbers, 1000+1+203=12041000 + 1 + 203 = 1204. □\square

Theorem (the limit of the gain of buffering, TAOCP Section 1.4.4). A program reads KK blocks from one unit, a block takes TT u to transfer, and the program works C0C_0 u for each block. With plain input, the time per block is T+C0T + C_0 up to a constant. For every method of buffering the time per block is at least max⁡(T,C0)\max(T, C_0). 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 T=C0T = C_0.

Node: 1.4.4-thm-factor-two · Lean: not yet stated

Explanation. The unit must do KK transfers of TT u each, one after another, and the processor must do KK pieces of work of C0C_0 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 T=1000T = 1000 and C0=203C_0 = 203, the plain method needs 12041204 u per block. No method of buffering can need less than 10001000 u per block, and 1204/1000<21204 / 1000 < 2. With T=C0=500T = C_0 = 500 the plain method needs 10001000 u and the best method needs 500500 u. This is the factor two.

Proof. The unit works on one block at a time, so KK transfers need at least KTKT u, and the processor needs at least KC0KC_0 u. The program ends after both are done, so it needs at least Kmax⁡(T,C0)K \max(T, C_0) u. The plain method needs K(T+C0)K(T + C_0) up to the constant. Since T+C0≤2max⁡(T,C0)T + C_0 \le 2\max(T, C_0), the ratio of the two is at most 22. It is 22 only if T=C0T = C_0. □\square

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 F=40F = 40 words, T=600T = 600, and the work on a block be a loop of w=100w = 100 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 100k+i100k + i for the block k=1,…,8k = 1, \ldots, 8 and the word i=0,…,39i = 0, \ldots, 39. The sum of the first words is 36003600, which is 100+200+⋯+800100 + 200 + \cdots + 800. The block took 682682 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 ss. 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 kk of the loop copies the block kk. The sum 36003600 confirms it for the run. □\square

Node: 1.4.4-(3) · Lean: not yet stated

The time of the loop. Let CC 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

max⁡(C,T)+1+(1+2F),\max(C, T) + 1 + (1 + 2F),

which is max⁡(C,T)+82\max(C, T) + 82 for F=40F = 40. The 11 is the JBUS that finds the unit ready, and 1+2F1 + 2F is the time of the MOVE.

Proof of the formula. The IN starts at ss, and the first JBUS starts at s+Cs + C. If C≥TC \ge T, the unit is ready then, and the JBUS falls through at once. If C<TC < T, the JBUS loop runs until the time s+Ts + T, and the last JBUS starts at s+Ts + T. In both cases the JBUS that falls through starts at s+max⁡(C,T)s + \max(C, T), takes 11 u, and the MOVE follows and takes 1+2F1 + 2F u. The IN comes right after. □\square

Example of the formula. With the work of ww passes, the program of the example has C=2w+11C = 2w + 11. These are the IN (11), LDA, ADD and STA (66), ENT2 (11), the 2w2w of the passes, DEC3 and J3P (22) and ENT1 (11). The plain method with the same work has C0=2w+9C_0 = 2w + 9 and takes T+1+C0T + 1 + C_0. The table gives the times of one block, measured on the simulator and computed from the two formulas, which agree.

TT ww plain, T+1+C0T + 1 + C_0 ahead, max⁡(C,T)+82\max(C, T) + 82
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 T=600T = 600 and w=400w = 400 the saving is 517517 u of 14101410. It is worse when the block is short in time. For T=600T = 600 and w=10w = 10 the plain method wins, since the copy costs 8181 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 8282 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 FF words each are put one after the other, and each is followed by a word with a value SS 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 SS, 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 SS, 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 F=10F = 10, the unit 55, and the negative word −1-1 as the value SS, 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 −1-1, the address BUFB, and BUFB: 10 words, the word −1-1, 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 FF words look like a unit with blocks of one word.

Example. A run reads 6060 words from 66 blocks whose words are 100k+i100k + i for k=1,…,6k = 1, \ldots, 6 and i=0,…,9i = 0, \ldots, 9. After each word the program runs a loop of dd passes of two instructions, adds the word to a sum and counts. The sum is 2127021270, which is the sum of the 6060 words, and the program ends. With d=2d = 2 and T=300T = 300, the unit starts a new read every 300300 u. With d=100d = 100 the new read starts every 21572157 u.

Proof of correctness. Let the blocks be b1,b2,…b_1, b_2, \ldots and let bjb_j be in the area that I6 points into. The invariant is that the unit has been told to read bjb_j and bj+1b_{j+1}, that bjb_j is complete, and that bj+1b_{j+1} goes to the other area. At the start this is true after the two IN instructions: the second waits for the first, so b1b_1 is complete, and I6 is at BUFA. While the words of bjb_j are read, the invariant does not change. When the sentinel is found, all words of the area of bjb_j are used. The IN is for bj+2b_{j+2} into this area. It waits for the end of the read of bj+1b_{j+1} by the rule of the model, so bj+1b_{j+1} is complete when I6 changes to the other area. The area of bjb_j is changed by the read of bj+2b_{j+2} only after all its words were read. So the invariant holds for j+1j + 1. The words come in the order of the blocks, and the sentinel is never returned, since the routine does not return a negative word. □\square

Proof of the time. Let CC 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 sjs_j be the start of the read jj. The read j+1j + 1 starts when the unit has finished jj and the processor has come to the IN, so sj+1=max⁡(sj+T,sj+C)s_{j+1} = \max(s_j + T, s_j + C) in the steady state, and the time of a block is max⁡(T,C)\max(T, C). For the program of the example each word costs 88 u for the call and the return (JMP 11, STJ 22, LDA 22, JAN 11, INC6 11, JMP 11), 77 u of the loop in the main program (ADD 22, STA 22, ENT1 11, DEC3 11, J3P 11) and 2d2d u of the passes, which is 15+2d15 + 2d. A block has 1010 words, and the call that finds the sentinel has the extra instructions LDA (22), JAN (11), IN (11), LD6 (22) and JMP (11), which is 77 u. So C=10(15+2d)+7C = 10(15 + 2d) + 7. For d=2d = 2 this is 197<300197 < 300, and the time is T=300T = 300. For d=100d = 100 it is 2157>3002157 > 300, and the time is 21572157. The simulator gives these two values. □\square

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 FF 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 800800 and F=10F = 10. The cells 800800 to 809809 hold 3,10,17,…,663, 10, 17, \ldots, 66 (the numbers 7k+37k + 3). The program does the sequence three times with a circle of three buffers and a unit of T=200T = 200. The simulator shows that the unit received three blocks, and each is the list 3,10,17,…,663, 10, 17, \ldots, 66. The time of the processor for one sequence is 10+1+21+2=3410 + 1 + 21 + 2 = 34 u: ASSIGN 1010 u as counted below, ENT1 11 u, MOVE 1+2⋅10=211 + 2 \cdot 10 = 21 u, and RELEASE 22 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 FF 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. □\square

Definition (the count of red buffers, TAOCP Section 1.4.4). In a circle of NN buffers, nn 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 nn satisfies 0≤n≤N0 \le n \le N. At the start of an input process, n=Nn = N, and at the start of an output process, n=0n = 0.

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 NN 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 N=3N = 3 buffers, a unit with T=200T = 200, four blocks, and a work of about 150150 u between two ASSIGN actions. Each row shows the registers after the action: nn is rI6, NEXTR is rI5, NEXTG is rI2 and CURRENT is rI4, and the buffers are numbered 1,2,31, 2, 3 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 nn 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 nn rises by one at each RELEASE and falls by one at each end of a transfer. In the table it rises at the times 2222, 199199, 380380 and 561561 and falls at 235235, 446446, 657657 and 867867, and the start values are 00. So nn is 11 after the first RELEASE, 22 after the second, and so on, as in the rows. The value never goes above 22, so it never gets to N=3N = 3, and no ASSIGN has to wait. At the time 867867 all four blocks are done and n=0n = 0. The first ASSIGN starts its return at the time 1818. The start-up takes 44 u of coroutine linkage (JMP, STJ, JMP) and 55 u of ENT instructions, and the ASSIGN takes 99 u up to its return. □\square

Algorithms and programs

Algorithm (ASSIGN, TAOCP Section 1.4.4, Algorithm A). This algorithm gives the program the next buffer. A1. If n=Nn = N, wait until n<Nn < N; 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 n<Nn < N, 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 195195 finds n=1<3n = 1 < 3. CURRENT becomes the buffer 22, which is the value of NEXTG before, and NEXTG moves to the buffer 33. At the time 557557 the NEXTG is the buffer 11 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. □\square

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 nn 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 nn 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 2222 takes nn from 00 to 11. The buffer 11, which CURRENT held, is red now and NEXTR points at it.

Proof. By the theorem below, n=n0+r−cn = n_0 + r - c, where rr is the number of RELEASE actions. Step R1 is the increase of rr, 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 nn buffers from NEXTR on. □\square

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 n=0n = 0, 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 nn 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 2828. At the time 2222 the program does RELEASE, and the control routine has the chance to see n=1n = 1 the next time the unit is found ready. This happens at the time 2828, which is about six u later. The routine starts the buffer 11. It is over at 228228, and the routine notices at 235235 in the run, and it moves NEXTR to the buffer 22 and takes nn to 11.

Proof that a transfer starts only on a red buffer and ends with a green one. Step B3 runs only after B2 has found n>0n > 0. 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. □\square

The programs use four registers. The register I6 holds nn, 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 NN 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 n=Nn = N, the program cannot go on. It must not spin without the control routine, since the control routine is the only one that can lower nn. 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 11 u. The path with n<Nn < N takes STJ (22), CMP6 (22), JNE (11), ENT4 (11), LD2 (22) and the return JMP (11), so the ASSIGN takes 1010 u with the call. Each pass of the wait takes CMP6, JNE, JRED not taken and JMP, which is 2+1+1+1=52 + 1 + 1 + 1 = 5 u.

Example. In the test run at the time 195195 of the table, the register I2 holds the address of the buffer 22 before the call. The program goes to 2H at the first test, since n=1n = 1. ENT4 gives I4 the value of the buffer 22, and LD2 loads the word before this buffer, which is the address of the buffer 33.

Proof. The path to 2H is taken exactly if n≠Nn \ne N, and n≤Nn \le N always, so it is taken exactly if n<Nn < N, 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 nn in the program, so the program can go on only when the control routine has made nn smaller. The return goes to the place after the call, by the linkage of Section 1.4.1. □\square

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 22 u, plus the work of the control routine if the jump is taken.

Example. In the table, the RELEASE at the time 2222 takes INC6 from n=0n = 0 to 11, and JRED finds the unit ready, since nothing has started yet. The control starts the transfer. The simulator shows the start at the time 2828, which is the 66 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. □\square

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 n=0n = 0, 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 (11), STJ COMPX (22), JMP at CONTROLX (11), LD5 (22), DEC6 (11), JMP 1B (11), J6Z (11), OUT or IN (11), JMP COMPUTE (11), STJ CONTROLX (22) and JMP at COMPX (11). The sum is 1414 u. A block that is never waited for therefore costs the processor 10+2+14=2610 + 2 + 14 = 26 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 143143 u in all, which is 1111 rounds of 1313 u, and the 1414th 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 11 starts at 2828 and is over at 228228. The control routine sees this at the first JRED of the program after 228228. In the run, DEC6 starts at 235235 and the next transfer starts at 238238.

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 nn. □\square

The start of a run. For input the program sets I5 and I2 to the address of the first buffer and I6 to NN. For output it sets I6 to 00 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 n=0n = 0, 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 aa, rr and cc be the numbers of ASSIGN actions, RELEASE actions and ended transfers. Then n=n0+r−cn = n_0 + r - c, where n0n_0 is the starting value, the pointer NEXTG is at the buffer a mod Na \bmod N and NEXTR is at the buffer c mod Nc \bmod N, counted from the starting buffer, and 0≤n≤N0 \le n \le N always. For input (n0=Nn_0 = N), the kk-th ASSIGN gives a buffer whose kk-th transfer is over, and the jj-th transfer starts only after the RELEASE number j−Nj - N. For output (n0=0n_0 = 0), the jj-th transfer starts only after the RELEASE number jj, and the kk-th ASSIGN gives a buffer whose transfer number k−Nk - N 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 n0n_0, which is the point of the section.

Example. Take the run of the table, an output run with N=3N = 3 and n0=0n_0 = 0. The ASSIGN at the time 557557 is the fourth. Before it, three ASSIGN actions (at 1818, 195195, 376376) and three RELEASE actions (at 2222, 199199, 380380) are done, and two transfers have ended (at 235235 and 446446). So r=3r = 3, c=2c = 2 and n=0+3−2=1n = 0 + 3 - 2 = 1, which is the value that the table shows after the ASSIGN. This ASSIGN needs the transfer number 4−3=14 - 3 = 1 to be over, and it ended at 235235. After it, a=4a = 4, so NEXTG is at the buffer 4 mod 3=14 \bmod 3 = 1 counted from 00, which is the buffer number 22 of the table, and NEXTR is at c mod 3=2c \bmod 3 = 2, the buffer number 33.

Proof. The statement n=n0+r−cn = n_0 + r - c holds because only R1 and B6 change nn, by +1+1 and −1-1. NEXTG moves one step in each A3, so after aa actions it is at the buffer a mod Na \bmod N. NEXTR moves one step in each B5, which comes with B6, so it is at the buffer c mod Nc \bmod N when cc transfers are over. During a transfer, NEXTR is at the buffer of the transfer.

For the bounds: n≥0n \ge 0, since B6 follows B3, and B3 ran after n>0n > 0 was found and only B6 lowers nn. And n≤Nn \le N, since R1 follows an ASSIGN that passed A1 with n<Nn < N, and nothing but R1 raises nn, so n≤Nn \le N after R1.

For input, with n0=Nn_0 = N: the kk-th ASSIGN runs when a=k−1a = k - 1 and r=k−1r = k - 1, since the program alternates and the earlier ones have been released. It passes A1, so N+(k−1)−c<NN + (k - 1) - c < N, which means c≥kc \ge k. The transfer number kk is over. By the rule of NEXTG and NEXTR, the kk-th ASSIGN and the kk-th transfer both use the buffer (k−1) mod N(k - 1) \bmod N. The jj-th transfer starts at B3 with c=j−1c = j - 1 and n>0n > 0, so N+r−(j−1)>0N + r - (j - 1) > 0, which means r≥j−Nr \ge j - N. The buffer of the transfer jj was used by the ASSIGN number j−Nj - N (if j>Nj > N), and its RELEASE number j−Nj - N is done.

For output, with n0=0n_0 = 0: the jj-th transfer starts with c=j−1c = j - 1 and n>0n > 0, so r−(j−1)>0r - (j - 1) > 0, which means r≥jr \ge j, and the buffer was released after it was filled. The kk-th ASSIGN passes A1 with r=k−1r = k - 1, so k−1−c<Nk - 1 - c < N, which is c≥k−Nc \ge k - N. The transfer number k−Nk - N of the same buffer is over. □\square

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 KK times: ASSIGN, work aa, RELEASE, work bb, with C=a+bC = a + b, and a unit with the transfer time TT. If N=2N = 2, the time to the end of the last transfer is max⁡((K−1)C+a+T, KC)\max((K - 1)C + a + T,\ KC) if C≥TC \ge T, and a+KTa + KT if C<TC < T. No NN and no method of buffering is faster, and N=1N = 1 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 T=40T = 40, a=10a = 10, b=20b = 20 and K=5K = 5, so C=30<TC = 30 < T. The formula gives a+KT=210a + KT = 210. A model program of the same events with N=2N = 2 gives 210210. With T=25T = 25 the formula gives max⁡(4⋅30+10+25,150)=155\max(4 \cdot 30 + 10 + 25, 150) = 155, and the model gives 155155. With N=1N = 1 the model gives 250250 for T=40T = 40 and 175175 for T=25T = 25, which are longer. The model was also run on 7575 cases with N=2N = 2 and N=5N = 5, and it gave the formula each time.

Proof. Let pkp_k, rk=pk+ar_k = p_k + a, sks_k and fk=sk+Tf_k = s_k + T be the time of the kk-th ASSIGN, of the kk-th RELEASE, and of the start and the end of the kk-th transfer. Then sk=max⁡(rk,fk−1)s_k = \max(r_k, f_{k-1}), and pk=max⁡(rk−1+b,fk−2)p_k = \max(r_{k-1} + b, f_{k-2}) for N=2N = 2, since the kk-th ASSIGN needs the transfer k−2k - 2 to be over (the theorem above), with fj=0f_j = 0 for j≤0j \le 0 and p1=0p_1 = 0.

Let C≥TC \ge T. It is shown by induction that pk=(k−1)Cp_k = (k - 1)C and sk=rks_k = r_k. If it holds for the indices before kk, then fk−1=rk−1+T≤rk−1+C=rkf_{k-1} = r_{k-1} + T \le r_{k-1} + C = r_k, so sk=rks_k = r_k. Also fk−2=(k−3)C+a+T≤(k−1)Cf_{k-2} = (k-3)C + a + T \le (k - 1)C, since a+T≤2Ca + T \le 2C, so pk=rk−1+b=(k−1)Cp_k = r_{k-1} + b = (k-1)C. The last transfer ends at rK+T=(K−1)C+a+Tr_K + T = (K-1)C + a + T, and the program ends at KCKC.

Let C<TC < T. It is shown by induction that sk=a+(k−1)Ts_k = a + (k-1)T and pk≤(k−1)Tp_k \le (k-1)T. For k=1k = 1 and k=2k = 2 this is true since p2=C<Tp_2 = C < T and r2=a+C≤f1=a+Tr_2 = a + C \le f_1 = a + T. If it holds before kk, then fk−2=a+(k−2)Tf_{k-2} = a + (k-2)T and pk=max⁡(pk−1+C,fk−2)≤(k−2)T+max⁡(C,a)<(k−1)Tp_k = \max(p_{k-1} + C, f_{k-2}) \le (k - 2)T + \max(C, a) < (k-1)T, since a≤C<Ta \le C < T. So rk=pk+a≤a+(k−1)T=fk−1r_k = p_k + a \le a + (k-1)T = f_{k-1} and sk=fk−1s_k = f_{k-1}. The last transfer ends at a+KTa + KT.

No method is faster. The last RELEASE comes at least (K−1)C+a(K-1)C + a after the start, and its transfer needs TT more. The unit needs KTKT after the first RELEASE at aa at the earliest, and the program itself needs KCKC. For C≥TC \ge T the largest of these bounds is the first or the last one, and for C<TC < T it is a+KTa + KT. The formulas are equal to them. □\square

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 T=400T = 400, 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.

NN 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 nn units. Exercises 8 to 11 are time charts. The model of this chapter, with the formulas sk=max⁡(rk,fk−1)s_k = \max(r_k, f_{k-1}) and pk=max⁡(rk−1+b,fk−N)p_k = \max(r_{k-1} + b, f_{k-N}), gives the charts for each NN, 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 n=0n = 0, 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 n≤Nn \le N, 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 aa, rr and cc 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 nn 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 NN, nn, 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.