Parallel brute force: Difference between revisions

Content added Content deleted
Line 752: Line 752:
</ul>
</ul>
<br/>
<br/>
Compile with "-O2 -threaded -rtsopts". Run with "+RTS -N<number of cores>"<br/>
Compile with "-O2 -threaded"<br/>
7.391s elapsed on a 2.5 GHz Dual-Core Intel Core i7 Macbook Pro.
7.391s elapsed on a 2.5 GHz Dual-Core Intel Core i7 Macbook Pro.
<lang haskell>import Control.Concurrent (getNumCapabilities)
<lang haskell>import Control.Concurrent (setNumCapabilities)
import Crypto.Hash (hashWith, SHA256 (..), Digest)
import Crypto.Hash (hashWith, SHA256 (..), Digest)
import Control.Monad (replicateM, join)
import Control.Monad (replicateM, join)
Line 760: Line 760:
import Data.ByteString (pack)
import Data.ByteString (pack)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import GHC.Conc (getNumProcessors)
import Text.Printf (printf)
import Text.Printf (printf)


Line 766: Line 767:
[ "3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b"
[ "3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b"
, "74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f"
, "74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f"
, "1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad"
, "1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad" ]
]


bruteForce :: Int -> [(String, String)]
bruteForce :: Int -> [(String, String)]
Line 782: Line 782:
main :: IO ()
main :: IO ()
main = do
main = do
cores <- getNumCapabilities
cpus <- getNumProcessors
setNumCapabilities cpus
printf "Using %d cores\n" cores
mapM_ (uncurry (printf "%s -> %s\n")) (bruteForce cores)</lang>
printf "Using %d cores\n" cpus
mapM_ (uncurry (printf "%s -> %s\n")) (bruteForce cpus)</lang>
{{out}}
{{out}}
<pre>brute +RTS -N2 -s
<pre>brute
Using 2 cores
Using 4 cores
3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b -> apple
3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b -> apple
74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f -> mmmmm
74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f -> mmmmm
Line 802: Line 803:
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Data.Word (Word8)
import Data.Word (Word8)
import GHC.Conc (getNumProcessors)
import System.Environment (getArgs)
import System.Environment (getArgs)
import Text.Printf (printf)
import Text.Printf (printf)
Line 829: Line 831:
searchWorker batchChan resultChan = forever (readChan batchChan >>= writeList2Chan resultChan . foldr findMatch [])
searchWorker batchChan resultChan = forever (readChan batchChan >>= writeList2Chan resultChan . foldr findMatch [])


parseInput :: [String] -> Int
parseInput :: [String] -> Int -> Int
parseInput [] = 2
parseInput [] n = n
parseInput (s:_) = if all isDigit s then read s else 2
parseInput (s:_) n = if all isDigit s then read s else n


main :: IO ()
main :: IO ()
main = do
main = do
workers <- getArgs
workers <- getArgs
cpus <- getNumProcessors
let wCount = parseInput workers
let wCount = parseInput workers cpus
setNumCapabilities wCount
setNumCapabilities wCount
printf "Using %d workers\n" wCount
printf "Using %d workers on %d cpus.\n" wCount cpus
resultChan <- newChan
resultChan <- newChan
batchChan <- newChan
batchChan <- newChan
Line 845: Line 848:
replicateM_ (length hashedValues) (readChan resultChan >>= uncurry (printf "%s -> %s\n") . first show)</lang>
replicateM_ (length hashedValues) (readChan resultChan >>= uncurry (printf "%s -> %s\n") . first show)</lang>
{{out}}
{{out}}
<pre>brute 2
<pre>brute2
Using 2 workers
Using 4 workers on 4 cpus.
3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b -> "apple"
3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b -> "apple"
74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f -> "mmmmm"
74e1bb62f8dabb8125a58852b63bdf6eaef667cb56ac7f7cdba6d7305c50a22f -> "mmmmm"