コード例 #1
0
ファイル: CLSVM.cs プロジェクト: ozweigle/opencltemplate
        /// <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 });
                }
            }
        }