mirror of
https://github.com/fverdugo/XM_40017.git
synced 2025-11-08 18:14:23 +01:00
Added solutions for Jacobi notebook
This commit is contained in:
parent
50fb289e5b
commit
7dd8b4fabc
@ -213,4 +213,58 @@ end
|
||||
|
||||
At each call to @spawnat we will communicate O(N) and compute O(N) in a worker process just like in algorithm 1. However, we will do this work N^2/P times on average at each worker. Thus, the total communication and computation on a worker will be O(N^3/P) for both communication and computation. Thus, the communication over computation ratio will still be O(1) and thus the communication will dominate in practice, making the algorithm inefficient.
|
||||
|
||||
## Jacobi method
|
||||
|
||||
### Exercise 1
|
||||
|
||||
```julia
|
||||
@everywhere workers() begin
|
||||
using MPI
|
||||
comm = MPI.Comm_dup(MPI.COMM_WORLD)
|
||||
function jacobi_mpi(n,niters)
|
||||
nranks = MPI.Comm_size(comm)
|
||||
rank = MPI.Comm_rank(comm)
|
||||
if mod(n,nranks) != 0
|
||||
println("n must be a multiple of nranks")
|
||||
MPI.Abort(comm,1)
|
||||
end
|
||||
n_own = div(n,nranks)
|
||||
u = zeros(n_own+2)
|
||||
u[1] = -1
|
||||
u[end] = 1
|
||||
u_new = copy(u)
|
||||
for t in 1:niters
|
||||
reqs = MPI.Request[]
|
||||
if rank != 0
|
||||
neig_rank = rank-1
|
||||
req = MPI.Isend(view(u,2:2),comm,dest=neig_rank,tag=0)
|
||||
push!(reqs,req)
|
||||
req = MPI.Irecv!(view(u,1:1),comm,source=neig_rank,tag=0)
|
||||
push!(reqs,req)
|
||||
end
|
||||
if rank != (nranks-1)
|
||||
neig_rank = rank+1
|
||||
s = n_own+1
|
||||
r = n_own+2
|
||||
req = MPI.Isend(view(u,s:s),comm,dest=neig_rank,tag=0)
|
||||
push!(reqs,req)
|
||||
req = MPI.Irecv!(view(u,r:r),comm,source=neig_rank,tag=0)
|
||||
push!(reqs,req)
|
||||
end
|
||||
for i in 3:n_own
|
||||
u_new[i] = 0.5*(u[i-1]+u[i+1])
|
||||
end
|
||||
MPI.Waitall(reqs)
|
||||
for i in (2,n_own+1)
|
||||
u_new[i] = 0.5*(u[i-1]+u[i+1])
|
||||
end
|
||||
u, u_new = u_new, u
|
||||
end
|
||||
return u
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
|
Before Width: | Height: | Size: 4.8 MiB After Width: | Height: | Size: 5.2 MiB |
Loading…
x
Reference in New Issue
Block a user