gpt4 book ai didi

c - 我需要向 DLL 添加什么才能导出指向 i_d 的指针?

转载 作者:行者123 更新时间:2023-11-30 15:57:43 27 4
gpt4 key购买 nike

我正在尝试编写DLL函数来分配cuda内存并获取指向cuda(设备)内存的指针。

第二个函数应该接受这个指针并进行计算。

我希望这个操作是分开的,因为我需要对相同的数据进行多次计算,并且我试图避免重复将相同的数据复制到 GPU 内存(这需要很多时间)-

问:我需要向 DLL 添加什么才能导出指向 i_d 的指针?

我的DLL:

main.cpp:

  extern "C" __declspec(dllexport) int cuda_Malloc ( float *i, void **i_d, int N ){
for( float x=0; x<N; x++ )
i[x]=x;
kernel_cuda_Malloc( i, i_d, N );
return 0;
}

extern "C" __declspec(dllexport) int cuda_Calculation( void *i_d, float *result, int N ) {
kernel_cuda_calculation( i_d, result, N );
return 0;
}

简单.cu:

  __global__ void kernelTest( float *i, int N ){
unsigned int tid = blockIdx.x*blockDim.x + threadIdx.x;
if ( tid<N )
i[tid] += 10;
}

int kernel_cuda_Malloc( float *i, void **i_d, int N ){
cudaMalloc( (void**)&i_d, N*sizeof( float ) );
cudaMemcpy( i_d, i, N*sizeof( float ), cudaMemcpyHostToDevice );
return 0;
}


void kernel_cuda_calculation( float *i_d, float *result, int N ){
dim3 threads; threads.x = 240;
dim3 blocks; blocks.x = ( N/threads.x ) + 1;
kernelTest<<< threads, blocks >>>( i_d, N );
cudaMemcpy( result, i_d, N*sizeof( float ), cudaMemcpyDeviceToHost );
cudaFree( i_d );

}

我无法从 LabVIEW 中的 cuda_Malloc 函数获取指向 i_d 的指针。

代码是https://decibel.ni.com/content/docs/DOC-20353的修改

最佳答案

所有 CUDA 函数都在 CUDA 上下文中执行。为了能够在函数之间传输指针,还必须保留上下文。

你的代码没有多大意义。 DLL 中的两个函数都称为 cuda_Malloc。这些函数实际上都没有返回任何内容。示例代码很好,但前提是您花时间提供您认为应该有效的代码。

编辑:抱歉,我错过了您试图通过修改作为参数传入的指针来返回指针的事实。为此,您必须传入一个指向指针的指针,而不仅仅是指针。

int kernel_cuda_Malloc( float *i, void *i_d, int N ){

应该是

int kernel_cuda_Malloc( float *i, void **i_d, int N ){

关于c - 我需要向 DLL 添加什么才能导出指向 i_d 的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10314239/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com