From fca4dde7cf4069b4a47dacf7d929e98d83928a38 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Wed, 28 Oct 2009 14:54:20 +0000 Subject: [PATCH] Initialize input vectors to random values. Use smaller data blocks that can be verified visually. Perform error checking when executing the kernel. Wait for the command queue to finish before displaying the results. --- Source/Examples/OpenCL/VectorAdd.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Source/Examples/OpenCL/VectorAdd.cs b/Source/Examples/OpenCL/VectorAdd.cs index 04227d9b..d2cd1289 100644 --- a/Source/Examples/OpenCL/VectorAdd.cs +++ b/Source/Examples/OpenCL/VectorAdd.cs @@ -17,7 +17,7 @@ namespace Examples { public static void Main() { - const int cnBlockSize = 512; + const int cnBlockSize = 4; const int cnBlocks = 3; IntPtr cnDimension = new IntPtr(cnBlocks * cnBlockSize); string sProgramSource = @" @@ -66,8 +66,12 @@ vectorAdd(__global const float * a, float[] B = new float[cnDimension.ToInt32()]; float[] C = new float[cnDimension.ToInt32()]; // initialize host memory - // randomInit(pA, cnDimension); - //randomInit(pB, cnDimension); + Random rand = new Random(); + for (int i = 0; i < A.Length; i++) + { + A[i] = rand.Next() % 256; + B[i] = rand.Next() % 256; + } // allocate device memory unsafe @@ -107,11 +111,19 @@ vectorAdd(__global const float * a, new IntPtr(pB), 0, null, (IntPtr[])null); // execute kernel - CL.EnqueueNDRangeKernel(hCmdQueue, hKernel, 1, null, &cnDimension, null, 0, null, null); + error = (ErrorCode)CL.EnqueueNDRangeKernel(hCmdQueue, hKernel, 1, null, &cnDimension, null, 0, null, null); + if (error != ErrorCode.Success) + throw new Exception(error.ToString()); + // copy results from device back to host - CL.EnqueueReadBuffer(hCmdQueue, hDeviceMemC, true, IntPtr.Zero, + IntPtr event_handle = IntPtr.Zero; + error = (ErrorCode)CL.EnqueueReadBuffer(hCmdQueue, hDeviceMemC, true, IntPtr.Zero, new IntPtr(cnDimension.ToInt32() * sizeof(float)), new IntPtr(pC), 0, null, (IntPtr[])null); + if (error != ErrorCode.Success) + throw new Exception(error.ToString()); + + CL.Finish(hCmdQueue); CL.ReleaseMemObject(hDeviceMemA); CL.ReleaseMemObject(hDeviceMemB);