Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3351742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:56:44+00:00 2026-05-18T01:56:44+00:00

One one hand, in Haskell Vector a seems to be the preferred type to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:56:44+00:00Added an answer on May 18, 2026 at 1:56 am

    It’s a hack job for parVector but this worked for me:

    import qualified Data.Vector as V
    import Control.Parallel.Strategies
    import Control.Parallel
    import Control.DeepSeq
    
    ack :: Int -> Int -> Int
    ack 0 n = n+1
    ack m 0 = ack (m-1) 1
    ack m n = ack (m-1) (ack m (n-1))
    
    main = do
      let vec = V.enumFromN 1 1000
      let res = (V.map (ack 2) vec) `using` parVector
      print res
    
    parVector :: NFData a => Strategy (V.Vector a)
    parVector vec = eval vec `seq` Done vec
      where
      chunkSize = 1
      eval v
        | vLen == 0 = ()
        | vLen <= chunkSize = rnf (v V.! 0) -- FIX this to handle chunks > 1
        | otherwise = eval (V.take half v) `par` eval (V.drop half v)
        where vLen = V.length v
              half = vLen `div` 2
    

    And running this code:

    [tommd@Mavlo Test]$ ghc --make -O2 -threaded t.hs
    ... dumb warning ...
    [tommd@Mavlo Test]$ time ./t +RTS -N1 >/dev/null
    real    0m1.962s user    0m1.951s sys     0m0.009s
    [tommd@Mavlo Test]$ time ./t +RTS -N2 >/dev/null
    real    0m1.119s user    0m2.221s sys 0m0.005s
    

    When I run the code with Integer instead of Int in the type signature:

    [tommd@Mavlo Test]$ time ./t +RTS -N2 >/dev/null
    
    real    0m4.754s
    user    0m9.435s
    sys     0m0.028s
    [tommd@Mavlo Test]$ time ./t +RTS -N1 >/dev/null
    
    real    0m9.008s
    user    0m8.952s
    sys     0m0.029s
    

    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:

    parVector :: NFData a => Strategy (V.Vector a)
    parVector vec =
      let vLen = V.length vec
          half = vLen `div` 2
          minChunk = 10
      in  if vLen > minChunk
          then do
            let v1 = V.unsafeSlice 0 half vec
                v2 = V.unsafeSlice half (vLen - half) vec
            parVector v1
            parVector v2
            return vec
          else
            evalChunk (vLen-1) >>
            return vec
      where
      evalChunk 0 = rpar (rdeepseq (vec V.! 0)) >> return vec
      evalChunk i = rpar (rdeepseq (vec V.! i)) >> evalChunk (i-1)
    

    Things to learn from this solution:

    1. It uses the Eval monad, which is strict so we’re sure to spark everything (compared to wrapping things in let and remembering to use bang patterns).
    2. Contrary to your proposed implementation it (a) doesn’t construct a new vector, which is costly (b) evalChunk forces evaluation of each element using rpar and rdeepseq (I don’t believe rpar vec forces any of the vector’s elements).
    3. Contrary to my belief, slice takes a start index and length, not a start and end index. Oops!
    4. We still need to import Control.DeepSeq (NFData), but I’ve e-mailed the libraries list to try and fix that issue.

    Performance seems similar to the first parVector solution in this answer, so I won’t post numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and
On one hand, a tuple is versatile, but on the other, can be less
On the one hand there is http://ckfinder.com/ CKFinder or the people behind it have
One of the fun parts of multi-cultural programming is number formats. Americans use 10,000.50
One may not always know the Type of an object at compile-time, but may
I'm trying my hand at Euler Problem 4 in Haskell. It asks for that
On one hand, I'm told that exceptions in C# are 'expensive', but on the
On one hand, I know that the advisable usage of Properties is to have
On the one hand mod_rewrite allows me to make userfriendly urls redirecting requests to
For our database development we have on one hand a full schema DDL script,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.