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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:54:39+00:00 2026-05-27T12:54:39+00:00

Motivation I have a long-running boolean function which should be executed in an array

  • 0

Motivation

I have a long-running boolean function which should be executed in an array and I want to return immediately if an element in the array satisfies the condition. I would like to do the search in parallel and terminate other threads when the first complete thread returns an correct answer.

Question

What is a good way to implement parallel exists function in F#? Since my goal is performance, an efficient solution is preferred to an easy or idiomatic one.

Test case

Suppose that I want to find whether one value exists in an array or not. And the comparison function (equals) is simulated as a computation-expensive one:

open System.Diagnostics
open System.Threading

// Source at http://parallelpatterns.codeplex.com/releases/view/50473
let doCpuIntensiveOperation seconds (token:CancellationToken) throwOnCancel =
        if (token.IsCancellationRequested) then
            if (throwOnCancel) then token.ThrowIfCancellationRequested()
            false
        else
            let ms = int64 (seconds * 1000.0)
            let sw = new Stopwatch()
            sw.Start()
            let checkInterval = Math.Min(20000000, int (20000000.0 * seconds))

            // Loop to simulate a computationally intensive operation
            let rec loop i = 
                // Periodically check to see if the user has requested 
                // cancellation or if the time limit has passed
                let check = seconds = 0.0 || i % checkInterval = 0
                if check && token.IsCancellationRequested then
                    if throwOnCancel then token.ThrowIfCancellationRequested()
                    false
                elif check && sw.ElapsedMilliseconds > ms then
                    true
                else 
                  loop (i + 1)

            // Start the loop with 0 as the first value
            loop 0

let inline equals x y =
    doCpuIntensiveOperation 0.01 CancellationToken.None false |> ignore
    x = y

The array consists of 1000 randomly generated elements and the searching value is guaranteed in the 2nd half of the array (so sequential search has to go through at least a half of the array):

let rand = new System.Random()
let m = 1000
let N = 1000000
let xs = [|for _ in 1..m -> rand.Next(N)|]
let i = rand.Next((m-1)/2, m-1);;

#time "on";;
let b1 = parallelExists (equals xs.[i]) xs;; // Parallel
let b2 = Array.exists (equals xs.[i]) xs;; // Sequential
  • 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-27T12:54:40+00:00Added an answer on May 27, 2026 at 12:54 pm

    I think you can take the following steps:

    1. Spawn a number of workers (threads or async computations), and pass each an equal slice of the array and a cancellation token which will be shared by all workers

    2. When a worker finds the searched item, it calls Cancel on the token (each worker should check the cancel state of the token on each iteration and bail if needed)

    I don’t have time at the moment to write the code, so there could be some detail I’m omitting.

    This answer, and related question, may be helpful.

    UPDATE

    This is an example of what I’m thinking

    open System
    open System.Collections.Generic
    open System.Threading
    open System.Threading.Tasks
    
    let getChunks size array =
      let rec loop s n =
        seq {
          if n > 0 then
            let r = n - size
            if r > 0 then yield (s, size); yield! loop (s + size) r
            else yield (s, size + r)
        }      
      loop 0 (Array.length array)
    
    [<Literal>]
    let CHUNK_SIZE = 3
    
    let parallelExists f (array:_[]) =
      use cts = new CancellationTokenSource()
      let rec checkSlice i n = 
        if n > 0 && not cts.IsCancellationRequested then
          if f array.[i] then cts.Cancel()
          else checkSlice (i + 1) (n - 1)
      let workers =
        array
        |> getChunks CHUNK_SIZE
        |> Seq.map (fun (s, c) -> Task.Factory.StartNew(fun () -> checkSlice s c))
        |> Seq.toArray
      try 
        Task.WaitAll(workers, cts.Token)
        false
      with :? OperationCanceledException -> true
    

    Usage

    let array = Array.init 10 id
    let exists = 
      array |> parallelExists (fun i ->
        Thread.Sleep(500)
        i = 9)
    printfn "%b" exists //true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Motivation: I have fxcop integrated in the build process, which makes fxcopcmd.exe run each
Let's say I have a function def odd_or_even n if n%2 == 0 return
Motivation: I have a function func(const string& s); { //... } of course this
My motivation for chaining my class constructors here is so that I have a
I have a service that accepts callbacks from a provider. Motivation : I do
I have a mid-size Spring application and I want to insert key/value pairs into
I have problems running the matches XPATH method: @Test(groups = regression) @Parameters( { baseUri,
Motivation: I miss MDI in Visual Studio 2010. In long term the best solution
This answer shows that there is a built-in __name__ attribute of a function which
I have written two different applications running as windows services. Each application represents one

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.