开发者

How to create a CUDA dll?

开发者 https://www.devze.com 2023-01-20 04:31 出处:网络
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) {

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开发者_如何学C 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消