summaryrefslogtreecommitdiff
path: root/packages/sparse
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-27 20:24:12 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-27 20:24:12 +0200
commit3c1c5e59e3d699f3e17519f19d47f7dab2403879 (patch)
treea749e0a3fb515ad1a904ce7387fbd3afd2ee0ed3 /packages/sparse
parent53559833d2166010eed754027484fb8d5525e710 (diff)
initial interface to MKL sparse solver
Diffstat (limited to 'packages/sparse')
-rw-r--r--packages/sparse/LICENSE26
-rw-r--r--packages/sparse/Setup.lhs4
-rw-r--r--packages/sparse/hmatrix-sparse.cabal39
-rw-r--r--packages/sparse/src/Numeric/LinearAlgebra/Sparse.hs32
-rw-r--r--packages/sparse/src/Numeric/LinearAlgebra/sparse.c65
5 files changed, 166 insertions, 0 deletions
diff --git a/packages/sparse/LICENSE b/packages/sparse/LICENSE
new file mode 100644
index 0000000..b86076a
--- /dev/null
+++ b/packages/sparse/LICENSE
@@ -0,0 +1,26 @@
1Copyright (c) 2014 Alberto Ruiz
2
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
diff --git a/packages/sparse/Setup.lhs b/packages/sparse/Setup.lhs
new file mode 100644
index 0000000..6b32049
--- /dev/null
+++ b/packages/sparse/Setup.lhs
@@ -0,0 +1,4 @@
1#! /usr/bin/env runhaskell
2
3> import Distribution.Simple
4> main = defaultMain
diff --git a/packages/sparse/hmatrix-sparse.cabal b/packages/sparse/hmatrix-sparse.cabal
new file mode 100644
index 0000000..877ab1f
--- /dev/null
+++ b/packages/sparse/hmatrix-sparse.cabal
@@ -0,0 +1,39 @@
1Name: hmatrix-sparse
2Version: 0.1.0
3License: BSD3
4License-file: LICENSE
5Author: Alberto Ruiz
6Maintainer: Alberto Ruiz <aruiz@um.es>
7Stability: experimental
8Homepage: https://github.com/albertoruiz/hmatrix
9Synopsis: Sparse linear solver
10Description: Interface to MKL direct sparse linear solver
11
12-- cabal install --extra-include-dirs=$MKL --extra-lib-dirs=$MKL
13
14Category: Math
15tested-with: GHC ==7.8
16
17cabal-version: >=1.6
18build-type: Simple
19
20
21library
22 Build-Depends: base, hmatrix
23
24 hs-source-dirs: src
25
26 Exposed-modules: Numeric.LinearAlgebra.Sparse
27
28 ghc-options: -Wall
29
30 c-sources: src/Numeric/LinearAlgebra/sparse.c
31
32 cc-options: -O4 -msse2 -Wall
33
34 extra-libraries: mkl_intel mkl_sequential mkl_core
35
36source-repository head
37 type: git
38 location: https://github.com/albertoruiz/hmatrix
39
diff --git a/packages/sparse/src/Numeric/LinearAlgebra/Sparse.hs b/packages/sparse/src/Numeric/LinearAlgebra/Sparse.hs
new file mode 100644
index 0000000..ccf28b7
--- /dev/null
+++ b/packages/sparse/src/Numeric/LinearAlgebra/Sparse.hs
@@ -0,0 +1,32 @@
1{-# LANGUAGE ForeignFunctionInterface #-}
2{-# LANGUAGE RecordWildCards #-}
3
4
5
6module Numeric.LinearAlgebra.Sparse (
7 dss
8) where
9
10import Foreign.C.Types(CInt(..))
11import Data.Packed.Development
12import System.IO.Unsafe(unsafePerformIO)
13import Foreign(Ptr)
14import Numeric.HMatrix
15import Text.Printf
16import Numeric.LinearAlgebra.Util((~!~))
17
18
19type IV t = CInt -> Ptr CInt -> t
20type V t = CInt -> Ptr Double -> t
21type SMxV = V (IV (IV (V (V (IO CInt)))))
22
23dss :: CSR -> Vector Double -> Vector Double
24dss CSR{..} b = unsafePerformIO $ do
25 size b /= csrNRows ~!~ printf "dss: incorrect sizes: (%d,%d) x %d" csrNRows csrNCols (size b)
26 r <- createVector csrNCols
27 app5 c_dss vec csrVals vec csrCols vec csrRows vec b vec r "dss"
28 return r
29
30foreign import ccall unsafe "dss"
31 c_dss :: SMxV
32
diff --git a/packages/sparse/src/Numeric/LinearAlgebra/sparse.c b/packages/sparse/src/Numeric/LinearAlgebra/sparse.c
new file mode 100644
index 0000000..b1e257a
--- /dev/null
+++ b/packages/sparse/src/Numeric/LinearAlgebra/sparse.c
@@ -0,0 +1,65 @@
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <math.h>
5
6#include "mkl_dss.h"
7#include "mkl_types.h"
8#include "mkl_spblas.h"
9
10#define KIVEC(A) int A##n, const int*A##p
11#define KDVEC(A) int A##n, const double*A##p
12#define DVEC(A) int A##n, double*A##p
13#define OK return 0;
14
15
16void check_error(int error)
17{
18 if(error != MKL_DSS_SUCCESS) {
19 printf ("Solver returned error code %d\n", error);
20 exit (1);
21 }
22}
23
24int dss(KDVEC(vals),KIVEC(cols),KIVEC(rows),KDVEC(x),DVEC(r)) {
25 MKL_INT nRows = rowsn-1, nCols = rn, nNonZeros = valsn, nRhs = 1;
26 MKL_INT *rowIndex = (MKL_INT*) rowsp;
27 MKL_INT *columns = (MKL_INT*) colsp;
28 double *values = (double*) valsp;
29 _DOUBLE_PRECISION_t *rhs = (_DOUBLE_PRECISION_t*) xp;
30// _DOUBLE_PRECISION_t *obtrhs = (_DOUBLE_PRECISION_t*) malloc((nCols)*sizeof(_DOUBLE_PRECISION_t));
31 _DOUBLE_PRECISION_t *solValues = (_DOUBLE_PRECISION_t*) rp;
32
33 _MKL_DSS_HANDLE_t handle;
34 _INTEGER_t error;
35// _CHARACTER_t *uplo;
36 MKL_INT opt;
37
38 opt = MKL_DSS_DEFAULTS;
39 error = dss_create(handle, opt);
40 check_error(error);
41
42 opt = MKL_DSS_NON_SYMMETRIC;
43 error = dss_define_structure(handle, opt, rowIndex, nRows, nCols, columns, nNonZeros);
44 check_error(error);
45
46 opt = MKL_DSS_DEFAULTS;
47 error = dss_reorder(handle, opt, 0);
48 check_error(error);
49
50 opt = MKL_DSS_INDEFINITE;
51 error = dss_factor_real(handle, opt, values);
52 check_error(error);
53
54 int j;
55 for (j = 0; j < nCols; j++) {
56 solValues[j] = 0.0;
57 }
58
59 // Solve system
60 opt = MKL_DSS_REFINEMENT_ON;
61 error = dss_solve_real(handle, opt, rhs, nRhs, solValues);
62 check_error(error);
63
64 OK
65}