| 411 | } |
| 412 | |
| 413 | Module loadModuleFromDisk(const int device, const string &moduleKey, |
| 414 | const bool isJIT) { |
| 415 | const string &cacheDirectory = getCacheDirectory(); |
| 416 | if (cacheDirectory.empty()) return Module{nullptr}; |
| 417 | |
| 418 | const string cacheFile = cacheDirectory + AF_PATH_SEPARATOR + |
| 419 | getKernelCacheFilename(device, moduleKey); |
| 420 | |
| 421 | CUmodule modOut = nullptr; |
| 422 | Module retVal{nullptr}; |
| 423 | try { |
| 424 | std::ifstream in(cacheFile, std::ios::binary); |
| 425 | if (!in) { |
| 426 | AF_TRACE("{{{:<20} : Unable to open {} for {}}}", moduleKey, |
| 427 | cacheFile, getDeviceProp(device).name); |
| 428 | removeFile(cacheFile); // Remove if exists |
| 429 | return Module{nullptr}; |
| 430 | } |
| 431 | in.exceptions(std::ios::failbit | std::ios::badbit); |
| 432 | |
| 433 | if (!isJIT) { |
| 434 | size_t mangledListSize = 0; |
| 435 | in.read(reinterpret_cast<char *>(&mangledListSize), |
| 436 | sizeof(mangledListSize)); |
| 437 | for (size_t i = 0; i < mangledListSize; ++i) { |
| 438 | size_t keySize = 0; |
| 439 | in.read(reinterpret_cast<char *>(&keySize), sizeof(keySize)); |
| 440 | vector<char> key; |
| 441 | key.reserve(keySize); |
| 442 | in.read(key.data(), keySize); |
| 443 | |
| 444 | size_t itemSize = 0; |
| 445 | in.read(reinterpret_cast<char *>(&itemSize), sizeof(itemSize)); |
| 446 | vector<char> item; |
| 447 | item.reserve(itemSize); |
| 448 | in.read(item.data(), itemSize); |
| 449 | |
| 450 | retVal.add(string(key.data(), keySize), |
| 451 | string(item.data(), itemSize)); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | size_t cubinHash = 0; |
| 456 | in.read(reinterpret_cast<char *>(&cubinHash), sizeof(cubinHash)); |
| 457 | size_t cubinSize = 0; |
| 458 | in.read(reinterpret_cast<char *>(&cubinSize), sizeof(cubinSize)); |
| 459 | vector<char> cubin(cubinSize); |
| 460 | in.read(cubin.data(), cubinSize); |
| 461 | in.close(); |
| 462 | |
| 463 | // check CUBIN binary data has not been corrupted |
| 464 | const size_t recomputedHash = |
| 465 | deterministicHash(cubin.data(), cubinSize); |
| 466 | if (recomputedHash != cubinHash) { |
| 467 | AF_ERROR("Module on disk seems to be corrupted", AF_ERR_LOAD_SYM); |
| 468 | } |
| 469 | |
| 470 | CU_CHECK(cuModuleLoadData(&modOut, cubin.data())); |
no test coverage detected