Beispiel #1
0
        static void Main()
        {
            //Initializes OpenCL Platforms and Devices and sets everything up
            CLCalc.InitCL();

            //Create vectors with 2000 numbers
            float[] v1 = new float[n], v2 = new float[n];

            var vResult = new float[n];

            //Creates population for v1 and v2
            for (int i = 0; i < n; i++)
            {
                v1[i] = (float)i / 10;
                v2[i] = -(float)i / 9;
            }

            //var prog = new ComputeProgram(CLCalc.Program.Context, "");

            //Compiles the source codes. The source is a string array because the user may want
            //to split the source into many strings.
            CLCalc.Program.Compile(new string[] { vecSum });

            //Gets host access to the OpenCL floatVectorSum kernel
            CLCalc.Program.Kernel VectorSum = new CLCalc.Program.Kernel("floatVectorSum");

            //Creates vectors v1 and v2 in the device memory
            CLCalc.Program.Variable varV1 = new CLCalc.Program.Variable(v1);
            CLCalc.Program.Variable varV2 = new CLCalc.Program.Variable(v2);

            //Arguments of VectorSum kernel
            CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[] { varV1, varV2 };

            //How many workers will there be? We need "n", one for each element
            int[] workers = new int[1] { n };

            sw.Start();
            //Execute the kernel
            for (int i = 0; i < count; i++) DoOCL(VectorSum, args, workers);
            sw.Stop();

            //Read device memory varV1 to host memory vResult
            varV1.ReadFromDeviceTo(vResult);

            Console.WriteLine("OpenCL: {0}", sw.ElapsedTicks);

            sw.Restart();
            for (int i = 0; i < count; i++) DoCPU(v1, v2, vResult);
            sw.Stop();

            Console.WriteLine("CPU: {0}", sw.ElapsedTicks);

            PressAny();
        }
Beispiel #2
0
        /// <summary>Constructor. Initializes OpenCL</summary>
        public CLMatrixMult()
        {
            if (CLCalc.GLAcceleration == CLCalc.GLAccelerationType.Unknown)
                CLCalc.InitCL();

            //Using OpenCL, declare variables
            if (CLCalc.GLAcceleration == CLCalc.GLAccelerationType.UsingGL)
            {
                floatMatrixMultSource src = new floatMatrixMultSource();
                CLCalc.Program.Compile(new string[] { src.matrixMultLocals, src.matrixMultNoLocals });

                //multiplication
                floatMatrixMultNoLocals = new CLCalc.Program.Kernel("floatMatrixMult");
                floatMatrixMultLocals = new CLCalc.Program.Kernel("floatMatrixMultLocals");
            }
        }
Beispiel #3
0
        /// <summary>Initializes class</summary>
        private static void Init(int FilterSize)
        {
            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) CLCalc.InitCL();

            //Compiles source code
            CLCalc.Program.Compile((new CLFilterSrc()).src);

            //Creates kernel
            kernelApplyFilter = new CLCalc.Program.Kernel("ApplyFilter");
            kernelApplyFilterWorkDim2 = new CLCalc.Program.Kernel("ImgFilter");

            //Creates filter
            varFilter = new CLCalc.Program.Variable(new float[3 * FilterSize * FilterSize]);
            //Width
            varWidth = new CLCalc.Program.Variable(new int[1]);

            Initialized = true;
        }
        /// <summary>Static Constructor. Builds kernels</summary>
        static SparseLinalg()
        {
            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
            {
                try { CLCalc.InitCL(); }
                catch
                {
                }
            }

            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
            {
                //Kernel
                CLLinalgSrc src = new CLLinalgSrc();
                CLCalc.Program.Compile(new string[] { src.srcDotProd, src.srcMatVecMult, src.srcLinConjGrad });
                kernelDotProduct = new CLCalc.Program.Kernel("dotProd");
                kernelSum = new CLCalc.Program.Kernel("sumDotProd");
                kernelGetDotSum = new CLCalc.Program.Kernel("GetResp");

                kernelSparseMatrixVecMult = new CLCalc.Program.Kernel("SparseMatrixVecMult");

                //Linear solving
                kernelInitRP = new CLCalc.Program.Kernel("InitRP");
                kernelMultiplyAdd = new CLCalc.Program.Kernel("MultiplyAdd");
                kernelCopyToTemp = new CLCalc.Program.Kernel("CopyToTemp");
            }
        }
