-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
let dotprod [n] (xs: [n]f32) (ys: [n]f32): f32 = | ||
#[sequential] reduce (+) 0.0 (map2 (*) xs ys) | ||
|
||
let house [d] (x: [d]f32): ([d]f32, f32) = | ||
let dot = dotprod x x | ||
let dot' = dot - x[0]**2 + x[0]**2 | ||
let beta = if dot' != 0 then 2.0/dot' else 0 | ||
in (x, beta) | ||
|
||
let matmul [n][p][m] (xss: [n][p]f32) (yss: [p][m]f32): [n][m]f32 = | ||
map (\xs -> map (dotprod xs) (transpose yss)) xss | ||
|
||
let outer [n][m] (xs: [n]f32) (ys: [m]f32): [n][m]f32 = | ||
matmul (map (\x -> [x]) xs) [ys] | ||
|
||
let matsub [m][n] (xss: [m][n]f32) (yss: [m][n]f32): *[m][n]f32 = | ||
map2 (\xs ys -> map2 (-) xs ys) xss yss | ||
|
||
let matadd [m][n] (xss: [m][n]f32) (yss: [m][n]f32): [m][n]f32 = | ||
map2 (\xs ys -> map2 (+) xs ys) xss yss | ||
|
||
let matmul_scalar [m][n] (xss: [m][n]f32) (k: f32): *[m][n]f32 = | ||
map (map (*k)) xss | ||
|
||
let block_householder [m][n] (A: [m][n]f32) (r: i32): ([][]f32, [][]f32) = | ||
#[unsafe] | ||
let Q = replicate m (replicate m 0) | ||
let (Q,A) = | ||
loop (Q,A) = (Q, copy A) for k in 0..<(n/r) do | ||
let s = k * r | ||
let V = replicate m (replicate r 0f32) | ||
let Bs = replicate r 0f32 | ||
|
||
let (A) = loop (A) for j in 0..<r do | ||
let u = s + j | ||
let block = A[u:, u:s+r] | ||
let (v, B) = house block[:, 0] | ||
let BvvT = (matmul_scalar (outer v v) B) | ||
let BvvTAk = matmul BvvT block | ||
let A[u:, u:s+r] = matsub block BvvTAk | ||
in A | ||
|
||
let Q[:, s:] = copy Q[:, s:] | ||
in (Q,A) | ||
in (Q,A) | ||
|
||
let main arrs r = map (\arr -> block_householder arr r) arrs |