C#C
C#3y ago
InsertPi

✅ Modify delegate for ILGPU acceptance

This is a long one, strap in!

So I’m incorporating ILGPU into a library I’m writing for very simple but powerful/expressive/flexible parallel programming. ILGPU is gonna help power the upcoming GPU API.

With that said, ILGPU accepts either a delegate or a static method as a GPU kernel. The method called has to incorporate an instantiation of the ILGPU.IIndex interface as the first parameter. For the sake of my API, I would like to avoid passing around ILGPU datatypes to delegates I accept, because that exposes implementation details to users of my library.

With that the background out of the way, ILGPU kernels, if they are a delegate, do not support captures or objects. This poses a problem for me, since I need to pass a delegate to the ILGPU library with an IIndex but abstract that away in my API. This problem exists with other parameters, too.

What I want to be able to do is have something like:
delegate<Index1D, ArrayView<T>, void> kernel = (idx, av) =>
{
    int i = idx.X; //simple example
    var g = new GPUArray(av);
    action_received_from_user(i, g);
}
but the problem here is that the kernel delegate captures action_received_from_user, which ILGPU doesn’t allow.

I tried passing in a struct as one of the kernel arguments that contains action_received_from_user as a member, but because delegates are managed types, they cannot be passed in as a kernel argument.

What I am thinking of is one of the following:
  1. A way to on-the-fly inline (inline in the C/C++ sense) action_received_from_user into my kernel body.
  2. A way to use Mono.Cecil or some other reflection API to manually inject CIL into the method body and change the argument types (I’m very familiar with CIL, less so with reflection)
  3. Use Roslyn -> ??? -> Profit?
  4. Some other way to accomplish this.
Phew, that was long. I’ve been trying to think of solutions for days and I have tried just about everything under the sun, but none of them have worked. Any ideas?
Was this page helpful?