Created
March 14, 2015 02:38
-
-
Save andrewray/0be90c852343d79c723a to your computer and use it in GitHub Desktop.
OCaml conjugate gradient solver (for positive semi-definite matrices). For use in the Coursera "VLSI CAD: Logic to Layout" mooc placement exersize.
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
(* | |
An OCaml (4.02) port of the C++ conjugate gradient solver. | |
Demo | |
---- | |
$ ocaml | |
# #use "solver.ml";; | |
# let b, (err, x) = Demo.solve_small();; | |
# let err, x = Demo.solve_psd();; | |
# let err, x = Demo.solve_big();; | |
Original license | |
---------------- | |
University of Illinois/NCSA Open Source License | |
Copyright (c) 2013 University of Illinois at Urbana-Champaign. All rights | |
reserved. | |
Developed by: | |
The teaching staff of VLSI CAD: Logic to Layout | |
University of Illinois | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal with | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
* Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimers. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimers in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the names of the CodingSpectator Team, University of Illinois, nor the | |
names of its contributors may be used to endorse or promote products derived | |
from this Software without specific prior written permission. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS | |
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. | |
*) | |
(* element *) | |
type 'a e = | |
{ | |
row : int; | |
col : int; | |
dat : 'a; | |
} | |
(* sparse matrix *) | |
type 'a spmat = 'a e array | |
type 'a vec = 'a array | |
(* multiply sparse matrix 'a' by vec 'b' *) | |
let ( *: ) mat vec = | |
let y = Array.init (Array.length vec) (fun _ -> 0.) in | |
for i=0 to Array.length mat - 1 do | |
y.(mat.(i).row) <- y.(mat.(i).row) +. (mat.(i).dat *. vec.(mat.(i).col)) | |
done; | |
y | |
(* vector ops *) | |
let dot x y = | |
let r = ref 0.0 in | |
for i=0 to Array.length x - 1 do | |
r := !r +. (x.(i) *. y.(i)) | |
done; | |
!r | |
let ( *:. ) a s = Array.init (Array.length a) (fun i -> a.(i) *. s) | |
let (-:) a b = Array.init (Array.length a) (fun i -> a.(i) -. b.(i)) | |
let (+:) a b = Array.init (Array.length a) (fun i -> a.(i) +. b.(i)) | |
(* a.x = b, solve for x => x = a^{-1}.b *) | |
let conjugate_gradient a b = | |
let maxit = 1000 in | |
let n = Array.length b in | |
let x = Array.init n (fun _ -> Random.float 1.) in | |
let ax = a *: x in | |
let r = b -: ax in | |
let p = Array.copy r in | |
let rec iter i x r p rnorm' error' = | |
if i >= maxit then error', x | |
else | |
let ap = a *: p in | |
let alpha = rnorm' /. (dot p ap) in | |
let x = x +: (p *:. alpha) in | |
let r = r -: (ap *:. alpha) in | |
let rnorm = dot r r in | |
if sqrt rnorm < 1e-8 then error', x | |
else | |
let error = abs_float (dot r x) in | |
let p = p *:. (rnorm /. rnorm') in | |
let p = p +: r in | |
iter (i+1) x r p rnorm error | |
in | |
iter 0 x r p (dot r r) 1. | |
(* Demos copied from C code *) | |
module Demo = struct | |
let mk row col dat = { row; col; dat } | |
(* simple test. Calcute b from x, then ensure we get x back again from the solver *) | |
let solve_small () = | |
let row = [|0; 0; 1; 1; 1; 2; 2|] in | |
let col = [|0; 1; 0; 1; 2; 1; 2|] in | |
let dat = [|4.0; -1.0; -1.0; 4.0; -1.0; -1.0; 4.0|] in | |
let a = Array.init 7 (fun i -> mk row.(i) col.(i) dat.(i)) in | |
let x = [| 1.; 1.; 1. |] in | |
let b = a *: x in | |
let x = conjugate_gradient a b in (* we should get x back *) | |
b, x | |
let read_coo_matrix fname = | |
let open Scanf in | |
let f = open_in fname in | |
let size,nnz = fscanf f "%i %i\n" (fun a b -> a,b) in | |
let rec read () = | |
match fscanf f " %i %i %f" (fun r c d -> mk r c d) with | |
| x -> x::read() | |
| exception _ -> [] | |
in | |
let mat = read () in | |
close_in f; | |
size, Array.of_list mat | |
let read_vec fname = | |
let open Scanf in | |
let f = open_in fname in | |
let rec read () = | |
match fscanf f " %f" (fun d -> d) with | |
| x -> x::read() | |
| exception _ -> [] | |
in | |
let vec = read () in | |
close_in f; | |
Array.of_list vec | |
let path = "../data/" | |
let solve_psd () = | |
let _,a = read_coo_matrix @@ path ^ "psd.txt" in | |
let b = read_vec @@ path ^ "b.txt" in | |
conjugate_gradient a b | |
let solve_big () = | |
let n,a = read_coo_matrix @@ path ^ "mat_helmholtz.txt" in | |
let b = Array.init n (fun _ -> 1.) in | |
conjugate_gradient a b | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment