diff options
author | Alberto Ruiz <aruiz@um.es> | 2009-04-17 11:55:32 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2009-04-17 11:55:32 +0000 |
commit | 33a8f087574c89d257fccefd58643bd9b8fa9f22 (patch) | |
tree | f017b6834367fd1bd29d58801d10a2eebf383be3 | |
parent | 71ed02d2728701130cf82e61a8633af0f6375812 (diff) |
restored C trans and constant for comparison
-rw-r--r-- | examples/benchmarks.hs | 15 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 41 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c | 48 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h | 6 |
4 files changed, 103 insertions, 7 deletions
diff --git a/examples/benchmarks.hs b/examples/benchmarks.hs index 4fd7a56..d7f8c40 100644 --- a/examples/benchmarks.hs +++ b/examples/benchmarks.hs | |||
@@ -18,9 +18,11 @@ time act = do | |||
18 | 18 | ||
19 | -------------------------------------------------------------------------------- | 19 | -------------------------------------------------------------------------------- |
20 | 20 | ||
21 | main = sequence_ [bench1,bench2,bench4, | 21 | main = sequence_ [bench1, |
22 | bench5 1000000 3, | 22 | bench2, |
23 | bench5 100000 50] | 23 | bench4, |
24 | bench5 1000000 3, bench5 100000 50, | ||
25 | bench6 100 (100000::Double), bench6 100000 (100::Double), bench6 10000 (1000::Double)] | ||
24 | 26 | ||
25 | w :: Vector Double | 27 | w :: Vector Double |
26 | w = constant 1 5000000 | 28 | w = constant 1 5000000 |
@@ -140,3 +142,10 @@ bench5 n d = do | |||
140 | 142 | ||
141 | timep $ foldl1' (<>) ms | 143 | timep $ foldl1' (<>) ms |
142 | timep $ foldl1' op1 ms | 144 | timep $ foldl1' op1 ms |
145 | |||
146 | -------------------------------------------------------------------------------- | ||
147 | |||
148 | bench6 sz n = do | ||
149 | putStrLn "-------------------------------------------------------" | ||
150 | putStrLn "many constants" | ||
151 | time $ print $ sum $ map ((@>0). flip constant sz) [1..n] \ No newline at end of file | ||
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index b1e7670..13ffc34 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -221,13 +221,13 @@ class (Storable a, Floating a) => Element a where | |||
221 | 221 | ||
222 | instance Element Double where | 222 | instance Element Double where |
223 | subMatrixD = subMatrix' | 223 | subMatrixD = subMatrix' |
224 | transdata = transdata' | 224 | transdata = transdataAux ctransR -- transdata' |
225 | constantD = constant' | 225 | constantD = constantAux cconstantR -- constant' |
226 | 226 | ||
227 | instance Element (Complex Double) where | 227 | instance Element (Complex Double) where |
228 | subMatrixD = subMatrix' | 228 | subMatrixD = subMatrix' |
229 | transdata = transdata' | 229 | transdata = transdataAux ctransC -- transdata' |
230 | constantD = constant' | 230 | constantD = constantAux cconstantC -- constant' |
231 | 231 | ||
232 | ------------------------------------------------------------------- | 232 | ------------------------------------------------------------------- |
233 | 233 | ||
@@ -257,6 +257,23 @@ transdata' c1 v c2 = | |||
257 | -- The above pragmas only seem to work on top level defs | 257 | -- The above pragmas only seem to work on top level defs |
258 | -- Fortunately everything seems to work using the above class | 258 | -- Fortunately everything seems to work using the above class |
259 | 259 | ||
260 | -- C versions, still a little faster: | ||
261 | |||
262 | transdataAux fun c1 d c2 = | ||
263 | if noneed | ||
264 | then d | ||
265 | else unsafePerformIO $ do | ||
266 | v <- createVector (dim d) | ||
267 | withForeignPtr (fptr d) $ \pd -> | ||
268 | withForeignPtr (fptr v) $ \pv -> | ||
269 | fun (fi r1) (fi c1) pd (fi r2) (fi c2) pv // check "transdataAux" | ||
270 | return v | ||
271 | where r1 = dim d `div` c1 | ||
272 | r2 = dim d `div` c2 | ||
273 | noneed = r1 == 1 || c1 == 1 | ||
274 | |||
275 | foreign import ccall "transR" ctransR :: TMM | ||
276 | foreign import ccall "transC" ctransC :: TCMCM | ||
260 | ---------------------------------------------------------------------- | 277 | ---------------------------------------------------------------------- |
261 | 278 | ||
262 | constant' v n = unsafePerformIO $ do | 279 | constant' v n = unsafePerformIO $ do |
@@ -267,6 +284,22 @@ constant' v n = unsafePerformIO $ do | |||
267 | go (n-1) | 284 | go (n-1) |
268 | return w | 285 | return w |
269 | 286 | ||
287 | -- C versions | ||
288 | |||
289 | constantAux fun x n = unsafePerformIO $ do | ||
290 | v <- createVector n | ||
291 | px <- newArray [x] | ||
292 | app1 (fun px) vec v "constantAux" | ||
293 | free px | ||
294 | return v | ||
295 | |||
296 | constantR :: Double -> Int -> Vector Double | ||
297 | constantR = constantAux cconstantR | ||
298 | foreign import ccall "constantR" cconstantR :: Ptr Double -> TV | ||
299 | |||
300 | constantC :: Complex Double -> Int -> Vector (Complex Double) | ||
301 | constantC = constantAux cconstantC | ||
302 | foreign import ccall "constantC" cconstantC :: Ptr (Complex Double) -> TCV | ||
270 | ---------------------------------------------------------------------- | 303 | ---------------------------------------------------------------------- |
271 | 304 | ||
272 | -- | Extracts a submatrix from a matrix. | 305 | -- | Extracts a submatrix from a matrix. |
diff --git a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c index a8ccf5f..d7248d1 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c +++ b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c | |||
@@ -908,3 +908,51 @@ int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r)) { | |||
908 | (doublecomplex*)rp,&ldc); | 908 | (doublecomplex*)rp,&ldc); |
909 | OK | 909 | OK |
910 | } | 910 | } |
911 | |||
912 | //////////////////// transpose ///////////////////////// | ||
913 | |||
914 | int transR(KDMAT(x),DMAT(t)) { | ||
915 | REQUIRES(xr==tc && xc==tr,BAD_SIZE); | ||
916 | DEBUGMSG("transR"); | ||
917 | int i,j; | ||
918 | for (i=0; i<tr; i++) { | ||
919 | for (j=0; j<tc; j++) { | ||
920 | tp[i*tc+j] = xp[j*xc+i]; | ||
921 | } | ||
922 | } | ||
923 | OK | ||
924 | } | ||
925 | |||
926 | int transC(KCMAT(x),CMAT(t)) { | ||
927 | REQUIRES(xr==tc && xc==tr,BAD_SIZE); | ||
928 | DEBUGMSG("transC"); | ||
929 | int i,j; | ||
930 | for (i=0; i<tr; i++) { | ||
931 | for (j=0; j<tc; j++) { | ||
932 | ((doublecomplex*)tp)[i*tc+j] = ((doublecomplex*)xp)[j*xc+i]; | ||
933 | } | ||
934 | } | ||
935 | OK | ||
936 | } | ||
937 | |||
938 | //////////////////// constant ///////////////////////// | ||
939 | |||
940 | int constantR(double * pval, DVEC(r)) { | ||
941 | DEBUGMSG("constantR") | ||
942 | int k; | ||
943 | double val = *pval; | ||
944 | for(k=0;k<rn;k++) { | ||
945 | rp[k]=val; | ||
946 | } | ||
947 | OK | ||
948 | } | ||
949 | |||
950 | int constantC(doublecomplex* pval, CVEC(r)) { | ||
951 | DEBUGMSG("constantC") | ||
952 | int k; | ||
953 | doublecomplex val = *pval; | ||
954 | for(k=0;k<rn;k++) { | ||
955 | ((doublecomplex*)rp)[k]=val; | ||
956 | } | ||
957 | OK | ||
958 | } | ||
diff --git a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h index 3f58243..dc7a98f 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h +++ b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h | |||
@@ -55,6 +55,12 @@ typedef short ftnlen; | |||
55 | int multiplyR(int ta, int tb, KDMAT(a),KDMAT(b),DMAT(r)); | 55 | int multiplyR(int ta, int tb, KDMAT(a),KDMAT(b),DMAT(r)); |
56 | int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r)); | 56 | int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r)); |
57 | 57 | ||
58 | int transR(KDMAT(x),DMAT(t)); | ||
59 | int transC(KCMAT(x),CMAT(t)); | ||
60 | |||
61 | int constantR(double * pval, DVEC(r)); | ||
62 | int constantC(doublecomplex* pval, CVEC(r)); | ||
63 | |||
58 | int svd_l_R(KDMAT(x),DMAT(u),DVEC(s),DMAT(v)); | 64 | int svd_l_R(KDMAT(x),DMAT(u),DVEC(s),DMAT(v)); |
59 | int svd_l_Rdd(KDMAT(x),DMAT(u),DVEC(s),DMAT(v)); | 65 | int svd_l_Rdd(KDMAT(x),DMAT(u),DVEC(s),DMAT(v)); |
60 | int svd_l_C(KCMAT(a),CMAT(u),DVEC(s),CMAT(v)); | 66 | int svd_l_C(KCMAT(a),CMAT(u),DVEC(s),CMAT(v)); |