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:  Performance of parallel mergesort (Tom Davie)
   2.  Re: Applicability of FP unchanging things to real world
      changing things (Stephen Blackheath [to Haskell-Beginners])
   3. Re:  Performance of parallel mergesort (Chadda? Fouch?)
   4. Re:  Performance of parallel mergesort (Jon Harrop)
   5. Re:  Performance of parallel mergesort (Jon Harrop)


----------------------------------------------------------------------

Message: 1
Date: Thu, 24 Dec 2009 09:09:41 +0000
From: Tom Davie <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: Jon Harrop <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="windows-1252"

The mergesort example given here has a rather nasty problem – it creates
sparks for mergeing the two lists at the same time as it creates the lists,
so there's always at least one task blocking waiting for it's producers.

This variation on a theme goes roughly twice as fast for me with -N1, and
*does* get a speedup from -N<somethinghigher>:

mergesort [] = []
mergesort [x] = [x]
mergesort [x,y] = if x < y then [x,y] else [y,x]
mergesort xs = (forceList sleft) `par`
               (forceList sright) `pseq`
               merge sleft sright
               where
                 (left,right) = split (length xs `div` 2) xs
                 sleft = mergesort left
                 sright = mergesort right

Note the `pseq` not `par` before the merge.  I also added one more
short-circuit case – we want to avoid spawning threads for tiny tasks like
sorting 1 element lists.

Here's my results on a dual core running OS X:

LappyBob:~ tatd2$ time ./test +RTS -N1
-0.117109518058233

real 0m4.608s
user 0m4.400s
sys 0m0.189s
LappyBob:~ tatd2$ time ./test +RTS -N2
-0.117109518058233

real 0m3.648s
user 0m6.360s
sys 0m0.220s
LappyBob:~ tatd2$ time ./test +RTS -N3
-0.117109518058233

real 0m50.679s
user 1m24.235s
sys 0m0.620s

The last result is still off the wall, but it's a lot better.

Bob

On Thu, Dec 24, 2009 at 2:24 AM, Jon Harrop <[email protected]> wrote:

