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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:55:12+00:00 2026-06-01T18:55:12+00:00

I am playing with parallel programming and F#. I created a function that integrates

  • 0

I am playing with parallel programming and F#. I created a function that integrates a 1-variable function, and then I tried to make it parallel in two different ways:

module Quadrature = 

    let Integrate (f: double -> double) (x1: double) (x2: double) (samples: int64) =
        let step = (x2 - x1) / (double samples)
        let samplePoints = seq {x1 + step .. step .. x2 - step}
        let sum = samplePoints |> Seq.map (fun x -> f x) |> Seq.sum
        let sum = sum + ((f x1) + (f x2)) / 2.0
        step * sum

    let IntegrateWithStep (f: double -> double) (x1: double) (x2: double) (step: double) =
        let samples = (x2 - x1) / step |> round |> int64
        Integrate f x1 x2 samples

    let IntegrateWithTasks (f: double -> double) (x1: double) (x2: double) (samples: int64) (tasks: int) =
        let step = (x2 - x1) / (double samples)
        let samplesPerTask = ceil <| (double samples) / (double tasks)
        let interval = step * samplesPerTask
        let intervals = 
            seq { 
                for i in 0 .. (tasks - 1) do
                    let lowerBound = x1 + (double i) * interval
                    let upperBound = min (lowerBound + interval) x2  
                    yield (lowerBound, upperBound)
                }
        let tasks = intervals 
                    |> Seq.map (fun (a, b) -> Task.Factory.StartNew(fun () -> IntegrateWithStep f a b step))
        tasks |> Seq.map (fun t -> t.Result) |> Seq.sum

    let IntegrateParallel (f: double -> double) (x1: double) (x2: double) (samples: int64) (tasks: int) =
        let step = (x2 - x1) / (double samples)
        let samplesPerTask = ceil <| (double samples) / (double tasks)
        let interval = step * samplesPerTask
        let intervals = 
               [| for i in 0 .. (tasks - 1) do
                    let lowerBound = x1 + (double i) * interval
                    let upperBound = min (lowerBound + interval) x2  
                    yield (lowerBound, upperBound) |]
        intervals |> Array.Parallel.map (fun (a, b) -> IntegrateWithStep f a b step)
                  |> Array.sum

I run this code with the following input on a machine with 4 cores:

let f = (fun x -> - 1.0 + 2.0 * x - 3.0 * x * x + 4.0 * x * x * x ) 
let x1, x2 = 0.0, 1.0
let samples = 100000000L
let tasks = 100

However, the method with the task factory is always slightly slower than the linear one, while the one with the Parallel.map gives me a good speed up.

I have tried varying the number of tasks from thousands down to the number of cores but the implementation with the Task.Factory is always slower than the linear one. What am I doing wrong?

  • 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-06-01T18:55:13+00:00Added an answer on June 1, 2026 at 6:55 pm

    Remember that sequences are lazily loaded. Here is the first time the task gets started:

    tasks |> Seq.map (fun t -> t.Result) |> Seq.sum
    

    And you are starting them sequentially and blocking each one for their result (when calling t.Result. You’ll want to save the list of tasks as an array, and then call .WaitAll before collecting the results to ensure they all get started in parallel.

    Try:

    let tasks = intervals 
                |> Seq.map (fun (a, b) -> Task.Factory.StartNew(fun () -> IntegrateWithStep f a b step))
                |> Array.ofSeq
    
    tasks.WaitAll()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
I just started playing with the Task Parallel Library, and ran into interesting issues;
Playing with Erlang, I've got a process-looping function like: process_loop(...A long list of parameters
Playing with new RTTI module, I couldn't find a way to set an event
I'm playing with parallel strategies and wondering if I'm doing the following the right
playing around with a property file i figured that there seems to be a
I was playing around with the LG LG P990 optimus speed and noticed that
Playing with Git and GitHub,I found that sometimes a git commit -a is needed
Playing with Disqus for commenting, but the problem is that we have a Silverlight

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.