簡體   English   中英

在CUDA分配模板函數中不建議將字符串常量轉換為'char *'

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

我做了一些輔助函數,用於使用CUDA __constant__指針(分配,copyToSymbol,copyFromSymbol等)進行操作。 我在這里也有由talonmies建議的錯誤檢查。 這是一個基本的工作示例:

#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編譯, -Wno-write-strings可以禁止該警告。 代碼運行良好,但是我不想養成忽略警告的習慣,如果我取消警告,則可能隱藏其他需要解決的問題。

那么,誰能幫助我弄清楚警告的出處以及如何消除該警告?

更改char* fileconst char* file中的聲明cudaAssert 您不需要修改字符串,因此您不應該要求可修改的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM