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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. i wait so much because of loops

i wait so much because of loops

Scheduled Pinned Locked Moved C#
questionalgorithmsperformance
23 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

    If things are getting slow, you should start doing some of them in parallel... :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    Prolific encyclopedia fixture proof-reader browser patron addict?
    We all depend on the beast below.


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

    :laugh: Win :) Anyway, how's your prime number thing going?

    L 1 Reply Last reply
    0
    • K karayel_kara

      it is floyd marshall algorithm but it is run slowly how can i increase speed? i have quad pc and it use %25 cpu but i wait so much . for (int k = 0; k < 2000; k++) for (int i = 0; i < 2000; i++) for (int j = 0; j < 2000; j++) G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #15

      I've just knocked up a blog post[^] to show how this code could be accomplished using .NET 4. Here's the same code rewritten to use the Task Parallel Library.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      L 1 Reply Last reply
      0
      • L Lost User

        :laugh: Win :) Anyway, how's your prime number thing going?

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

        very good, thanks, it currently uses 1.1 threads. There isn't enough work to keep a dual-core busy :laugh: :laugh: I'll publish it later this year.

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Prolific encyclopedia fixture proof-reader browser patron addict?
        We all depend on the beast below.


        1 Reply Last reply
        0
        • P Pete OHanlon

          I've just knocked up a blog post[^] to show how this code could be accomplished using .NET 4. Here's the same code rewritten to use the Task Parallel Library.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

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

          Hi Pete, I'm afraid I disagree with your parallel sample in this particular case; it seems to violate the Floyd-Warshall_algorithm[^], which theoretically uses a three-dimensional array, but then maps the k-iterations (the outer loop) onto a single two-dimensional array, provided it gets executed in the correct sequence. In another part of this thread I have shown Harold how parts of the calculation could be executed in parallel, but your and my approach are quite different. FWIW: I have no clue what the speed up would be for say a 2000 node graph and a 4-way core; I'm not overly optimistic as there is a lot of data involved, so using multiple cores is likely to seriously reduce cache efficiency. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Prolific encyclopedia fixture proof-reader browser patron addict?
          We all depend on the beast below.


          P 2 Replies Last reply
          0
          • L Luc Pattyn

            Hi Pete, I'm afraid I disagree with your parallel sample in this particular case; it seems to violate the Floyd-Warshall_algorithm[^], which theoretically uses a three-dimensional array, but then maps the k-iterations (the outer loop) onto a single two-dimensional array, provided it gets executed in the correct sequence. In another part of this thread I have shown Harold how parts of the calculation could be executed in parallel, but your and my approach are quite different. FWIW: I have no clue what the speed up would be for say a 2000 node graph and a 4-way core; I'm not overly optimistic as there is a lot of data involved, so using multiple cores is likely to seriously reduce cache efficiency. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Prolific encyclopedia fixture proof-reader browser patron addict?
            We all depend on the beast below.


            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #18

            Luc - I wasn't trying to demonstrate the algorithm. I was demonstrating purely and simply how to convert his code into a Parallel.For.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            L 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi Pete, I'm afraid I disagree with your parallel sample in this particular case; it seems to violate the Floyd-Warshall_algorithm[^], which theoretically uses a three-dimensional array, but then maps the k-iterations (the outer loop) onto a single two-dimensional array, provided it gets executed in the correct sequence. In another part of this thread I have shown Harold how parts of the calculation could be executed in parallel, but your and my approach are quite different. FWIW: I have no clue what the speed up would be for say a 2000 node graph and a 4-way core; I'm not overly optimistic as there is a lot of data involved, so using multiple cores is likely to seriously reduce cache efficiency. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Prolific encyclopedia fixture proof-reader browser patron addict?
              We all depend on the beast below.


              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #19

              BTW - your code sample won't compile. The following line is the problem because of an uninitialized variable:

              G[i, j] = Math.Min(G[i, j], G[i, j] + G[j, j]); // special case k==j

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                Luc - I wasn't trying to demonstrate the algorithm. I was demonstrating purely and simply how to convert his code into a Parallel.For.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

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

                I appreciate that, Pete. However I'm afraid you wrecked the algorithm, which isn't a very nice thing to do. When the results aren't the same, there isn't much point in measuring the speed difference, is there? :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Prolific encyclopedia fixture proof-reader browser patron addict?
                We all depend on the beast below.


                1 Reply Last reply
                0
                • P Pete OHanlon

                  BTW - your code sample won't compile. The following line is the problem because of an uninitialized variable:

                  G[i, j] = Math.Min(G[i, j], G[i, j] + G[j, j]); // special case k==j

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

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

                  you're right, I replaced k's by j's, it should have been the other way around. Sorry for that. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Prolific encyclopedia fixture proof-reader browser patron addict?
                  We all depend on the beast below.


                  K 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    you're right, I replaced k's by j's, it should have been the other way around. Sorry for that. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Prolific encyclopedia fixture proof-reader browser patron addict?
                    We all depend on the beast below.


                    K Offline
                    K Offline
                    karayel_kara
                    wrote on last edited by
                    #22

                    hi ,i modified my code folowing that, but i get some error folowing code running ! double[,] a, b, c; a = new double[2000, 2000]; b=new double[2000,2000]; c=new double[2000,2000]; int s = 2000; Parallel.For(0, s, delegate(int i) { for (int j = 0; j < s; j++) { double v = 0; for (int k = 0; k < s; k++) { v += a[i, k] * b[k, j]; } c[i, j] = v; } }); bu my code isn't run :( Parallel.For(0, nrons, delegate(int k) { for (i = 0; i < nrons; i++) for (j = 0; j < nrons; j++) //G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]); if (G[i, j]> (G[i, k] + G[k, j]) ) G[i, j] =(G[i, k] + G[k, j]) ; } ); why did i get error ?

                    L 1 Reply Last reply
                    0
                    • K karayel_kara

                      hi ,i modified my code folowing that, but i get some error folowing code running ! double[,] a, b, c; a = new double[2000, 2000]; b=new double[2000,2000]; c=new double[2000,2000]; int s = 2000; Parallel.For(0, s, delegate(int i) { for (int j = 0; j < s; j++) { double v = 0; for (int k = 0; k < s; k++) { v += a[i, k] * b[k, j]; } c[i, j] = v; } }); bu my code isn't run :( Parallel.For(0, nrons, delegate(int k) { for (i = 0; i < nrons; i++) for (j = 0; j < nrons; j++) //G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]); if (G[i, j]> (G[i, k] + G[k, j]) ) G[i, j] =(G[i, k] + G[k, j]) ; } ); why did i get error ?

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

                      karayel_kara wrote:

                      my code isn't run

                      is not informative. does it compile? if not, what is the first error? does it run? if not, what is the first exception, with all the details? and at what line is it pointing? anyway, your code looks all wrong. You now have three arrays. Why? You don't initialize the arrays. I see six for loops, you only need three. And the way I understand the algorithm you can not possibly run the outer loop in parallel (that was the essence of my comment to Pete too). And please, please, please, please, please, please, show code in PRE tags, not in CODE tags. Final remark: when all you do to the algorithm is run it in parallel, the best you can hope for is to get it N times faster, with N the number of cores, say 4. That is something, but not much. And there are no guarantees. My first experiment did run 5 times slower, because it trashed the caches all the time! (and I'm afraid that will be true for your app too). :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Prolific encyclopedia fixture proof-reader browser patron addict?
                      We all depend on the beast below.


                      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