Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come Haskell got a segmentation fault when the vector is very large but under memory limit?

Tags:

haskell

I wanted to write a program that operates on a really large array, and does a lot of random access read/write operations. I figured vector is the most suitable way of doing it in Haskell, so I wrote a simple program to test its performance:

import Data.Int
import qualified Data.Vector.Unboxed.Mutable as UM

n = 1000000000

main = do
    a <- UM.new n
    UM.read a 42 :: IO Int32

However, when I ran it, it failed with segmentation fault:

$ ghc -O2 test.hs
$ ./test
Segmentation fault (core dumped)

This machine has more than enough memory for the array. Here is the output of free -h:

             total       used       free     shared    buffers     cached
Mem:          251G       150G       100G       672K       419M       141G
-/+ buffers/cache:       9.2G       242G
Swap:         255G       870M       255G

Was it because Haskell's vector package cannot handle very large arrays? Can I modify my code so that it can work on large arrays without too much performance compromise?


Edit: My GHC version is 7.10.2.20150906, and my vector version is 0.11.0.0. This is on a 64-bit linux machine, with

> maxBound :: Int
9223372036854775807
like image 790
xzhu Avatar asked Sep 18 '15 06:09

xzhu


People also ask

What causes a segmentation fault?

Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

How can segmentation fault be resolved?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

What causes segmentation fault 11?

1) Segmentation Fault (also known as SIGSEGV and is usually signal 11) occur when the program tries to write/read outside the memory allocated for it or when writing memory which can only be read.In other words when the program tries to access the memory to which it doesn't have access to.


1 Answers

This is due to a bug in primitive that is apparently fixed in the recently-released primitive-0.6.1.0. I suggest you add a lower bound on primitive to your project accordingly.

like image 171
Reid Barton Avatar answered Sep 23 '22 06:09

Reid Barton