* OpenCL/VectorAdd.cs: Use hCmdQueue instead of hContext and added

missing CL.EnqueueWriteBuffer calls (patch by mike vasiljevs, fixes
  issue [#1233]: "OpenCL - AddVector.cs - example corrections").
Output calculation results.
This commit is contained in:
the_fiddler 2009-10-19 17:45:50 +00:00
parent ac404e35fb
commit e905452c54

View file

@ -13,7 +13,7 @@ namespace Examples
using cl_mem = IntPtr; using cl_mem = IntPtr;
[Example("Vector Addition", ExampleCategory.OpenCL, "1.0")] [Example("Vector Addition", ExampleCategory.OpenCL, "1.0")]
class FFT class VectorAdd
{ {
public static void Main() public static void Main()
{ {
@ -98,10 +98,18 @@ vectorAdd(__global const float * a,
CL.SetKernelArg(hKernel, 1, new IntPtr(sizeof(cl_mem)), new IntPtr(&hDeviceMemB)); CL.SetKernelArg(hKernel, 1, new IntPtr(sizeof(cl_mem)), new IntPtr(&hDeviceMemB));
CL.SetKernelArg(hKernel, 2, new IntPtr(sizeof(cl_mem)), new IntPtr(&hDeviceMemC)); CL.SetKernelArg(hKernel, 2, new IntPtr(sizeof(cl_mem)), new IntPtr(&hDeviceMemC));
// write data from host to device
CL.EnqueueWriteBuffer(hCmdQueue, hDeviceMemA, true, IntPtr.Zero,
new IntPtr(cnDimension.ToInt32() * sizeof(float)),
new IntPtr(pA), 0, null, (IntPtr[])null);
CL.EnqueueWriteBuffer(hCmdQueue, hDeviceMemB, true, IntPtr.Zero,
new IntPtr(cnDimension.ToInt32() * sizeof(float)),
new IntPtr(pB), 0, null, (IntPtr[])null);
// execute kernel // execute kernel
CL.EnqueueNDRangeKernel(hCmdQueue, hKernel, 1, null, &cnDimension, null, 0, null, null); CL.EnqueueNDRangeKernel(hCmdQueue, hKernel, 1, null, &cnDimension, null, 0, null, null);
// copy results from device back to host // copy results from device back to host
CL.EnqueueReadBuffer(hContext, hDeviceMemC, true, IntPtr.Zero, 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);
@ -110,6 +118,11 @@ vectorAdd(__global const float * a,
CL.ReleaseMemObject(hDeviceMemC); CL.ReleaseMemObject(hDeviceMemC);
} }
} }
for (int i = 0; i < A.Length; i++)
{
System.Diagnostics.Trace.WriteLine(String.Format("{0} + {1} = {2}", A[i], B[i], C[i]));
}
} }
} }
} }