Beispiel #5
0
        public OpenCLTest()
        {
            CLCalc.InitCL();
            var buildlogs = new List<string>();
            CLCalc.Program.Compile(new[] { run }, out buildlogs);
            Random r = new Random();

            float[] sample = Enumerable.Range(0, 768).Select(x => (float)r.NextDouble() * 2 - 1).ToArray();
            float[] hlW = Enumerable.Range(0, 10000 * 769).Select(x => (float)r.NextDouble() * 2 - 1).ToArray();
            float[] olW = Enumerable.Range(0, 10001 * 10).Select(x => (float)r.NextDouble() * 2 - 1).ToArray();
            float[] hlDest = new float[10000];
            float[] olDest = new float[10];

            //float[] sample = new[] { .3f, .7f };

            //float[] hlW = new[] { .4f, .4f, .1f, .5f, .5f, .7f };

            //float[] olW = new[] { .4f, .6f, .8f };

            //float[] hlDest = new[] { 0f, 0f };

            //float[] olDest = new[] { 0f };
            var sw = Stopwatch.StartNew();
            var sw2 = Stopwatch.StartNew();
            var gSample = sample.ToGraphics();
            var ghlW = hlW.ToGraphics();
            var golW = olW.ToGraphics();
            var ghlDest = hlDest.ToGraphics();
            var golDest = olDest.ToGraphics();
            sw2.Stop();

            var kernel = new CLCalc.Program.Kernel("runNN");

            var args = new[] {gSample, new CLCalc.Program.Variable(new[] { sample.Length}), ghlDest,
               new CLCalc.Program.Variable(new[] { hlDest.Length}), ghlW};

            kernel.Execute(args, hlDest.Length);

            args = new CLCalc.Program.Variable[5];
            args[0] = ghlDest;
            args[1] = new CLCalc.Program.Variable(new[] { hlDest.Length });
            args[2] = golDest;
            args[3] = new CLCalc.Program.Variable(new[] { olDest.Length });
            args[4] = golW;

            kernel.Execute(args, olDest.Length);

            sw2.Start();
            ghlDest.ReadFromDeviceTo(hlDest);
            golDest.ReadFromDeviceTo(olDest);
            sw2.Stop();
            sw.Stop();

            Console.WriteLine("hlDest\n" + string.Join("\n", hlDest));
            Console.WriteLine("olDest\n" + string.Join("\n", olDest));

            Console.WriteLine("Total {0}ms", sw.ElapsedMilliseconds);
            Console.WriteLine("Memory {0}ms", sw2.ElapsedMilliseconds);
        }
Beispiel #6
0
                /// <summary>Constructor. Builds OpenCL program.</summary>
                public doubleLinearAlgebra()
                {
                    if (CLCalc.CLDevices == null)
                    {
                        try
                        {
                            CLCalc.InitCL();
                        }
                        catch
                        {
                            throw new Exception("Could not initialize OpenCL");
                        }
                    }

                    try
                    {
                        LinalgSource Source = new LinalgSource();
                        string[] s = new string[] { Source.dblInclude, Source.vecSum, Source.matrixMult,
                            Source.GaussSeidel, Source.LUDecomp };

                        CLCalc.Program.Compile(s);

                        //sum
                        doubleVecSum = new Program.Kernel("doubleVectorSum");
                        //multiplication
                        doubleMatrixMult = new Program.Kernel("doubleMatrixMult");
                        //Linear system (Gauss Seidel)
                        doubleGaussSeidel = new Program.Kernel("doubleGaussSeidel");
                        doubleGaussSeidelError = new Program.Kernel("doubleGaussSeidelError");
                        doubleCalcMtM = new Program.Kernel("doubleCalcMtM");
                        doubleCalcMtb = new Program.Kernel("doubleCalcMtb");

                        //Linear system (LU factorization)
                        doubleLUScale = new Program.Kernel("doubleLUScale");
                        doubleLUCalcBetas = new Program.Kernel("doubleLUCalcBetas");
                        doubleLUCalcAlphas = new Program.Kernel("doubleLUCalcAlphas");
                        doubleLUCalcPivo = new Program.Kernel("doubleLUCalcPivo");
                        doubleLUTrocaCols = new Program.Kernel("doubleLUTrocaCols");
                        doubleLUDivByPivot = new Program.Kernel("doubleLUDivByPivot");
                        doubleLUForwardSubs = new Program.Kernel("doubleLUForwardSubs");
                        doubleLUBackSubs = new Program.Kernel("doubleLUBackSubs");
                        doubleLUDivide = new Program.Kernel("doubleLUDivide");
                        doubleLUUnscramble = new Program.Kernel("doubleLUUnscramble");
                        doubleSolveError = new Program.Kernel("doubleSolveError");
                        doubleLUSubErr = new Program.Kernel("doubleLUSubErr");
                    }
                    catch
                    {
                        throw new Exception("Could not compile program");
                    }
                }
