#2171: Parallel program crashes
------------------------------------------------+---------------------------
    Reporter:  fed                              |       Owner:          
        Type:  bug                              |      Status:  new     
    Priority:  normal                           |   Component:  Compiler
     Version:  6.8.2                            |    Severity:  critical
    Keywords:  parallel, crash, race condition  |    Testcase:          
Architecture:  x86                              |          Os:  Windows 
------------------------------------------------+---------------------------
 I wrote a parallel program for computing prime numbers (see code below).
 Compiled it using
   ghc -threaded --make primes.hs
 Then run
   primes 200000 200010 +RTS -N2 -sstderr
 In 80% of runs the program crashes with segmentation fault message. My
 operating system is Windows XP SP2. One time I saw a typical Windows red
 cross dialog box with access violation report as a result of the run.
 But few times the program finished with correct result.

 {{{
 module Main where

 import System.Environment
 import Control.Parallel.Strategies
 import Data.Maybe

 chunkSize = 1000
 threads = 2

 maybePrimes :: [Maybe Integer]
 maybePrimes = Just 2 : Just 3 : Just 5 : [ if isPrime x then (Just x) else
 Nothing | x <- candidates 7 11 ]
     where
       candidates a1 a2 = a1 : a2 : candidates (a1+6) (a2+6)
       isPrime x = all ((0 /=) . (x `mod`)) $ takeWhile ((x>=).(^2)) primes

 splitOnSubLists :: Int -> [a] -> [[a]]
 splitOnSubLists n xs =
   let
      (begin, end) = splitAt n xs
   in
     begin : splitOnSubLists n end

 maybePrimes2 :: [Maybe Integer]
 maybePrimes2 = concat $ parBuffer (threads-1) rnf
   $ splitOnSubLists chunkSize maybePrimes

 primes :: [Integer]
 primes = catMaybes maybePrimes2

 main = do
   args <- getArgs

   (first, last) <- case args of
        [] -> return (10, 19)
        (x:[]) -> return (f, f+9) where f = read x
        (x:y:_) -> return (read x, read y)

   let p = drop (first-1) $ take last $ zip [1..] primes in
     mapM (\(x,y) -> putStrLn (show x ++ " -> " ++ show y)) p
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2171>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to