CUDA 分配模板函数中从字符串常量到 'char *' 的已弃用转换

Deprecated conversion from string constant to 'char *' in CUDA allocation template function

本文关键字:char 转换 分配 函数 常量 字符串 CUDA      更新时间:2023-10-16

我做了一些辅助函数,用于使用 CUDA __constant__指针(allocation、copyToSymbol、copyFromSymbol 等)进行操作。 我也有错误检查,正如这里的爪子所建议的那样。 这是一个基本的工作示例:

#include <cstdio>
#include <cuda_runtime.h>
__constant__ float* d_A;
__host__ void cudaAssert(cudaError_t code,
                         char* file,
                         int line,
                         bool abort=true) {
  if (code != cudaSuccess) {
    fprintf(stderr, "CUDA Error: %s in %s at line %dn",
           cudaGetErrorString(code), file, line);
    if (abort) {
      exit(code);
    }   
  }
}
#define cudaTry(ans) { cudaAssert((ans), __FILE__, __LINE__); }
template<typename T>
void allocateCudaConstant(T* &d_ptr,
                          size_t size) {
  size_t memsize = size * sizeof(T);
  void* ptr;
  cudaTry(cudaMalloc((void**) &ptr, memsize));
  cudaTry(cudaMemset(ptr, 0, memsize));
  cudaTry(cudaMemcpyToSymbol(d_ptr, &ptr, sizeof(ptr),
                             0, cudaMemcpyHostToDevice));
}
int main() {
  size_t size = 16; 
  allocateCudaConstant<float>(d_A, size);
  return 0;
}

当我使用 nvcc 编译它时,我收到以下警告:

In file included from tmpxft_0000a3e8_00000000-3_example.cudafe1.stub.c:2:
example.cu: In function ‘void allocateCudaConstant(T*&, size_t) [with T = float]’:
example.cu:35:   instantiated from here
example.cu:29: warning: deprecated conversion from string constant to ‘char*’

我明白警告的意思,但我一辈子都无法弄清楚它来自哪里。 如果我不allocateCudaConstant模板函数,则不会收到警告。 如果我不cudaMemcpyToSymbol包裹在cudaTry中,我也不会收到警告。 我知道这只是一个警告,如果我用-Wno-write-strings编译,我可以抑制警告。 代码运行良好,但我不想养成忽略警告的习惯,如果我抑制警告,我可能会隐藏其他需要解决的问题。

那么,谁能帮我弄清楚警告来自哪里以及如何抑制它?

cudaAssert声明中将char* file更改为const char* file。您不需要修改字符串,因此不应要求可修改的字符串。