One one hand, in Haskell Vector a seems to be the preferred type to use as an array of numbers. There is even an (incomplete) Vector Tutorial.
On the other hand, Control.Parallel.Strategies are defined mostly in terms of Traversable. Vector library doesn’t provide these instances.
The minimal complete definition of Traversable t should also define Foldable and
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
sequenceA :: Applicative f => t (f a) -> f (t a)
I don’t see how sequenceA can be defined for Data.Vector.Unboxed.Vector. So, what is the best approach to writing parallel code with unboxed vectors? Defining some new ad hoc strategies like evalVector or using par and pseq explicitly or using plain Data.Array instead of vectors?
P.S. Plain Arrays are parallelizable without problems: https://gist.github.com/701888
It’s a hack job for
parVectorbut this worked for me:And running this code:
When I run the code with
Integerinstead ofIntin the type signature:Rock!
EDIT: And a solution that is a bit closer to your earlier attempt is cleaner (it doesn’t use functions from three separate modules) and works great:
Things to learn from this solution:
Evalmonad, which is strict so we’re sure to spark everything (compared to wrapping things inletand remembering to use bang patterns).evalChunkforces evaluation of each element usingrparandrdeepseq(I don’t believerpar vecforces any of the vector’s elements).slicetakes a start index and length, not a start and end index. Oops!Control.DeepSeq (NFData), but I’ve e-mailed the libraries list to try and fix that issue.Performance seems similar to the first
parVectorsolution in this answer, so I won’t post numbers.