Merge pull request #45 from fverdugo/francesc

Rather minor changes in async and distributed notebooks
This commit is contained in:
Francesc Verdugo 2024-09-12 11:25:07 +02:00 committed by GitHub
commit 1d6a3b6b7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 25 deletions

View File

@ -283,7 +283,7 @@
"metadata": {},
"outputs": [],
"source": [
"fun = () -> compute_π(4_000_000_000)\n",
"fun = () -> @show compute_π(4_000_000_000)\n",
"t = Task(fun)"
]
},
@ -359,7 +359,7 @@
"metadata": {},
"outputs": [],
"source": [
"fun = () -> compute_π_yield(3_000_000_000)\n",
"fun = () -> @show compute_π_yield(3_000_000_000)\n",
"t = Task(fun)\n",
"schedule(t)"
]
@ -729,6 +729,18 @@
"take!(chnl)"
]
},
{
"cell_type": "markdown",
"id": "8a1ef849",
"metadata": {},
"source": [
"In summary:\n",
"\n",
"- `put!` will wait for a `take!` if there is not space left in the channel's buffer.\n",
"- `take!` will wait for a `put!` if there is no data to be consumed in the channel.\n",
"- Both `put!` and `take!` will raise an error if the channel is closed."
]
},
{
"cell_type": "markdown",
"id": "9ddd66ca",
@ -746,7 +758,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = @elapsed compute_π(100_000_000)"
"t = @elapsed @show compute_π(140_000_000)"
]
},
{
@ -772,7 +784,7 @@
"outputs": [],
"source": [
"@time for i in 1:10\n",
" compute_π(100_000_000)\n",
" @show compute_π(140_000_000)\n",
"end"
]
},
@ -810,7 +822,7 @@
"outputs": [],
"source": [
"@time for i in 1:10\n",
" @async compute_π(100_000_000)\n",
" @async @show compute_π(140_000_000)\n",
"end"
]
},
@ -847,7 +859,7 @@
"outputs": [],
"source": [
"@time @sync for i in 1:10\n",
" @async compute_π(100_000_000)\n",
" @async @show compute_π(140_000_000)\n",
"end"
]
},
@ -970,6 +982,22 @@
"</div>"
]
},
{
"cell_type": "markdown",
"id": "dfab0c90",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"In order to start \"thinking in parallel\" you first need to be familiar with concepts of asynchronous programming, in particular tasks. In this notebook, we have seen the basics of working with tasks. Some key points to remember:\n",
"\n",
"- How to create, schedule, and fetch from a task.\n",
"- Tasks run asynchronously, but not in parallel. You can have a single core CPU and still be able to work with several tasks.\n",
"- Channels are used to communicate data between tasks.\n",
"- Adding data (`put!`) or taking data (`take!`) from a channel might wait depending on the channel state. Be careful to avoid dead locks.\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "a5d3730b",

File diff suppressed because one or more lines are too long