1 / 27

CS162 Week 5

Learn about atomic blocks and reactive variables in programming. Explore examples and solutions for updating variables during loops and computations.

jlu
Download Presentation

CS162 Week 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS162 Week 5 • Kyle Dewey

  2. Overview • atomic blocks • More examples • Question / answer session

  3. RIP Refresher

  4. newCons Output: 01...10 var a in a := 0; newCons { output a // `a` is reactive }; while (a < 10) { a := a + 1 // trigger `output` }

  5. atomic Blocks

  6. Problem • We need to update a variable multiple times during a loop • The computation is not “done” until the last assignment • We want to update only when the computation is done

  7. Example Output: 03691224 var a in a := 0; newCons { output a }; while (a < 11) { a := a + 3 }; a := a + a // now `a` is ready

  8. Hacky Solution • Add a flag isDone • Set to false beforehand • Set to true when a constraint is ready • In the constraint, only process if isDone is true

  9. Better Solution • Let the language handle it • Introduce a special atomic block • Constraints are only updated once we leave the atomic block • Instead of having multiple updates of the same constraint, only update the constraint once at the end

  10. With atomic var a in a := 0; newCons { output a }; atomic { while (a < 11) { a := a + 3 }; a := a + a // now `a` is ready } Output: 024

  11. Nesting atomic var a, b in newCons { output b }; newCons { output a }; atomic { a := 2; atomic { b := 4 }; a := 3 } Output: undefundef34

  12. More Examples

  13. What’s the Output? var b in b:=4;atomic { newCons { output b }; atomic { b := b+1 }};b := 10

  14. var b in b:=4;atomic { newCons { output b }; atomic { b := b+1 }};b := 10 Output: 4 5 10 What’s the Output?

  15. var a, b in a := 0; b := 0; newCons {   output a;  output b }; atomic {  a := a + 1;  atomic {    b := b + 1;  } } What’s the Output?

  16. var a, b in a := 0; b := 0; newCons {   output a;  output b }; atomic {  a := a + 1;  atomic {    b := b + 1;  } } Output: 0 0 1 1 1 1 What’s the Output?

  17. var a in a := 0; newCons {   output a; }; atomic {  a := a + 1;  atomic {    a := a + 1;  } } What’s the Output?

  18. var a in a := 0; newCons {   output a; }; atomic {  a := a + 1;  atomic {    a := a + 1;  } } Output: 0 2 What’s the Output?

  19. test52.not

  20. Question / Answer

  21. Question • Is it possible to switch from atomic mode to constraint mode without exiting an atomic block?

  22. Answer • Yes var a in // normal mode a := 0; atomic { // atomic mode newCons { // constraint mode output a } }; a := 10

  23. Question • After the constraint stack is implemented, does the cSelf field of ConstraintMode have any use?

  24. Answer Part #1 • Yes • cSelf indicates the constraint you are currently executing • The constraint stack indicates how deeply nested you are within newCons blocks • These are not exactly the same

  25. Answer Part #2 var a in newCons { // pushes `a := a + 1` a := a + 1 }; // pops `a := a + 1` a := 5 // doesn’t push anything

  26. Question • Should all addresses that have been updated be in an atomic block be pushed onto the atomic stack?

  27. Answer • Only addresses that are reactive at the time of the update should be pushed • If the address is not reactive at the time but becomes reactive before the end of the atomic block, this can result in constraints getting executed that should not be • See test52.not

More Related