Beispiel #7
0
                /// <summary>Constructor.</summary>
                /// <param name="nMasses">Number of masses in the system</param>
                /// <param name="nConnections">Number of connections</param>
                /// <param name="Masses">Mass of each vertex</param>
                /// <param name="InitialStateSpace">Position and velocity of vertexes 
                /// [2*3*i] - posx, [2*(3*i+1)] - posy, [2*(3*i+2)] - posz, 
                /// [1+2*3*i] - velx, [1+2*(3*i+1)] - vely, [1+2*(3*i+2)] - velz</param>
                /// <param name="Origins">Origin vertex of connections. Spring connects Origin[i] to Dests[i]</param>
                /// <param name="Dests">Destination vertex of connections. Spring connects Origin[i] to Dests[i]</param>
                /// <param name="SpringKs">Spring constant for each connection</param>
                /// <param name="GroundKs">Spring constant for each mass, connecting to ground (nMass)</param>
                /// <param name="Damp">Structural damping (relative-speed dependant) (nConnections)</param>
                /// <param name="GroundDamp">Absolute damping proportional to speed relative to Earth (nMass)</param>
                public floatDEM(int nMasses, int nConnections,
                    float[] Masses, float[] InitialStateSpace,
                    int[] Origins, int[] Dests, float[] SpringKs, float[] Damp, float[] GroundKs, float[] GroundDamp)
                {
                    #region Consistency check
                    if (Masses.Length != nMasses)
                        throw new Exception("Invalid Masses length (!=nMasses)");
                    if (InitialStateSpace.Length != 6 * nMasses)
                        throw new Exception("Invalid positions length (!=6*nMasses - x, y, z)");
                    if (Origins.Length != nConnections)
                        throw new Exception("Invalid Origins length (!=nConnections)");
                    if (Dests.Length != nConnections)
                        throw new Exception("Invalid Dests length (!=nConnections)");
                    if (SpringKs.Length != nConnections)
                        throw new Exception("Invalid SpringKs length (!=nConnections)");
                    if (GroundKs.Length != nMasses)
                        throw new Exception("Invalid GroundKs length (!=nMasses)");
                    if (Damp.Length != nConnections)
                        throw new Exception("Invalid Damp length (!=nConnections)");
                    if (GroundDamp.Length != nMasses)
                        throw new Exception("Invalid GroundDamp length (!=nMasses)");
                    #endregion

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

                    #region Variables reading
                    //Sizes
                    nConn = new int[1] { nConnections };
                    nM = new int[1] { nMasses };

                    //Inputs
                    m = new Program.Variable(Masses);

                    float[] InitialPositions = new float[3 * nMasses];
                    for (int i = 0; i < 3 * nMasses; i++) InitialPositions[i] = InitialStateSpace[2 * i];

                    posOrig = new Program.Variable(InitialPositions);
                    origs = new Program.Variable(Origins);
                    dests = new Program.Variable(Dests);
                    k = new Program.Variable(SpringKs);
                    kGround = new Program.Variable(GroundKs);
                    c = new Program.Variable(Damp);
                    cGround = new Program.Variable(GroundDamp);

                    //Outputs
                    L0 = new Program.Variable(new float[nConnections]);
                    forces = new Program.Variable(new float[3 * nMasses]);
                    connForces = new Program.Variable(new float[3 * nConnections]);
                    nConnec = new Program.Variable(new int[1] { nConnections });

                    int[] nodesConnects = new int[30 * nMasses];
                    for (int i = 0; i < nodesConnects.Length; i++) nodesConnects[i] = -1;

                    nodesConnections = new Program.Variable(nodesConnects);
                    #endregion

                    #region Kernels initialization
                    DEMSource Source = new DEMSource();
                    string[] s = new string[] { Source.floatcalcL0, Source.floatresetForces, Source.floatcalcForces, Source.floatderivs, Source.floatcalcGroundForces, Source.floatcalcNodesConnections };
                    CLCalc.Program.Compile(s);

                    KernelcalcL0 = new Program.Kernel("floatcalcL0");
                    argscalcL0 = new Program.Variable[] { posOrig, origs, dests, L0 };

                    KernelcalcNodesConnections = new Program.Kernel("floatcalcNodesConnections");
                    argscalcNodesConnections = new Program.Variable[] { nodesConnections, nConnec, origs, dests };

                    KernelresetForces = new Program.Kernel("floatresetForces");
                    argsresetForces = new Program.Variable[] { forces };

                    KernelcalcForces = new Program.Kernel("floatcalcForces");
                    KernelcalcGroundForces = new Program.Kernel("floatcalcGroundForces");

                    Kernelderivs = new Program.Kernel("floatderivs");

                    #endregion

                    // Initial lengths calculation
                    KernelcalcL0.Execute(argscalcL0, nConn);

                    //Connections calculation
                    KernelcalcNodesConnections.Execute(argscalcNodesConnections, nM);

                    //nodesConnections.ReadFromDeviceTo(nodesConnects);
                }
Beispiel #8
0
 public float[] Add(float[] B, int startx, int starty, int startz, int Bx, int By)
 {
     float[] start = new float[3] { startx, starty, startz };
     float[] size = new float[6] { X, Y, Z, B.GetLength(0), Bx, By };
     float[] csResult = new float[X * Y * Z];
     /*In order to avoid OOM, will start at the startz value, and pass
     in one 2D matrix at a time.
      * */
     if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
         CLCalc.InitCL();
     if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
     {
         CLSource source = new CLSource();
         CLCalc.Program.Compile(new string[] { source.AddSubset_3string });
         Matrix = new CLCalc.Program.Kernel("AddSubset3");
     }
     CLCalc.Program.Variable dev_A = new CLCalc.Program.Variable(M);
     CLCalc.Program.Variable dev_B = new CLCalc.Program.Variable(B);
     CLCalc.Program.Variable dev_start = new CLCalc.Program.Variable(start);
     CLCalc.Program.Variable dev_size = new CLCalc.Program.Variable(size);
     CLCalc.Program.Variable dev_Result = new CLCalc.Program.Variable(csResult);
     CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[5] { dev_A, dev_B, dev_start, dev_size, dev_Result };
     Matrix.Execute(args, new int[] { X * Y * Z });
     dev_Result.ReadFromDeviceTo(csResult);
     M = csResult;
     return csResult;
 }
Beispiel #9
0
 private static void InitKernels()
 {
     string s = new CLFFTSrc().s;
     CLCalc.InitCL();
     try
     {
         CLCalc.Program.Compile(s);
     }
     catch
     {
     }
     kernelfft_radix16 = new CLCalc.Program.Kernel("fft_radix16");
     kernelfft_radix4 = new CLCalc.Program.Kernel("fft_radix4");
     kernelConjugate = new CLCalc.Program.Kernel("Conjugate");
     CLp = new CLCalc.Program.Variable(new int[1]);
 }
