static void Main(string[] args)
        {
            //Generate the arrays for the demo
            const int arrayLength = 20;

            int[] a = new int[arrayLength];
            int[] b = new int[arrayLength];
            int[] c = new int[arrayLength]; //this will be the array storing the sum result
            for (int i = 0; i < arrayLength; i++)
            {
                a[i] = i;
                b[i] = i;
            }

            //initialize the OpenCL object
            OpenCL.OpenCL cl = new OpenCL.OpenCL();
            cl.Accelerator = AcceleratorDevice.GPU;

            //Load the kernel from the file into a string
            string kernel = File.ReadAllText("kernel.cl");

            //Specify the kernel code (in our case in the variable kernel) and which function from the kernel file we intend to call
            cl.SetKernel(kernel, "addArray");

            //Specify the parameters in the same order as in the function definition
            cl.SetParameter(a, b, c);

            /*
             * Specify the number of worker threads,
             * in our case the same as the number of elements since every threads processes only one element,
             * and launch the code on the GPU
             */
            cl.Execute(arrayLength);


            Console.WriteLine("Done...");
            for (int i = 0; i < arrayLength; i++)
            {
                Console.WriteLine(c[i].ToString()); //print the result on screen
            }
            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            OpenCL.OpenCL cl = new OpenCL.OpenCL();
            cl.Accelerator = AcceleratorDevice.GPU;

            string kernel = File.ReadAllText(@"../../../kernel.cl");

            const int aX = 2000, aY = 2000, bX = 2000, bY = 2000; //x=number of lines, y = number of columns

            double[] a          = new double[aX * aY];
            double[] b          = new double[bX * bY];
            double[] c          = new double[aX * bY]; // resulting matrix, with aX*bY dimensions
            int[]    dimensions = new int[3] {
                aX, aY, bY
            };

            Random rand = new Random();

            for (int i = 0; i < aX; i++)
            {
                for (int j = 0; j < aY; j++)
                {
                    a[i * aY + j] = rand.NextDouble();
                }
            }
            for (int i = 0; i < bX; i++)
            {
                for (int j = 0; j < bY; j++)
                {
                    b[i * bY + j] = rand.NextDouble();
                }
            }

            cl.SetKernel(kernel, "MatrixMulti");
            //Pass the dimensions and matrix pointers:
            cl.SetParameter(dimensions, a, b, c);
            //Each cell of the result will be computed at the same time
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            cl.Execute(aX * bY);
            stopwatch.Stop();

            Console.WriteLine("GPU parallel multiplication done in " + stopwatch.ElapsedMilliseconds / 1000.00 + " seconds");
            for (int i = 0; i < aX; i++)
            {
                for (int j = 0; j < bY; j++)
                {
                    //Console.Write(c[i*bY + j] + " ");
                }
                //Console.WriteLine();
            }


            //Run linearily on CPU:
            stopwatch.Reset();
            stopwatch.Start();
            for (int id = 0; id < aX * bY; id++)
            {
                int NLin_1 = dimensions[0]; //number of lines of first matrix
                int NCol_1 = dimensions[1]; //number of columns of first matrix
                int NCol_2 = dimensions[2]; //number of columns of second matrix

                int L = id / NCol_2;        //get the position in the final matrix from the id
                int C = id - L * NCol_2;

                double element = 0;
                for (int i = 0; i < NCol_1; i++)
                {
                    element = element + a[L * NCol_1 + i] * b[C + NCol_2 * i];
                }
                c[id] = element;
            }
            stopwatch.Stop();
            Console.WriteLine("CPU linear multiplication done in " + stopwatch.ElapsedMilliseconds / 1000.00 + " seconds");
            stopwatch.Reset();
            stopwatch.Start();
            Parallel.For(0, aX * bY, id =>
            {
                int NLin_1 = dimensions[0]; //number of lines of first matrix
                int NCol_1 = dimensions[1]; //number of columns of first matrix
                int NCol_2 = dimensions[2]; //number of columns of second matrix

                int L = id / NCol_2;        //get the position in the final matrix from the id
                int C = id - L * NCol_2;

                double element = 0;
                for (int i = 0; i < NCol_1; i++)
                {
                    element = element + a[L * NCol_1 + i] * b[C + NCol_2 * i];
                }
                c[id] = element;
            }
                         );

            stopwatch.Stop();
            Console.WriteLine("CPU parallel multiplication done in " + stopwatch.ElapsedMilliseconds / 1000.00 + " seconds");

            Console.ReadKey();
        }
 Task Enqueue(int from, int to, OpenCL acc)
 {
     return(Task.Run(() => acc.Execute(from, to - from, -1)));
 }