|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Cloo; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | + |
| 7 | +namespace Blockcore.Networks.X1.Components |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// The famous SpartaCrypt OpenCLMiner, visit the original here: |
| 11 | + /// https://github.com/spartacrypt/xds-1/blob/master/src/components/Fullnode/UnnamedCoin.Bitcoin.Features.Miner/OpenCLMiner.cs |
| 12 | + /// </summary> |
| 13 | + public class OpenCLMiner : IDisposable |
| 14 | + { |
| 15 | + private const string KernelFunction = "kernel_find_pow"; |
| 16 | + |
| 17 | + private readonly ILogger logger; |
| 18 | + private readonly ComputeDevice computeDevice; |
| 19 | + |
| 20 | + private List<ComputeKernel> computeKernels = new List<ComputeKernel>(); |
| 21 | + private ComputeProgram computeProgram; |
| 22 | + private ComputeContext computeContext; |
| 23 | + private ComputeKernel computeKernel; |
| 24 | + private string[] openCLSources; |
| 25 | + private bool isDisposed; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Create a new OpenCLMiner instance. |
| 29 | + /// </summary> |
| 30 | + /// <param name="minerSettings">the minerSettings</param> |
| 31 | + /// <param name="loggerFactory">the loggerFactory</param> |
| 32 | + public OpenCLMiner(X1MinerSettings minerSettings, ILoggerFactory loggerFactory) |
| 33 | + { |
| 34 | + this.logger = loggerFactory.CreateLogger(this.GetType().FullName); |
| 35 | + var devices = ComputePlatform.Platforms.SelectMany(p => p.Devices).Where(d => d.Available && d.CompilerAvailable).ToList(); |
| 36 | + |
| 37 | + if (!devices.Any()) |
| 38 | + { |
| 39 | + this.logger.LogWarning($"No OpenCL Devices Found!"); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + foreach (ComputeDevice device in devices) |
| 44 | + { |
| 45 | + this.logger.LogInformation($"Found OpenCL Device: Name={device.Name}, MaxClockFrequency{device.MaxClockFrequency}"); |
| 46 | + } |
| 47 | + |
| 48 | + this.computeDevice = devices.FirstOrDefault(d => d.Name.Equals(minerSettings.OpenCLDevice, StringComparison.OrdinalIgnoreCase)) ?? devices.FirstOrDefault(); |
| 49 | + if (this.computeDevice != null) |
| 50 | + { |
| 51 | + this.logger.LogInformation($"Using OpenCL Device: Name={this.computeDevice.Name}"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Destructor. |
| 58 | + /// </summary> |
| 59 | + ~OpenCLMiner() |
| 60 | + { |
| 61 | + this.DisposeOpenCLResources(); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// If a compute device for mining is available. |
| 66 | + /// </summary> |
| 67 | + /// <returns>true if a usable device is found, otherwise false</returns> |
| 68 | + public bool CanMine() |
| 69 | + { |
| 70 | + return this.computeDevice != null; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the currently used device name. |
| 75 | + /// </summary> |
| 76 | + /// <returns></returns> |
| 77 | + public string GetDeviceName() |
| 78 | + { |
| 79 | + if (this.computeDevice == null) |
| 80 | + { |
| 81 | + throw new InvalidOperationException("GPU not found"); |
| 82 | + } |
| 83 | + |
| 84 | + return this.computeDevice.Name; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Finds the nonce for a block header hash that meets the given target. |
| 89 | + /// </summary> |
| 90 | + /// <param name="header">serialized block header</param> |
| 91 | + /// <param name="bits">the target</param> |
| 92 | + /// <param name="nonceStart">the first nonce value to try</param> |
| 93 | + /// <param name="iterations">the number of iterations</param> |
| 94 | + /// <returns></returns> |
| 95 | + public uint FindPow(byte[] header, byte[] bits, uint nonceStart, uint iterations) |
| 96 | + { |
| 97 | + if (this.computeDevice == null) |
| 98 | + { |
| 99 | + throw new InvalidOperationException("GPU not found"); |
| 100 | + } |
| 101 | + |
| 102 | + this.ConstructOpenCLResources(); |
| 103 | + |
| 104 | + using var headerBuffer = new ComputeBuffer<byte>(this.computeContext, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, header); |
| 105 | + using var bitsBuffer = new ComputeBuffer<byte>(this.computeContext, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, bits); |
| 106 | + using var powBuffer = new ComputeBuffer<uint>(this.computeContext, ComputeMemoryFlags.WriteOnly, 1); |
| 107 | + |
| 108 | + this.computeKernel.SetMemoryArgument(0, headerBuffer); |
| 109 | + this.computeKernel.SetMemoryArgument(1, bitsBuffer); |
| 110 | + this.computeKernel.SetValueArgument(2, nonceStart); |
| 111 | + this.computeKernel.SetMemoryArgument(3, powBuffer); |
| 112 | + |
| 113 | + using var commands = new ComputeCommandQueue(this.computeContext, this.computeDevice, ComputeCommandQueueFlags.None); |
| 114 | + commands.Execute(this.computeKernel, null, new long[] { iterations }, null, null); |
| 115 | + |
| 116 | + var nonceOut = new uint[1]; |
| 117 | + commands.ReadFromBuffer(powBuffer, ref nonceOut, true, null); |
| 118 | + commands.Finish(); |
| 119 | + |
| 120 | + this.DisposeOpenCLResources(); |
| 121 | + |
| 122 | + return nonceOut[0]; |
| 123 | + } |
| 124 | + |
| 125 | + private void ConstructOpenCLResources() |
| 126 | + { |
| 127 | + if (this.computeDevice != null) |
| 128 | + { |
| 129 | + if (this.openCLSources == null) |
| 130 | + { |
| 131 | + GetOpenCLSources(); |
| 132 | + } |
| 133 | + var properties = new ComputeContextPropertyList(this.computeDevice.Platform); |
| 134 | + this.computeContext = new ComputeContext(new[] { this.computeDevice }, properties, null, IntPtr.Zero); |
| 135 | + this.computeProgram = new ComputeProgram(this.computeContext, this.openCLSources); |
| 136 | + this.computeProgram.Build(new[] { this.computeDevice }, null, null, IntPtr.Zero); |
| 137 | + this.computeKernels = this.computeProgram.CreateAllKernels().ToList(); |
| 138 | + this.computeKernel = this.computeKernels.First((k) => k.FunctionName == KernelFunction); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void GetOpenCLSources() |
| 143 | + { |
| 144 | + this.openCLSources = new[] |
| 145 | + { |
| 146 | + Properties.Resources.SpartacryptOpenCLMiner_opencl_device_info_h, |
| 147 | + Properties.Resources.SpartacryptOpenCLMiner_opencl_misc_h, |
| 148 | + Properties.Resources.SpartacryptOpenCLMiner_opencl_sha2_common_h, |
| 149 | + Properties.Resources.SpartacryptOpenCLMiner_opencl_sha512_h, |
| 150 | + Properties.Resources.SpartacryptOpenCLMiner_sha512_miner_cl |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + private void DisposeOpenCLResources() |
| 155 | + { |
| 156 | + this.computeKernels.ForEach(k => k.Dispose()); |
| 157 | + this.computeKernels.Clear(); |
| 158 | + this.computeProgram?.Dispose(); |
| 159 | + this.computeProgram = null; |
| 160 | + this.computeContext?.Dispose(); |
| 161 | + this.computeContext = null; |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Releases the OpenCL resources. |
| 166 | + /// </summary> |
| 167 | + public void Dispose() |
| 168 | + { |
| 169 | + Dispose(true); |
| 170 | + GC.SuppressFinalize(this); |
| 171 | + } |
| 172 | + |
| 173 | + void Dispose(bool disposing) |
| 174 | + { |
| 175 | + if (this.isDisposed) |
| 176 | + return; |
| 177 | + |
| 178 | + if (disposing) |
| 179 | + { |
| 180 | + DisposeOpenCLResources(); |
| 181 | + } |
| 182 | + |
| 183 | + this.isDisposed = true; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments