CUDA 5.0:CUBIN和CUBLAS_device,计算能力3.5

3

我想编译一个使用动态并行性运行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

你使用驱动程序API的原因是什么? - KiaMorot
KiaMorot:我使用pycuda,它使用驱动程序API。我包含C代码的原因是为了使其更加透明。 - Soren
1个回答

4
您在第一种方法中只是缺少了-dlink参数:
nvcc -cubin -m64 -lcudadevrt -lcublas_device -gencode arch=compute_35,code=sm_35 -o test.cubin -c test.cu -dlink

您也可以分两步来完成:

nvcc -m64 test.cu -gencode arch=compute_35,code=sm_35 -o test.o -dc
nvcc -dlink test.o -arch sm_35 -lcublas_device -lcudadevrt -cubin -o test.cubin

1
有人能解释一下为什么我需要两个编译步骤吗? - Soren
很好的问题,Soren。我已经更新了我的答案,提供了一种单步操作的方式。 - Piotr Jaroszyński
再次感谢您的跟进! - Soren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接