/* * Copyright (C) 2017-2018 Intel Corporation * * SPDX-License-Identifier: MIT * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include namespace OCLRT { std::mutex BinaryCache::cacheAccessMtx; const std::string BinaryCache::getCachedFileName(const HardwareInfo &hwInfo, const ArrayRef input, const ArrayRef options, const ArrayRef internalOptions) { Hash hash; hash.update("----", 4); hash.update(&*input.begin(), input.size()); hash.update("----", 4); hash.update(&*options.begin(), options.size()); hash.update("----", 4); hash.update(&*internalOptions.begin(), internalOptions.size()); hash.update("----", 4); hash.update(reinterpret_cast(hwInfo.pPlatform), sizeof(*hwInfo.pPlatform)); hash.update("----", 4); hash.update(reinterpret_cast(hwInfo.pSkuTable), sizeof(*hwInfo.pSkuTable)); hash.update("----", 4); hash.update(reinterpret_cast(hwInfo.pWaTable), sizeof(*hwInfo.pWaTable)); auto res = hash.finish(); std::stringstream stream; stream << std::setfill('0') << std::setw(sizeof(res) * 2) << std::hex << res; return stream.str(); } bool BinaryCache::cacheBinary(const std::string kernelFileHash, const char *pBinary, uint32_t binarySize) { if (pBinary == nullptr || binarySize == 0) { return false; } std::string hashFilePath = CL_CACHE_LOCATION; hashFilePath.append(Os::fileSeparator); hashFilePath.append(kernelFileHash + ".cl_cache"); std::lock_guard lock(cacheAccessMtx); if (writeDataToFile( hashFilePath.c_str(), pBinary, binarySize) == 0) { return false; } return true; } bool BinaryCache::loadCachedBinary(const std::string kernelFileHash, Program &program) { void *pBinary = nullptr; size_t binarySize = 0; std::string hashFilePath = CL_CACHE_LOCATION; hashFilePath.append(Os::fileSeparator); hashFilePath.append(kernelFileHash + ".cl_cache"); { std::lock_guard lock(cacheAccessMtx); binarySize = loadDataFromFile(hashFilePath.c_str(), pBinary); } if ((pBinary == nullptr) || (binarySize == 0)) { deleteDataReadFromFile(pBinary); return false; } program.storeGenBinary(pBinary, binarySize); deleteDataReadFromFile(pBinary); return true; } } // namespace OCLRT