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

  • SEARCH
  • Home
  • 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 6190433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:35:19+00:00 2026-05-24T02:35:19+00:00

My aim is to have a parallel foldr function. At first, it seemed rather

  • 0

My aim is to have a parallel foldr function. At first, it seemed
rather straight forward to achieve and this is what I had in mind:

First break up the input list into partitions based on the number of
cores (numCapabilities). Then apply foldr to each partition, which
will result in a list of folded values for each partition. Then do a
foldr again on that list to obtain the final value.

    listChunkSize = numCapabilities

    chunk n [] = []
    chunk n xs = ys : chunk n zs
      where (ys,zs) = splitAt n xs

    parfoldr f z [] = z
    parfoldr f z xs = res
      where
            parts = chunk listChunkSize xs
            partsRs = map (foldr f z) parts `using` parList rdeepseq
            res = foldr f z partsRs

The above code does not work because obviously the definition of
foldr, (a -> b -> b) -> b -> [a] -> b, implies that the input list
type is (well, can be) different from the accumulator and result type.

For example,

1) foldr (+) 0 [1..10] => list type = accumulator type (Integer)

2) foldr (\i acc -> (i>5) && acc) True [1..10] => list type (Integer) !
= accumulator type (Bool)

So, looking at my code above, the map will generate a list of type b
which is then passed as argument to the second foldr. But the second
foldr accepts list of type a. So, that won’t work.

An ugly solution would be to provide a different type signature for
the parfoldr, e.g.
parfoldr :: (NFData a) => (a -> a -> a) -> a -> [a] -> a

This will work but then it is not exactly equivalent to foldr. Example
1 above will do just fine, but not example 2.
So, question 1 is: how to define parfoldr to have same type signature
as foldr?

Comparing the 2 folds:

    input = [1..1000000]
    seqfold = foldr (+) 0
    parfold = parfoldr (+) 0

I get the foll. times on a dual core machine:
(no -threaded flag)

    $ ./test
    seqfold: 4.99s
    parfold: 25.16s

(-threaded flag on)

    $ ./test
    seqfold: 5.32s
    parfold: 25.55s
    $ ./test +RTS -N1
    seqfold: 5.32s
    parfold: 25.53s
    $ ./test +RTS -N2
    seqfold: 3.48s
    parfold: 3.68s
    $ ./test +RTS -N3
    seqfold: 3.57s
    parfold: 2.36s
    $ ./test +RTS -N4
    seqfold: 3.03s
    parfold: 1.70s

Observations from these measurements:

  • foldr seems to give lower runtime when num of cores is increased.
    why is that?

  • parfold gives better runtimes for N => 3.

Any suggestions and ideas for improvement is appreciated 🙂

  • 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-24T02:35:19+00:00Added an answer on May 24, 2026 at 2:35 am

    foldr is not in general parallelizable, as its interface allows sequential dependencies. In order to be able to rearrange the computations in the way you described you’ll need to limit yourself to associative operators with an identity element. This is known as a monoid, and what you’ve implemented is essentially a parallel mconcat.

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

Sidebar

Related Questions

So I have this code m, b = eval(input()) the aim is to have
Aim to Achieve : I want to have 3 different dataTables from 3 different
I'm learning python for the first time. I have an aim which is to
I have a problem with my recursive function in PHP. The aim of my
Aim The aim is to a have a container DIV with a fixed height
My ultimate aim is to have an inline div on my page which horizontally
I have this kind of path architecture : >main_path/ __init__.py config/ __init__.py common.py app_1/
I have problem in creating modular analysis architecture for C# application. Aim: It is
I have a real-life example that I have in my project below. My aim
I have read that it is best to aim keep functions to no more

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.