• About
    • Learn More
    • Highlights
    • Features
    • News
    • FAQ
  • Project
    • Sourcecode
    • Releases
    • Milestones
  • Documentation
  • Discord
  • Get Started

Documentation

Primers
  • Setting Up ILGPU
  • A GPU Is Not A CPU
  • Memory & Bandwidth Threads
Beginner
  • Context & Accelerators
  • MemoryBuffers & ArrayViews
  • Kernels & Simple Programs
  • Structs
Advanced
  • Memory Buffers & Views
  • Kernels
  • Shared Memory
  • Math Functions
  • Dynamically Specialized Kernels
  • Debugging & Profiling
  • Inside ILGPU
Upgrade Guides
  • v0.8.X to v0.9.X
  • v0.8.0 to v0.8.1
  • v0.7.X to v0.8.X
  • v0.6.X to v0.7.X
  • v0.3.X to v0.5.X
  • v0.1.X to v0.2.X
  1. Home
  2. Shared Memory

Shared Memory

ILGPU support both static and dynamic shared memory. Static shared memory is limited to statically known allocations which have a known size at compile time of the kernel. The latest ILGPU versions allow the use of dynamic shared memory, which can be specified for each kernel launch individually.

class ...
{
    static void SharedMemKernel(ArrayView<int> data)
    {
        // Static memory allocation
        var staticMemory = SharedMemory.Allocate<int>>(1024);

        // Use GetDynamic access dynamically specified shared memory
        var dynamicMemory = SharedMemory.GetDynamic<int>>();

        ...

        // Use GetDynamic with a different element type to access
        // the same memory region in a different way
        var dynamicMemory2 = SharedMemory.GetDynamic<double>>();

        ...
    }

    static void ...(...)
    {
        using var context = Context.CreateDefault();
        using var accl = context.CreateCudaAccelerator(0);

        // Create shared memory configuration using a custom element type.
        // Note that this does not need to be the same type that is used in the scope of the kernel.
        // Therefore, the following two configurations will allocate the same amount of shared memory:
        var config = SharedMemoryConfig.RequestDynamic<byte>(<GroupSize> * sizeof(int));
        var config2 = SharedMemoryConfig.RequestDynamic<int>(<GroupSize>);

        ...
        // Pass the shared memory configuration to the kernel configuration
        kernel((<UserGridDim>, <UserGroupDim>, config), buffer.View);
        ...
    }
}

Note that this feature available on CPU, Cuda and OpenCL accelerators.

Help us make these docs great!

All ILGPU docs are open source. See something that's wrong or unclear? Submit a pull request.

Make a contribution

Or, learn how to contribute.

© Copyright ILGPU. All Rights Reserved.