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.

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