CUDA 分配模板函数中从字符串常量到 'char *' 的已弃用转换
Deprecated conversion from string constant to 'char *' in CUDA allocation template function
我做了一些辅助函数,用于使用 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
。您不需要修改字符串,因此不应要求可修改的字符串。
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串