【问题标题】:CUDA 5.0: CUBIN and CUBLAS_device, compute capability 3.5CUDA 5.0:CUBIN 和 CUBLAS_device,计算能力 3.5
【发布时间】:2013-03-02 16:23:39
【问题描述】:

我正在尝试编译一个使用动态并行性将 CUBLAS 运行到 cu​​bin 文件的内核。 当我尝试使用命令编译代码时

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

我收到ptxas fatal : Unresolved extern function '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");
}
}

提前致谢

  • 索伦

【问题讨论】:

  • 您使用 Driver API 有什么原因吗?
  • KiaMorot:我使用 pycuda,它使用驱动 API。我包含 C 代码的原因是让它更透明

标签: cuda nvcc cublas


【解决方案1】:

您在第一种方法中只是缺少-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

【讨论】:

  • 谢谢,你让我开心:)
  • 有人解释为什么我需要两步编译吗?
  • 好问题 Soren,一步一步更新了我的答案。
  • 再次感谢您的跟进!
猜你喜欢
  • 2013-05-03
  • 2018-05-03
  • 2016-12-25
  • 1970-01-01
  • 2015-05-10
相关资源
最近更新 更多