的cubin我有个CUDA核心,看起来像下面这样:CUBLAS内核中不工作编译一次使用-G标志与NVCC
#include <cublas_v2.h>
#include <math_constants.h>
#include <stdio.h>
extern "C" {
__device__ float ONE = 1.0f;
__device__ float M_ONE = -1.0f;
__device__ float ZERO = 0.0f;
__global__ void kernel(float *W, float *input, int i, float *output, int o) {
int idx = blockIdx.x*blockDim.x+threadIdx.x;
cublasHandle_t cnpHandle;
if(idx == 0) {
cublasCreate(&cnpHandle);
cublasStatus_t s = cublasSgemv(cnpHandle, CUBLAS_OP_N, o, i, &ONE, W, o, input, 1, &ZERO, output, 1);
printf("status %d\n", s);
cudaError_t e = cudaDeviceSynchronize();
printf("sync %d\n", e);
}
}
}
主机代码:
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <cstring>
#include <cuda_runtime.h>
#include <cuda.h>
#include <cublas_v2.h>
extern "C" {
__global__ void kernel(float *W, float *input, int i, float *output, int o);
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
int main(int argc, char* argv[])
{
cuInit(0);
CUcontext pctx;
CUdevice dev;
cuDeviceGet(&dev, 0);
cuCtxCreate(&pctx, 0, dev);
CUmodule module;
CUresult t = cuModuleLoad(&module, "pathto/src/minimalKernel.cubin");
CUfunction function;
CUresult r = cuModuleGetFunction(&function, module, "kernel");
float *W = new float[2];
W[0] = 0.1f;
W[1] = 0.1f;
float *input = new float[2];
input[0] = 0.1f;
input[1] = 0.1f;
float *out = new float[1];
out[0] = 0.0f;
int i = 2;
int o = 1;
float *d_W;
float *d_input;
float *d_out;
cudaMalloc((void**)&d_W, 2*sizeof(float));
cudaMalloc((void**)&d_input, 2*sizeof(float));
cudaMalloc((void**)&d_out, sizeof(float));
cudaMemcpy(d_W, W, 2*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_input, input, 2*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_out, out, sizeof(float), cudaMemcpyHostToDevice);
//kernel<<<1, 2>>>(d_W, d_input, i, d_out, o);
//cudaMemcpy(out, d_out, sizeof(float), cudaMemcpyDeviceToHost);
//std::cout<<"out:"<<out[0]<<std::endl;
void * kernelParams[] { &d_W, &d_input, &i, &d_out, &o };
CUresult k = cuLaunchKernel(function, 1, 1, 1, 2, 1, 1, 0, 0, (void**)kernelParams, 0);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
cudaMemcpy(out, d_out, sizeof(float), cudaMemcpyDeviceToHost);
std::cout<<"out:"<<out[0]<<std::endl;
}
当这个内核运行内嵌kernel<<<1,2>>>()
,构建并链接(在eclipse Nsight中),内核运行完全正常,并且out
返回0.02
如预期的那样。
如果我在编译内核到使用-G
(生成设备调试符号)一.cubin的CUBLAS函数从不运行,而out
总是0.0
我可以把断点时.cubin正在运行和我可以看到进入cublas函数的数据是正确的,但它看起来像cublas函数永远不会运行。 cublas函数也总是返回0 CUDA_SUCCESS
。更重要的这从.cubin
运行这个时候要编译到我与-G
使用的cubin只发生:
nvcc -G -cubin -arch=sm_52 --device-c kernel.cu -o kernel.cubin -dlink -lcudadevrt -lcublas_device
返回没有错误。
如果添加-G
选项,为什么cubc在.cubin中的功能停止工作?
CUDA 7.0 的Linux 14.04 64 980GTX
提供了一个MCVE,包括您用来加载和调用内核的代码。 –
已经在上面编辑提供MCVE – Bam4d