>
> For some reason all traffic from this beginners list stopped reaching me
> after
> the 13th of December.
>
> Thanks for the response, Antoine. I've tried to install more recent
> versions
> of GHC (6.10 and 6.12) but I cannot get either of them to work at all.
>
> GHC 6.8 generates code that segfaults and the GHC 6.10 from Debian testing
> cannot compile anything for me. So I thought I'd try GHC 6.12. That isn't
> in
> any repo so I decided to build it from source. I uninstalled all other GHCs
> to give it a fresh start. The GHC page says "Stop, install the Haskell
> Platform" but the Haskell Platform says you must install GHC first, so I've
> got a nice circular dependency right from the start!
>
> So I tried installing GHC 6.12 from source but that requires GHC to be
> installed. So I installed GHC 6.8 (the one that segfaults) using apt again
> from Debian. That seems to have built a GHC 6.12 that I can run from the
> command line but it cannot compile that program because it doesn't have
> parallel stuff. So I thought I'd install the Haskell Platform.
>
> Unfortunately, the Haskell Platform configure script gives the nonsensical
> error that I must "upgrade" from GHC 6.12 down to GHC 6.10. I was getting
> pretty run down by this point so I just told it to sod off using
> the --enable-unsupported-ghc-version command line option. The Haskell
> Platform's configure script then completed but building it failed with:
>
> [21 of 21] Compiling Graphics.UI.GLUT ( Graphics/UI/GLUT.hs,
> dist/build/Graphics/UI/GLUT.p_o )
> Registering GLUT-2.1.1.2...
> Setup: GLUT-2.1.1.2: dependency
> "OpenGL-2.2.1.1-182b091280ce0de861295bc592bae77c" doesn't exist (use
> --force
> to override)
>
> Error:
> Building the GLUT-2.1.1.2 package failed
> make: *** [build.stamp] Error 2
>
> I have glut and use it all the time from OCaml without a hitch so I've no
> idea
> what the problem is here.
>
> Oh well, I just discovered that installing GHC 6.12 has at least fixed GHC
> 6.10 so it can now compile and run that mergesort. Oh FFS, spoke too soon:
>
> $ time ./mergesort +RTS -N8
> Stack space overflow: current size 8388608 bytes.
> Use `+RTS -Ksize' to increase it.
>
> real    0m38.801s
> user    4m0.851s
> sys     0m1.308s
>
> --
> Dr Jon Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/?e
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091224/29363886/attachment-0001.html

------------------------------

Message: 2
Date: Fri, 25 Dec 2009 01:58:54 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: [Haskell-beginners] Re: Applicability of FP unchanging things
        to real world changing things
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Glurk,

You wrote:
> I think a lot of people, looking at the real world, would answer, yes, it
> clearly IS the same book, now in a different state. And yes, I CAN
step into
> the same river twice. This is how people see and think about the real
world.

I think this is a really interesting question, and I may be too new at
functional programming myself to do it justice.  I've been doing a lot
of Haskell in a short time, so my experience is fairly intuitive and
this email is part of the process of making it more conscious.

Here's one way to structure the library:

lend :: BookID -> Library -> Library

actOn :: Request -> Library -> (Library, [Result])
actOn (Lend bookID) lib = (lend bookID lib, [])
...

-- | A library is a transformation from Request events (e.g. 'lend') to
Result events (e.g. overdue book email),
-- implemented using lazy lists.
library :: Library -> [Request] -> [Result]
library lib0 reqs = concat $ snd $ mapAccumL (\lib req -> actOn req lib)
lib0 reqs

Now, are we mutating a library, or are we not?  In spite of what the
more Zen-oriented Haskellers might say, it seems to me we are.  Call me
uneducated but I can't really see what is actually wrong with the idea
of mutation of state over time, where the abstraction is appropriate.
Perhaps the point is that in Haskell, this is one of many ways of
looking at it.  Perhaps the essential difference here is that the
library goes round in an "eddy" with an inflow of events influencing it
as it goes round (and producing an outflow of Result events).  This
difference accounts for more than it seems to.  (Trying to put my finger
on it...) it's a much more restricted activity than mutating state
outright, that means that if you change your code locally (e.g. re-write
the I/O parts), your chance of breaking things is minimal to zero.

> I guess that brings up another point - that eventually we are drawn into
> monads, which to me seems a bit unfortunate, because it seems that it
leads us
> back to imperative programming with mutable state...

In many situations where I could use a state monad I don't (though I
went through a stage of using them).  My advice would be just write the
code straight, and if the state management gets complex, a state monad
may be warranted, or some better way may present itself.  The great
beauty of Haskell is that you have a lot of choices when a situation
like this arises, and going for a state monad before you've examined the
alternatives potentially restrict them.

In the library example, if Library is a complicated data structure, you
might want to have some lift functions that allow functions that affect
only parts of the library to be lifted into a 'Library -> Library' type,
e.g.

liftDueDates :: (DueDates -> DueDates) -> Library -> Library
liftDueDates f lib = lib { libDueDates = f (libDueDates lib) }

Or to take the concept further ... There may be situations where you
want to do some process where there are a number of parts of the library
you want to work with, but conceptually some other way of viewing it
makes sense.  The "Haskell Way" would be to take the right parts of
Library and create a new data structure that reflects the conception
better.  (In FP you often abstract things using data where you would use
code in imperative languages.)  If some output from that process has to
contribute to a modified Library, then implement that as yet another
transformation.  Putting things back together like this is the extra
work that Haskell makes you do - but the benefits outweigh the costs.

So I'd say this:  If you write your code in really plain Haskell, you'll
tend towards a functional approach (e.g. why pass Library when it's
better to transform library in some way first?).  In that process, a
state monad might suggest itself as a way of making the code more
readable, but the thinking is still "plain Haskell".  Then you're using
a state monad with "right mindfulness" so to speak.

Way of looking at it #2: If you are using a state monad because it's
easier for you to understand the code that way, that's probably the
wrong reason to use one.  If you're using it because your
straightforward Haskell is looking a bit unreadable, and the state monad
tidies it up, then that's probably the right reason to use one.

Way of looking at it #3:  Your data needs to go through transformations
from one form to another.  In functional programming, you turn that
process into a flow of data (or a sequence of transformations) from one
form into another.  If the flow loops, it starts to look a bit like
mutating state, but in a way that's really only a surface appearance.

One difference between state monads and mutating state in place (as done
in OO languages) is that (in my experience) even with a state monad, the
state mutation never really gets a chance to dominate the program.
Haskellers have a saying "don't fear the monad - only IO is impure"
that's applicable here.

However, it's true that monads can lead to bad code, so they need to be
used only where they're actually appropriate.  I had an example of an
excellent use for a monad recently:  A parser where looking up an XML
tag by its id string is abstracted as a continuation (so it can use IO
if it wants) and it needs the ability to bail out if there's a parse
error.  A monad allowed me to write this code purely and cleanly.

Another difference (state monad vs. mutating in place) is that a state
monad can never lead to race conditions.


Steve


------------------------------

Message: 3
Date: Thu, 24 Dec 2009 14:24:30 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: Tom Davie <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Dec 24, 2009 at 10:09 AM, Tom Davie <[email protected]> wrote:
> The mergesort example given here has a rather nasty problem – it creates
> sparks for mergeing the two lists at the same time as it creates the lists,
> so there's always at least one task blocking waiting for it's producers.

Don't worry too much about this thread, here is why :

- Jon Harrop is definitely not a beginner in Haskell land and has no
business asking this question on this list
- Jon Harrop is very well known on established functional programming
mailing lists and forum as a very disagreeable troll (not only on
Haskell). He love to trash whatever language hasn't his favours at the
moment (I think right now he likes F#, which is a very interesting
language nonetheless).
- He apparently made an effort to include approximately all elements
that could hurt Haskell reputation in this thoughtful gift to the
community in this Christmas period :
** As you pointed the initial code he chose doesn't parallelize very
well basically
** He tried to compile it with GHC 6.8 which was an important step
towards better internals to handle parallelism in GHC but was also a
time of big upheaval in this field and so not as stable as one could
wish.
** He then installed GHC 6.10 and did not even clean up his previous
compilations attempts before declaring that it didn't work (the error
message he cited is usually observed when GHC encounters an interface
file created by a previous version of GHC or on another architecture)
at all without testing on some other clean sources.
** He then apparently decided that using a binary package for the 6.12
version was beneath its dignity and went on to install it from source
(generally not recommended except for GHC developers that needs to
work on the code or for port to unusual architectures) and claimed
that there was a "circularity problem" since its 6.10 could not
compile it (which it obviously could, as he discovered and qualified
as "miraculous", not even considering that he had a working 6.8
before).

I could choose to believe that Harrop really is as incompetent as he
sounds and really did encounter all those problems (which are all
possibles if unlikely to be met in quick succession) but the simple
fact that he decided to post this message on the Haskell-Beginner list
instead of the Haskell-cafe list where he's already well known and an
occasional poster makes it that much more unlikely. The other
interpretation is that JdH is up to his usual tricks, just sinking to
a new low by posting in a venue where he's less likely to be
recognized and more likely to worry unwary would-be-haskell-beginners.

Merry Christmas !
-- 
Jedaï


------------------------------

Message: 4
Date: Thu, 24 Dec 2009 22:50:13 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: Tom Davie <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 24 December 2009 09:09:41 you wrote:
> The mergesort example given here has a rather nasty problem – it creates
> sparks for mergeing the two lists at the same time as it creates the lists,
> so there's always at least one task blocking waiting for it's producers.

I see. So the sparks either contend for locks on the thunks to force that list 
or they repeat the same work.

> This variation on a theme goes roughly twice as fast for me with -N1, and
> *does* get a speedup from -N<somethinghigher>:
>
> mergesort [] = []
> mergesort [x] = [x]
> mergesort [x,y] = if x < y then [x,y] else [y,x]
> mergesort xs = (forceList sleft) `par`
>                (forceList sright) `pseq`
>                merge sleft sright
>                where
>                  (left,right) = split (length xs `div` 2) xs
>                  sleft = mergesort left
>                  sright = mergesort right
>
> Note the `pseq` not `par` before the merge.

Can you explain why that helps?

> I also added one more short-circuit case – we want to avoid spawning threads
> for tiny tasks like sorting 1 element lists.

This is something that concerns me. Lots of discussions of parallelism, 
including the Haskell literature I have read, neglect this critical problem 
of making sure that more time is spent doing useful work than spawning tasks 
(sparks). How is this solved in Haskell? Do you just put magic numbers in 
that work on the machine you're currently using?

Also, how much work does a Haskell thread usually have to do to make it worth 
sparking?

> Here's my results on a dual core running OS X:
>
> LappyBob:~ tatd2$ time ./test +RTS -N1
> -0.117109518058233
>
> real 0m4.608s
> user 0m4.400s
> sys 0m0.189s
> LappyBob:~ tatd2$ time ./test +RTS -N2
> -0.117109518058233
>
> real 0m3.648s
> user 0m6.360s
> sys 0m0.220s
> LappyBob:~ tatd2$ time ./test +RTS -N3
> -0.117109518058233
>
> real 0m50.679s
> user 1m24.235s
> sys 0m0.620s
>
> The last result is still off the wall, but it's a lot better.

That's another question: why did you get such awful performance for N=3 and I 
get similar results for N=8 on this 8 core?

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


------------------------------

Message: 5
Date: Thu, 24 Dec 2009 22:53:51 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Performance of parallel mergesort
To: Antoine Latter <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 24 December 2009 02:55:33 you wrote:
> The Haskell Platform is currently the recommended way to go, but it
> won't be up on GHC 6.12 until some time in the new year (I don't know
> their timelines).

I see, thanks.

> For me, I didn't have a working GHC 6.12 setup until this morning. The
> compiler is ready, but we're still working on moving the libraries
> over to the new major version.
>
> Maybe compiling your own 6.10.4 would be the best choice until the
> next platform release.

Is that better than using the 6.10.4 from Debian (and the haskell-platform 
package)?

> If you're willing to dive in, the version of cabal-install that works
> for GHC 6.12.1 is out now, but it does require some boot-strapping to
> get going, as GHC no longer ships libraries that aren't required to
> build GHC.

Yeah, I tried installing cabal from source as well but couldn't get it to work 
either.

> But back to the GHC panic, it might be worth posting that error to the
> GHC-users list I mentioned earlier.

I cannot reproduce it with GHC 6.10 so it has probably long since been fixed 
(6.8 is two years old!).

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 18, Issue 24
*****************************************

Reply via email to