I hate to necrobump something but I’ve found this thread to be a great use for a very unused and little documented feature.
In sum, copy the Cloo.DLL to your project, then add the lines above to /Unity/Unity.app/Contents/Frameworks/Mono/etc/mono/config and then you’re good to go, whether it’s Cloo or not, as the DLLs are provided by OSX (obviously, since the CL libs are provided by OSX, you can just as easily add, for example, Lumial instead of Cloo).
Keep in mind that printf is not part of OpenCL, it’s a host convenience function. So if you want to test things out use something like this:
class ClooTest (MonoBehaviour):
kernel_code as string
def Awake():
kernel_code = """
kernel void VectorAdd(global read_only float* a, global read_only float* b, global write_only float* c) {
int index = get_global_id(0);
c[index] = a[index] + b[index];
}
"""
def Start():
Debug.Log("BEGIN CLOO")
platform_info()
vector_addition()
def platform_info():
platform_count = ComputePlatform.Platforms.Count
Debug.Log("Found $platform_count platforms")
for p as ComputePlatform in ComputePlatform.Platforms:
Debug.Log("Found platform: $(p.Name)")
for device in p.Devices:
Debug.Log(" Device: $(device.Name)")
def vector_addition():
count = 10
array_a = array(single, count)
array_b = array(single, count)
array_c = array(single, count)
rand = System.Random()
for i in range(count):
array_a[i] = rand.NextDouble() * 100
array_b[i] = rand.NextDouble() * 100
platform as ComputePlatform = ComputePlatform.Platforms[0]
context as ComputeContext = ComputeContext(ComputeDeviceTypes.Gpu, ComputeContextPropertyList(platform), null, IntPtr.Zero)
a as ComputeBuffer[of single] = ComputeBuffer[of single](context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, array_a)
b as ComputeBuffer[of single] = ComputeBuffer[of single](context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, array_b)
c as ComputeBuffer[of single] = ComputeBuffer[of single](context, ComputeMemoryFlags.WriteOnly, array_c.Length)
program = ComputeProgram(context, kernel_code)
program.Build(null, null, null, IntPtr.Zero)
kernel = program.CreateKernel("VectorAdd")
kernel.SetMemoryArgument(0, a)
kernel.SetMemoryArgument(1, b)
kernel.SetMemoryArgument(2, c)
event_list = ComputeEventList()
commands = ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None)
# j as (long) = (count cast long)
j = array(long, 1)
j[0] = count
commands.Execute(kernel, null, j, null, event_list)
commands.ReadFromBuffer(c, array_c, false, event_list)
commands.Finish()
i = count - 1
Debug.Log("$(array_a[i]) + $(array_b[i]) = $(array_c[i])")
It’s all QUITE manual, but, it does work. Pretty easily, really.