Alex Rivera | Logout

How to make a Strategy in Haskell Control.Parallel.Strategies?

Asked 2010-09-09T19:05:32.130
11

Update: I just found this documentation page. Wish there was a link to it from the documentation that I'd been using, which seemed to be the definitive API doc. But maybe it's a new, unreleased work.

Update 2: This documentation has given me a lot better idea how to use the Control.Parallel.Strategies module. However I haven't quite solved the problem... see end of question.

I've been trying to use parListChunk or some other parallel control features in Haskell. But I can't figure out how to use them. Warning: I'm a Haskell noob. I learned some things about functional programming with Scheme about 20 years ago (!).

Here's my non-parallelized function:

possibKs n r = [ (k, (hanoiRCountK n k r)) | k <- [1 .. n-1] ]

I want to parallelize it, something like this naive attempt:

possibKs n r 
    | n < parCutoff  = results
    | otherwise      = parListChunk parChunkSize results
    where results = [ (k, (hanoiRCountK n k r)) | k <- [1 .. n-1] ]

But that structure isn't right for parListChunk. The docs say:

parListChunk :: Int -> Strategy a -> Strategy [a]

parListChunk sequentially applies a strategy to chunks (sub-sequences) of a list in parallel. Useful to increase grain size

Good, that's what I want. But how to use it? I haven't been able to find any examples of this. If I'm understanding the type declaration, parListChunk is a function that takes an Int a

Edit
Report

1 Answer

2

OK, I got the code working. I got it to compile by using rwhnf instead of rseq:

| otherwise      = results `using` (parListChunk parChunkSize rwhnf)

According to this source code, rwhnf was renamed to rseq in version 3. So I guess my version of the Parallel package is obsolete with respect to this documentation. :-S

I guess that's part of the price of using "experimental" packages.

Anyway, the code compiles and runs. Whether it's doing anything useful with parallelism is another question...

answered 2010-09-09T20:27:08.420

Your Answer