martedì 29 settembre 2026

Nvidia Dev Env con Docker e Visual Code

Mi sono comprato un vecchio portatile MSI con una NVidia GeForce GTX 860M che supporta i driver 580.178.04 Cuda 13

La scheda video e' molto vecchia per cui la soluzione migliore per fare lo sviluppo e' utilizzare un docker container con Cuda SDK 12 gia' pronto 


 

Si installa Visual Code con estensione Dev Containers e C/C++ 

 Da notare che il container non ha installato gdb... per questo motivo in postCreateCommand lo installa tramite apt

 in ./devcontainer/devcontainer.json

{
"name": "CUDA Development Environment",
"image": "nvidia/cuda:12.0.0-devel-ubuntu22.04",
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools",
"ms-vscode.cmake-tools"
]
}
},
"containerEnv": {
"NVCC_FLAGS": "-arch=sm_50"
},
"runArgs": [
"--gpus=all"
],
"postCreateCommand": "apt-get update && apt-get install -y gdb && nvcc --version"
}


 in .vscode/launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug CUDA (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Abilita pretty-printing per gdb",
"text": "enable pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compila CUDA con nvcc"
}
]
}

 

in .vscode/tasks.json

si deve impostare il target sm_50 perche' la scheda e' vecchia ed ha cuda capabilities 5 

{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compila CUDA con nvcc",
"command": "nvcc",
"args": [
"-g",
"-arch=sm_50",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
],
"detail": "Compilatore CUDA NVCC per GTX 860M"
}
]
}

 

test.cu

#include <iostream>

__global__ void helloFromGPU() {
printf("Hello from GPU! Thread index: %d\n", threadIdx.x);
}

int main() {
std::cout << "Hello from CPU!" << std::endl;
helloFromGPU<<<1, 5>>>();
cudaDeviceSynchronize();
return 0;
}

 per iniziare lo sviluppo si deve aprire CTRL+SHIFT+P Dev Containers: Reopen in container

A questo punto in basso a sinistra si ha un box azzurro

ed il terminale punta alla shell del container root@02cb6c6a1ae0:/workspaces/progetto_cuda#

 

Per fare il debug del kernel Cuda si usa compute-sanitizer

compute-sanitizer ./test_cuda
========= COMPUTE-SANITIZER
Hello from CPU!
Hello from GPU! Thread index: 0
Hello from GPU! Thread index: 1
Hello from GPU! Thread index: 2
Hello from GPU! Thread index: 3
Hello from GPU! Thread index: 4
========= ERROR SUMMARY: 0 errors 

 

 

 

 

 

Nessun commento:

Posta un commento

Nvidia Dev Env con Docker e Visual Code

Mi sono comprato un vecchio portatile MSI con una NVidia GeForce GTX 860M che supporta i driver 580.178.04 Cuda 13 La scheda video e' mo...