Skip to content

Parallel program crashes

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
Trac metadata
Trac field Value
Version 6.8.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Windows
Architecture x86
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information