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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:32:44+00:00 2026-05-21T04:32:44+00:00

I am experimenting with the new parallelism tools in .NET 4, by calculating Pi

  • 0

I am experimenting with the new parallelism tools in .NET 4, by calculating Pi using Monte Carlo methods.

(The actual algorithm is not so important, but for clarity’s sake, here it is:

  1. Pick numIterations random points inside a unit square.
  2. Count the number of these points that lie within a circle that is bounded by that square (i.e. the points whose distance from the centre of the square is less than 0.5)
  3. Then, for very large numIterations, PI=4 * iterationsInsideCircle / numIterations.)

I have a method int ThrowDarts(int numDarts) which picks numDarts random points inside the unit square (described above) and returns the number of points which lie within the unit circle:

    protected static int ThrowDarts(int iterations)
    {
        int dartsInsideCircle = 0;
        Random random = new Random();
        for (int iteration = 0; iteration < iterations; iteration++)
        {
            double pointX = random.NextDouble() - 0.5;
            double pointY = random.NextDouble() - 0.5;

            double distanceFromOrigin = Math.Sqrt(pointX*pointX + pointY*pointY);
            bool pointInsideCircle = distanceFromOrigin <= 0.5;

            if (pointInsideCircle)
            {
                dartsInsideCircle++;
            }
        }
        return dartsInsideCircle;
    }

Essentially, in each of my different implementations (which each use different parallel mechanisms), I am writing different ways of throwing and counting the darts inside the circle.

For example, my single threaded implementation is simply:

    protected override int CountInterationsInsideCircle()
    {
        return ThrowDarts(_numInterations);
    }

I also have this method for one of my parallel algorithms:

    protected override int CountInterationsInsideCircle()
    {
        Task<int>[] tasks = new Task<int>[_numThreads];

        for (int i = 0; i < _numThreads; i++)
        {
            tasks[i] = Task.Factory.StartNew(() => ThrowDarts(_numInterations/_numThreads));
        }

        int iterationsInsideCircle = 0;
        for (int i = 0; i < _numThreads; i++)
        {
            iterationsInsideCircle += tasks[i].Result;
        }

        return iterationsInsideCircle;
    }

Hopefully you get the picture.

Here, I get to my conundrum. The Parallel.For version I am writing causes massive amounts of context switching. The code is below:

    protected override int CountInterationsInsideCircle()
    {
        ConcurrentBag<int> results = new ConcurrentBag<int>();
        int result = 0;

        Parallel.For(0, _numInterations,
                     // initialise each thread by setting it's hit count to 0
                     () => 0,
                     //in the body, we throw one dart and see whether it hit or not
                     (iteration, state, localState) => localState + ThrowDarts(1),
                     // finally, we sum (in a thread-safe way) all the hit counts of each thread together
                     results.Add);

        foreach(var threadresult in results)
        {
            result+=threadresult;
        }

        return result;
    }

The version using Parallel.For does work, but very, very slowly, because of the aforementioned context switching (which does not occur in the previous two methods).

Is anyone able to enlighten me as to why this may be happening?

  • 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-21T04:32:44+00:00Added an answer on May 21, 2026 at 4:32 am

    I’ve actually found the solution to the question.

    Previously, in my ThrowDarts method, I was creating a new Random with every call (this was because the Random class is not thread safe.)

    However, turns out, this is relatively expensive. (At least, it is when only performing one dart throw, such that we generate a new Random for each iteration.)

    Thus, I have modified my ThrowDarts method to take a Random which the caller creates, and modified my LoopState to contain it’s own Random.

    Therefore, each thread in the Parallel.For contains it’s own Random. My new implementation is as follows:

        protected override int CountInterationsInsideCircle()
        {
            ConcurrentBag<int> results = new ConcurrentBag<int>();
            Parallel.For(0, _numInterations,
                         // initialise each thread by setting it's hit count to 0
                         () => new LoopThreadState(),
                         // in the body, we throw one dart and see whether it hit or not
                         (iteration, _, localState) =>
                            {
                                localState.Count += ThrowDarts(1, localState.RandomNumberGenerator);
                                return localState;
                            },
                         // finally, we sum (in a thread-safe way) all the hit counts of each thread together
                         result => results.Add(result.Count));
    
            int finalResult = 0;
            foreach (int threadresult in results)
            {
                finalResult += threadresult;
            }
    
            return finalResult;
        }
    

    I guess the context switching metric was a bit of a red herring, and a simple profile would have done the trick. Nice curve ball, .NET, nice. Anyway, lesson learned!

    Thanks all,
    Alex

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

Sidebar

Related Questions

I'm experimenting with the new System.Threading.Parallel methods like parallel for and foreach. They seem
I am quite new to programming, but now experimenting with a table view app.
I am new to using OpenGL and am experimenting with jogl. I am able
New to ASP.NET MVC (using MVC3 with Razor now) and I'm confused on passing
While experimenting with this question on collections in Spring.NET , I discovered that Spring
I'm currently experimenting with build script, and since I have an ASP.net Web Part
We have been experimenting with a new technique to manage our release branches. Generally,
I just started experimenting with a new technique I name (for the moment at
I'm new to iPhone and experimenting with the platform. I have a root view
I am new to Prolog and am experimenting around with some stuff, in particular

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.