我已经制作了一些辅助函数来使用 CUDA__constant__
指针(分配、copyToSymbol、copyFromSymbol 等)进行操作。我也按照 talonmies here的建议进行了错误检查。这是一个基本的工作示例:
#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 %d\n",
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
我可以抑制警告。代码运行良好,但我不想养成忽略警告的习惯,如果我禁止警告,我可能会隐藏其他需要解决的问题。
那么,谁能帮我弄清楚警告来自哪里以及如何抑制它?