|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using OpenCL.NetCore; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using MoreLinq; |
| 8 | + |
| 9 | +namespace MCSlimeClusterFinder |
| 10 | +{ |
| 11 | + public class OpenCLWrapper |
| 12 | + { |
| 13 | + CommandQueue queue; |
| 14 | + Kernel kernel; |
| 15 | + IMem dataOut; |
| 16 | + Program program; |
| 17 | + Context context; |
| 18 | + public int[] candidates; |
| 19 | + int squareLength; |
| 20 | + int globalSize; |
| 21 | + |
| 22 | + |
| 23 | + private long worldSeed { get; } |
| 24 | + private Device device { get; } |
| 25 | + |
| 26 | + public static List<Device> GetDevices() |
| 27 | + => Cl.GetPlatformIDs(out ErrorCode error) |
| 28 | + .SelectMany(p => Cl.GetDeviceIDs(p, DeviceType.Gpu, out error)) |
| 29 | + .ToList(); |
| 30 | + |
| 31 | + public OpenCLWrapper(int squareLength, Device dev, long seed) |
| 32 | + { |
| 33 | + this.squareLength = squareLength; |
| 34 | + globalSize = squareLength * squareLength; |
| 35 | + candidates = new int[globalSize]; |
| 36 | + device = dev; |
| 37 | + worldSeed = seed; |
| 38 | + ready(); |
| 39 | + } |
| 40 | + |
| 41 | + ~OpenCLWrapper() |
| 42 | + { |
| 43 | + Cl.ReleaseKernel(kernel); |
| 44 | + Cl.ReleaseMemObject(dataOut); |
| 45 | + Cl.ReleaseCommandQueue(queue); |
| 46 | + Cl.ReleaseProgram(program); |
| 47 | + Cl.ReleaseContext(context); |
| 48 | + } |
| 49 | + private void allGood(ErrorCode ec) |
| 50 | + { |
| 51 | + if (ec != ErrorCode.Success) |
| 52 | + throw new Exception($"OpenCL had an error: {ec}"); |
| 53 | + } |
| 54 | + private void ready() |
| 55 | + { |
| 56 | + ErrorCode error; |
| 57 | + |
| 58 | + context = Cl.CreateContext(null, 1, new[] { device }, null, IntPtr.Zero, out error); |
| 59 | + |
| 60 | + string source = System.IO.File.ReadAllText("kernels.cl"); |
| 61 | + program = Cl.CreateProgramWithSource(context, 1, new[] { source }, null, out error); |
| 62 | + |
| 63 | + error = Cl.BuildProgram(program, 1, new[] { device }, string.Empty, null, IntPtr.Zero); |
| 64 | + InfoBuffer buildStatus = Cl.GetProgramBuildInfo(program, device, ProgramBuildInfo.Status, out error); |
| 65 | + if (buildStatus.CastTo<BuildStatus>() != BuildStatus.Success) |
| 66 | + throw new Exception($"OpenCL could not build the kernel successfully: {buildStatus.CastTo<BuildStatus>()}"); |
| 67 | + allGood(error); |
| 68 | + |
| 69 | + Kernel[] kernels = Cl.CreateKernelsInProgram(program, out error); |
| 70 | + kernel = kernels[0]; |
| 71 | + allGood(error); |
| 72 | + |
| 73 | + queue = Cl.CreateCommandQueue(context, device, CommandQueueProperties.None, out error); |
| 74 | + allGood(error); |
| 75 | + |
| 76 | + dataOut = Cl.CreateBuffer(context, MemFlags.WriteOnly, (IntPtr)(globalSize * sizeof(int)), out error); |
| 77 | + allGood(error); |
| 78 | + |
| 79 | + var intSizePtr = new IntPtr(Marshal.SizeOf(typeof(int))); |
| 80 | + error |= Cl.SetKernelArg(kernel, 2, new IntPtr(Marshal.SizeOf(typeof(IntPtr))), dataOut); |
| 81 | + error |= Cl.SetKernelArg(kernel, 3, intSizePtr, new IntPtr(worldSeed)); |
| 82 | + error |= Cl.SetKernelArg(kernel, 4, intSizePtr, new IntPtr(globalSize)); |
| 83 | + allGood(error); |
| 84 | + } |
| 85 | + |
| 86 | + public void Work((long x, long z) startingPoint) |
| 87 | + { |
| 88 | + ErrorCode error; |
| 89 | + int local_size = 256; |
| 90 | + int global_size = (int)Math.Ceiling(globalSize / (float)local_size) * local_size; |
| 91 | + |
| 92 | + var intSizePtr = new IntPtr(Marshal.SizeOf(typeof(int))); |
| 93 | + error = Cl.SetKernelArg(kernel, 0, intSizePtr, new IntPtr(startingPoint.x)); |
| 94 | + error |= Cl.SetKernelArg(kernel, 1, intSizePtr, new IntPtr(startingPoint.z)); |
| 95 | + allGood(error); |
| 96 | + |
| 97 | + error = Cl.EnqueueNDRangeKernel(queue, kernel, 1, null, new IntPtr[] { new IntPtr(global_size) }, null, 0, null, out Event clevent); |
| 98 | + allGood(error); |
| 99 | + Cl.EnqueueReadBuffer(queue, dataOut, Bool.False, IntPtr.Zero, (IntPtr)(globalSize * sizeof(int)), candidates, 0, null, out clevent); |
| 100 | + Cl.Finish(queue); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments