vscode编译cuda

千辛万苦,竟然还是用命令行编译通过的。
源程序:

#include "cuda_runtime.h"
 
#include "device_launch_parameters.h"
 
#include 
 
void addWithCuda(int *c, const int *a, const int *b, size_t size);
 
__global__ void addKernel(int *c, const int *a, const int *b)
 
{
 
    int i = threadIdx.x;
 
    c[i] = a[i] + b[i];
 
}
 
int main()
 
{
 
    const int arraySize = 5;
 
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
 
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
 
    int c[arraySize] = { 0 };
 
    // Add vectors in parallel.
 
    addWithCuda(c, a, b, arraySize);
 
    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
 
        c[0], c[1], c[2], c[3], c[4]);
 
    // cudaThreadExit must be called before exiting in order for profiling and
 
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
 
    cudaThreadExit();
 
    return 0;
 
}
 
// Helper function for using CUDA to add vectors in parallel.
 
void addWithCuda(int *c, const int *a, const int *b, size_t size)
 
{
 
    int *dev_a = 0;
 
    int *dev_b = 0;
 
    int *dev_c = 0;
 
    // Choose which GPU to run on, change this on a multi-GPU system.
 
    cudaSetDevice(0);
 
    // Allocate GPU buffers for three vectors (two input, one output)    .
 
    cudaMalloc((void**)&dev_c, size * sizeof(int));
 
    cudaMalloc((void**)&dev_a, size * sizeof(int));
 
    cudaMalloc((void**)&dev_b, size * sizeof(int));
 
    // Copy input vectors from host memory to GPU buffers.
 
    cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
 
    cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
 
    // Launch a kernel on the GPU with one thread for each element.
 
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);
 
    // cudaThreadSynchronize waits for the kernel to finish, and returns
 
    // any errors encountered during the launch.
 
    cudaThreadSynchronize();
 
    // Copy output vector from GPU buffer to host memory.
 
    cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
 
    cudaFree(dev_c);
 
    cudaFree(dev_a);
 
    cudaFree(dev_b);
 
}

编译命令

nvcc -o kernel -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include" kernel.cu

执行生成的可执行程序:

./kernel.exe

终端输出:

(base) PS E:\document\vscode\cuda> .\kernel.exe
{1,2,3,4,5} + {10,20,30,40,50} = {11,22,33,44,55}

好奇地查看了一下依赖:

(base) PS E:\document\vscode\cuda> dumpbin /dependents .\kernel.exe
Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file .\kernel.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    KERNEL32.dll

  Summary

        4000 .data
        1000 .gfids
        1000 .nvFatBi
        1000 .nv_fatb
        2000 .pdata
       1A000 .rdata
        1000 .reloc
       26000 .text
        1000 .tls

竟然没有依赖cuda动态库,出乎意料。

参考:Windows下 VSCode配置cuda编译环境

你可能感兴趣的:(cuda)