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.

757 lines
26 KiB

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