我想编译一个使用动态并行性运行CUBLAS的内核到cubin文件中。 当我试图使用以下命令编译代码时
nvcc -cubin -m64 -lcudadevrt -lcublas_device -gencode arch=compute_35,code=sm_35 -o test.cubin -c test.cu
我收到了 ptxas致命错误: 未解决的外部函数'cublasCreate_v2'
如果我添加-rdc=true
编译选项,它会成功编译,但是当我尝试使用cuModuleLoad加载模块时,会出现500错误:CUDA_ERROR_NOT_FOUND。来自cuda.h:
/**
* This indicates that a named symbol was not found. Examples of symbols
* are global/constant variable names, texture names, and surface names.
*/
CUDA_ERROR_NOT_FOUND = 500,
内核代码:
#include <stdio.h>
#include <cublas_v2.h>
extern "C" {
__global__ void a() {
cublasHandle_t cb_handle = NULL;
cudaStream_t stream;
if( threadIdx.x == 0 ) {
cublasStatus_t status = cublasCreate_v2(&cb_handle);
cublasSetPointerMode_v2(cb_handle, CUBLAS_POINTER_MODE_HOST);
if (status != CUBLAS_STATUS_SUCCESS) {
return;
}
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
cublasSetStream_v2(cb_handle, stream);
}
__syncthreads();
int jp;
double A[3];
A[0] = 4.0f;
A[1] = 5.0f;
A[2] = 6.0f;
cublasIdamax_v2(cb_handle, 3, A, 1, &jp );
}
}
注意:变量
A
的范围是局部的,因此指针传递给cublasIdamax_v2
的数据是未定义的,在这段代码中,jp
最终会成为一个或多个随机值。正确的方法是将A
存储在全局内存中。
宿主代码:
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
int main() {
CUresult error;
CUdevice cuDevice;
CUcontext cuContext;
CUmodule cuModule;
CUfunction testkernel;
// Initialize
error = cuInit(0);
if (error != CUDA_SUCCESS) printf("ERROR: cuInit, %i\n", error);
error = cuDeviceGet(&cuDevice, 0);
if (error != CUDA_SUCCESS) printf("ERROR: cuInit, %i\n", error);
error = cuCtxCreate(&cuContext, 0, cuDevice);
if (error != CUDA_SUCCESS) printf("ERROR: cuCtxCreate, %i\n", error);
error = cuModuleLoad(&cuModule, "test.cubin");
if (error != CUDA_SUCCESS) printf("ERROR: cuModuleLoad, %i\n", error);
error = cuModuleGetFunction(&testkernel, cuModule, "a");
if (error != CUDA_SUCCESS) printf("ERROR: cuModuleGetFunction, %i\n", error);
return 0;
}
主机代码使用
nvcc -lcuda test.cpp
进行编译。
如果我将内核替换为简单的内核(如下所示)并在不使用 -rdc=true
的情况下进行编译,它可以正常工作。
简单的可工作内核
#include <stdio.h>
extern "C" {
__global__ void a() {
printf("hello\n");
}
}
提前感谢您
- Soren