Skip to main content
update result
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25

Haskell, n=30n=31 (57s54s)

Haskell, n=30 (57s)

Haskell, n=31 (54s)

simple code is faster :-)
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
import Control.Parallel.Strategies
import qualified Data.Vector.Unboxed as V
import Data.Int

type Row = V.Vector Int8

x :: Row -> [Row] -> Integer -> Int -> Integer
x p (v:vs) m c = let c' = c - 1
                     r = if c>0 then parTuple2 rseq rseq else r0
                     (a,b) = ( x p                  vs m    c' ,
                               x (V.zipWith(-) p v) vs (-m) c' )
                             `using` r
                 in a+b
x p _      m _ = prod m p

prod :: Integer -> Row -> Integer
prod am p | V.null p= if 0 `V.elem` p then =0 a
         | V.head p == 0 = 0
         | otherwise     =else prodV.foldl' (a * fromIntegral\a (V.headb->a*fromIntegral p)b) (V.tailm p)

p, pt :: [Row] -> Integer
p (v:vs) = x (foldl (V.zipWith (+)) v vs) (map (V.map (2*)) vs) 1 11
           `div` 2^(length vs)
p [] = 1 -- handle 0x0 matrices too  :-)

pt (v:vs) | even (length vs) = p ((V.map (2*) v) : vs ) `div` 2
pt mat                       = p mat

main = getContents >>= print . pt . map V.fromList . read
import Control.Parallel.Strategies
import qualified Data.Vector.Unboxed as V
import Data.Int

type Row = V.Vector Int8

x :: Row -> [Row] -> Integer -> Int -> Integer
x p (v:vs) m c = let c' = c - 1
                     r = if c>0 then parTuple2 rseq rseq else r0
                     (a,b) = ( x p                  vs m    c' ,
                               x (V.zipWith(-) p v) vs (-m) c' )
                             `using` r
                 in a+b
x p _      m _ = prod m p

prod :: Integer -> Row -> Integer
prod a p | V.null p      = a
         | V.head p == 0 = 0
         | otherwise     = prod (a * fromIntegral (V.head p)) (V.tail p)

p, pt :: [Row] -> Integer
p (v:vs) = x (foldl (V.zipWith (+)) v vs) (map (V.map (2*)) vs) 1 11
           `div` 2^(length vs)
p [] = 1 -- handle 0x0 matrices too  :-)

pt (v:vs) | even (length vs) = p ((V.map (2*) v) : vs ) `div` 2
pt mat                       = p mat

main = getContents >>= print . pt . map V.fromList . read
import Control.Parallel.Strategies
import qualified Data.Vector.Unboxed as V
import Data.Int

type Row = V.Vector Int8

x :: Row -> [Row] -> Integer -> Int -> Integer
x p (v:vs) m c = let c' = c - 1
                     r = if c>0 then parTuple2 rseq rseq else r0
                     (a,b) = ( x p                  vs m    c' ,
                               x (V.zipWith(-) p v) vs (-m) c' )
                             `using` r
                 in a+b
x p _      m _ = prod m p

prod :: Integer -> Row -> Integer
prod m p = if 0 `V.elem` p then 0 
                           else V.foldl' (\a b->a*fromIntegral b) m p

p, pt :: [Row] -> Integer
p (v:vs) = x (foldl (V.zipWith (+)) v vs) (map (V.map (2*)) vs) 1 11
           `div` 2^(length vs)
p [] = 1 -- handle 0x0 matrices too  :-)

pt (v:vs) | even (length vs) = p ((V.map (2*) v) : vs ) `div` 2
pt mat                       = p mat

main = getContents >>= print . pt . map V.fromList . read
rewrite prod, extra compiler options
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
import Control.Parallel.Strategies
import qualified Data.Vector.Unboxed as V
import Data.Maybe
import Data.Int

type Row = V.Vector Int8

