I tried both methods , threadapproach and taskapproach, but i couldnt get two or more tasks start to run simultaneously because when i check cpu utilization, i see that only one of the specific processors is working with 100%, but what i want is two or more specific processors should work with 100% or near 100% simultaneously.
If i summarize what i am trying to do is, I want to do parallel computing but i want to specify which processors should work in parallel.
Here is your code, i did some modifications, but it doesnt do what i want.
First, i have a mathematical function, a random calculation, then i would like to have processor 2 and 3 should work on this task simultaneously to get the result.
When i run the code, only one of the specific processor is working with 100%, not both of them simultaneously.
<pre>using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication
{
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 < 20000; i++)
{
for (int j = 1; j < 15000; j++)
{
result += random.Next(0, 10) * Math.Sqrt(i) * Math.Sqrt(j) + Math.Abs(Math.Sqrt(i));
}
}
return result;
}
public static void MyTask()
{
for (int i = 0; i < 1; i++)
{
Process Proc = Process.GetCurrentProcess();
long AffinityMask = (long)Proc.ProcessorAffinity;
AffinityMask = 0x0004;
Proc.ProcessorAffinity = (IntPtr)AffinityMask;
var result1 = DumbCountSqr(i);
Console.WriteLine("result1 = " + result1);
}
}
public static void MyTask2()
{
for (int i = 1; i < 2; i++)
{
Process Proc = Process.GetCurrentProcess();
long AffinityMask = (long)Proc.ProcessorAffinity;
AffinityMask = 0x0002;
Proc.ProcessorAffinity = (IntPtr)AffinityMask;
var result2 = DumbCountSqr(i);
Console.WriteLine("result2 = " + result2);
}
}
static void ThreadApproach()
{
var t1 = new Thread(MyTask);
var t2 = new Thread(MyTask2);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
}
static void Main(string[] args)
{
ThreadApproach();
Console.WriteLine("Job Completed!");
Console.ReadLine();
}
}
}