在VSCode中使用CUDA

配置VSCode运行CUDA环境

配置环境主要就是三个文件的内容,记录在这里,怕自己忘记了,如下所示:

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}",
                "/usr/include",             
                "/usr/include/x86_64-linux-gnu/sys",
                "/usr/local/cuda-9.0/include"
            ],
            "defines": [],
            "browse":{
                "path":[
                    "/usr/include"
                ]
            },
            "compilerPath": "/usr/local/cuda-9.0/bin",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

launch.js:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "build",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "nvcc",
            "args":["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.out",
                // include 头文件
                "-I", "/usr/local/cuda/include",
                "-I", "/usr/local/cuda-9.0/samples/common/inc",
                // lib 库文件地址
                "-L", "/usr/local/cuda/lib64",   
                "-L", "/usr/local/cuda-9.0/samples/common/lib",  
                "-l", "cudart",                           
                "-l", "cublas",
                "-l", "cudnn",
                "-l", "curand",
                "-D_MWAITXINTRIN_H_INCLUDED"  
            ]
        }
    ]
}

自动补齐代码与其他扩展插件

VSCode深受我喜欢的一点就在于他那个类似于Steam创意工坊的扩展商店,现在只需要在商店里下载一些扩展,不用自己手动配置,很轻松的就能完成自动补齐代码,方便我们的工作:

扩展推荐(根据自己的喜好添加)

VSCuda
CUDA code suggestions for VSCode.
这个扩展的介绍已经写的很清楚了吧!

PyCUDA highlighter
This extension highlights embedded cuda sources in python script.
cuda+python,必备插件+1!

vscode-cudacpp
CUDA C++ language support for Visual Studio Code.
很明显了吧,喜加一。

还有一个,你要是喜欢也可以尝试一下
CUDA WebGPU ECE408/CS483 UIUC Remote Control
I hereby, introduce a VS Code Extension called ECE 408 Remote Control, which allows students to use their code created locally, using VS Code as the editor with decent syntax highlighting systems, and use the extension for posting the code back onto the WebGPU website, automatically running the tests online, and show the results on the Chrome browser.

想知道什么意思?自己翻译一下吧,嘻嘻!

  • 9
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
要在VS Code使用CUDA,你需要进行以下步骤: 1. 首先,确保你已经安装了NVIDIA GPU Computing Toolkit和CUDA驱动程序,并且路径设置正确。你可以在VS Code的设置找到"code-runner.executorMap",在其添加CUDA文件的编译语句。例如,你可以在"cu"字段添加以下语句: ``` "cu": "cd $dir; nvcc $fileName -o $fileNameWithoutExt.exe -I'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.7\\include\\CL' && $dir$fileNameWithoutExt" ``` 请确保将路径设置为你的实际安装路径。\[1\] 2. 如果你想使用VS Code扩展来远程控制CUDA WebGPU,你可以尝试使用名为"ECE 408 Remote Control"的扩展。该扩展允许你在本地使用VS Code作为编辑器,并将代码上传到WebGPU网站上,在Chrome浏览器运行测试并显示结果。\[2\] 3. 如果你想在VS Code编写和运行CUDA代码,你可以创建一个新的CUDA项目。你可以在VS Code创建一个新的文件,将以下示例代码粘贴到文件: ``` #include <iostream> #include <math.h> __global__ void add(int n, float *x, float *y) { for (int i = 0; i < n; i++) y\[i\] = x\[i\] + y\[i\]; } int main(void) { int N = 1<<25; float *x, *y; cudaMallocManaged(&x, N*sizeof(float)); cudaMallocManaged(&y, N*sizeof(float)); for (int i = 0; i < N; i++) { x\[i\] = 1.0f; y\[i\] = 2.0f; } add<<<1, 1>>>(N, x, y); cudaDeviceSynchronize(); float maxError = 0.0f; for (int i = 0; i < N; i++) maxError = fmax(maxError, fabs(y\[i\]-3.0f)); std::cout << "Max error: " << maxError << std::endl; cudaFree(x); cudaFree(y); return 0; } ``` 这是一个简单的CUDA程序,它将两个数组的元素相加,并计算最大误差。你可以根据需要修改代码。然后,你可以使用适当的编译命令来编译和运行该程序。\[3\] 希望这些信息对你有所帮助! #### 引用[.reference_title] - *1* *3* [VScode配置cuda开发环境](https://blog.csdn.net/lijj0304/article/details/130810197)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [在VSCode使用CUDA](https://blog.csdn.net/MyYaip/article/details/122882482)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值