/// <summary> /// Construct a device. /// </summary> /// /// <param name="device">The OpenCL device to base on.</param> public EncogCLQueue(EncogCLDevice device) { EncogCLPlatform platform = device.Platform; this.device = device; this.commands = this.commands = new ComputeCommandQueue(platform.Context, device.Device, ComputeCommandQueueFlags.None); }
/// <summary> /// Choose a device. If a GPU is found, return that. Otherwise try to find a /// CPU. /// </summary> /// /// <returns>The first device detected.</returns> public EncogCLDevice ChooseDevice() { EncogCLDevice result = ChooseDevice(true); if (result == null) { result = ChooseDevice(false); } return(result); }
/// <summary> /// Construct an OpenCL platform. /// </summary> /// /// <param name="platform">The OpenCL platform.</param> public EncogCLPlatform(ComputePlatform platform) { this.platform = platform; this.devices = new List <EncogCLDevice>(); ComputeContextPropertyList cpl = new ComputeContextPropertyList(platform); this.context = new ComputeContext(ComputeDeviceTypes.Default, cpl, null, IntPtr.Zero); this.Name = platform.Name; this.Vender = platform.Vendor; this.Enabled = true; foreach (ComputeDevice device in context.Devices) { EncogCLDevice adapter = new EncogCLDevice(this, device); this.devices.Add(adapter); } }
/// <summary> /// Evaluate training, use OpenCL. /// </summary> /// <param name="device">The OpenCL device, null for CPU.</param> /// <param name="input">Input neurons.</param> /// <param name="hidden1">Hidden 1 neurons.</param> /// <param name="hidden2">Hidden 2 neurons.</param> /// <param name="output">Output neurons.</param> /// <returns>The result of the evaluation.</returns> public static int EvaluateTrain(EncogCLDevice device, int input, int hidden1, int hidden2, int output) { BasicNetwork network = EncogUtility.SimpleFeedForward(input, hidden1, hidden2, output, true); INeuralDataSet training = RandomTrainingFactory.Generate(1000, 10000, input, output, -1, 1); OpenCLTrainingProfile profile = null; #if !SILVERLIGHT if (device != null) profile = new OpenCLTrainingProfile(device); #endif return EvaluateTrain(profile, network, training); }
/// <summary> /// Construct an OpenCL device performer. /// </summary> /// <param name="number">The device number.</param> /// <param name="device">The device.</param> public ConcurrentTrainingPerformerOpenCL(int number, EncogCLDevice device) : base(number) { if (EncogFramework.Instance.CL == null) { throw new NeuralNetworkError( "Can't use an OpenCL performer, because OpenCL " + "is not enabled."); } if (EncogFramework.Instance.CL == null) { throw new NeuralNetworkError("Can't use a null OpenCL device."); } this.device = device; }
/// <summary> /// Construct an OpenCL platform. /// </summary> /// /// <param name="platform">The OpenCL platform.</param> public EncogCLPlatform(ComputePlatform platform) { this.platform = platform; this.devices = new List<EncogCLDevice>(); ComputeContextPropertyList cpl = new ComputeContextPropertyList(platform); this.context = new ComputeContext(ComputeDeviceTypes.Default, cpl, null, IntPtr.Zero); this.Name = platform.Name; this.Vender = platform.Vendor; this.Enabled = true; foreach (ComputeDevice device in context.Devices) { EncogCLDevice adapter = new EncogCLDevice(this, device); this.devices.Add(adapter); } }
/// <summary> /// Construct a training profile with the specified device and the value of one for all ratios. /// </summary> /// /// <param name="device">The device to use.</param> public OpenCLTrainingProfile(EncogCLDevice device) : this(device, 1.0d, 1, 1.0d) { }
/// <summary> /// Construct a training profile. /// </summary> /// /// <param name="device">The device to use.</param> /// <param name="localRatio">The local ratio.</param> /// <param name="globalRatio">The global ratio.</param> /// <param name="segmentationRatio">The segmentation ratio.</param> public OpenCLTrainingProfile(EncogCLDevice device, double localRatio, int globalRatio, double segmentationRatio) : base() { this.device = device; if (localRatio < 0 || globalRatio < 0 || segmentationRatio < 0) { throw new OpenCLError("None of the ratios can be below zero."); } if (localRatio > 1.0d) { throw new OpenCLError( "The local ratio cannot be greater than 1.0. That would cause the OpenCL device to have more local items than it can handle."); } if (globalRatio < 1.0d) { throw new OpenCLError( "The global ratio cannot be less than 1.0. That would cause the global work area to be less than a local work area."); } if (segmentationRatio > 1.0d) { throw new OpenCLError( "The segmentation ratio cannot be greater than 1.0. That would cause the trainer to require more training elements per iteration than exist."); } this.localRatio = localRatio; this.globalRatio = globalRatio; this.segmentationRatio = segmentationRatio; }
/// <summary> /// Evaluate the OpenCL device. /// </summary> private void EvalOpenCL() { try { // did the caller assign a device? If not, use the first GPU, // failing that, // use the first CPU. Failing that, as well, don't test OpenCL. if (this.device == null) { if (EncogFramework.Instance.CL == null) EncogFramework.Instance.InitCL(); this.device = EncogFramework.Instance.CL.ChooseDevice(); } } catch (Exception) { this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "No OpenCL devices, result: 0"); this.clScore = 0; } int small = 0, medium = 0, large = 0, huge = 0; try { small = Evaluate.EvaluateTrain(device, 2, 4, 0, 1); this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, tiny= " + Format.FormatInteger(small / 100)); } catch (Exception) { this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, tiny FAILED"); } try { medium = Evaluate.EvaluateTrain(device, 10, 20, 0, 1); this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, small= " + Format.FormatInteger(medium / 30)); } catch (Exception) { this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, small FAILED"); } try { large = Evaluate.EvaluateTrain(device, 100, 200, 40, 5); this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, large= " + Format.FormatInteger(large)); } catch (Exception) { this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, large FAILED"); } try { huge = Evaluate.EvaluateTrain(device, 200, 300, 200, 50); this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, huge= " + Format.FormatInteger(huge)); } catch (Exception) { this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "Evaluate OpenCL, huge FAILED"); } int result = (small / 100) + (medium / 30) + large + huge; this.report.Report(EncogBenchmark.STEPS, EncogBenchmark.STEP2, "OpenCL result: " + result); this.clScore = result; }
/// <summary> /// Construct a device. /// </summary> /// /// <param name="device">The OpenCL device to base on.</param> public EncogCLQueue(EncogCLDevice device) { EncogCLPlatform platform = device.Platform; this.device = device; this.commands = this.commands = new ComputeCommandQueue(platform.Context, device.Device, ComputeCommandQueueFlags.None); }