我想将 CUDA 代码组织到单独的目标文件中,以便在编译结束时链接,就像在 C++ 中一样。为此,我希望能够__constant__在头文件中声明一个指向内存的外部指针,并将定义放入其中一个 .cu 文件中,同样遵循 C++ 的模式。但似乎当我这样做时,nvcc 会忽略“extern” - 它将每个声明作为定义。有没有解决的办法?
为了更具体地了解代码和错误,我将其放在头文件中:
\n\nextern __device__ void* device_function_table[];\nRun Code Online (Sandbox Code Playgroud)\n\n接下来是 .cu 文件中的内容:
\n\nvoid* __device__ device_function_table[200];\nRun Code Online (Sandbox Code Playgroud)\n\n这在编译时给出了这个错误:
\n\n(path).cu:40: error: redefinition of \xe2\x80\x98void* device_function_table [200]\xe2\x80\x99\n(path).hh:29: error: \xe2\x80\x98void* device_function_table [200]\xe2\x80\x99 previously declared here\nRun Code Online (Sandbox Code Playgroud)\n\n我当前的解决方案是使用 Makefile 魔法将我所有的 .cu 文件组合在一起,实际上拥有一个大的翻译单元,但有一些类似的文件组织。但这已经明显减慢了编译速度,因为对我的任何一个类的更改都意味着重新编译所有类;我预计还会增加几个课程。
\n\n编辑:我看到我放入__constant__了文本和__device__示例中;这个问题对两者都适用。
我试图在彩色四边形上方绘制一个部分透明的纹理,以便颜色透过透明部分,形成轮廓.这是代码:
// Setup
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// (...)
// Get image bits in object 't'
glBindTexture(GL_TEXTURE_2D, textureIDs[idx]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
// (...)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Underlying coloured quad
glColor4d(0.0, 1.0, 0.0, 1.0);
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_QUADS);
glVertex3d(coords11.first, coords11.second, -0.001);
glVertex3d(coords12.first, coords12.second, -0.001);
glVertex3d(coords21.first, coords21.second, -0.001);
glVertex3d(coords22.first, coords22.second, -0.001);
glEnd();
// Textured quad
glColor4d(1.0, 1.0, 1.0, 0.5);
glBindTexture(GL_TEXTURE_2D, textureIDs[castleTextureIndices[0]]);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); …Run Code Online (Sandbox Code Playgroud)