Beispiel #10
0
        private void frmCLInfo_Load(object sender, EventArgs e)
        {
            CLCalc.InitCL(ComputeDeviceTypes.All);

            if (CLCalc.CLAcceleration != CLCalc.CLAccelerationType.UsingCL)
            {
                cmbPlat.Items.Add("OpenCL ERROR");
                if (cmbPlat.Items.Count > 0) cmbPlat.SelectedIndex = 0;
            }
            else
            {
                foreach(ComputePlatform p in CLCalc.CLPlatforms)
                {
                    cmbPlat.Items.Add(p.Name + " " + p.Profile + " " + p.Vendor + " " + p.Version);
                }
                if (cmbPlat.Items.Count > 0) cmbPlat.SelectedIndex = 0;

                int i=0;
                foreach (ComputeDevice d in CLCalc.CLDevices)
                {
                    //if (d.CLDeviceAvailable)
                    //{
                        cmbDevices.Items.Add(d.Name + " " + d.Type + " " + d.Vendor + " " + d.Version);
                        cmbCurDevice.Items.Add(d.Name + " " + d.Type + " " + d.Vendor + " " + d.Version);
                    //}
                    //else
                    //{
                    //    cmbDevices.Items.Add("NOT AVAILABLE: " + d.CLDeviceName + " " + d.CLDeviceType + " " + d.CLDeviceVendor + " " + d.CLDeviceVersion);
                    //    cmbCurDevice.Items.Add("NOT AVAILABLE: " + d.CLDeviceName + " " + d.CLDeviceType + " " + d.CLDeviceVendor + " " + d.CLDeviceVersion);
                    //}

                    i++;
                }

                if (cmbDevices.Items.Count > 0)
                {
                    cmbDevices.SelectedIndex = 0;
                    cmbCurDevice.SelectedIndex = CLCalc.Program.DefaultCQ;
                }
            }

            ReadImportantRegistryEntries();

            //int[] n = new int[3] {1,1,1};
            //int[] nn = new int[3];
            //CLCalc.Program.Variable v = new CLCalc.Program.Variable(n);

            //v.WriteToDevice(n);

            //v.ReadFromDeviceTo(nn);

            string s = @" kernel void teste() {}";

            CLCalc.Program.Compile(s);
            try
            {
                CLCalc.Program.Kernel k = new CLCalc.Program.Kernel("teste");
            }
            catch
            {
                MessageBox.Show("");
            }
        }
Beispiel #11
0
        /// <summary>Compiles code and initializes kernel for this svm stance</summary>
        private void CLSVMInit()
        {
            if (CLResource == null) CLResource = new int[0];

            lock (CLResource)
            {
                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) CLCalc.InitCL();
                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
                {
                    if (kernelComputeKernelRBF == null)
                    {
                        CLSVMSrc s = new CLSVMSrc();
                        CLCalc.Program.Compile(new string[] { s.srcKernels, s.srcFindMaxMinErr, s.srcMultClass });

                        //Kernel computation
                        kernelComputeKernelRBF = new CLCalc.Program.Kernel("ComputeKernelRBF");

                        kernelMaxErr = new CLCalc.Program.Kernel("maxErr");
                        kernelComputeMax = new CLCalc.Program.Kernel("computeMax");
                        kernelMinErr = new CLCalc.Program.Kernel("minErr");
                        kernelComputeMin = new CLCalc.Program.Kernel("computeMin");
                        kernelGetResp = new CLCalc.Program.Kernel("getResp");

                        //Update error
                        kernelUpdateErr = new CLCalc.Program.Kernel("UpdateErr");

                        //Multiple classification
                        kernelComputeMultiKernelRBF = new CLCalc.Program.Kernel("ComputeMultiKernelRBF");
                        kernelSumKernels=new CLCalc.Program.Kernel("SumKernels");
                    }

                    //Memory obbjects

                    //Find max/min
                    CLErrLen = new CLCalc.Program.Variable(new int[1]);
                    HostResp = new int[1];
                    CLResp = new CLCalc.Program.Variable(HostResp);
                    CLMaxMinErrs = new CLCalc.Program.Variable(new float[MAXMINWORKSIZE]);
                    CLMaxMinInds = new CLCalc.Program.Variable(new int[MAXMINWORKSIZE]);
                    //Update error
                    CLUpdtErrParams = new CLCalc.Program.Variable(new float[3]);
                }
            }
        }
Beispiel #12
0
        private void button4_Click(object sender, EventArgs e)
        {
            nMassas = new int[1] { 10 };

            //Integrador de EDO
            float x0 = 0;
            float[] y0 = new float[2 * nMassas[0]];

            //y0[0]=posicao, y0[1] = velocidade
            for (int i = 0; i < 2 * nMassas[0]; i += 2)
            {
                y0[i] = 1;
            }

            CLCalc.CLPrograms.floatODE46 ode46 = new CLCalc.CLPrograms.floatODE46(x0, 0.005f, y0, MassaMola);

            //CLCalc.Program.DefaultCQ = 0;

            //Retorno de derivadas
            string[] s = new string[] {CLCalc.EnableDblSupport, floatDerivsMMola };
            CLCalc.Program.Compile(s);
            KernelDerivs = new CLCalc.Program.Kernel("floatDerivs");

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            ode46.Integrate(20);
            sw.Stop();
            this.Text = sw.ElapsedMilliseconds.ToString();
            Application.DoEvents();

            float[] y = ode46.State;

            ode46.Integrate(25);
            y = ode46.State;
            float[] yerr = ode46.AbsError;
            float x = ode46.IndepVar;
        }
