如何创建 CUDA dll?
我需要在我的应用程序中使用 cuda。但我无法创建 dll。这里有一些代码。
__global__ void calc(float *a, int n) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
float val = a[idx];
if (idx < n){
a[idx] = 4.0 /(1.0 + val*val);
}
}
...
extern "C" __declspec(dllexport) void GPU_Code ( float *a_h, float *sum ) {
float *a_d;
const int numSteps = 10000;
cudaMalloc((void **) &a_d, sizeof(float)*numSteps);
int blockSize = 4;
int blocks = numSteps / blockSize + (numSteps % blockSize == 0 ? 0:1);
cudaMemcpy(a_d, a_h, sizeof(float)*numSteps, cudaMemcpyHostToDevice);
calc<<< blocks, blockSize >>> (a_d, numSteps);
cudaMemcpy(a_h, a_d, sizeof(float)*numSteps, cudaMemcpyDeviceToHost);
...
return;
}
并且 dll 已成功创建!但是当我尝试包含在我的应用程序代码中时,我犯了一个错误 - 致命错误 LNK1107:无效或损坏的文件:无法在 0x2D0 处读取。
__declspec(dllimport) void GPU_Code ( float *a_h, float *sum );
int main() {
float*a_h;
a_h = (float*)malloc(sizeof(double)*10000);
float sum = 0.0;
GPU_Code(a_h, &sum);
...
return 0;
}
如果可以的话,请给我一些使用dll的源代码。 PS 抱歉我的英语不好。
I need to use cuda in my application. But i can't create a dll. Some code here.
__global__ void calc(float *a, int n) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
float val = a[idx];
if (idx < n){
a[idx] = 4.0 /(1.0 + val*val);
}
}
...
extern "C" __declspec(dllexport) void GPU_Code ( float *a_h, float *sum ) {
float *a_d;
const int numSteps = 10000;
cudaMalloc((void **) &a_d, sizeof(float)*numSteps);
int blockSize = 4;
int blocks = numSteps / blockSize + (numSteps % blockSize == 0 ? 0:1);
cudaMemcpy(a_d, a_h, sizeof(float)*numSteps, cudaMemcpyHostToDevice);
calc<<< blocks, blockSize >>> (a_d, numSteps);
cudaMemcpy(a_h, a_d, sizeof(float)*numSteps, cudaMemcpyDeviceToHost);
...
return;
}
and dll successfully created! But when i try to include in my application code, i'm take a mistake - fatal error LNK1107: invalid or corrupt file: cannot read at 0x2D0.
__declspec(dllimport) void GPU_Code ( float *a_h, float *sum );
int main() {
float*a_h;
a_h = (float*)malloc(sizeof(double)*10000);
float sum = 0.0;
GPU_Code(a_h, &sum);
...
return 0;
}
If you can, take me please a some source code with using dll.
P.S. Sorry for my bad english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我最近在尝试创建和链接 CUDA 库时遇到了大量问题,我建议您创建一个 .lib,如果您愿意,您可以稍后将其包装在 dll 中,
我今天发布了这个问题,因为我遇到了问题这并得到了回答。这将帮助您创建包含 CUDA 代码的 .lib 并将其链接到 exe。链接到 dll 不应该有什么可怕的不同:
CUDA & Visual Studio 2008:尝试链接不同项目时出现问题
Since I recently faced a truckload of problems when trying to create and link a CUDA library I would suggest that you create a .lib instead and if you want you can later wrap this in a dll
I posted this question today cause I was having trouble with this and it was answered. This will help you create a .lib containing CUDA code and link it to an exe. Linking to a dll shouldnt be terrible different:
CUDA & Visual Studio 2008: Problems when trying to link different projects