<s> OCM_SIZE = <NUM_LIT:2> ** <NUM_LIT:8> <EOL> READ_MODE = <NUM_LIT:0> <EOL> WRITE_MODE = <NUM_LIT:1> <EOL> COMM_SIZE = <NUM_LIT:2> ** <NUM_LIT:4> <EOL> STREAM_SIZE = <NUM_LIT:2> ** <NUM_LIT:10> <EOL> DATA_BITWIDTH = <NUM_LIT:64> <EOL> WORD_SIZE = DATA_BITWIDTH / <NUM_LIT:8> <EOL> instream = CoramInStream ( <NUM_LIT:0> , datawidth = DATA_BITWIDTH , size = STREAM_SIZE ) <EOL> outstream = CoramOutStream ( <NUM_LIT:0> , datawidth = DATA_BITWIDTH , size = STREAM_SIZE ) <EOL> channel = CoramChannel ( idx = <NUM_LIT:0> , datawidth = <NUM_LIT:32> , size = COMM_SIZE ) <EOL> register = CoramRegister ( idx = <NUM_LIT:0> , datawidth = <NUM_LIT:32> ) <EOL> DOWN_LEFT = <NUM_LIT:0> <EOL> DOWN_PARENT = <NUM_LIT:1> <EOL> DOWN_RIGHT = <NUM_LIT:2> <EOL> UP_PARENT = <NUM_LIT:1> <EOL> UP_CHILD = <NUM_LIT:0> <EOL> offset = <NUM_LIT:0> <EOL> num_entries = <NUM_LIT:0> <EOL> def downheap ( ) : <EOL> if num_entries == <NUM_LIT:0> : <EOL> return <EOL> if num_entries + <NUM_LIT:1> >= OCM_SIZE : <EOL> instream . write_nonblocking ( offset + num_entries * WORD_SIZE + WORD_SIZE , <NUM_LIT:1> ) <EOL> index = <NUM_LIT:1> <EOL> while True : <EOL> if index * <NUM_LIT:2> > num_entries : <EOL> if index >= OCM_SIZE : <EOL> outstream . read_nonblocking ( index * WORD_SIZE + offset , <NUM_LIT:1> ) <EOL> break <EOL> if ( index * <NUM_LIT:2> ) >= OCM_SIZE : <EOL> instream . write_nonblocking ( index * WORD_SIZE * <NUM_LIT:2> + offset , <NUM_LIT:2> ) <EOL> elif ( index * <NUM_LIT:2> ) + <NUM_LIT:1> >= OCM_SIZE : <EOL> instream . write_nonblocking ( index * WORD_SIZE * <NUM_LIT:2> + offset + WORD_SIZE , <NUM_LIT:1> ) <EOL> select = channel . read ( ) <EOL> if index >= OCM_SIZE : <EOL> outstream . read_nonblocking ( index * WORD_SIZE + offset , <NUM_LIT:1> ) <EOL> if select == DOWN_LEFT : <EOL> index = index * <NUM_LIT:2> <EOL> elif select == DOWN_RIGHT : <EOL> index = index * <NUM_LIT:2> + <NUM_LIT:1> <EOL> else : <EOL> break <EOL> def upheap ( ) : <EOL> index = num_entries <EOL> while index > <NUM_LIT:1> : <EOL> if ( index / <NUM_LIT:2> ) >= OCM_SIZE : <EOL> instream . write_nonblocking ( ( index / <NUM_LIT:2> ) * WORD_SIZE + offset , <NUM_LIT:1> ) <EOL> select = channel . read ( ) <EOL> if index >= OCM_SIZE : <EOL> outstream . read_nonblocking ( index * WORD_SIZE + offset , <NUM_LIT:1> ) <EOL> index = index / <NUM_LIT:2> <EOL> if select == UP_PARENT : break <EOL> if index >= OCM_SIZE : <EOL> outstream . read_nonblocking ( index * WORD_SIZE + offset , <NUM_LIT:1> ) <EOL> def heap ( ) : <EOL> global offset <EOL> global num_entries <EOL> mode = channel . read ( ) <EOL> offset = register . read ( ) <EOL> if mode == <NUM_LIT:1> : <EOL> num_entries -= <NUM_LIT:1> <EOL> downheap ( ) <EOL> elif mode == <NUM_LIT:2> : <EOL> num_entries += <NUM_LIT:1> <EOL> upheap ( ) <EOL> elif mode == <NUM_LIT> : <EOL> num_entries = <NUM_LIT:0> <EOL> while True : <EOL> heap ( ) </s>
