Beispiel #1
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 #2
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 #3
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();
        }
        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 #5
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();
        }