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. making parllel for loop with uses of all processor

making parllel for loop with uses of all processor

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
6 Posts 2 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.
  • K Offline
    K Offline
    KUNWAR999
    wrote on last edited by
    #1

    here is my code, word is a string array. i want to convert this code into parllel ,so that i can reduce time and use all processor of my i5 processor system.. for (int i = 0; i < k1; i++) { for (int j = 0; j < k1; j++) { num[j][i] = Convert.ToInt32(words[count]); count++; } } can anyone help me out in this????

    L 1 Reply Last reply
    0
    • K KUNWAR999

      here is my code, word is a string array. i want to convert this code into parllel ,so that i can reduce time and use all processor of my i5 processor system.. for (int i = 0; i < k1; i++) { for (int j = 0; j < k1; j++) { num[j][i] = Convert.ToInt32(words[count]); count++; } } can anyone help me out in this????

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

      Easiest solution is to convert it to a parallel loop[^], available in .NET 4;

      using System;
      using System.Threading.Tasks;

      class Demo
      {
      int N = 1000;

      void TestMethod()
      {
          Parallel.For(0, N, (i, loopState) =>
          {
              Console.WriteLine(i);
              if (i == 100)
              {
                  loopState.Break();
              }
          });
      }
      

      }

      If you need more control, you'd typically use a thread[^].

      Bastard Programmer from Hell :suss:

      K 1 Reply Last reply
      0
      • L Lost User

        Easiest solution is to convert it to a parallel loop[^], available in .NET 4;

        using System;
        using System.Threading.Tasks;

        class Demo
        {
        int N = 1000;

        void TestMethod()
        {
            Parallel.For(0, N, (i, loopState) =>
            {
                Console.WriteLine(i);
                if (i == 100)
                {
                    loopState.Break();
                }
            });
        }
        

        }

        If you need more control, you'd typically use a thread[^].

        Bastard Programmer from Hell :suss:

        K Offline
        K Offline
        KUNWAR999
        wrote on last edited by
        #3

        I tried this code.. but i m getting high execution time than its sequential code.. and parllel execution should take less execution time than sequential code.. :(

        L 1 Reply Last reply
        0
        • K KUNWAR999

          I tried this code.. but i m getting high execution time than its sequential code.. and parllel execution should take less execution time than sequential code.. :(

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

          KUNWAR999 wrote:

          and parllel execution should take less execution time than sequential code.. :(

          ..if the problem can be solved using parallel threads, and only when implemented correctly. The example was the easiest method, but it's only a shorthand for a specific type of problem. You can spawn threads[^] to run code concurrently. It's usually the OS that decides on which core the thread runs, but I believe you can set it's affinity.

          Bastard Programmer from Hell :suss:

          K 1 Reply Last reply
          0
          • L Lost User

            KUNWAR999 wrote:

            and parllel execution should take less execution time than sequential code.. :(

            ..if the problem can be solved using parallel threads, and only when implemented correctly. The example was the easiest method, but it's only a shorthand for a specific type of problem. You can spawn threads[^] to run code concurrently. It's usually the OS that decides on which core the thread runs, but I believe you can set it's affinity.

            Bastard Programmer from Hell :suss:

            K Offline
            K Offline
            KUNWAR999
            wrote on last edited by
            #5

            i hv tried it one more way as using #pragma omp parllel .. but vs2010 saying that its unrecognized concept.. actually i am avoiding using threads because i want to use parllel processing in processor directly.. :(

            L 1 Reply Last reply
            0
            • K KUNWAR999

              i hv tried it one more way as using #pragma omp parllel .. but vs2010 saying that its unrecognized concept.. actually i am avoiding using threads because i want to use parllel processing in processor directly.. :(

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

              KUNWAR999 wrote:

              actually i am avoiding using threads because i want to use parllel processing in processor directly.. :(

              ..and how would you do that?

              Bastard Programmer from Hell :suss:

              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