Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: ghc process gets killed on a 512MB machine
(Felipe Almeida Lessa)
2. Re: ghc process gets killed on a 512MB machine (Ionu? G. Stan)
3. Re: Calling a foreign function: superlinear comlexity
(Edward Z. Yang)
4. Re: Calling a foreign function: superlinear comlexity
(Felipe Almeida Lessa)
----------------------------------------------------------------------
Message: 1
Date: Sat, 16 Apr 2011 09:51:33 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] ghc process gets killed on a 512MB
machine
To: Ionu? G. Stan <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, Apr 16, 2011 at 2:45 AM, "Ionu? G. Stan" <[email protected]> wrote:
> Is there any workaround for this problem? Other than fork the package repo
> and reduce the number of supported syntax files.
Use swap space. If you don't want to bother creating a new partition etc., just
# dd if=/dev/zero of=/tmp/swap bs=1M count=2048
# mkswap /tmp/swap
# swapon /tmp/swap
Cheers,
--
Felipe.
------------------------------
Message: 2
Date: Sat, 16 Apr 2011 22:33:48 +0300
From: "Ionu? G. Stan" <[email protected]>
Subject: Re: [Haskell-beginners] ghc process gets killed on a 512MB
machine
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On Apr/16/2011 15:51, Felipe Almeida Lessa wrote:
> Use swap space. If you don't want to bother creating a new partition etc.,
> just
>
> # dd if=/dev/zero of=/tmp/swap bs=1M count=2048
> # mkswap /tmp/swap
> # swapon /tmp/swap
Thank you Felipe! It worked that way. Slow of course, but it worked.
--
Ionu? G. Stan | http://igstan.ro/
------------------------------
Message: 3
Date: Sat, 16 Apr 2011 19:05:36 -0400
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] Calling a foreign function:
superlinear comlexity
To: Serguei Son <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <1302994797-sup-2435@ezyang>
Content-Type: text/plain; charset=UTF-8
I suspect you want something a little more clever. In particular, because
replicateM uses sequence under the hood, which is quite strict: it requires
all of the IO actions to be evaluated before evaluation continues, so you
end up with a big list built up in memory before it gets summed by 'sum'.
I suspect if you run it manually with something like this you might do better:
let loop 0 s = return s
loop n s = do
x <- c'gsl_rng_uniform rng
loop (n-1) (s+x)
print =<< loop n 0
With an actually pure function, you can do it more nicely, but it's a bit
more fragile that way. Lazy IO would be another mechanism, although some
people regard that to be a bit evil (and you'd still need to write the
lazy IO equivalent of sequence).
Edward
Excerpts from Serguei Son's message of Sun Apr 10 19:26:16 -0400 2011:
> I call GSL's gsl_ran_ugaussian function in the following way (using
> bindings-gsl):
>
> module Main where
>
> import Bindings.Gsl.RandomNumberGeneration
> import Bindings.Gsl.RandomNumberDistributions
> import Foreign
> import Control.Monad
> import Data.List
>
> main = do
> let n = 100000
> p <- peek p'gsl_rng_mt19937
> rng <- c'gsl_rng_alloc p
> lst <- replicateM n $ c'gsl_rng_uniform rng
> print $ sum lst
>
> As I increase n from 10^4 to 10^5 to 10^6 execution time grows superlinearly.
>
> To forestall the answer that the reason is the overhead of List,
> this code scales approximately linearly:
>
> module Main where
>
>
> import Foreign
> import Control.Monad
> import Data.List
>
> main = do
> let n = 100000
> let lst = map sin [1..n]
> print $ sum lst
>
> Another interesting observation: when I wrap the sin function
> of math.h with signature CDouble -> IO CDouble calling it
> repeatedly scales superlinearly, whereas when I wrap it as a pure
> function calling it repeatedly scales linearly.
>
> What is the reason for this performance and how can
> I make the first code scale linearly in execution time?
>
------------------------------
Message: 4
Date: Sat, 16 Apr 2011 20:47:48 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Calling a foreign function:
superlinear comlexity
To: "Edward Z. Yang" <[email protected]>
Cc: beginners <[email protected]>, Serguei Son
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, Apr 16, 2011 at 8:05 PM, Edward Z. Yang <[email protected]> wrote:
> I suspect you want something a little more clever. ?In particular, because
> replicateM uses sequence under the hood, which is quite strict: it requires
> all of the IO actions to be evaluated before evaluation continues, so you
> end up with a big list built up in memory before it gets summed by 'sum'.
> I suspect if you run it manually with something like this you might do better:
>
> ? ?let loop 0 s = return s
> ? ? ? ?loop n s = do
> ? ? ? ? ? ?x <- c'gsl_rng_uniform rng
> ? ? ? ? ? ?loop (n-1) (s+x)
> ? ?print =<< loop n 0
>
> With an actually pure function, you can do it more nicely, but it's a bit
> more fragile that way. ?Lazy IO would be another mechanism, although some
> people regard that to be a bit evil (and you'd still need to write the
> lazy IO equivalent of sequence).
You may use an iteratee for the same purpose. Using John Millikin's
enumerator package, you can separate the function that calls the C
code in an enumerator
import qualified Data.Enumerator as E
enumerator :: Integer -> E.Enumerator Double IO b
enumerator n = E.replicateM n c'gsl_mg_uniform
and the code that sums the values in an iteratee
iteratee :: Monad m => E.Iteratee m Double
iteratee = E.foldl' (+) 0
Calling them is as simple as
E.run $ enumerator n $$ iteratee
but note that you can also test your iteratee (which may be more
complex than just summing) on any monad, including Identity. But
unfortunately I don't think GHC (at least 6.12) can transform this
code into something tidy as Edward's code. =(
Cheers! =)
--
Felipe.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 34, Issue 29
*****************************************