/// <summary>
        /// Perform the addition.
        /// </summary>
        ///
        /// <param name="device">The OpenCL device to use.</param>
        /// <param name="inputA">The first vector to add.</param>
        /// <param name="inputB">The second vector to add.</param>
        /// <returns>The result of the addition.</returns>
        public double[] Add(EncogCLDevice device, double[] inputA,
                            double[] inputB)
        {
            for (int i = 0; i < inputA.Length; i++)
            {
                this.arrayA[i] = (float)inputA[i];
                this.arrayB[i] = (float)inputB[i];
            }

            this.Kernel.SetMemoryArgument(0, bufferArrayA);
            this.Kernel.SetMemoryArgument(1, bufferArrayB);
            this.Kernel.SetMemoryArgument(2, bufferTargetArray);

            EncogCLQueue queue = Device.Queue;

            queue.Array2Buffer(this.arrayA, this.bufferArrayA);
            queue.Array2Buffer(this.arrayB, this.bufferArrayB);

            queue.Execute(this);

            queue.Buffer2Array(this.bufferTargetArray, this.targetArray);
            queue.WaitFinish();

            double[] result = new double[this.targetArray.Length];

            for (int i = 0; i < this.targetArray.Length; i++)
            {
                result[i] = this.targetArray[i];
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Calculate one iteration over the specified range.
        /// </summary>
        ///
        /// <param name="start">The starting position to calculate for.</param>
        /// <param name="size">The ending position to calculate for.</param>
        /// <param name="iterations">The number of iterations to execute.</param>
        /// <param name="learn">True, if we should learn.</param>
        public void Calculate(int start, int size, bool learn,
                              int iterations)
        {
            PrepareKernel();

            this.paramArray[KernelNetworkTrain.PARRAY_LEARN]      = (learn) ? 1 : 0;
            this.paramArray[KernelNetworkTrain.PARRAY_START]      = start;
            this.paramArray[KernelNetworkTrain.PARRAY_ITEMS_PER]  = size;
            this.paramArray[KernelNetworkTrain.PARRAY_ITERATIONS] = iterations;

            EngineArray.ArrayCopy(this.flat.Weights, this.weightInArray);

            this.Kernel.SetMemoryArgument(0, this.paramBuffer);
            this.Kernel.SetMemoryArgument(1, this.errorBuffer);
            this.Kernel.SetMemoryArgument(2, this.layerIndexBuffer);
            this.Kernel.SetMemoryArgument(3, this.layerCountBuffer);
            this.Kernel.SetMemoryArgument(4, this.layerFeedCountBuffer);
            this.Kernel.SetMemoryArgument(5, this.weightIndexBuffer);
            this.Kernel.SetMemoryArgument(6, this.inputBuffer);
            this.Kernel.SetMemoryArgument(7, this.idealBuffer);
            this.Kernel.SetMemoryArgument(8, this.weightInArrayBuffer);
            this.Kernel.SetMemoryArgument(9, this.weightOutArrayBuffer);
            this.Kernel.SetMemoryArgument(10, this.gradientOutBuffer);
            this.Kernel.SetMemoryArgument(11, this.activationTypeBuffer);
            this.Kernel.SetMemoryArgument(12, this.tempDataInBuffer);
            this.Kernel.SetMemoryArgument(13, this.tempDataOutBuffer);
            this.Kernel.SetMemoryArgument(14, this.gradientInBuffer);

            try
            {
                EncogCLQueue queue = this.device.Queue;

                EngineArray.Fill(this.gradients, 0);

                if (learn)
                {
                    this.paramArray[3] = 1;
                }
                else
                {
                    this.paramArray[3] = 0;
                }

                this.paramArray[4] = start;

                queue.Array2Buffer(this.weightInArray, this.weightInArrayBuffer);
                queue.Array2Buffer(this.tempDataArray, this.tempDataInBuffer);
                queue.Array2Buffer(this.gradients, this.gradientInBuffer);
                queue.Array2Buffer(this.paramArray, this.paramBuffer);

                // Execute the kernel
                queue.Execute(this);
                queue.WaitFinish();

                // Read the results
                queue.Buffer2Array(this.errorBuffer, this.errors);
                queue.Buffer2Array(this.weightOutArrayBuffer, this.weightOutArray);
                queue.Buffer2Array(this.tempDataOutBuffer, this.tempDataArray);
                queue.Buffer2Array(this.gradientOutBuffer, this.gradients);
            }
            catch (Cloo.ComputeException ex)
            {
                if (ex.Message.IndexOf("OutOfResources") != -1)
                {
                    throw new OutOfOpenCLResources(ex);
                }
                else
                {
                    throw new OpenCLError(ex);
                }
            }
            catch (Exception ex)
            {
                throw new OpenCLError(ex);
            }
        }