Beispiel #13
0
        private void button3_Click(object sender, EventArgs e)
        {
            float[] x = new float[] { 1, 2, 3, 0.123f };
            float[] y = new float[] { 1, 2, 1, 1 };

            string s = @"
                       kernel void
                       sum (global float4 * x, global float4 * y)
                       {
                           x[0] = x[0] + y[0];
                       }
            ";

            CLCalc.Program.Compile(new string[] { s });
            CLCalc.Program.Kernel sum = new CLCalc.Program.Kernel("sum");

            CLCalc.Program.Variable varx=new CLCalc.Program.Variable(x);
            CLCalc.Program.Variable vary=new CLCalc.Program.Variable(y);
            CLCalc.Program.Variable[] args = { varx, vary };

            int[] max = new int[] { 1 };

            sum.Execute(args, max);

            varx.ReadFromDeviceTo(x);

            //float[] t = new float[1] { 0 };
            //float[] pos = new float[] { 1, 2, 3, 4, 5, 6 };
            //float[] vel = new float[] { 1, 0, 0, 0, 0, 0 };
            //float[] forces = new float[] { 0, 1, 0, 0, 0, 0 };

            //float[] masses = new float[] { 1, 1 };
            //float[] colSizes = new float[] { 0.1f, 0.1f };

            //CLCalc.InitCL();

            //CLCalc.CLPrograms.floatBodyPhysics phys = new CLCalc.CLPrograms.floatBodyPhysics(10);
            //CLCalc.CLPrograms.floatBodyPhysics phys2 = new CLCalc.CLPrograms.floatBodyPhysics(20);

            //CLCalc.FinishCL();
        }
Beispiel #14
0
        private void button1_Click(object sender, EventArgs e)
        {
            CLCalc.InitCL();

            double[] a = new double[] { 2, 147483647, 2, 7 };
            double[] b = new double[] { 1, 2, 7, 4 };
            double[] c = new double[4];

            CLCalc.Program.Variable v1 = new CLCalc.Program.Variable(a);
            CLCalc.Program.Variable v2 = new CLCalc.Program.Variable(b);
            CLCalc.Program.Variable v3 = new CLCalc.Program.Variable(c);

            CLCalc.CLPrograms.VectorSum VecSum = new CLCalc.CLPrograms.VectorSum();
            CLCalc.CLPrograms.MinDifs Mdifs = new CLCalc.CLPrograms.MinDifs();

            //string[] s = new string[] { VecSum.intVectorSum, VecSum.floatVectorSum };
            string[] s = new string[] { VecSum.doubleVectorSum };

            CLCalc.Program.Compile(s);

            CLCalc.Program.Kernel k = new CLCalc.Program.Kernel("doubleVectorSum");
            //CLCalc.Program.Kernel k2 = new CLCalc.Program.Kernel("intVectorSum");
            //CLCalc.Program.Kernel k = new CLCalc.Program.Kernel("floatMinDifs");

            CLCalc.Program.Variable[] vv = new CLCalc.Program.Variable[3] { v1, v2, v3 };

            int[] max=new int[1] {a.Length};

            k.Execute(vv, max);

            CLCalc.Program.Sync();

            v3.ReadFromDeviceTo(c);

            CLCalc.FinishCL();
        }
        static Kernels()
        {
            try
            {
                CLCalc.Program.Compile(src);
                CLCalc.Program.MemoryObject[] Args = new CLCalc.Program.MemoryObject[100]; ;
                int globalWorkSize = 4;

                // compile the kernels

                KernelStart = new CLCalc.Program.Kernel("KernelStart");
                coalesced = new CLCalc.Program.Kernel("coalesced");

                // run kernel start

                KernelStart.Execute(Args, globalWorkSize);
            }
            catch (NullReferenceException nre)
            {
                System.Console.WriteLine("" + nre);
            }

            //           System.Diagnostics.Debug.WriteLine("Hello");
        }
