Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delegates and Parallel.For

Delegates and Parallel.For

Scheduled Pinned Locked Moved C#
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fahad Sadah
    wrote on last edited by
    #1

    for (number=0;primes<10001;number++)

    That is a valid for loop. However, I am trying to parallelize this, and Parallel.For will only accept one delegate. Therefore, if I wanted

    for (number=0;number<10001;number++)

    I could do

    Parallel.For(0,10001, delegate(int number))

    Parallel.For has no overload which accepts two delegates. What is the Parallel.For equivalent of the first snippet? Thanks, Fahad Sadah

    S N 2 Replies Last reply
    0
    • F Fahad Sadah

      for (number=0;primes<10001;number++)

      That is a valid for loop. However, I am trying to parallelize this, and Parallel.For will only accept one delegate. Therefore, if I wanted

      for (number=0;number<10001;number++)

      I could do

      Parallel.For(0,10001, delegate(int number))

      Parallel.For has no overload which accepts two delegates. What is the Parallel.For equivalent of the first snippet? Thanks, Fahad Sadah

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Fahad Sadah wrote:

      for (number=0;primes<10001;number++)

      If primes doesn't change in the body of the loop, why doesn't

      Fahad Sadah wrote:

      Parallel.For(0,10001, delegate(int number))

      work?

      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

      L 1 Reply Last reply
      0
      • S S Senthil Kumar

        Fahad Sadah wrote:

        for (number=0;primes<10001;number++)

        If primes doesn't change in the body of the loop, why doesn't

        Fahad Sadah wrote:

        Parallel.For(0,10001, delegate(int number))

        work?

        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It probably does though. Why else would he have asked?

        S 1 Reply Last reply
        0
        • L Lost User

          It probably does though. Why else would he have asked?

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Well, if it does, the code isn't really parallelizable. If each iteration of the for loop overwrites the variable in a way that is dependent on previous iterations, it doesn't really make sense to parellilize it. For example, if it really is calculation of primes, then numbers = 10000 could run before numbers = 2.

          Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

          F 1 Reply Last reply
          0
          • S S Senthil Kumar

            Well, if it does, the code isn't really parallelizable. If each iteration of the for loop overwrites the variable in a way that is dependent on previous iterations, it doesn't really make sense to parellilize it. For example, if it really is calculation of primes, then numbers = 10000 could run before numbers = 2.

            Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

            F Offline
            F Offline
            Fahad Sadah
            wrote on last edited by
            #5

            Impossible? Thanks.

            1 Reply Last reply
            0
            • F Fahad Sadah

              for (number=0;primes<10001;number++)

              That is a valid for loop. However, I am trying to parallelize this, and Parallel.For will only accept one delegate. Therefore, if I wanted

              for (number=0;number<10001;number++)

              I could do

              Parallel.For(0,10001, delegate(int number))

              Parallel.For has no overload which accepts two delegates. What is the Parallel.For equivalent of the first snippet? Thanks, Fahad Sadah

              N Offline
              N Offline
              Nicholas Butler
              wrote on last edited by
              #6

              Good question! The problem you have is that you don't know the bounds of your input - you don't know how big number will have to get to generate the required number of primes. However, you can still parellelise chunks of work - something like this:

              const int N = ( int ) 1e6;
              static readonly int STEP = Environment.ProcessorCount * ( int ) 1e6;

              ...

              var primes = new List<int>( N );

              for ( int start = 2 ; primes.Count < N ; start += STEP )
              primes.AddRange(
              Enumerable.Range( start, STEP )
              .AsParallel().AsOrdered()
              .Where( i => IsPrime( i ) ) );

              primes = primes.Take( N ).ToList();

              You'll have to tune the values of N and STEP for your specific case. Nick

              ---------------------------------- Be excellent to each other :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups