Merge pull request #70 from fverdugo/2025-26

Small fixes for lecture 03
This commit is contained in:
Francesc Verdugo
2025-09-08 12:04:41 +02:00
committed by GitHub
2 changed files with 17 additions and 12 deletions

View File

@@ -48,11 +48,12 @@ heatmap(x,y,values)
```julia ```julia
f = () -> Channel{Int}(1) f = () -> Channel{Int}(1)
chnls = [ RemoteChannel(f,w) for w in workers() ] worker_ids = workers()
@sync for (iw,w) in enumerate(workers()) chnls = [ RemoteChannel(f,w) for w in worker_ids ]
@sync for (iw,w) in enumerate(worker_ids)
@spawnat w begin @spawnat w begin
chnl_snd = chnls[iw] chnl_snd = chnls[iw]
if w == 2 if iw == 1
chnl_rcv = chnls[end] chnl_rcv = chnls[end]
msg = 2 msg = 2
println("msg = $msg") println("msg = $msg")
@@ -70,23 +71,26 @@ chnls = [ RemoteChannel(f,w) for w in workers() ]
end end
``` ```
This is another possible solution. This is another possible solution that does not use remote channels.
```julia ```julia
@everywhere function work(msg) @everywhere function work(msg,iw,worker_ids)
println("msg = $msg") println("msg = $msg")
if myid() != nprocs() if iw < length(worker_ids)
next = myid() + 1 inext = iw+1
@fetchfrom next work(msg+1) next = worker_ids[iw+1]
@fetchfrom next work(msg+1,inext,worker_ids)
else else
@fetchfrom 2 println("msg = $msg") @fetchfrom worker_ids[1] println("msg = $msg")
end end
return nothing
end end
msg = 2 msg = 2
@fetchfrom 2 work(msg) iw = 1
worker_ids = workers()
@fetchfrom worker_ids[iw] work(msg,iw,worker_ids)
``` ```
## Matrix-matrix multiplication ## Matrix-matrix multiplication
### Exercise 1 ### Exercise 1

View File

@@ -738,7 +738,8 @@
"\n", "\n",
"- `put!` will wait for a `take!` if there is not space left in the channel's buffer.\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", "- `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." "- `put!` will raise an error if the channel is closed.\n",
"- `take!` will raise an error if the channel is closed *and* empty."
] ]
}, },
{ {