<<< >>\> vscode中的cuda

新手上路,请多包涵

有没有办法用 vscode-cpptools 抑制“<<< >>>”错误。

我在 setting.json 中将“*.cu”与“cpp”相关联。

 // use normal c++ syntax highlighting for CUDA files
  "files.associations": {"*.cu": "cpp"},

并且工作正常,除了一个问题, kernel execution configuration parameters surrounded by <<< and >>> 误认为错误 expected an expression

 dim3 dimGrid(2, 2, 1);
dim3 dimBlock(width / 2, width / 2, 1);
MatrixMulKernel<<<dimGrid, dimBlock>>>(d_M, d_N, d_P, width);

任何建议

原文由 BugKiller 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
1 个回答

谷歌搜索几个小时,没有找到完美的解决方案,但有一些解决方法。

我在这里总结一下:

  • 通过编辑 setting.json 对 CUDA 文件使用普通的 c++ 语法高亮显示
  • 在程序中包含必要的 CUDA 标头
  • 在解决方法 INTELLISENSE 中包含虚拟标头

贝娄是一个具体的例子

  • 设置.json
>  "files.associations": {
>     "*.cu": "cpp",
>     "*.cuh": "cpp"
>   }
>
> ```

- cudaDmy.cuh

> ```
>  #pragma once
> #ifdef __INTELLISENSE__
> void __syncthreads();  // workaround __syncthreads warning
> #define KERNEL_ARG2(grid, block)
> #define KERNEL_ARG3(grid, block, sh_mem)
> #define KERNEL_ARG4(grid, block, sh_mem, stream)
> #else
> #define KERNEL_ARG2(grid, block) <<< grid, block >>>
> #define KERNEL_ARG3(grid, block, sh_mem) <<< grid, block, sh_mem >>>
> #define KERNEL_ARG4(grid, block, sh_mem, stream) <<< grid, block, sh_mem,
> stream >>>
> #endif
>
> ```

- 矩阵Mul.cu

> ```
>  #include <stdio.h>
> #include <math.h>
> #include <time.h>
> #include <cuda.h>
> #include "cuda_runtime.h"
> #include "device_launch_parameters.h"
> #include <device_functions.h>
> #include <cuda_runtime_api.h>
> #include "cudaDmy.cuh"
>
> __global__ void MatrixMulKernel(float *M, float *N, float *P, int width)
> {
>     int Row = blockIdx.y * blockDim.y + threadIdx.y;
>     int Col = blockIdx.x * blockDim.x + threadIdx.x;
>     if (Row < width && Col < width)
>     {
>         float Pvalue = 0;
>         for (int i = 0; i < width; ++i)
>         {
>             Pvalue += M[Row * width + i] * N[width * i + Col];
>         }
>         P[Row * width + Col] = Pvalue;
>     }
> }
>
> void MatMul(float *M, float *N, float *P, int width)
> {
>     float *d_M;
>     float *d_N;
>     float *d_P;
>     int size = width * width * sizeof(float);
>     cudaMalloc((void **)&d_M, size);
>     cudaMemcpy(d_M, M, size, cudaMemcpyHostToDevice);
>
>     cudaMalloc((void **)&d_N, size);
>     cudaMemcpy(d_N, N, size, cudaMemcpyHostToDevice);
>
>     cudaMalloc((void **)&d_P, size);
>
>     dim3 dimGrid(2, 2, 1);
>     dim3 dimBlock(width / 2, width / 2, 1);
>     // <<<>>> will replace macro KERNEL_ARG2 when compiling
>     MatrixMulKernel KERNEL_ARG2(dimGrid,dimBlock) (d_M, d_M, d_P, width);
>     cudaMemcpy(P, d_P, size, cudaMemcpyDeviceToHost);
>     cudaFree(d_M);
>     cudaFree(d_N);
>     cudaFree(d_P);
> }
>
> int main()
> {
>     int elem = 100;
>     float *M = new float[elem];
>     float *N = new float[elem];
>     float *P = new float[elem];
>
>     for (int i = 0; i < elem; ++i)
>         M[i] = i;
>
>     for (int i = 0; i < elem; ++i)
>         N[i] = i + elem;
>
>     time_t t1 = time(NULL);
>     MatMul(M, N, P, sqrt(elem));
>     time_t t2 = time(NULL);
>     double seconds = difftime(t2,t1);
>     printf ("%.3f seconds total time\n", seconds);
>     for (int i = 0; i < elem/1000000; ++i)
>         printf("%.1f\t", P[i]);
>     printf("\n");
>     delete[] M;
>     delete[] N;
>     delete[] P;
>     return 0;
> }
>
> ```

让我们用 NVCC 编译它

nvcc matrixMul.cu -Xcudafe “–diag_suppress=unrecognized_pragma” -o runcuda

”`

有用的链接:

原文由 BugKiller 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题