示例#1
0
        /// <summary>
        /// Multiply an array by a constant value
        /// </summary>
        internal static void Multiply()
        {
            try
            {
                CLError err = new CLError();
                //Allocate memory
                float[]  arr          = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                GCHandle arrGC        = GCHandle.Alloc(arr, GCHandleType.Pinned);
                CLMem    bufferFilter = OpenCLDriver.clCreateBuffer(ctx, CLMemFlags.ReadWrite | CLMemFlags.CopyHostPtr, new SizeT(arr.Length * sizeof(float)), arrGC.AddrOfPinnedObject(), ref err);

                //set parameters
                unsafe
                {
                    OpenCLDriver.clSetKernelArg(kernel_mult, 0, new SizeT(sizeof(CLMem)), ref bufferFilter);
                }

                //Define grid & execute
                err = OpenCLDriver.clFinish(comQ);
                SizeT[] localws  = { 5, 1 };
                SizeT[] globalws = { 10, 1 };
                CLEvent eventObj = new CLEvent();
                err = OpenCLDriver.clEnqueueNDRangeKernel(comQ, kernel_mult, 2, null,
                                                          globalws, localws, 0, null, ref eventObj);
                CLEvent[] eventObjs = new CLEvent[1];
                eventObjs[0] = eventObj;
                OpenCLDriver.clWaitForEvents(1, eventObjs);
                // read buffer
                err = OpenCLDriver.clEnqueueReadBuffer(comQ, bufferFilter, CLBool.True, 0,
                                                       10 * sizeof(float), arrGC.AddrOfPinnedObject(), 0, null, ref eventObj);

                TableForm.ShowTableStatic(arr, "OpenCL Test");
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }