这个问题类似于cuModuleLoadDataEx 选项,但我想再次提出这个主题并提供更多信息。
通过 cuModuleLoadDataEx 使用 NV 驱动程序加载 PTX 字符串时,它似乎忽略了所有选项。我提供了完整的工作示例,以便任何有兴趣的人都可以直接轻松地复制它。首先是一个小的 PTX 内核(将其保存为 small.ptx),然后是加载 PTX 内核的 C++ 程序。
.version 3.1
.target sm_20, texmode_independent
.address_size 64
.entry main()
{
ret;
}
主文件
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<map>
#include "cuda.h"
int main(int argc,char *argv[])
{
CUdevice cuDevice;
CUcontext cuContext;
CUfunction func;
CUresult ret;
CUmodule cuModule;
cuInit(0);
std::cout << "trying to get device 0\n";
ret = cuDeviceGet(&cuDevice, 0);
if (ret != CUDA_SUCCESS) { exit(1);}
std::cout << "trying to create a context\n";
ret = cuCtxCreate(&cuContext, 0, cuDevice);
if (ret != CUDA_SUCCESS) { exit(1);}
std::cout << "loading PTX string from file " << argv[1] << "\n";
std::ifstream ptxfile( argv[1] );
std::stringstream buffer;
buffer << ptxfile.rdbuf();
ptxfile.close();
std::string ptx_kernel = buffer.str();
std::cout << "Loading PTX kernel with driver\n" << ptx_kernel;
const unsigned int jitNumOptions = 3;
CUjit_option *jitOptions = new CUjit_option[jitNumOptions];
void **jitOptVals = new void*[jitNumOptions];
// set up size of compilation log buffer
jitOptions[0] = CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES;
int jitLogBufferSize = 1024*1024;
jitOptVals[0] = (void *)&jitLogBufferSize;
// set up pointer to the compilation log buffer
jitOptions[1] = CU_JIT_INFO_LOG_BUFFER;
char *jitLogBuffer = new char[jitLogBufferSize];
jitOptVals[1] = jitLogBuffer;
// set up wall clock time
jitOptions[2] = CU_JIT_WALL_TIME;
float jitTime = -2.0;
jitOptVals[2] = &jitTime;
ret = cuModuleLoadDataEx( &cuModule , ptx_kernel.c_str() , jitNumOptions, jitOptions, (void **)jitOptVals );
if (ret != CUDA_SUCCESS) { exit(1);}
std::cout << "walltime: " << jitTime << "\n";
std::cout << std::string(jitLogBuffer) << "\n";
}
构建(假设 CUDA 安装在 /usr/local/cuda 下,我使用 CUDA 5.0):
g++ -I/usr/local/cuda/include -L/usr/local/cuda/lib64/ main.cc -o main -lcuda
如果有人能够从编译过程中提取任何有意义的信息,那就太好了!解释 cuModuleLoadDataEx 的 CUDA 驱动程序 API 的文档(以及它应该接受的选项)http://docs.nvidia.com/cuda/cuda-driver-api/index.html
如果我运行它,日志是空的,jitTime
甚至没有被 NV 驱动程序触及:
./main small.ptx
trying to get device 0
trying to create a context
loading PTX string from file empty.ptx
Loading PTX kernel with driver
.version 3.1
.target sm_20, texmode_independent
.address_size 64
.entry main()
{
ret;
}
walltime: -2
编辑:
我设法获得了 JIT 编译时间。然而,驱动程序似乎需要一个 32 位值的数组作为 OptVals。void *
不像手册中所说的那样,我的系统上的指针数组 ( ) 是 64 位。所以,这有效:
const unsigned int jitNumOptions = 1;
CUjit_option *jitOptions = new CUjit_option[jitNumOptions];
int *jitOptVals = new int[jitNumOptions];
jitOptions[0] = CU_JIT_WALL_TIME;
// here the call to cuModuleLoadDataEx
std::cout << "walltime: " << (float)jitOptions[0] << "\n";
我相信不可能对数组做同样的事情void *
。以下代码不起作用:
const unsigned int jitNumOptions = 1;
CUjit_option *jitOptions = new CUjit_option[jitNumOptions];
void **jitOptVals = new void*[jitNumOptions];
jitOptions[0] = CU_JIT_WALL_TIME;
// here the call to cuModuleLoadDataEx
// here I also would have a problem casting a 64 bit void * to a float (32 bit)
编辑
查看 JIT 编译时间jitOptVals[0]
会产生误导。如评论中所述,JIT 编译器缓存以前的翻译,如果找到缓存的编译,则不会更新 JIT 编译时间。由于我正在查看此值是否已更改,因此我假设该调用忽略了所有选项。它没有。它工作正常。