【发布时间】:2019-01-11 21:18:54
【问题描述】:
我参考了几乎所有类似的问题,但没有找到答案。错误检查被很多人推荐,所以我尝试使用CHECKED_CALL()类型的宏来使程序强大,但是我的代码遇到了两个问题:
-
正如标题所说,我收到了一条警告消息,但在我使用
#pragma hd_warning_disable之前,我收到了错误消息:cuEntityIDBuffer.cu(9): error: identifier "stderr" is undefined in device code 当我编译
maintest.cpp时,我得到另一个错误:
编辑:
g++ -c maintest.cpp -std=c++11
cuEntityIDBuffer.h:1:27: fatal error: thrust/reduce.h: No such file or directory
但是,在编译cuEntityIDBuffer.cu时它工作正常cuEntityIDBuffer.h也包含在这个文件中。nvcc -arch=sm_35 -Xcompiler '-fPIC' -dc cuEntityIDBuffer.cu
cuEntityIDBuffer.cu 和 maintest.cpp #include "cuEntityIDBuffer.h",但 maintest.cpp 抛出错误,我对此没有任何想法。
代码如下:
cuEntityIDBuffer.h
#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
#include <stdio.h>
#include <assert.h>
#include <cuda_runtime.h>
#ifdef __CUDACC__
#define CUDA_CALLABLE_MEMBER __host__ __device__
#else
#define CUDA_CALLABLE_MEMBER
#endif
class cuEntityIDBuffer
{
public:
CUDA_CALLABLE_MEMBER cuEntityIDBuffer();
CUDA_CALLABLE_MEMBER cuEntityIDBuffer(unsigned int* buffer);
CUDA_CALLABLE_MEMBER void cuCallBackEntityIDBuffer(unsigned int* buffer);
CUDA_CALLABLE_MEMBER ~cuEntityIDBuffer();
CUDA_CALLABLE_MEMBER void cuTest();
private:
size_t buffersize;
unsigned int* cuBuffer;
};
cuEntityIDBuffer.cu
#include "cuEntityIDBuffer.h"
#include <stdio.h>
#pragma hd_warning_disable
#define nTPB 256
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
__global__ void mykernel(unsigned int* buffer)
{
int idx = threadIdx.x + (blockDim.x * blockIdx.x);
buffer[idx]++;
//other things.
}
cuEntityIDBuffer::cuEntityIDBuffer()
{
buffersize=1024;
gpuErrchk(cudaMalloc(&cuBuffer, buffersize * sizeof(unsigned int)));
}
cuEntityIDBuffer::cuEntityIDBuffer(unsigned int* buffer)
{
buffersize=1024;
gpuErrchk(cudaMalloc(&cuBuffer, buffersize * sizeof(unsigned int)));
gpuErrchk(cudaMemcpy(cuBuffer,buffer,buffersize*sizeof(unsigned int),cudaMemcpyHostToDevice));
}
void cuEntityIDBuffer::cuCallBackEntityIDBuffer(unsigned int* buffer)
{
gpuErrchk(cudaMemcpy(buffer,cuBuffer,buffersize*sizeof(unsigned int),cudaMemcpyDeviceToHost));
}
cuEntityIDBuffer::~cuEntityIDBuffer()
{
gpuErrchk(cudaFree((cuBuffer)));
}
void cuEntityIDBuffer::cuTest()
{
mykernel<<<((buffersize+nTPB-1)/nTPB),nTPB>>>(cuBuffer);
gpuErrchk(cudaPeekAtLastError());
}
maintest.cpp
#include "cuEntityIDBuffer.h"
#include <iostream>
int main(int argc, char const *argv[])
{
unsigned int *h_buf;
h_buf=malloc(1024*sizeof(unsigned int));
cuEntityIDBuffer d_buf(h_buf);
d_buf.cuTest();
d_buf.cuCallBackEntityIDBuffer(h_buf);
return 0;
}
是我使用CHECKED_CALL() 类型宏的方式错误还是我的代码组织有问题?任何建议表示赞赏。
【问题讨论】:
-
你为什么要使用 gcc 编译 main?您必须使用 nvcc 并使用 .cu 扩展名
标签: cuda