Constant Memory on GPU

When you using the hybridizer, you probably want to use some constant memory on the device.

This lab let you learn how to have some constant memory on the device.


Run code on the GPU

Using hybridizer to run some code, we need to:

  • adding [EntryPoint] on method of interest.
  • Using ThreadIdx.x(Position of the thread in its block), BlockIdx.x(Position of the Block in its Grid), BlockDim.x(Number of thread in the block) and GridDim.x(Number of block in the grid). According to this:

  • By default, hybridizer configure with 128 threads by block and 16 * number on multiprocessor block. you can modify it with the SetDistrib function that you call on the wrapper before execute your kernel.

    wrapped.SetDistrib(number_of_blocks, number_of_threads).mykernel(...);
    
  • Add boilerplate code ton invoke method on gpu

Modify the 01-gpu-run.cs so the Run method runs on a GPU.

If you get stuck, you can refer to the solution.


In [ ]:
!hybridizer-cuda ./01-gpu-run/01-gpu-run.cs -o ./01-gpu-run/01-gpu-run.exe -run

Constant memory

Now, after you run the method on the gpu, you can create a static member that goes on the GPU's constant memory. The GPU's constant memory can accelerate performance when lots of thread are reading the same adress by passing it in a cache. For that we need to:

  • create a public static member and decorate it like the following
    [HybridConstant(Location = ConstantLocation.ConstantMemory)]
    public static type variableName = value;
    

Modify the 02-memory-constant so data is now a static member and go in constant memory.

If you get stuck, you can refer to the solution.


In [ ]:
!hybridizer-cuda ./02-memory-constant/02-memory-constant.cs -o ./02-memory-constant/02-memory-constant.exe -run