internal void InitializeOpenCL()
{
// only initialize once
if (clKernel != null)
return;
// unused memory so Cloo doesn't break with a null ptr
var userDataPtr = Marshal.AllocCoTaskMem(512);
try
{
clDevice = Gpu.CLDevice;
// context we'll be working underneath
clContext = new ComputeContext(
new[] { clDevice },
new ComputeContextPropertyList(clDevice.Platform),
(p1, p2, p3, p4) => { },
userDataPtr);
// queue to control device
clQueue = new ComputeCommandQueue(clContext, clDevice, ComputeCommandQueueFlags.None);
// buffers to store kernel output
clBuffer0 = new ComputeBuffer<uint>(clContext, ComputeMemoryFlags.ReadOnly, 16);
clBuffer1 = new ComputeBuffer<uint>(clContext, ComputeMemoryFlags.ReadOnly, 16);
// obtain the program
clProgram = new ComputeProgram(clContext, Gpu.GetSource());
var b = new StringBuilder();
if (Gpu.WorkSize > 0)
b.Append(" -D WORKSIZE=").Append(Gpu.WorkSize);
if (Gpu.HasBitAlign)
b.Append(" -D BITALIGN");
if (Gpu.HasBfiInt)
b.Append(" -D BFIINT");
try
{
// build kernel for device
clProgram.Build(new[] { clDevice }, b.ToString(), (p1, p2) => { }, userDataPtr);
}
catch (ComputeException)
{
throw new Exception(clProgram.GetBuildLog(clDevice));
}
clKernel = clProgram.CreateKernel("search");
}
finally
{
Marshal.FreeCoTaskMem(userDataPtr);
}
}