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. Parallel.For Issue

Parallel.For Issue

Scheduled Pinned Locked Moved C#
csharpvisual-studiodata-structureshelpquestion
16 Posts 6 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.
  • L Luc Pattyn

    David1987 wrote:

    adding to a linked list is not an atomic operation.

    Neither is any operation on a BigInteger! :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    B Offline
    B Offline
    Bassam Abdul Baki
    wrote on last edited by
    #7

    Aghhh! That explains it and crap.

    1 Reply Last reply
    0
    • L Luc Pattyn

      David1987 wrote:

      adding to a linked list is not an atomic operation.

      Neither is any operation on a BigInteger! :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      D Offline
      D Offline
      David1987
      wrote on last edited by
      #8

      Indeed, I didn't see any problems with that though, the bigint is a local variable..

      L 1 Reply Last reply
      0
      • B Bassam Abdul Baki

        No summing involved.  Just some computations to see if the data gets added to the list or not. Each loop could work independently since they don't really depend on one another, but they all add to the same LinkedList using AddLast.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #9

        Collections (and most every other kind of objects) aren't thread safe by themselves. You may consider having a thread-local list-of-things-to-add-later, fill those in parallel, then add them in sequentially. And if that is the majority of the work, parallellizing won't help you a bit. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        B 1 Reply Last reply
        0
        • L Luc Pattyn

          Collections (and most every other kind of objects) aren't thread safe by themselves. You may consider having a thread-local list-of-things-to-add-later, fill those in parallel, then add them in sequentially. And if that is the majority of the work, parallellizing won't help you a bit. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          B Offline
          B Offline
          Bassam Abdul Baki
          wrote on last edited by
          #10

          Thanks, but it is the majority of the work. :(

          M 1 Reply Last reply
          0
          • D David1987

            Bassam Abdul-Baki wrote:

            LinkedList array

            What? Anyway you may have a problem there, adding to a linked list is not an atomic operation.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #11

            Yep, it's the add to the collection that's causing the problem. I'm guessing this is a vast oversimplification because what's posted there is not worth parallelising anyway. Assuming the stuff inside the loop (calculating what's to be put in the list) is the expensive part, have a thread-local list (a local variable to the lambda) and merge them all at the end. If it's actually adding millions of simple things to a list then you'll need to be cleverer about how you put things in there, for example creating an array large enough for everything beforehand and interleaving the results into well defined indices so they can't collide.

            A B 2 Replies Last reply
            0
            • D David1987

              Indeed, I didn't see any problems with that though, the bigint is a local variable..

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #12

              locals are fine; however when there is one BigInteger, there is bound to be more, and the code looked rather abstract and incomplete, so I mentioned it just in case it would ring a bell. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              1 Reply Last reply
              0
              • B BobJanova

                Yep, it's the add to the collection that's causing the problem. I'm guessing this is a vast oversimplification because what's posted there is not worth parallelising anyway. Assuming the stuff inside the loop (calculating what's to be put in the list) is the expensive part, have a thread-local list (a local variable to the lambda) and merge them all at the end. If it's actually adding millions of simple things to a list then you'll need to be cleverer about how you put things in there, for example creating an array large enough for everything beforehand and interleaving the results into well defined indices so they can't collide.

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #13

                BobJanova wrote:

                creating an array large enough for everything beforehand and interleaving the results into well defined indices so they can't collide

                I had never thought of that before. :doh: :thumbsup:

                [WikiLeaks Cablegate Cables]

                1 Reply Last reply
                0
                • B Bassam Abdul Baki

                  Thanks, but it is the majority of the work. :(

                  M Offline
                  M Offline
                  Mirko1980
                  wrote on last edited by
                  #14

                  Since it seems you are already using .NET 4, you can give a look to the new Thread-safe collections.

                  B 1 Reply Last reply
                  0
                  • B BobJanova

                    Yep, it's the add to the collection that's causing the problem. I'm guessing this is a vast oversimplification because what's posted there is not worth parallelising anyway. Assuming the stuff inside the loop (calculating what's to be put in the list) is the expensive part, have a thread-local list (a local variable to the lambda) and merge them all at the end. If it's actually adding millions of simple things to a list then you'll need to be cleverer about how you put things in there, for example creating an array large enough for everything beforehand and interleaving the results into well defined indices so they can't collide.

                    B Offline
                    B Offline
                    Bassam Abdul Baki
                    wrote on last edited by
                    #15

                    BobJanova wrote:

                    If it's actually adding millions of simple things to a list

                    It is, but it's all done inside a while loop that I didn't show. However, my inner array grows by an order of 4*3^(n-1) for each while loop and goes on for a while (pardon the pun).

                    Web - BM - RSS - Math - LinkedIn

                    1 Reply Last reply
                    0
                    • M Mirko1980

                      Since it seems you are already using .NET 4, you can give a look to the new Thread-safe collections.

                      B Offline
                      B Offline
                      Bassam Abdul Baki
                      wrote on last edited by
                      #16

                      Cool, I'll look into that. Thanks!

                      Web - BM - RSS - Math - LinkedIn

                      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