0

I have multiple tasks and i need to run them on specific multiple cores or processors simultaneously.

For Example, if i have two different tasks or just one task but will run it twice I want to run these two tasks on specific cores or processors simultaneously like task1 will run on processor1 and task2 will run on processor2 at the same time.

I know how to run each specific processor but i do not know how to run them simultaneously. I was trying to use multithreading then task parallel library to run different tasks on specific processors at the same time but i failed.

In below code, i was trying to use multithreading but it doesnt work or can i use task parallel library???

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;


namespace ConsoleApplication14

{
    class Program
{

    public static double DumbCountSqr(int dumnum)
    {

        Random random = new Random();
        int randomNumber = random.Next(0, 10);
        double result = 0;
        for (int i = 1; i < 10000; i++)
        {
            for (int j = 1; j < 10000; j++)
            {
                result += random.Next(0, 10) * Math.Sqrt(i) * Math.Sqrt(j) + Math.Abs(Math.Sqrt(i));
            }
        }
        return result;
    }

    public static void Main()
    {

        Thread t = new Thread(new ThreadStart(Go));
        t.Start();


        for (int i = 0; i < 1; i++)
        {

            Process Proc = Process.GetCurrentProcess();
            long AffinityMask = (long)Proc.ProcessorAffinity;

            AffinityMask = 0x0004;//processor    3
            Proc.ProcessorAffinity = (IntPtr)AffinityMask;

            var result1 = DumbCountSqr(i);
            Console.WriteLine("result1 =  " + result1);

        }
    }


   public static void Go()
    {

        for (int i = 0; i < 1; i++)
        {

                Process Proc = Process.GetCurrentProcess();
                long AffinityMask = (long)Proc.ProcessorAffinity;

                AffinityMask = 0x0001;//processor    1
                Proc.ProcessorAffinity = (IntPtr)AffinityMask;

                var result2 = DumbCountSqr(i);
                Console.WriteLine("result2 =  " + result2);

        }

    }

}

}

1
  • 3
    How are you determining that they aren't running simultaneously? "It doesn't work"...how is it not working? Commented Mar 19, 2014 at 23:09

1 Answer 1

1

Use the task parallel library and take a look at the Parallel.For Method. This takes advantage of the multiple cores for you. But be aware how you use it. Parallel looping may not always be faster than sequential looping.

Here is an example on how to use on MSDN: http://msdn.microsoft.com/en-us/library/dd460713(v=vs.110).aspx

and here: http://msdn.microsoft.com/en-us/library/dd460703(v=vs.110).aspx

and here: http://msdn.microsoft.com/en-us/library/ff963552.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.