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.
This commit is contained in:
the_fiddler 2009-10-28 14:54:20 +00:00
parent 2ca02070a5
commit 7fbfd334c2

View file

@ -17,7 +17,7 @@ namespace Examples
{ {
public static void Main() public static void Main()
{ {
const int cnBlockSize = 512; const int cnBlockSize = 4;
const int cnBlocks = 3; const int cnBlocks = 3;
IntPtr cnDimension = new IntPtr(cnBlocks * cnBlockSize); IntPtr cnDimension = new IntPtr(cnBlocks * cnBlockSize);
string sProgramSource = @" string sProgramSource = @"
@ -66,8 +66,12 @@ vectorAdd(__global const float * a,
float[] B = new float[cnDimension.ToInt32()]; float[] B = new float[cnDimension.ToInt32()];
float[] C = new float[cnDimension.ToInt32()]; float[] C = new float[cnDimension.ToInt32()];
// initialize host memory // initialize host memory
// randomInit(pA, cnDimension); Random rand = new Random();
//randomInit(pB, cnDimension); for (int i = 0; i < A.Length; i++)
{
A[i] = rand.Next() % 256;
B[i] = rand.Next() % 256;
}
// allocate device memory // allocate device memory
unsafe unsafe
@ -107,11 +111,19 @@ vectorAdd(__global const float * a,
new IntPtr(pB), 0, null, (IntPtr[])null); new IntPtr(pB), 0, null, (IntPtr[])null);
// execute kernel // 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 // 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(cnDimension.ToInt32() * sizeof(float)),
new IntPtr(pC), 0, null, (IntPtr[])null); new IntPtr(pC), 0, null, (IntPtr[])null);
if (error != ErrorCode.Success)
throw new Exception(error.ToString());
CL.Finish(hCmdQueue);
CL.ReleaseMemObject(hDeviceMemA); CL.ReleaseMemObject(hDeviceMemA);
CL.ReleaseMemObject(hDeviceMemB); CL.ReleaseMemObject(hDeviceMemB);