-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
76 lines (67 loc) · 1.92 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Diagnostics;
using System.Linq;
using Cloo.Extensions;
namespace TestGpuProg
{
static class Program
{
static void Main(string[] args)
{
var devices = ClooExtensions.GetDeviceNames();
foreach (var dev in devices.Where(d => args.Length == 0 || args.Contains(d.Trim())))
{
WriteLine(dev.Trim(), ConsoleColor.Yellow);
int[] primes = Enumerable.Range(2, 1000000).ToArray();
var sw = new Stopwatch();
try
{
sw.Start();
primes.ClooForEach(IsPrime, k => true, (i, d, v) => d == dev);
WriteLine($"{string.Join(", ", primes.Where(n => n != 0).Take(100))}, ...", ConsoleColor.Gray);
}
catch (Exception ex)
{
WriteLine($"Error: {ex.Message}", ConsoleColor.Red);
}
finally
{
sw.Stop();
WriteLine($"Time: {sw.ElapsedMilliseconds}ms");
}
WriteLine();
}
WriteLine("Press enter to quit...", ConsoleColor.White, true);
}
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)
{
message[index]=0;
return;
}
}
}";
}
}
static void WriteLine(string text = "", ConsoleColor color = ConsoleColor.White, bool wait = false)
{
Console.ForegroundColor = color;
Console.WriteLine(text);
if (wait)
{
Console.ReadKey();
}
}
}
}