/// <summary>Creates a new vector allocated in OpenCL Image2D object.</summary>
            /// <param name="Length">Vector length. For convenience some extra memory is allocated but calculations only go up to vector dimensions</param>
            public CLImgVector(int Length)
            {
                //Stores length and computes number of necessary rows
                n = Length;
                //nRows = n/2^14 (4096 float4's) + 1 (at least one row)
                nRows = ((n - 1) >> 14) + 1;

                //Trick:
                //y = n >> 12; //y = n / 2^12;
                //x = n & 0xfff; //x = n mod (2^12-1);

                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
                {
                    try { CLCalc.InitCL(); }
                    catch
                    {
                    }
                }

                //Allocates vector. Width = IMGWIDTH, Height = nRows, Total number of elements = 4*Width*Height
                VectorData = new float[IMGWIDTH * nRows * 4];
                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
                {
                    CLVector = new CLCalc.Program.Image2D(VectorData, IMGWIDTH, nRows);
                }
            }
Exemple #2
0
        /// <summary>Writes Training Set into device memory</summary>
        private void WriteToDevice()
        {
            //Vector length
            if (HostVLen == null)
            {
                HostVLen = new int[1];
                HostVLen[0] = (1 + ((TrainingSet.trainingArray[0].xVector.Length - 1) >> 2)) << 2;
            }

            //Populates OpenCL buffer
            if (TrainingFeatures == null) // || TrainingFeatures.Length != TrainingSet.getN * HostVLen[0])
            {
                TrainingFeatures = new float[TrainingSet.getN * HostVLen[0]];
            }

            for (int i = 0; i < TrainingSet.getN; i++)
            {
                for (int j = 0; j < TrainingSet.trainingArray[0].xVector.Length; j++)
                {
                    TrainingFeatures[j + i * HostVLen[0]] = TrainingSet.trainingArray[i].xVector[j];
                }
            }

            if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL) return;
            lock (CLResource)
            {
                //Writes to OpenCL memory
                //if (CLTrainingFeatures != null) CLTrainingFeatures.Dispose();
                if (CLTrainingFeatures == null || CLTrainingFeatures.OriginalVarLength != TrainingFeatures.Length)
                {
                    if (CLTrainingFeatures != null)
                        CLTrainingFeatures.Dispose();
                    CLTrainingFeatures = new CLCalc.Program.Variable(TrainingFeatures);
                }
                else CLTrainingFeatures.WriteToDevice(TrainingFeatures);

                //if (CLXVecLen != null) CLXVecLen.Dispose();
                if (CLXVecLen == null) CLXVecLen = new CLCalc.Program.Variable(HostVLen);
                else CLXVecLen.WriteToDevice(HostVLen);

                HostSample = new float[HostVLen[0]];
                //if (CLSample != null) CLSample.Dispose();
                if (CLSample == null) CLSample = new CLCalc.Program.Image2D(HostSample, HostVLen[0] >> 2, 1);
                else CLSample.WriteToDevice(HostSample);

                //if (CLKernelValues != null) CLKernelValues.Dispose();
                if (CLKernelValues == null || CLKernelValues.OriginalVarLength != TrainingSet.getN) CLKernelValues = new CLCalc.Program.Variable(new float[TrainingSet.getN]);
                else CLKernelValues.WriteToDevice(new float[TrainingSet.getN]);

                //if (CLLambda != null) CLLambda.Dispose();
                if (CLLambda == null)
                    CLLambda = new CLCalc.Program.Variable(new float[] { this.ProblemCfg.lambda });
                else CLLambda.WriteToDevice(new float[] { this.ProblemCfg.lambda });
            }
        }
            /// <summary>Constructor.</summary>
            /// <param name="N">NxN dimension of the matrix</param>
            /// <param name="nonZeroElemsPerRow">Maximum number of non-zero elements per row</param>
            public CLImgSparseMatrix(int N, int nonZeroElemsPerRow)
            {
                elemsPerRow = nonZeroElemsPerRow - 1;
                elemsPerRow = (elemsPerRow >> 2) + 1;
                elemsPerRow = elemsPerRow << 2;

                numRows = N;

                nElems = numRows * elemsPerRow;

                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
                {
                    try { CLCalc.InitCL(); }
                    catch
                    {
                    }
                }

                //OpenCL image allocation
                int CLImgNumRows = ((nElems - 1) >> 14) + 1;
                MatrixData = new float[IMGWIDTH * CLImgNumRows * 4];
                Columns = new int[IMGWIDTH * CLImgNumRows * 4];
                for (int i = 0; i < Columns.Length; i++) Columns[i] = -1;

                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
                {
                    CLMatrixData = new CLCalc.Program.Image2D(MatrixData, IMGWIDTH, CLImgNumRows);
                    CLColumns = new CLCalc.Program.Image2D(Columns, IMGWIDTH, CLImgNumRows);
                }
            }
        public Kernels(CLCalc.Program.Image2D CLGLRenderImage)
        {
            this.CLimg = CLGLRenderImage;
            dimensions = new int[CLimg.Width * CLimg.Height];

            for (int y = 0; y < CLimg.Height; y++)
            {
                CLY.VarSize += 100;
            }

            for (int x = 0; x < CLimg.Width; x++)
            {
                CLX.VarSize += dimensions[CLimg.Height - 1 + CLimg.Height * x];
                CLZ.VarSize += dimensions[CLimg.Height * x] = 240;
            }
            CLDimensions = new CLCalc.Program.Variable(dimensions);
        }