Beispiel #16
0
        /// <summary>Constructor.</summary>
        /// <param name="InitialState">Initial state of system</param>
        /// <param name="StepSize">Desired step per integration pass</param>
        /// <param name="InitialIndepVarValue">Initial independent variable value</param>
        /// <param name="DerivativeCalculator">Function to calculate derivatives vector</param>
        public doubleODE46(double InitialIndepVarValue, double StepSize, double[] InitialState, DerivCalcDeleg DerivativeCalculator)
        {
            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown)
            {
                CLCalc.InitCL();
            }

            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.NotUsingCL)
                throw new Exception("OpenCL not available");

            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
            {
                ODE46Source Source = new ODE46Source();
                string[] s = new string[] { @"
                            #pragma OPENCL EXTENSION cl_khr_fp64 : enable
                            ", Source.doubleStep2, Source.doubleStep3, Source.doubleStep4, Source.doubleStep5, Source.doubleStep6, Source.doubleFinalizeCalc };
                CLCalc.Program.Compile(s);

                //Calculador de derivada
                Derivs = DerivativeCalculator;

                //Scalars
                double[] xx = new double[1] { InitialIndepVarValue };
                x = new CLCalc.Program.Variable(xx);
                xsav = new CLCalc.Program.Variable(xx);

                //Sets initial values to Device and local variables
                hdid = new CLCalc.Program.Variable(xx);
                currentX = InitialIndepVarValue;
                SetStep(StepSize);

                //Vectors
                yy = new double[InitialState.Length];
                for (int i = 0; i < InitialState.Length; i++) yy[i] = InitialState[i];

                ysav = new CLCalc.Program.Variable(yy);
                k1 = new CLCalc.Program.Variable(InitialState);
                k2 = new CLCalc.Program.Variable(InitialState);
                k3 = new CLCalc.Program.Variable(InitialState);
                k4 = new CLCalc.Program.Variable(InitialState);
                k5 = new CLCalc.Program.Variable(InitialState);
                k6 = new CLCalc.Program.Variable(InitialState);
                absError = new CLCalc.Program.Variable(new double[InitialState.Length]);

                y = new CLCalc.Program.Variable(yy);

                //Kernels
                KernelFinalizeCalc = new CLCalc.Program.Kernel("doubleFinalizeCalc");
                KernelUpdateX = new CLCalc.Program.Kernel("doubleUpdateX");
                KernelRK46YStep2 = new CLCalc.Program.Kernel("doubleYStep2");
                KernelRK46XStep2 = new CLCalc.Program.Kernel("doubleXStep2");
                KernelRK46YStep3 = new CLCalc.Program.Kernel("doubleYStep3");
                KernelRK46XStep3 = new CLCalc.Program.Kernel("doubleXStep3");
                KernelRK46YStep4 = new CLCalc.Program.Kernel("doubleYStep4");
                KernelRK46XStep4 = new CLCalc.Program.Kernel("doubleXStep4");
                KernelRK46YStep5 = new CLCalc.Program.Kernel("doubleYStep5");
                KernelRK46XStep5 = new CLCalc.Program.Kernel("doubleXStep5");
                KernelRK46YStep6 = new CLCalc.Program.Kernel("doubleYStep6");
                KernelRK46XStep6 = new CLCalc.Program.Kernel("doubleXStep6");

                //Kernel arguments
                ArgsFinalize = new CLCalc.Program.Variable[] { x, hdid, y, ysav, absError, k1, k2, k3, k4, k5, k6 };
                ArgsRK46Y = new CLCalc.Program.Variable[] { x, hdid, y, ysav, k1, k2, k3, k4, k5, k6 };
                ArgsRK46X = new CLCalc.Program.Variable[] { x, hdid, xsav };
                NStates = new int[1] { InitialState.Length };
                NScalar = new int[1] { 1 };

                //Data retrieving
                yerr = new double[NStates[0]];
                xRet = new double[NScalar[0]];

            }
        }
Beispiel #17
0
                /// <summary>Initializes physics program. Components indexes: [i] - x, [i+1] - y, [i+2] - z</summary>
                /// <param name="nParticles">Number of particles</param>
                public floatBodyPhysics(int nParticles)
                {
                    string[] s = new string[] { CollisionAppliers, ForceAppliers, ConstAccelMotionEDOSolver };
                    Program.Compile(s);
                    //Kernels
                    MotionStep = new Program.Kernel("constAccelStep");
                    Kernel_ApplyGravity = new Program.Kernel("ApplyGravity");
                    Kernel_FloorCollision = new Program.Kernel("FloorCollision");
                    Kernel_SelfCollision = new Program.Kernel("SelfCollision");
                    Kernel_WallCollision = new Program.Kernel("WallCollision");
                    Kernel_ResetForces = new Program.Kernel("ResetForces");
                    Kernel_ResetCloseNeighbors = new Program.Kernel("ResetCloseNeighbors");

                    float[] t = new float[1] { 0 };
                    float[] gg = new float[3] { 0, 0, 0 };
                    step = new float[1] { 0 };
                    //Tamanho de alocacao de velocidades e posicoes
                    float[] aloc = new float[nParticles * 3];

                    //Tamanho de alocacao de caracteristicas das particulas
                    float[] alocPart = new float[nParticles];
                    //3*Nparticulas
                    CL_pos = new CLCalc.Program.Variable(aloc);
                    CL_vel = new CLCalc.Program.Variable(aloc);
                    CL_forces = new CLCalc.Program.Variable(aloc);
                    //Nparticulas
                    closeNeighbors = new int[nParticles];
                    CL_closeNeighbors = new CLCalc.Program.Variable(closeNeighbors);
                    for (int i = 0; i < nParticles; i++) alocPart[i] = 1f; //inicializa massas como 1 e tamanhos de colisao como 1
                    CL_masses = new CLCalc.Program.Variable(alocPart);
                    CL_collisionSizes = new CLCalc.Program.Variable(alocPart);

                    //escalares
                    CL_t = new CLCalc.Program.Variable(t);
                    CL_step = new CLCalc.Program.Variable(step);

                    //gravidade
                    CL_g = new CLCalc.Program.Variable(gg);

                    //Argumentos de funcoes
                    stepArgs = new CLCalc.Program.Variable[] { CL_t, CL_step, CL_forces, CL_masses, CL_pos, CL_vel };
                    applyGravArgs = new CLCalc.Program.Variable[] { CL_forces, CL_masses, CL_g };
                    floorCollisionArgs = new CLCalc.Program.Variable[] { CL_vel, CL_pos, CL_collisionSizes };
                    wallCollisionArgs = floorCollisionArgs;
                    selfCollisionArgs = new CLCalc.Program.Variable[] { CL_vel, CL_pos, CL_masses, CL_forces, CL_closeNeighbors, CL_collisionSizes };
                    resetForcesArgs = new Program.Variable[] { CL_forces };
                    resetCloseNeighborsArgs = new Program.Variable[] { CL_closeNeighbors };

                    nArgs = new int[1] { nParticles * 3 };
                    nPartics = new int[1] { nParticles };
                    nPartics2 = new int[2] { nParticles, nParticles };
                }
