error: ‘class nvinfer1::IRuntime’ has no member named ‘getLastError’ 32 | std::cerr << "Error: " << runtime->getLastError() << std::endl;
时间: 2023-12-21 10:03:43 浏览: 67
对不起,我之前的回答有误。`nvinfer1::IRuntime`确实没有`getLastError()`函数。如果您想要获取错误信息,可以使用`nvinfer1::IHostMemory`对象来捕获反序列化引擎的错误信息。以下是一个修改后的示例代码片段:
```
int main()
{
// Create the inference runtime
nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(...);
// Deserialize the CUDA engine
std::ifstream engineFile("engine.plan", std::ios::binary);
engineFile.seekg(0, std::ios::end);
const size_t fileSize = engineFile.tellg();
engineFile.seekg(0, std::ios::beg);
std::vector<char> engineData(fileSize);
engineFile.read(engineData.data(), fileSize);
nvinfer1::IHostMemory* engineMemory = runtime->deserializeCudaEngine(engineData.data(), fileSize);
// Check if deserialization was successful
if (engineMemory == nullptr)
{
std::cerr << "Error: Failed to deserialize CUDA engine" << std::endl;
return 1;
}
// Check if there were any errors during deserialization
if (engineMemory->size() == 0)
{
std::cerr << "Error: " << std::string(static_cast<char*>(engineMemory->data()), engineMemory->size()) << std::endl;
return 1;
}
// Deserialize the engine from the host memory
nvinfer1::ICudaEngine* engine = runtime->deserializeCudaEngine(engineMemory->data(), engineMemory->size(), nullptr);
// ...
}
```
在上述示例中,`deserializeCudaEngine()`函数返回一个`nvinfer1::IHostMemory`对象,该对象包含了反序列化引擎的状态。如果返回的`IHostMemory`对象的大小为0,则说明发生了错误。此时,您可以使用`std::string(static_cast<char*>(engineMemory->data()), engineMemory->size())`来获取错误消息。
阅读全文