From c84a485f148063f6d0c23f016fe348ec94fb6b19 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 4 Oct 2015 19:46:43 +0200 Subject: updated devel example --- examples/devel/ej1/functions.c | 35 ----------------------------- examples/devel/ej1/wrappers.hs | 44 ------------------------------------- examples/devel/ej2/functions.c | 24 -------------------- examples/devel/ej2/wrappers.hs | 32 --------------------------- examples/devel/example/functions.c | 22 +++++++++++++++++++ examples/devel/example/wrappers.hs | 45 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 135 deletions(-) delete mode 100644 examples/devel/ej1/functions.c delete mode 100644 examples/devel/ej1/wrappers.hs delete mode 100644 examples/devel/ej2/functions.c delete mode 100644 examples/devel/ej2/wrappers.hs create mode 100644 examples/devel/example/functions.c create mode 100644 examples/devel/example/wrappers.hs (limited to 'examples/devel') diff --git a/examples/devel/ej1/functions.c b/examples/devel/ej1/functions.c deleted file mode 100644 index 02a4cdd..0000000 --- a/examples/devel/ej1/functions.c +++ /dev/null @@ -1,35 +0,0 @@ -/* assuming row order */ - -typedef struct { double r, i; } doublecomplex; - -#define DVEC(A) int A##n, double*A##p -#define CVEC(A) int A##n, doublecomplex*A##p -#define DMAT(A) int A##r, int A##c, double*A##p -#define CMAT(A) int A##r, int A##c, doublecomplex*A##p - -#define AT(M,row,col) (M##p[(row)*M##c + (col)]) - -/*-----------------------------------------------------*/ - -int c_scale_vector(double s, DVEC(x), DVEC(y)) { - int k; - for (k=0; k<=yn; k++) { - yp[k] = s*xp[k]; - } - return 0; -} - -/*-----------------------------------------------------*/ - -int c_diag(DMAT(m),DVEC(y),DMAT(z)) { - int i,j; - for (j=0; j<5) [1..] - ------------------------------------------------------ - -foreign import ccall unsafe "c_scale_vector" - cScaleVector :: Double -- scale - -> CInt -> Ptr Double -- argument - -> CInt -> Ptr Double -- result - -> IO CInt -- exit code - -myScale s x = unsafePerformIO $ do - y <- createVector (dim x) - app2 (cScaleVector s) vec x vec y "cScaleVector" - return y - ------------------------------------------------------ --- forcing row order - -foreign import ccall unsafe "c_diag" - cDiag :: CInt -> CInt -> Ptr Double -- argument - -> CInt -> Ptr Double -- result1 - -> CInt -> CInt -> Ptr Double -- result2 - -> IO CInt -- exit code - -myDiag m = unsafePerformIO $ do - y <- createVector (min r c) - z <- createMatrix RowMajor r c - app3 cDiag mat (cmat m) vec y mat z "cDiag" - return (y,z) - where r = rows m - c = cols m diff --git a/examples/devel/ej2/functions.c b/examples/devel/ej2/functions.c deleted file mode 100644 index 4dcd377..0000000 --- a/examples/devel/ej2/functions.c +++ /dev/null @@ -1,24 +0,0 @@ -/* general element order */ - -typedef struct { double r, i; } doublecomplex; - -#define DVEC(A) int A##n, double*A##p -#define CVEC(A) int A##n, doublecomplex*A##p -#define DMAT(A) int A##r, int A##c, double*A##p -#define CMAT(A) int A##r, int A##c, doublecomplex*A##p - -#define AT(M,r,c) (M##p[(r)*sr+(c)*sc]) - -int c_diag(int ro, DMAT(m),DVEC(y),DMAT(z)) { - int i,j,sr,sc; - if (ro==1) { sr = mc; sc = 1;} else { sr = 1; sc = mr;} - for (j=0; j<5) [1..] - ------------------------------------------------------ --- arbitrary data order - -foreign import ccall unsafe "c_diag" - cDiag :: CInt -- matrix order - -> CInt -> CInt -> Ptr Double -- argument - -> CInt -> Ptr Double -- result1 - -> CInt -> CInt -> Ptr Double -- result2 - -> IO CInt -- exit code - -myDiag m = unsafePerformIO $ do - y <- createVector (min r c) - z <- createMatrix (orderOf m) r c - app3 (cDiag o) mat m vec y mat z "cDiag" - return (y,z) - where r = rows m - c = cols m - o = if orderOf m == RowMajor then 1 else 0 diff --git a/examples/devel/example/functions.c b/examples/devel/example/functions.c new file mode 100644 index 0000000..67d3270 --- /dev/null +++ b/examples/devel/example/functions.c @@ -0,0 +1,22 @@ + +typedef struct { double r, i; } doublecomplex; + +#define VEC(T,A) int A##n, T* A##p +#define MAT(T,A) int A##r, int A##c, int A##Xr, int A##Xc, T* A##p + +#define AT(m,i,j) (m##p[(i)*m##Xr + (j)*m##Xc]) +#define TRAV(m,i,j) int i,j; for (i=0;i, ::> +type (:>) t r = CInt -> Ptr t -> r +type (::>) t r = CInt -> CInt -> CInt -> CInt -> Ptr t -> r +type Ok = IO CInt + +----------------------------------------------------- + +x = (3><5) [1..] + +main = do + print x + print $ myDiag x + print $ myDiag (tr x) + +----------------------------------------------------- +foreign import ccall unsafe "c_diag" cDiag :: Double ::> Double :> Double ::> Ok + +myDiag m = unsafePerformIO $ do + y <- createVector (min r c) + z <- createMatrix RowMajor r c + cDiag # m # y # z #| "cDiag" + return (y,z) + where + (r,c) = size m + -- cgit v1.2.3