我对如何使用 cula 设备界面有点困惑。现在,我在 cpp 文件上使用 cula 接口,并从 cu 文件生成一些随机数。
.cu 文件:
...
__global__ void kernel( double * A,double * B, curandState * globalState, int Asize, int Bsize )
{
// generate random numbers
...
void kernel_wrapper(
double ** const A_host,
double ** const B_host,
const int Asize ,
const int Bsize )
{
...
// create random states
curandState * devStates;
gpuErrchk( cudaMalloc( &devStates, N * sizeof(curandState) ) );
// allocate host memory
*A_host = (double*) malloc( Asize * sizeof(double) );
*B_host = (double*) malloc( Bsize * sizeof(double) );
// allocate device memory
double * A_dev, * B_dev;
gpuErrchk( cudaMalloc( (void**) &A_dev, Asize * sizeof(double) ) );
gpuErrchk( cudaMalloc( (void**) &B_dev, Bsize * sizeof(double) ) );
// setup seeds
setup_kernel<<<1,N>>>( devStates, unsigned( time(NULL)) );
...
// generate random numbers
kernel<<<1,1>>>( A_dev, B_dev, devStates, Asize, Bsize );
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
// copy result from device to host
gpuErrchk( cudaMemcpy( *A_host, A_dev, Asize * sizeof(double), cudaMemcpyDeviceToHost ) );
gpuErrchk( cudaMemcpy( *B_host, B_dev, Bsize * sizeof(double), cudaMemcpyDeviceToHost ) );
// clean up device memory
gpuErrchk( cudaFree( A_dev ) );
gpuErrchk( cudaFree( B_dev ) );
gpuErrchk( cudaFree( devStates ) );
return;
}
cpp文件:
...
extern void kernel_wrapper(double** A,double** B, int Asize ,int Bsize);
...
culaDouble* A;
culaDouble* B;
kernel_wrapper( &A, &B, Asize, Bsize );
...
status = culaDgels('N',N,N, NRHS, A, N, B, N);
所以,我从 cu 文件分配主机内存并将其传递给 cpp 文件。
如果我想使用 cula 设备?
我不知道如何管理内存传输。