Beispiel #18
0
        /// <summary>Creates a new isosurface calculator. You may pass variables created from a OpenGL context to the CL variables if you are using interop or NULL
        /// if not using OpenCL/GL interop.</summary>
        /// <param name="FuncValues">Values of the evaluated 3D function f(x,y,z). FuncValues=float[maxX,maxY,maxZ]</param>
        /// <param name="CLEdgeCoords">OpenCL variable (float) to hold edge coordinates. Dimension has to be 9 * maxX * maxY * maxZ</param>
        /// <param name="CLEdgeNormals">OpenCL variable (float) to hold edge normals. Dimension has to be 9 * maxX * maxY * maxZ</param>
        /// <param name="CLElementArrayIndex">OpenCL variable (int) to hold element array index. Dimension has to be 5 * 3 * (maxX - 1) * (maxY - 1) * (maxZ - 1)</param>
        private void InitMarchingCubes(float[, ,] FuncValues, CLCalc.Program.Variable CLEdgeCoords, CLCalc.Program.Variable CLEdgeNormals, CLCalc.Program.Variable CLElementArrayIndex)
        {
            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) CLCalc.InitCL();

            if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
            {
                //Reads maximum lengths
                int maxX = FuncValues.GetLength(0);
                int maxY = FuncValues.GetLength(1);
                int maxZ = FuncValues.GetLength(2);
                max = new int[] { maxX, maxY, maxZ };

                #region Creating variables

                //Isolevel
                isoLevel = new float[1] { 1.32746E-5f };
                varIsoLevel = new CLCalc.Program.Variable(isoLevel);

                //Step size and x0,y0,z0
                varStep = new CLCalc.Program.Variable(step);
                varInitVals = new CLCalc.Program.Variable(initVals);

                //Create and copy function values
                funcVals = new float[maxX * maxY * maxZ];
                CLFuncVals = new CLCalc.Program.Variable(funcVals);
                SetFuncVals(FuncValues);

                //Edge coordinates - 3 coords * 3 possible directions * number of points
                edgeCoords = new float[9 * maxX * maxY * maxZ];
                if (CLEdgeCoords != null)
                {
                    varEdgeCoords = CLEdgeCoords;
                    varEdgeCoords.WriteToDevice(edgeCoords);
                }
                else varEdgeCoords = new CLCalc.Program.Variable(edgeCoords);

                //4 preliminary normals per edge - has to be averaged afterwards
                edgePrelimNormals = new float[36 * maxX * maxY * maxZ];
                varEdgePrelimNormals = new CLCalc.Program.Variable(edgePrelimNormals);

                //Edge normals
                edgeNormals = new float[9 * maxX * maxY * maxZ];
                if (CLEdgeNormals != null)
                {
                    varEdgeNormals = CLEdgeNormals;
                    varEdgeNormals.WriteToDevice(edgeNormals);
                }
                else varEdgeNormals = new CLCalc.Program.Variable(edgeNormals);

                //Number of cubes: (maxX-1)*(maxY-1)*(maxZ-1)
                //Marching cube algorithm: each cube can have 5 triangles drawn, 3 vertexes per triangle
                //q-th vertex of p-th triangle of the ijk-th cube: [(5*(i+(maxX-1)*j+k*(maxX-1)*(maxY-1))+p)*3+q]
                elementIndex = new int[5 * 3 * (maxX - 1) * (maxY - 1) * (maxZ - 1)];
                if (CLElementArrayIndex != null)
                {
                    varElemIndex = CLElementArrayIndex;
                    varElemIndex.WriteToDevice(elementIndex);
                }
                else varElemIndex = new CLCalc.Program.Variable(elementIndex);

                //Edge remapping to build output
                edges = new int[edgeCoords.Length / 3];
                for (int i = 0; i < edges.Length; i++) edges[i] = -1;

                #endregion

                #region Compile code and create kernels

                CLMarchingCubesSrc cmsrc = new CLMarchingCubesSrc();

                CLCalc.Program.Compile(new string[] { cmsrc.definitions, cmsrc.src });
                kernelInterpPts = new CLCalc.Program.Kernel("interpPts");
                kernelPolygonize = new CLCalc.Program.Kernel("Polygonize");
                kernelSmoothNormals = new CLCalc.Program.Kernel("SmoothNormals");
                kernelPolygonizeNoNormals = new CLCalc.Program.Kernel("PolygonizeNoNormals");
                #endregion
            }
            else throw new Exception("OpenCL not available");
        }
