# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <div align="center"><h1>Constant Memory on GPU</h1></div>

# 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:![Thread And Block](Images/threadAndBlock.jpg)
#
# - 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.
# ```csharp
# wrapped.SetDistrib(number_of_blocks, number_of_threads).mykernel(...);
# ```
# - Add boilerplate code ton invoke method on gpu
#
# Modify the [01-gpu-run.cs](../../edit/05_ConstantMemory/01-gpu-run/01-gpu-run.cs) so the `Run` method runs on a GPU.
#
# If you get stuck, you can refer to the [solution](../../edit/05_ConstantMemory/01-gpu-run/solution/01-gpu-run.cs).

# !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
# ```csharp
# [HybridConstant(Location = ConstantLocation.ConstantMemory)]
# public static type variableName = value;
# ```
#
# Modify the [02-memory-constant](../../edit/05_ConstantMemory/02-memory-constant/02-memory-constant.cs) so data is now a static member and go in constant memory.
#
# If you get stuck, you can refer to the [solution](../../edit/05_ConstantMemory/02-memory-constant/solution/02-memory-constant.cs).

# !hybridizer-cuda ./02-memory-constant/02-memory-constant.cs -o ./02-memory-constant/02-memory-constant.exe -run

StarcoderdataJupyterScriptsDedupFiltered