Giter Site home page Giter Site logo

OpenCL Backend: Argument Mapping about ilgpu HOT 2 CLOSED

m4rs-mt avatar m4rs-mt commented on May 18, 2024
OpenCL Backend: Argument Mapping

from ilgpu.

Comments (2)

m4rs-mt avatar m4rs-mt commented on May 18, 2024

Unfortunately, it is not possible to pass (global) pointers to OpenCL buffers within structures, since they require a user-defined driver-specific argument mapping (interested readers should refer to this post). In other words, a C# structure

public struct Foo
{
    public ArrayView<int> Data;
    public long SomeField;
}

cannot simply be passed to a kernel by creating a marshalling OpenCL structure (and a corresponding host structure in managed code) View_Int:

struct View_Int
{
    __global int *ptr;
    int length;
}

struct Foo
{
    struct View Data;
    long SomeField;
}

One solution to this problem is to extract all nested ArrayView<> values from the arguments of a kernel. We should extract all source view objects (i.e. CLBuffer instances) and map their values separately with distinct calls to clSetKernelArg. After bringing the buffer pointers into the OpenCL world, we can reconstruct the actual view structures to simulate the intended behavior. Consider the following ILGPU kernel program:

void Kernel(Index index, ArrayView<int> source, Foo foo)
{
     // ...
}

As previously mentioned, this kernel cannot be represented in OpenCL via:

__kernel void Kernel(struct View_Int source, struct Foo foo)
{
    // ...
}

However, to avoid the problems presented, we can output the following OpenCL code:

__kernel void Kernel(
    __global int *ptr1,         // First nested view pointer
    __global int *ptr2,         // Second nested view pointer
    struct View_Int source, // Source structure without pointer information from host
    struct Foo foo)             // Source structure without pointer information from host
{
    // Setup view pointers in kernel code
    source.ptr = ptr1;
    foo.Data.ptr = ptr2;
    // ...
}

from ilgpu.

m4rs-mt avatar m4rs-mt commented on May 18, 2024

Unfortunately, the proposed solution does not work properly, since OpenCL does not allow nested pointers within structures to be passed to the kernel. However, some drivers accept such code, while others simply fail. Therefore, passing arguments requires an interop structure to be passed to the kernel, which stores the required offset and length information for each view. Then, each kernel argument is converted into its internal representation, which can contain (and work on) pointers to device-memory addresses.

from ilgpu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.