gpt4 book ai didi

c++ - cuModuleLoadDataEx 忽略所有选项

转载 作者:行者123 更新时间:2023-11-30 01:58:41 25 4
gpt4 key购买 nike

这个问题类似于cuModuleLoadDataEx options但我想再次提出这个话题,并提供更多信息。

当通过 cuModuleLoadDataEx 使用 NV 驱动程序加载 PTX 字符串时,它似乎忽略了所有选项。我提供了完整的工作示例,以便任何感兴趣的人都可以直接且毫不费力地重现它。首先是一个小型 PTX 内核(将其保存为 small.ptx),然后是加载 PTX 内核的 C++ 程序。

.version 3.1
.target sm_20, texmode_independent
.address_size 64
.entry main()
{
ret;
}

主.cc

#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

如果有人能够从编译过程中提取任何有用的信息,那就太好了! CUDA 驱动程序 API 的文档,其中解释了 cuModuleLoadDataEx(以及它应该接受的选项)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 编译时间。由于我正在查看此值是否已更改,因此我假设调用忽略了所有选项。它没有。它工作正常。

最佳答案

您的 jitOptVals 不应包含指向您的值的指针,而是将值转换为 void*:

// 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;
//Keep jitOptVals[2] empty as it only an Output value:
//jitOptVals[2] = (void*)jitTime;

cuModuleLoadDataEx 之后,您将获得 jitTime,例如 jitTime = (float)jitOptions[2];

关于c++ - cuModuleLoadDataEx 忽略所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com