Beispiel #19
0
        /// <summary>Initializes CL kernels</summary>
        public static void Init()
        {
            if (kernelCholeskyDiagBlock == null)
            {
                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.Unknown) CLCalc.InitCL();

                if (CLCalc.CLAcceleration == CLCalc.CLAccelerationType.UsingCL)
                {
                    if (kernelCholeskyDiagBlock == null)
                    {
                        SUBMATRIXSIZE = (int)Math.Sqrt((double)CLCalc.Program.CommQueues[CLCalc.Program.DefaultCQ].Device.MaxWorkGroupSize);
                        SUBMATRIXSIZE = Math.Min(16, SUBMATRIXSIZE);

                        string strSubSize = SUBMATRIXSIZE.ToString();
                        string strTotSize = (SUBMATRIXSIZE * (SUBMATRIXSIZE + 1) / 2).ToString();

                        LinalgSrc src = new LinalgSrc();
                        string srcBlockChol = src.srcBlockCholesky.Replace("CONSTSUBMATRIXSIZE", strSubSize).Replace("CONSTGLOBALSIZE", strTotSize);
                        CLCalc.Program.Compile(new string[] { srcBlockChol, src.srcBkSubs, src.srcOperations, src.srcVecSum,
                            src.srcpNorm, src.strFeasibFunc, src.srcLogistReg });

                        kernelCholeskyDiagBlock = new CLCalc.Program.Kernel("CholeskyDiagBlock");
                        kernelCholeskyComputePanel = new CLCalc.Program.Kernel("CholeskyComputePanel");
                        kernelCholeskyForwardProp = new CLCalc.Program.Kernel("CholeskyForwardProp");

                        kernelFwdUpperBackSubs = new CLCalc.Program.Kernel("FwdUpperBackSubs");
                        kernelBkLowerBackSubs = new CLCalc.Program.Kernel("BkLowerBackSubs");
                        kernelFwdPropag = new CLCalc.Program.Kernel("FwdPropag");
                        kernelFwdPropag2 = new CLCalc.Program.Kernel("FwdPropag2");
                        kernelBackPropag = new CLCalc.Program.Kernel("BackPropag");
                        kernelBackPropag2 = new CLCalc.Program.Kernel("BackPropag2");

                        kernelInPlaceSubtract = new CLCalc.Program.Kernel("InPlaceSubtract");
                        kernelElemWiseAbs = new CLCalc.Program.Kernel("ElemWiseAbs");

                        kernelInnerProd = new CLCalc.Program.Kernel("InnerProd");

                        //Linear algebra
                        kernelSymMatrVecMultiply = new CLCalc.Program.Kernel("SymMatrVecMultiply");
                        kernelSymMatrMatrMultiply = new CLCalc.Program.Kernel("SymMatrMatrMultiply");
                        kernelComputeAtWA = new CLCalc.Program.Kernel("ComputeAtWA");
                        kernelComputeAinvHAt = new CLCalc.Program.Kernel("ComputeAinvHAt");
                        kernelRegularMatrTranspMatrProd = new CLCalc.Program.Kernel("RegularMatrTranspMatrProd");

                        kernelCopyBuffer = new CLCalc.Program.Kernel("CopyBuffer");
                        kernelLinearComb = new CLCalc.Program.Kernel("LinearComb");
                        kernelMatrVecProd = new CLCalc.Program.Kernel("MatrVecProd");
                        kernelTranspMatrVecProdW = new CLCalc.Program.Kernel("TranspMatrVecProdW");
                        kernelMatrVecProdSumVec = new CLCalc.Program.Kernel("MatrVecProdSumVec");
                        kernelDiagVecProd = new CLCalc.Program.Kernel("DiagVecProd");
                        kernelDiagTranspMatProd = new CLCalc.Program.Kernel("DiagTranspMatProd");
                        kernelElemWiseProd = new CLCalc.Program.Kernel("ElemWiseProd");
                        kernelElemWiseInv = new CLCalc.Program.Kernel("ElemWiseInv");
                        kernelElemWiseInv2 = new CLCalc.Program.Kernel("ElemWiseInv2");

                        kernelClear = new CLCalc.Program.Kernel("ClearResps");
                        kernelPreSum = new CLCalc.Program.Kernel("PreSum");
                        kernelCoalLocalSum = new CLCalc.Program.Kernel("CoalLocalSum");

                        kernelHasPositiveEntry = new CLCalc.Program.Kernel("HasPositiveEntry");

                        //pNorm minimization
                        floatOptimization.CurveFitting.kernelpNorm = new CLCalc.Program.Kernel("pNorm");
                        floatOptimization.CurveFitting.kerneldpNorm = new CLCalc.Program.Kernel("dpNorm");

                        //Logistic regression
                        floatOptimization.LogisticRegression.kernelComputeLogistRegParams = new CLCalc.Program.Kernel("ComputeLogistRegParams");
                        floatOptimization.LogisticRegression.kernelpNorm = floatOptimization.CurveFitting.kernelpNorm;
                        floatOptimization.LogisticRegression.kerneldpNorm = floatOptimization.CurveFitting.kerneldpNorm;

                        //Feasibility
                        floatOptimization.QuadraticProgramming.kernelgetLast = new CLCalc.Program.Kernel("getLast");
                    }
                }
            }
        }