| 1166 | } |
| 1167 | |
| 1168 | inline void updateTimingCacheFile(std::string const& fileName, nvinfer1::ITimingCache const* timingCache) |
| 1169 | { |
| 1170 | // Prepare empty timingCache in case that there is no existing file to read |
| 1171 | std::unique_ptr<nvinfer1::IBuilder> builder{createBuilder()}; |
| 1172 | std::unique_ptr<nvinfer1::IBuilderConfig> config{builder->createBuilderConfig()}; |
| 1173 | std::unique_ptr<nvinfer1::ITimingCache> fileTimingCache{ |
| 1174 | config->createTimingCache(static_cast<const void*>(nullptr), 0)}; |
| 1175 | |
| 1176 | std::unique_ptr<samplesCommon::FileLock> fileLock{new samplesCommon::FileLock(fileName)}; |
| 1177 | std::ifstream iFile(fileName, std::ios::in | std::ios::binary); |
| 1178 | if (iFile) |
| 1179 | { |
| 1180 | iFile.seekg(0, std::ifstream::end); |
| 1181 | size_t fsize = iFile.tellg(); |
| 1182 | iFile.seekg(0, std::ifstream::beg); |
| 1183 | std::vector<char> content(fsize); |
| 1184 | iFile.read(content.data(), fsize); |
| 1185 | iFile.close(); |
| 1186 | sample::gLogInfo << "Loaded " << fsize << " bytes of timing cache from " << fileName << std::endl; |
| 1187 | fileTimingCache.reset(config->createTimingCache(static_cast<const void*>(content.data()), content.size())); |
| 1188 | if (!fileTimingCache) |
| 1189 | { |
| 1190 | throw std::runtime_error("Failed to create timingCache from " + fileName + "!"); |
| 1191 | } |
| 1192 | } |
| 1193 | fileTimingCache->combine(*timingCache, false); |
| 1194 | std::unique_ptr<nvinfer1::IHostMemory> blob{fileTimingCache->serialize()}; |
| 1195 | if (!blob) |
| 1196 | { |
| 1197 | throw std::runtime_error("Failed to serialize ITimingCache!"); |
| 1198 | } |
| 1199 | std::ofstream oFile(fileName, std::ios::out | std::ios::binary); |
| 1200 | if (!oFile) |
| 1201 | { |
| 1202 | sample::gLogWarning << "Could not write timing cache to: " << fileName << std::endl; |
| 1203 | return; |
| 1204 | } |
| 1205 | oFile.write((char*) blob->data(), blob->size()); |
| 1206 | oFile.close(); |
| 1207 | sample::gLogInfo << "Saved " << blob->size() << " bytes of timing cache to " << fileName << std::endl; |
| 1208 | } |
| 1209 | |
| 1210 | } // namespace samplesCommon |
| 1211 |
no test coverage detected