CS 662: Theory of Parallel Algorithms SR Matrix Examples

San Diego State University -- This page last updated March 15, 1995

Matrix Multiplication

Mesh - Heartbeat

Multiply two N*N matrices

Fake the input of initial data

```global sizes
var N := 6
body sizes
getarg(1, N)
end

resource point
op compute(right, down: cap point)
op rowValue(value: real)
op colValue(value: real)
body point(i, j: int)
import sizes
var aij, bij real
var cij: real := 0.0

proc compute(right, down)
fa k := 1 to N ->
cij +:= aij*bij
send down.colValue(bij)
send right.rowValue(aij)
af
end

final write(i, j, cij) end

end point
resource main()
import sizes, point
var grid[N, N]: cap point

fa i := 1 to N, j := 1 to N ->
grid[i,j] := create point(i,j)
af

#connect the grid together
fa i := 1 to N-1, j := 1 to N-1 ->
send grid[i,j].compute(grid(i,j+1),
grid(i+1,j))
af
fa i := 1 to N-1 ->
send grid[i,N].compute(noop,
grid(i+1,j))
send grid[N,i].compute(grid(N,i+1),
noop)
fa
send grid[N,N].compute(noop,noop)
var A[N,N], B[N,N]: real

readMatrix( A, B) # code not shown

# Feed the processors
fa i := N downto 1, j := 1 to N ->
send grid[1,j].colValue(B[j,i])
send grid[j,1].rowValue(A[j,i])
af

fa i := N downto 1, j := 1 to N ->
destroy grid[i,j]
af

end
```

Prescheduled Strips

```global sizes
var N := 10
var PR :=2
var S: int

body sizes
getarg(1, N)
getarg(2, PR)
S := N/PR
if N mod PR != 0 ->
write("N must be a multiple of PR")
stop (1)
fi
end

resource mult ()
import sizes
var a[N,N], b[N,N], c[N,N]: real
sem done := 0, continue := 0
var start := age ()

process strip(p := 1 to PR)
const R := (p-1)*S + 1
fa i := R to R+S-1, j := 1 to N ->
a[i,j] := 1.0; b[i,j] := 1.0
af

V(done); P(continue)

fa i ;= R to R+S-1, j := 1 to N ->
var inner prod := 0.0
af
fa k := 1 to N ->
c[i,j] := inner_prod
af
end
process coordinator
fa i := 1 to PR -> P(done) af
fa i := 1 to PR -> V(continue) af
end

final
#code here to print results
end
```