diff --git a/2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md b/2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md index f69d85fdcaf7fe32d5cb6404eab60be5db70d13e..531188451fa0bd7a6c512a8fa9dadc28a520d4fd 100644 --- a/2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md +++ b/2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md @@ -6,82 +6,11 @@ <i class="twa twa-mechanical-leg"></i> <br> Compiler & language internals +(part 1) </div> -# What is this "compiled code"? - -You can inspect various stages of code representation (and reveal bugs): - -- `code_lowered(+)` -- `code_typed(+)` -- `code_typed(+, (Int, Float64))` -- `code_llvm` -- `code_native` - -What happens if we use an abstract type? - - - -<center style="font-size:200%; line-height:120%; margin-bottom: 1em"> -Performance rule #1:<br><br> -<strong>Less instructions is typically better,</strong><br> -<strong>less jumps is typically much better.</strong> -</center> - - - -<center style="font-size:200%; line-height:120%; margin-bottom: 1em"> -Performance rule #2:<br><br> -<strong>Memory allocations comprise of many instructions<br> and plenty of jumps.</strong> -</center> - - - -# What are all these things starting with `@`? - -Macros allow you to generate code before compiler runs it: -- `@info` can know what variable names to display -- `@benchmark` can run the code several times -- `@test` can safely wrap the failing code and print what failed - -## Important types for macros: - -- symbols: `:abc`, `:+`, ... -- expressions: `:(1+2)`, `quote ... end` - -Macros are functions that get unevaluated arguments (as expressions) and return unevaluated results (as expressions). - -```julia -macro clear_data(v) - quote - $(Symbol(v, :_orig)) = load_original_data() - $(Symbol(v, :_enriched)) = nothing - $(Symbol(v, :_final)) = nothing - end -end - -@clear_data patients # reloads patients_orig, clears patients_enriched, patients_final -``` - - - -# Macro use-cases - -Use macros to improve your code whenever Julia syntax is too unwieldy. - -```julia -model = @reaction_network MyModel begin - c1, s + i --> 2i - c2, i --> r -end -``` - -(Taken from `Catalyst.jl`) - - - # How do I package my code? Use `] generate MyPackageName`. diff --git a/2023/2023-03-21_ProgrammingWithJulia-5/slides/5a-para.md b/2023/2023-03-21_ProgrammingWithJulia-5/slides/5a-para.md index 4a59cfbab433e127dfdadef8e20bf3fa8b031a30..96c7c4f5d516b892ce1a5238e8a52fa57df7b567 100644 --- a/2023/2023-03-21_ProgrammingWithJulia-5/slides/5a-para.md +++ b/2023/2023-03-21_ProgrammingWithJulia-5/slides/5a-para.md @@ -172,30 +172,68 @@ We will simulate a cookie distribution network: -# Homework 3 (Data) +# Homework 3 (Assignment) + +Tasks: +- read the cookie network from a JSON file (we'll provide example data, use `JSON.jl`) +- make a nice data structure to hold this problem, make sure the input is valid +- make functions that: + - find the length of the *longest chain* (by transport "steps") from the factory to the muncher + - find out how many cookies the factory needs to produce daily so that *all munchers are fed* + - construct a network where all *transports are split in half*, each half with half cookie consumption + - find out *how many cookies are wasted* by being routed to munchers who can't eat them + - construct a network where the *distribution points are balanced* so that no cookies get wasted + - *BONUS: print the network nicely* +- for simplicity, data structures and functions may be recursive + - performance optimization _is not_ a goal + - nice short code _is_ a goal + + + + +# Homework 3 (Example data) ```json -{ type: "distribution point", - serves: [ - { type: "muncher", consumption: 3 }, - { type: "transport", - capacity: 5, - serves: { - type: "distribution point", - serves: [ - { type: "muncher", consumption: 7 }, - { type: "muncher", consumption: 2 }, - { type: "muncher", consumption: 1 } - ] - ratios: [1,1,1] +{ "type": "distribution point", + "serves": [ + { "type": "muncher", + "consumption": 3 }, + { "type": "transport", + "capacity": 5, + "serves": { + "type": "distribution point", + "serves": [ + { "type": "muncher", + "consumption": 7 }, + { "type": "muncher", + "consumption": 2 }, + { "type": "muncher", + "consumption": 1 } ], + "ratios": [ 1, 1, 1 ] } } ], - ratios: [1,5] + "ratios": [ 1, 5 ] } ``` -Pretty-printed (one possibility): + + + +# Homework 3 (expected results on the example data) + +- make one common abstract type for "everything in the network" +- longest chain length: 3 (any of the "nested" munchers is at the longest chain) +- required daily production for feeding all munchers: 36 (7+7+7 cookies for the nested munchers because of 1:1:1 ratio, that needs 5 cookies for transport (26 total), and overfeeds the first muncher by 3 cookies because of 6+30 in ratio 1:5). Alternative ways to compute are OK (if allowing non-integer cookies, we'd get something like 30.24 daily consumption). +- transports split in half: instead of `... -> transport 5 -> ...` the network should contain something like `... -> transport 10 -> transport 10 -> ...`. +- how many cookies are wasted: the easiest way to guess is to assume perfect distribution and make a difference from the required daily production. In this case, if distribution ratios were perfect, the network would consume 15 cookies daily (10 for the nested munchers + 2 for transport + 3 for the other muncher), which wastes 21 or 15.24 cookies depending on the interpretation. + + + + +# Homework 3 (expected results) + +Pretty-printed example network (one possibility): ``` 1 -> munch 3 5 -> transport 5 -> 1 -> munch 7 @@ -203,20 +241,19 @@ Pretty-printed (one possibility): 1 -> munch 1 ``` +Balanced distribution network (you don't need to pretty-print the modified network! here we are just showing it in the pretty format): +``` +1 -> munch 3 +4 -> transport 5 -> 7 -> munch 7 + 2 -> munch 2 + 1 -> munch 1 +``` +This is equally valid: +``` +3 -> munch 3 +12 -> transport 5 -> 7 -> munch 7 + 2 -> munch 2 + 1 -> munch 1 +``` -# Homework 3 (Assignment) - -Tasks: -- read the cookie network from a JSON file (we'll provide example data, use `JSON.jl`) -- make a nice data structure to hold this problem, make sure the input is valid -- make functions that: - - find the length of the *longest chain* (by transport "steps") from the factory to the muncher - - find out how many cookies the factory needs to produce daily so that *all munchers are fed* - - construct a network where all *transports are split in half*, each half with half cookie consumption - - find out *how many cookies are wasted* by being routed to munchers who can't eat them - - construct a network where the *distribution points are balanced* so that no cookies get wasted - - *BONUS: print the network nicely* -- for simplicity, data structures and functions may be recursive - - performance optimization _is not_ a goal - - nice short code _is_ a goal