public static void doItCPU()
{
var sw = new Stopwatch();
sw.Start();
int[] message = Enumerable.Range(2, 100000000).ToArray();
Parallel.ForEach(message, (number, state, index) =>
{
int upperlimit = (int)Math.Sqrt(number);
for (int i = 2; i <= upperlimit; i++)
{
if (message[index] % i == 0) //no lock needed. every index is independent
{
message[index] = 0;
break;
}
}
});
sw.Stop();
Console.WriteLine($"Total time: {sw.ElapsedMilliseconds.ToString()}");
Console.ReadKey();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCL;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int[] Primes = Enumerable.Range(2, 1000000).ToArray();
EasyCL cl = new EasyCL();
cl.Accelerator = OpenCL.AcceleratorDevice.GPU; //You can also set the accelerator after loading the kernel
cl.LoadKernel(IsPrime); //Load kernel string here, (Compiles in the background)
cl.Invoke("GetIfPrime", Primes.Length, Primes); //Call Function By Name With Parameters
//Primes now contains all Prime Numbers
}
static string IsPrime
{
get
{
return @"
kernel void GetIfPrime(global int* message)
{
int index = get_global_id(0);
int upperl=(int)sqrt((float)message[index]);
for(int i=2;i<=upperl;i++)
{
if(message[index]%i==0)
{
//printf("" %d / %d\n"",index,i );
message[index]=0;
return;
}
}
//printf("" % d"",index);
}";
}
}
}
}
key1[1 1 1 0]
key2 [1 0 0 0]
key3 [1 0 0 0]
PS C:\Users\Sergey> cd e:
PS E:\> cd test
PS E:\test> dotnet new console
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring E:\test\test.csproj:
Determining projects to restore...
Restored E:\test\test.csproj (in 123 ms).
Restore succeeded.
PS E:\test> dotnet run
The number of processors on this computer is 44.
PS E:\test>
while(true)
int idForTheCalc = int.MaxValue
// Ищу задачу на день и если нашел, тогда idForTheCalc получает id
// Если так и остался id по умолчанию, значит спим
if (idForTheCalc == int.MaxValue)
{
System.Threading.Thread.Sleep(2000);
continue;
}
//Если нашел, то делаю что нибудь или опять искать и спать, если задач нет
}