This contains my bachelors thesis and associated tex files, code snippets and maybe more. Topic: Data Movement in Heterogeneous Memories with Intel Data Streaming Accelerator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

742 lines
27 KiB

  1. #pragma once
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <shared_mutex>
  5. #include <mutex>
  6. #include <memory>
  7. #include <sched.h>
  8. #include <numa.h>
  9. #include <numaif.h>
  10. #include <dml/dml.hpp>
  11. namespace dml {
  12. inline const std::string StatusCodeToString(const dml::status_code code) {
  13. switch (code) {
  14. case dml::status_code::ok: return "ok";
  15. case dml::status_code::false_predicate: return "false predicate";
  16. case dml::status_code::partial_completion: return "partial completion";
  17. case dml::status_code::nullptr_error: return "nullptr error";
  18. case dml::status_code::bad_size: return "bad size";
  19. case dml::status_code::bad_length: return "bad length";
  20. case dml::status_code::inconsistent_size: return "inconsistent size";
  21. case dml::status_code::dualcast_bad_padding: return "dualcast bad padding";
  22. case dml::status_code::bad_alignment: return "bad alignment";
  23. case dml::status_code::buffers_overlapping: return "buffers overlapping";
  24. case dml::status_code::delta_delta_empty: return "delta delta empty";
  25. case dml::status_code::batch_overflow: return "batch overflow";
  26. case dml::status_code::execution_failed: return "execution failed";
  27. case dml::status_code::unsupported_operation: return "unsupported operation";
  28. case dml::status_code::queue_busy: return "queue busy";
  29. case dml::status_code::error: return "unknown error";
  30. case dml::status_code::config_error: return "config error";
  31. default: return "unhandled error";
  32. }
  33. }
  34. }
  35. namespace dsacache {
  36. constexpr bool WAIT_WEAK = true;
  37. class Cache;
  38. /*
  39. * Class Description:
  40. * Holds all required information on one cache entry and is used
  41. * both internally by the Cache and externally by the user.
  42. *
  43. * Important Usage Notes:
  44. * The pointer is only updated in WaitOnCompletion() which
  45. * therefore must be called by the user at some point in order
  46. * to use the cached data. Using this class as T for
  47. * std::shared_ptr<T> is not recommended as references are
  48. * already counted internally.
  49. *
  50. * Cache Lifetime:
  51. * As long as the instance is referenced, the pointer it stores
  52. * is guaranteed to be either nullptr or pointing to a valid copy.
  53. *
  54. * Implementation Detail:
  55. * Performs self-reference counting with a shared atomic integer.
  56. * Therefore on creating a copy the reference count is increased
  57. * and with the destructor it is deacresed. If the last copy is
  58. * destroyed the actual underlying data is freed and all shared
  59. * variables deleted.
  60. *
  61. * Notes on Thread Safety:
  62. * Class is thread safe in any possible state and performs
  63. * reference counting and deallocation itself entirely atomically.
  64. */
  65. class CacheData {
  66. public:
  67. using dml_handler = dml::handler<dml::mem_copy_operation, std::allocator<uint8_t>>;
  68. private:
  69. static constexpr uint64_t maxptr = 0xffff'ffff'ffff'ffff;
  70. // set to false if we do not own the cache pointer
  71. bool delete_ = false;
  72. // data source and size of the block
  73. uint8_t* src_;
  74. size_t size_;
  75. // global reference counting object
  76. std::atomic<int32_t>* active_;
  77. // global cache-location pointer
  78. std::atomic<uint8_t*>* cache_;
  79. // object-local incomplete cache location pointer
  80. // contract: only access when being in sole posession of handlers
  81. uint8_t** incomplete_cache_;
  82. // dml handler vector pointer which is used
  83. // to wait on caching task completion
  84. std::atomic<std::vector<dml_handler>*>* handlers_;
  85. // deallocates the global cache-location
  86. // and invalidates it
  87. void Deallocate();
  88. size_t GetSize() const { return size_; }
  89. uint8_t* GetSource() const { return src_; }
  90. int32_t GetRefCount() const { return active_->load(); }
  91. void SetTaskHandlersAndCache(uint8_t* cache, std::vector<dml_handler>* handlers);
  92. // initializes the class after which it is thread safe
  93. // but may only be destroyed safely after setting handlers
  94. void Init();
  95. friend Cache;
  96. public:
  97. CacheData(uint8_t* data, const size_t size);
  98. CacheData(const CacheData& other);
  99. ~CacheData();
  100. // waits on completion of caching operations
  101. // for this task and is safe to be called in
  102. // any state of the object
  103. void WaitOnCompletion(const bool weak = false);
  104. // returns the cache data location for this
  105. // instance which is valid as long as the
  106. // instance is alive - !!! this may also
  107. // yield a nullptr !!!
  108. uint8_t* GetDataLocation() const { return cache_->load(); }
  109. };
  110. /*
  111. * Class Description:
  112. * Class will handle access to data through internal copies.
  113. * These are obtained via work submission to the Intel DSA which takes
  114. * care of asynchronously duplicating the data. The user will define
  115. * where these copies lie and which system nodes will perform the copy.
  116. * This is done through policy functions set during initialization.
  117. *
  118. * Placement Policy:
  119. * The Placement Policy Function decides on which node a particular
  120. * entry is to be placed, given the current executing node and the
  121. * data source node and data size. This in turn means that for one
  122. * datum, multiple cached copies may exist at one time.
  123. *
  124. * Cache Lifetime:
  125. * When accessing the cache, a CacheData-object will be returned.
  126. * As long as this object lives, the pointer which it holds is
  127. * guaranteed to be either nullptr or a valid copy. When destroyed
  128. * the entry is marked for deletion which is only carried out
  129. * when system memory pressure drives an automated cache flush.
  130. *
  131. * Restrictions:
  132. * - Overlapping Pointers may lead to undefined behaviour during
  133. * manual cache invalidation which should not be used if you
  134. * intend to have these types of pointers
  135. * - Cache Invalidation may only be performed manually and gives
  136. * no ordering guarantees. Therefore, it is the users responsibility
  137. * to ensure that results after invalidation have been generated
  138. * using the latest state of data. The cache is best suited
  139. * to static data.
  140. *
  141. * Notes on Thread Safety:
  142. * - Cache is completely thread-safe after initialization
  143. * - CacheData-class will handle deallocation of data itself by
  144. * performing self-reference-counting atomically and only
  145. * deallocating if the last reference is destroyed
  146. * - The internal cache state has one lock which is either
  147. * acquired shared for reading the state (upon accessing an already
  148. * cached element) or unique (accessing a new element, flushing, invalidating)
  149. * - Waiting on copy completion is done over an atomic-wait in copies
  150. * of the original CacheData-instance
  151. * - Overall this class may experience performance issues due to the use
  152. * of locking (in any configuration), lock contention (worsens with higher
  153. * core count, node count and utilization) and atomics (worse in the same
  154. * situations as lock contention)
  155. *
  156. * Improving Performance:
  157. * When data is never shared between threads or memory size for the cache is
  158. * not an issue you may consider having one Cache-instance per thread and removing
  159. * the lock in Cache and modifying the reference counting and waiting mechanisms
  160. * of CacheData accordingly (although this is high effort and will yield little due
  161. * to the atomics not being shared among cores/nodes).
  162. * Otherwise, one Cache-instance per node could also be considered. This will allow
  163. * the placement policy function to be barebones and reduces the lock contention and
  164. * synchronization impact of the atomic variables.
  165. */
  166. class Cache {
  167. public:
  168. // cache policy is defined as a type here to allow flexible usage of the cacher
  169. // given a numa destination node (where the data will be needed), the numa source
  170. // node (current location of the data) and the data size, this function should
  171. // return optimal cache placement
  172. // dst node and returned value can differ if the system, for example, has HBM
  173. // attached accessible directly to node n under a different node id m
  174. typedef int (CachePolicy)(const int numa_dst_node, const int numa_src_node, const size_t data_size);
  175. // copy policy specifies the copy-executing nodes for a given task
  176. // which allows flexibility in assignment for optimizing raw throughput
  177. // or choosing a conservative usage policy
  178. typedef std::vector<int> (CopyPolicy)(const int numa_dst_node, const int numa_src_node, const size_t data_size);
  179. private:
  180. // mutex for accessing the cache state map
  181. // map from [dst-numa-node,map2]
  182. // map2 from [data-ptr,cache-structure]
  183. struct LockedNodeCacheState {
  184. std::shared_mutex cache_mutex_;
  185. std::unordered_map<uint8_t*, CacheData> node_cache_state_;
  186. };
  187. std::unordered_map<uint8_t, LockedNodeCacheState*> cache_state_;
  188. CachePolicy* cache_policy_function_ = nullptr;
  189. CopyPolicy* copy_policy_function_ = nullptr;
  190. // function used to submit a copy task on a specific node to the dml
  191. // engine on that node - will change the current threads node assignment
  192. // to achieve this so take care to restore this
  193. dml::handler<dml::mem_copy_operation, std::allocator<uint8_t>> ExecuteCopy(
  194. const uint8_t* src, uint8_t* dst, const size_t size, const int node
  195. ) const;
  196. // allocates the required memory on the destination node
  197. // and then submits task to the dml library for processing
  198. // and attaches the handlers to the cache data structure
  199. void SubmitTask(CacheData* task, const int dst_node, const int src_node);
  200. // querries the policy functions for the given data and size
  201. // to obtain destination cache node, also returns the datas
  202. // source node for further usage
  203. // output may depend on the calling threads node assignment
  204. // as this is set as the "optimal placement" node
  205. void GetCacheNode(uint8_t* src, const size_t size, int* OUT_DST_NODE, int* OUT_SRC_NODE) const;
  206. // allocates memory of size "size" on the numa node "node"
  207. // and returns nullptr if this is not possible, also may
  208. // try to flush the cache of the requested node to
  209. // alleviate encountered shortage
  210. uint8_t* AllocOnNode(const size_t size, const int node);
  211. // checks whether the cache contains an entry for
  212. // the given data in the given memory node and
  213. // returns it, otherwise returns nullptr
  214. std::unique_ptr<CacheData> GetFromCache(uint8_t* src, const size_t size, const int dst_node);
  215. public:
  216. ~Cache();
  217. Cache() = default;
  218. Cache(const Cache& other) = delete;
  219. // initializes the cache with the two policy functions
  220. // only after this is it safe to use in a threaded environment
  221. void Init(CachePolicy* cache_policy_function, CopyPolicy* copy_policy_function);
  222. // function to perform data access through the cache
  223. std::unique_ptr<CacheData> Access(uint8_t* data, const size_t size);
  224. // flushes the cache of inactive entries
  225. // if node is -1 then the whole cache is
  226. // checked and otherwise the specified
  227. // node - no checks on node validity
  228. void Flush(const int node = -1);
  229. // forces out all entries from the
  230. // cache and therefore will also "forget"
  231. // still-in-use entries, these will still
  232. // be properly deleted, but the cache
  233. // will be fresh - use for testing
  234. void Clear();
  235. void Invalidate(uint8_t* data);
  236. };
  237. }
  238. inline void dsacache::Cache::Clear() {
  239. for (auto& nc : cache_state_) {
  240. std::unique_lock<std::shared_mutex> lock(nc.second->cache_mutex_);
  241. nc.second->node_cache_state_.clear();
  242. }
  243. }
  244. inline void dsacache::Cache::Init(CachePolicy* cache_policy_function, CopyPolicy* copy_policy_function) {
  245. cache_policy_function_ = cache_policy_function;
  246. copy_policy_function_ = copy_policy_function;
  247. // initialize numa library
  248. numa_available();
  249. // obtain all available nodes
  250. // and those we may allocate
  251. // memory on
  252. const int nodes_max = numa_num_configured_nodes();
  253. const bitmask* valid_nodes = numa_get_mems_allowed();
  254. // prepare the cache state with entries
  255. // for all given nodes
  256. for (int node = 0; node < nodes_max; node++) {
  257. if (numa_bitmask_isbitset(valid_nodes, node)) {
  258. void* block = numa_alloc_onnode(sizeof(LockedNodeCacheState), node);
  259. auto* state = new(block)LockedNodeCacheState;
  260. cache_state_.insert({node,state});
  261. }
  262. }
  263. }
  264. inline std::unique_ptr<dsacache::CacheData> dsacache::Cache::Access(uint8_t* data, const size_t size) {
  265. // get destination numa node for the cache
  266. int dst_node = -1;
  267. int src_node = -1;
  268. GetCacheNode(data, size, &dst_node, &src_node);
  269. // TODO: at this point it could be beneficial to check whether
  270. // TODO: the given destination node is present as an entry
  271. // TODO: in the cache state to see if it is valid
  272. // check whether the data is already cached
  273. std::unique_ptr<CacheData> task = GetFromCache(data, size, dst_node);
  274. if (task != nullptr) {
  275. return std::move(task);
  276. }
  277. // at this point the requested data is not present in cache
  278. // and we create a caching task for it
  279. task = std::make_unique<CacheData>(data, size);
  280. {
  281. LockedNodeCacheState* local_cache_state = cache_state_[dst_node];
  282. std::unique_lock<std::shared_mutex> lock(local_cache_state->cache_mutex_);
  283. const auto state = local_cache_state->node_cache_state_.emplace(task->GetSource(), *task);
  284. // if state.second is false then no insertion took place
  285. // which means that concurrently whith this thread
  286. // some other thread must have accessed the same
  287. // resource in which case we return the other
  288. // threads data cache structure
  289. if (!state.second) {
  290. std::cout << "[!] Found another cache instance for 0x" << std::hex << (uint64_t)task->GetSource() << std::dec << std::endl;
  291. return std::move(std::make_unique<CacheData>(state.first->second));
  292. }
  293. // initialize the task now for thread safety
  294. // as we are now sure that we will submit work
  295. // to it and will not delete it beforehand
  296. task->Init();
  297. }
  298. SubmitTask(task.get(), dst_node, src_node);
  299. return std::move(task);
  300. }
  301. inline uint8_t* dsacache::Cache::AllocOnNode(const size_t size, const int node) {
  302. // allocate data on this node and flush the unused parts of the
  303. // cache if the operation fails and retry once
  304. // TODO: smarter flush strategy could keep some stuff cached
  305. // check currently free memory to see if the data fits
  306. long long int free_space = 0;
  307. numa_node_size64(node, &free_space);
  308. if (free_space < size) {
  309. std::cout << "[!] Memory shortage when allocating " << size << "B on node " << node << std::endl;
  310. // dst node lacks memory space so we flush the cache for this
  311. // node hoping to free enough currently unused entries to make
  312. // the second allocation attempt successful
  313. Flush(node);
  314. // re-test by getting the free space and checking again
  315. numa_node_size64(node, &free_space);
  316. if (free_space < size) {
  317. std::cout << "[x] Memory shortage after flush when allocating " << size << "B on node " << node << std::endl;
  318. return nullptr;
  319. }
  320. }
  321. uint8_t* dst = reinterpret_cast<uint8_t*>(numa_alloc_onnode(size, node));
  322. if (dst == nullptr) {
  323. std::cout << "[x] Allocation try failed for " << size << "B on node " << node << std::endl;
  324. return nullptr;
  325. }
  326. return dst;
  327. }
  328. inline void dsacache::Cache::SubmitTask(CacheData* task, const int dst_node, const int src_node) {
  329. uint8_t* dst = AllocOnNode(task->GetSize(), dst_node);
  330. if (dst == nullptr) {
  331. std::cout << "[x] Allocation failed so we can not cache" << std::endl;
  332. return;
  333. }
  334. // querry copy policy function for the nodes to use for the copy
  335. const std::vector<int> executing_nodes = copy_policy_function_(dst_node, src_node, task->GetSize());
  336. const size_t task_count = executing_nodes.size();
  337. // each task will copy one fair part of the total size
  338. // and in case the total size is not a factor of the
  339. // given task count the last node must copy the remainder
  340. const size_t size = task->GetSize() / task_count;
  341. const size_t last_size = size + task->GetSize() % task_count;
  342. // save the current numa node mask to restore later
  343. // as executing the copy task will place this thread
  344. // on a different node
  345. auto handlers = new std::vector<CacheData::dml_handler>();
  346. for (uint32_t i = 0; i < task_count; i++) {
  347. const size_t local_size = i + 1 == task_count ? size : last_size;
  348. const size_t local_offset = i * size;
  349. const uint8_t* local_src = task->GetSource() + local_offset;
  350. uint8_t* local_dst = dst + local_offset;
  351. handlers->emplace_back(ExecuteCopy(local_src, local_dst, local_size, executing_nodes[i]));
  352. }
  353. task->SetTaskHandlersAndCache(dst, handlers);
  354. }
  355. inline dml::handler<dml::mem_copy_operation, std::allocator<uint8_t>> dsacache::Cache::ExecuteCopy(
  356. const uint8_t* src, uint8_t* dst, const size_t size, const int node
  357. ) const {
  358. dml::const_data_view srcv = dml::make_view(src, size);
  359. dml::data_view dstv = dml::make_view(dst, size);
  360. return dml::submit<dml::hardware>(
  361. dml::mem_copy.block_on_fault(), srcv, dstv,
  362. dml::execution_interface<dml::hardware,std::allocator<uint8_t>>(), node
  363. );
  364. }
  365. inline void dsacache::Cache::GetCacheNode(uint8_t* src, const size_t size, int* OUT_DST_NODE, int* OUT_SRC_NODE) const {
  366. // obtain numa node of current thread to determine where the data is needed
  367. const int current_cpu = sched_getcpu();
  368. const int current_node = numa_node_of_cpu(current_cpu);
  369. // obtain node that the given data pointer is allocated on
  370. *OUT_SRC_NODE = -1;
  371. get_mempolicy(OUT_SRC_NODE, NULL, 0, (void*)src, MPOL_F_NODE | MPOL_F_ADDR);
  372. // querry cache policy function for the destination numa node
  373. *OUT_DST_NODE = cache_policy_function_(current_node, *OUT_SRC_NODE, size);
  374. }
  375. inline void dsacache::Cache::Flush(const int node) {
  376. // this lambda is used because below we have two code paths that
  377. // flush nodes, either one single or all successively
  378. const auto FlushNode = [](std::unordered_map<uint8_t*,CacheData>& map) {
  379. // begin at the front of the map
  380. auto it = map.begin();
  381. // loop until we reach the end of the map
  382. while (it != map.end()) {
  383. // if the iterator points to an inactive element
  384. // then we may erase it
  385. if (it->second.GetRefCount() <= 1) {
  386. // erase the iterator from the map
  387. map.erase(it);
  388. // as the erasure invalidated out iterator
  389. // we must start at the beginning again
  390. it = map.begin();
  391. }
  392. else {
  393. // if element is active just move over to the next one
  394. it++;
  395. }
  396. }
  397. };
  398. // we require exclusive lock as we modify the cache state
  399. // node == -1 means that cache on all nodes should be flushed
  400. if (node == -1) {
  401. for (auto& nc : cache_state_) {
  402. std::unique_lock<std::shared_mutex> lock(nc.second->cache_mutex_);
  403. FlushNode(nc.second->node_cache_state_);
  404. }
  405. }
  406. else {
  407. std::unique_lock<std::shared_mutex> lock(cache_state_[node]->cache_mutex_);
  408. FlushNode(cache_state_[node]->node_cache_state_);
  409. }
  410. }
  411. inline std::unique_ptr<dsacache::CacheData> dsacache::Cache::GetFromCache(uint8_t* src, const size_t size, const int dst_node) {
  412. // the best situation is if this data is already cached
  413. // which we check in an unnamed block in which the cache
  414. // is locked for reading to prevent another thread
  415. // from marking the element we may find as unused and
  416. // clearing it
  417. LockedNodeCacheState* local_cache_state = cache_state_[dst_node];
  418. // lock the cache state in shared-mode because we read
  419. std::shared_lock<std::shared_mutex> lock(local_cache_state->cache_mutex_);
  420. // search for the data in our cache state structure at the given node
  421. const auto search = local_cache_state->node_cache_state_.find(src);
  422. // if the data is in our structure we continue
  423. if (search != local_cache_state->node_cache_state_.end()) {
  424. // now check whether the sizes match
  425. if (search->second.GetSize() >= size) {
  426. // return a unique copy of the entry which uses the object
  427. // lifetime and destructor to safely handle deallocation
  428. return std::move(std::make_unique<CacheData>(search->second));
  429. }
  430. else {
  431. // if the sizes missmatch then we clear the current entry from cache
  432. // which will cause its deletion only after the last possible outside
  433. // reference is also destroyed
  434. local_cache_state->node_cache_state_.erase(search);
  435. }
  436. }
  437. return nullptr;
  438. }
  439. void dsacache::Cache::Invalidate(uint8_t* data) {
  440. // as the cache is modified we must obtain a unique writers lock
  441. // loop through all per-node-caches available
  442. for (auto node : cache_state_) {
  443. std::unique_lock<std::shared_mutex> lock(node.second->cache_mutex_);
  444. // search for an entry for the given data pointer
  445. auto search = node.second->node_cache_state_.find(data);
  446. if (search != node.second->node_cache_state_.end()) {
  447. // if the data is represented in-cache
  448. // then it will be erased to re-trigger
  449. // caching on next access
  450. node.second->node_cache_state_.erase(search);
  451. }
  452. }
  453. }
  454. inline dsacache::Cache::~Cache() {
  455. for (auto node : cache_state_) {
  456. node.second->~LockedNodeCacheState();
  457. numa_free(reinterpret_cast<void*>(node.second), sizeof(LockedNodeCacheState));
  458. }
  459. }
  460. inline dsacache::CacheData::CacheData(uint8_t* data, const size_t size) {
  461. src_ = data;
  462. size_ = size;
  463. delete_ = false;
  464. active_ = new std::atomic<int32_t>(1);
  465. cache_ = new std::atomic<uint8_t*>(data);
  466. handlers_ = new std::atomic<std::vector<dml_handler>*>();
  467. incomplete_cache_ = new uint8_t*(nullptr);
  468. }
  469. inline dsacache::CacheData::CacheData(const dsacache::CacheData& other) {
  470. // we copy the ptr to the global atomic reference counter
  471. // and increase the amount of active references
  472. active_ = other.active_;
  473. const int current_active = active_->fetch_add(1);
  474. src_ = other.src_;
  475. size_ = other.size_;
  476. cache_ = other.cache_;
  477. incomplete_cache_ = other.incomplete_cache_;
  478. handlers_ = other.handlers_;
  479. }
  480. inline dsacache::CacheData::~CacheData() {
  481. // due to fetch_sub returning the preivously held value
  482. // we must subtract one locally to get the current value
  483. const int32_t v = active_->fetch_sub(1) - 1;
  484. // if the returned value is zero or lower
  485. // then we must execute proper deletion
  486. // as this was the last reference
  487. if (v == 0) {
  488. // on deletion we must ensure that all offloaded
  489. // operations have completed successfully
  490. WaitOnCompletion();
  491. // only then can we deallocate the memory
  492. Deallocate();
  493. delete active_;
  494. delete cache_;
  495. delete handlers_;
  496. delete incomplete_cache_;
  497. }
  498. }
  499. inline void dsacache::CacheData::Deallocate() {
  500. // although deallocate should only be called from
  501. // a safe context to do so, it can not hurt to
  502. // defensively perform the operation atomically
  503. // and check for incomplete cache if no deallocation
  504. // takes place for the retrieved local cache
  505. uint8_t* cache_local = cache_->exchange(nullptr);
  506. if (cache_local != nullptr && delete_) numa_free(cache_local, size_);
  507. else if (*incomplete_cache_ != nullptr) numa_free(*incomplete_cache_, size_);
  508. else;
  509. }
  510. inline void dsacache::CacheData::WaitOnCompletion(const bool weak) {
  511. // first check if waiting is even neccessary as a valid
  512. // cache pointer signals that no waiting is to be performed
  513. if (cache_->load() != nullptr) {
  514. return;
  515. }
  516. // then check if the handlers are available
  517. handlers_->wait(nullptr);
  518. // exchange the global handlers pointer with nullptr to have a local
  519. // copy - this signals that this thread is the sole owner and therefore
  520. // responsible for waiting for them. we can not set to nullptr here but
  521. // set to maximum of 64-bit in order to prevent deadlocks from the above
  522. // waiting construct
  523. std::vector<dml_handler>* local_handlers = handlers_->exchange(reinterpret_cast<std::vector<dml_handler>*>(maxptr));
  524. // ensure that no other thread snatched the handlers before us
  525. // and in case one did, wait again and then return
  526. if (local_handlers == nullptr || local_handlers == reinterpret_cast<std::vector<dml_handler>*>(maxptr)) {
  527. cache_->wait(nullptr);
  528. return;
  529. }
  530. // at this point we are responsible for waiting for the handlers
  531. // and handling any error that comes through them gracefully
  532. bool error = false;
  533. for (auto& handler : *local_handlers) {
  534. if (weak && !handler.is_finished()) {
  535. handlers_->store(local_handlers);
  536. return;
  537. }
  538. auto result = handler.get();
  539. if (result.status != dml::status_code::ok) {
  540. std::cerr << "[x] Encountered bad status code for operation: " << dml::StatusCodeToString(result.status) << std::endl;
  541. // if one of the copy tasks failed we abort the whole task
  542. // after all operations are completed on it
  543. error = true;
  544. }
  545. }
  546. // at this point all handlers have been waited for
  547. // and therefore may be decomissioned
  548. delete local_handlers;
  549. // handle errors now by aborting the cache
  550. if (error) {
  551. cache_->store(src_);
  552. numa_free(*incomplete_cache_, size_);
  553. delete_ = false;
  554. *incomplete_cache_ = nullptr;
  555. }
  556. else {
  557. cache_->store(*incomplete_cache_);
  558. }
  559. // notify all waiting threads so they wake up quickly
  560. cache_->notify_all();
  561. handlers_->notify_all();
  562. }
  563. void dsacache::CacheData::SetTaskHandlersAndCache(uint8_t* cache, std::vector<dml_handler>* handlers) {
  564. *incomplete_cache_ = cache;
  565. handlers_->store(handlers);
  566. handlers_->notify_one();
  567. }
  568. void dsacache::CacheData::Init() {
  569. cache_->store(nullptr);
  570. delete_ = true;
  571. }