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 593327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:50:03+00:00 2026-05-13T15:50:03+00:00

I am thinking about exploiting parallelism for one problem I am trying to solve.

  • 0

I am thinking about exploiting parallelism for one problem I am trying to solve. The problem is roughly this: given input (sequence of points) find a best output (biggest triangle composed from these points, longest line etc.). There are 3 different ‘shapes’ to be found in the sequence of points, however I am interested only in the one with ‘best score’ (usually some form of ‘length’ times coefficient). Let’s call the shapes S1, S2, S3.

I have 2 different algorithms for solving S1 – ‘S1a’ is in O(n2), ‘S1b’ mostly behaves better, but the worst case is about O(n4).

First question: is there some simple way to run S1a and S1b in parallel, use the one that finishes first and stop the other? As far as I am reading documentation, this could be programmed using some forkIO and killing the threads when a result is obtained – just asking if there is something simpler?

Second question – much tougher: I am calling the optimization function this way:

optimize valueOfSx input

valueOfSx is specific for every shape and returns a ‘score’ (or a guess of a score) a possible solution. Optimize calls this function to find out best solution. What I am interested in is:

s1 = optimize valueOfS1 input
s2 = optimize valueOfS2 input
s3 = optimize valueOfS3 input
<- maximum [s1,s2,s3]

However, if I know the result of S1, I can discard all solutions that are smaller, thus making s2 and s3 converge faster if no better solution exists (or at least throw away the worst solutions and thus be more space efficient). What I am doing now is:

zeroOn threshold f = decide .f
    where decide x = if (x < threshold) then 0 else x
s1 = optimize valueOfS1 input
s2 = optimize (zeroOn s1 valueOfS2) input
s3 = optimize (zeroOn (max s1 s2) valueOfS3) input

The question is: can I run e.g. S2 and S3 in parallel in such a way, that whichever finishes first would update the ‘threshold’ parameter of the score function running in the other thread? Something in the sense of:

threshold = 0
firstSolution = firstOf (optimize (zeroOn threshold valueOfS2), optimize (zeroOn threshold valueofS3))
update threshold from firstSolution
wait for second solution
  • 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-13T15:50:03+00:00Added an answer on May 13, 2026 at 3:50 pm

    Ultimately, any solution will wind up using ForkIO under the hood because you want multiple transactions to be occurring in parallel. Even Conal’s unamb works this way.

    For the latter you probably want a more complicated monad that batches up and runs for a while before checking an MVar occasionally for a monotonically posted improving value, but the simplest answer to interleave (within one thread) is to just write a Partiality monad.

    data Partial a = Return a | Partial (Partial a)
    
    instance Monad Partial where
        return = Return
        Return a >>= f = f a
        Partial m >>= f = Partial (m >>= k)
    
    
    run :: Partial a -> a
    run (Partial m) = run m
    run (Return a) = a
    
    race :: Partial a -> Partial a -> Partial a
    race (Return a) _ = a
    race _ (Return b) = b
    race (Partial m) (Partial n) = race m n
    
    yield :: Partial ()
    yield = Partial (Return ())
    

    With an appropriate MonadFix instance to deal with recursion or liberally sprinkled ‘yield’ calls, you can perform both of your operations in the Partial monad and race them to obtain a deterministic result.

    But in practice, if you want to get the full benefit of parallelism you’ll want to update and check some kind of ‘improving’ MVar periodically.

    Something like (off the cuff, sorry, no compiler handy!):

    import Control.Concurrent.MVar (newMVar, readMVar)
    import Control.Parallel.Strategies (using, parList)
    import GHC.IOBase (unsafeDupablePerformIO, unsafePerformIO)
    
    diag x = (x,x)
    
    parMax :: (Bounded a, Ord a) => [a] -> a
    parMax xs = unsafePerformIO $ do
        threshold <- newMVar minBound
        let optimize x = unsafeDupablePerformIO $
            x `seq` modifyMVar threshold (return . diag . max x)
        return . maximum $ map optimize xs `using` parList
    

    Of course, that should be able to be rewritten to support any idempotent commutative monoid, not just max.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.