<jupyter_start><jupyter_text>Debugging Pycuda Kernels with cuda-gdbTo debug an executable `gdb` must attatch to the process of that executable. This is usually achieved by passing an executable as an argument such as:```bashnvcc kernel.cucuda-gdb a.out```But Pycuda uses JIT (just in time) compilation to execute CUDA code so how do we debug kernels if we don't have access to the binary?- **Step1**: Set Debugging Flags.    we must complie with debugging option by passing `-g -G` flags to the nvidia compiler.    This is achived by running    `    export PYCUDA_DEFAULT_NVCC_FLAGS="-g -G"    `    before launching this notebook.- **Step2**: Attach to the correct process.    After the debugging environment variable has been set and the ipython notebook has been launched,    the python kernel can be attached to with the following command:    ```    cuda-gdb -p $(pgrep -f ipykernel)    ```        We are using the command line tool `pgrep` to get the pid of the nootbook session.    This is the same as running    ```    cuda-gdb --pid $(ps aux | grep [i]pykernel | awk '{print $2}')    ```    Note: this might fail if you have more than one ipython kernel running. In that case you can copy and paste the pid    for the desired process. Use the same technique to attach to a python process     e.g. `cuda-gdb -p $(pgrep -f python)`. - **Step3**: set desired breakpoints.    when the cuda-gdb attaches to the ipython kernel it will freeze the process so that the desired breakpoints    can be set. Breakpoints can be set in the usual gdb way. e.g.    ```    break cuda_kernel_name    ```    then `continue` to resume the ipython process. See [cuda-gdb docs](https://docs.nvidia.com/cuda/cuda-gdb/index.html) for more.<jupyter_code>%matplotlib inline

# ensure that any edits to libraries
# are reloaded automatically
%reload_ext autoreload
%autoreload 2
# make cuda_slic importable from the notebook directory
import sys
sys.path.append("./..")
# does not work for some reason

# this environment variable must be set in the same bash session
# that the jupyter notebook is being launched from
#%set_env PYCUDA_DEFAULT_NVCC_FLAGS="-g -G"
import pycuda
from cuda_slic.slic import slic3d as cuda_slic
import numpy as np
from skimage import data, color, filters, segmentation
from skimage.util import img_as_float32, img_as_float64
from skimage.segmentation import slic as sk_slic
from skimage.segmentation import mark_boundaries

import matplotlib.pyplot as plt
from ipywidgets import interact<jupyter_output><empty_output><jupyter_text>Using `cuda-gdb`, set a breakpint such as `break init_clusters` then run the following code block.<jupyter_code># initializing a 3 dim dataset

blob = data.binary_blobs(length=20, n_dim=3, seed=2)
blob = np.float32(blob)

cuda_labels = cuda_slic(blob, n_segments=4, compactness=.5)

pycuda.autoinit.context.detach()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07662968099861303, 'fraction_numerical': 0.006588072122052704, 'mean_word_length': 4.264598540145985, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '331548', 'license': 'Apache-2.0', 'n_tokens_mistral': 896, 'n_tokens_neox': 868, 'n_words': 372, 'path': 'notebooks/pycuda_gdb.ipynb', 'repo_name': 'abonawas/cuda-slic', 'size': '2884'}