x :: Row -> [Row] -> Integer -> Int -> Integer
x p (v:vs) m c = let c' = c - 1
                     r = if c>0 then parTuple2 rseq rseq else r0
                     (a,b) = ( x p                  vs m    c' ,
                               x (V.zipWith(-) p v) vs (-m) c' )
                             `using` r
                 in a+b
x p _      m _ = prod m * prod p

prod :: Integer -> Row -> Integer
prod p = fromMaybea 0p $| V.foldM'null (\ap b -> if b==0 then Nothing else= a
                    | V.head p == 0 = 0
         | otherwise     Just= $prod (a * fromIntegral b(V.head p)) 1(V.tail p)

p, pt :: [Row] -> Integer
p (v:vs) = x (foldl (V.zipWith (+)) v vs) (map (V.map (2*)) vs) 1 11
           `div` 2^(length vs)
p [] = 1 -- handle 0x0 matrices too  :-)

pt (v:vs) | even (length vs) = p ((V.map (2*) v) : vs ) `div` 2
pt mat                       = p mat

main = getContents >>= print . pt . map V.fromList . read

Compile with ghc -O2 -threaded -fllvm -feager-blackholing -o <name> <name>.hs. To run with parallelisation, give it runtime parameters like this: ./<name> +RTS -N. Input is from stdin with nested comma separated lists in brackets like [[1,2],[3,4]] as in the last example (newlines allowed everywhere).

import Control.Parallel.Strategies
import qualified Data.Vector.Unboxed as V
import Data.Maybe
import Data.Int

type Row = V.Vector Int8

x :: Row -> [Row] -> Integer -> Int -> Integer
x p (v:vs) m c = let c' = c - 1
                     r = if c>0 then parTuple2 rseq rseq else r0
                     (a,b) = ( x p                  vs m    c' ,
                               x (V.zipWith(-) p v) vs (-m) c' )
                             `using` r
                 in a+b
x p _      m _ =  m * prod p

prod :: Row -> Integer
prod p = fromMaybe 0 $ V.foldM' (\a b -> if b==0 then Nothing else 
                                         Just $ a * fromIntegral b) 1 p

p, pt :: [Row] -> Integer
p (v:vs) = x (foldl (V.zipWith (+)) v vs) (map (V.map (2*)) vs) 1 11
           `div` 2^(length vs)
p [] = 1 -- handle 0x0 matrices too  :-)

pt (v:vs) | even (length vs) = p ((V.map (2*) v) : vs ) `div` 2
pt mat                       = p mat

main = getContents >>= print . pt . map V.fromList . read

Compile with ghc -O2 -threaded -o <name> <name>.hs. To run with parallelisation, give it runtime parameters like this: ./<name> +RTS -N. Input is from stdin with nested comma separated lists in brackets like [[1,2],[3,4]] as in the last example (newlines allowed everywhere).

import Control.Parallel.Strategies
import qualified Data.Vector.Unboxed as V
import Data.Int

type Row = V.Vector Int8

x :: Row -> [Row] -> Integer -> Int -> Integer
x p (v:vs) m c = let c' = c - 1
                     r = if c>0 then parTuple2 rseq rseq else r0
                     (a,b) = ( x p                  vs m    c' ,
                               x (V.zipWith(-) p v) vs (-m) c' )
                             `using` r
                 in a+b
x p _      m _ = prod m p

prod :: Integer -> Row -> Integer
prod a p | V.null p      = a
         | V.head p == 0 = 0
         | otherwise     = prod (a * fromIntegral (V.head p)) (V.tail p)

p, pt :: [Row] -> Integer
p (v:vs) = x (foldl (V.zipWith (+)) v vs) (map (V.map (2*)) vs) 1 11
           `div` 2^(length vs)
p [] = 1 -- handle 0x0 matrices too  :-)

pt (v:vs) | even (length vs) = p ((V.map (2*) v) : vs ) `div` 2
pt mat                       = p mat

main = getContents >>= print . pt . map V.fromList . read

Compile with ghc -O2 -threaded -fllvm -feager-blackholing -o <name> <name>.hs. To run with parallelisation, give it runtime parameters like this: ./<name> +RTS -N. Input is from stdin with nested comma separated lists in brackets like [[1,2],[3,4]] as in the last example (newlines allowed everywhere).

update result
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
8 bit are enough and seem slightly faster
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
transform to put zeros into the odd case
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
short circuit product
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
cleanup
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
use Vector
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
official result
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
remove double type declaration
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
no inner rpar needed
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
can't wait for the results
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
better style (I hope), and correct name
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
better style (I hope)
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
improved version
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
this seems to be better
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading
Source Link
Christian Sievers
  • 7.1k
  • 1
  • 19
  • 25
Loading