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.

749 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. auto handlers = new std::vector<dml_job_t*>();
  317. for (uint32_t i = 0; i < task_count; i++) {
  318. const size_t local_size = i + 1 == task_count ? size : last_size;
  319. const size_t local_offset = i * size;
  320. const uint8_t* local_src = task->GetSource() + local_offset;
  321. uint8_t* local_dst = dst + local_offset;
  322. handlers->emplace_back(ExecuteCopy(local_src, local_dst, local_size, executing_nodes[i]));
  323. }
  324. task->SetTaskHandlersAndCache(dst, handlers);
  325. }
  326. inline dml_job_t* dsacache::Cache::ExecuteCopy(
  327. const uint8_t* src, uint8_t* dst, const size_t size, const int node
  328. ) const {
  329. uint32_t job_size = 0;
  330. dml_status_t status = dml_get_job_size(DML_PATH_HW, &job_size);
  331. if (status != DML_STATUS_OK) {
  332. std::cerr << "[x] Error querying job size!" << std::endl;
  333. return nullptr;
  334. }
  335. dml_job_t* job = (dml_job_t*)malloc(job_size);
  336. status = dml_init_job(DML_PATH_HW, job);
  337. if (status != DML_STATUS_OK) {
  338. std::cerr << "[x] Error initializing job!" << std::endl;
  339. delete job;
  340. return nullptr;
  341. }
  342. job->operation = DML_OP_MEM_MOVE;
  343. job->source_first_ptr = (uint8_t*)src;
  344. job->destination_first_ptr = dst;
  345. job->source_length = size;
  346. job->flags |= DML_FLAG_BLOCK_ON_FAULT | DML_FLAG_COPY_ONLY;
  347. job->numa_id = node;
  348. status = dml_submit_job(job);
  349. if (status != DML_STATUS_OK) {
  350. std::cerr << "[x] Error submitting job!" << std::endl;
  351. delete job;
  352. return nullptr;
  353. }
  354. return job;
  355. }
  356. inline void dsacache::Cache::GetCacheNode(uint8_t* src, const size_t size, int* OUT_DST_NODE, int* OUT_SRC_NODE) const {
  357. // obtain numa node of current thread to determine where the data is needed
  358. const int current_cpu = sched_getcpu();
  359. const int current_node = numa_node_of_cpu(current_cpu);
  360. // obtain node that the given data pointer is allocated on
  361. *OUT_SRC_NODE = -1;
  362. get_mempolicy(OUT_SRC_NODE, NULL, 0, (void*)src, MPOL_F_NODE | MPOL_F_ADDR);
  363. // querry cache policy function for the destination numa node
  364. *OUT_DST_NODE = cache_policy_function_(current_node, *OUT_SRC_NODE, size);
  365. }
  366. inline void dsacache::Cache::Flush(const int node) {
  367. // this lambda is used because below we have two code paths that
  368. // flush nodes, either one single or all successively
  369. const auto FlushNode = [](std::unordered_map<uint8_t*,CacheData>& map) {
  370. // begin at the front of the map
  371. auto it = map.begin();
  372. // loop until we reach the end of the map
  373. while (it != map.end()) {
  374. // if the iterator points to an inactive element
  375. // then we may erase it
  376. if (it->second.GetRefCount() <= 1) {
  377. // erase the iterator from the map
  378. map.erase(it);
  379. // as the erasure invalidated out iterator
  380. // we must start at the beginning again
  381. it = map.begin();
  382. }
  383. else {
  384. // if element is active just move over to the next one
  385. it++;
  386. }
  387. }
  388. };
  389. // we require exclusive lock as we modify the cache state
  390. // node == -1 means that cache on all nodes should be flushed
  391. if (node == -1) {
  392. for (auto& nc : cache_state_) {
  393. std::unique_lock<std::shared_mutex> lock(nc.second->cache_mutex_);
  394. FlushNode(nc.second->node_cache_state_);
  395. }
  396. }
  397. else {
  398. std::unique_lock<std::shared_mutex> lock(cache_state_[node]->cache_mutex_);
  399. FlushNode(cache_state_[node]->node_cache_state_);
  400. }
  401. }
  402. inline std::unique_ptr<dsacache::CacheData> dsacache::Cache::GetFromCache(uint8_t* src, const size_t size, const int dst_node) {
  403. // the best situation is if this data is already cached
  404. // which we check in an unnamed block in which the cache
  405. // is locked for reading to prevent another thread
  406. // from marking the element we may find as unused and
  407. // clearing it
  408. LockedNodeCacheState* local_cache_state = cache_state_[dst_node];
  409. // lock the cache state in shared-mode because we read
  410. std::shared_lock<std::shared_mutex> lock(local_cache_state->cache_mutex_);
  411. // search for the data in our cache state structure at the given node
  412. const auto search = local_cache_state->node_cache_state_.find(src);
  413. // if the data is in our structure we continue
  414. if (search != local_cache_state->node_cache_state_.end()) {
  415. // now check whether the sizes match
  416. if (search->second.GetSize() >= size) {
  417. // return a unique copy of the entry which uses the object
  418. // lifetime and destructor to safely handle deallocation
  419. return std::move(std::make_unique<CacheData>(search->second));
  420. }
  421. else {
  422. // if the sizes missmatch then we clear the current entry from cache
  423. // which will cause its deletion only after the last possible outside
  424. // reference is also destroyed
  425. local_cache_state->node_cache_state_.erase(search);
  426. }
  427. }
  428. return nullptr;
  429. }
  430. void dsacache::Cache::Invalidate(uint8_t* data) {
  431. // as the cache is modified we must obtain a unique writers lock
  432. // loop through all per-node-caches available
  433. for (auto node : cache_state_) {
  434. std::unique_lock<std::shared_mutex> lock(node.second->cache_mutex_);
  435. // search for an entry for the given data pointer
  436. auto search = node.second->node_cache_state_.find(data);
  437. if (search != node.second->node_cache_state_.end()) {
  438. // if the data is represented in-cache
  439. // then it will be erased to re-trigger
  440. // caching on next access
  441. node.second->node_cache_state_.erase(search);
  442. }
  443. }
  444. }
  445. inline dsacache::Cache::~Cache() {
  446. for (auto node : cache_state_) {
  447. node.second->~LockedNodeCacheState();
  448. numa_free(reinterpret_cast<void*>(node.second), sizeof(LockedNodeCacheState));
  449. }
  450. }
  451. inline dsacache::CacheData::CacheData(uint8_t* data, const size_t size) {
  452. src_ = data;
  453. size_ = size;
  454. delete_ = false;
  455. active_ = new std::atomic<int32_t>(1);
  456. cache_ = new std::atomic<uint8_t*>(data);
  457. handlers_ = new std::atomic<std::vector<dml_job_t*>*>();
  458. incomplete_cache_ = new uint8_t*(nullptr);
  459. }
  460. inline dsacache::CacheData::CacheData(const dsacache::CacheData& other) {
  461. // we copy the ptr to the global atomic reference counter
  462. // and increase the amount of active references
  463. active_ = other.active_;
  464. const int current_active = active_->fetch_add(1);
  465. src_ = other.src_;
  466. size_ = other.size_;
  467. cache_ = other.cache_;
  468. incomplete_cache_ = other.incomplete_cache_;
  469. handlers_ = other.handlers_;
  470. }
  471. inline dsacache::CacheData::~CacheData() {
  472. // due to fetch_sub returning the preivously held value
  473. // we must subtract one locally to get the current value
  474. const int32_t v = active_->fetch_sub(1) - 1;
  475. // if the returned value is zero or lower
  476. // then we must execute proper deletion
  477. // as this was the last reference
  478. if (v == 0) {
  479. // on deletion we must ensure that all offloaded
  480. // operations have completed successfully
  481. WaitOnCompletion();
  482. // only then can we deallocate the memory
  483. Deallocate();
  484. std::vector<dml_job_t*>* handlers = handlers_->load();
  485. if (handlers != nullptr && handlers != reinterpret_cast<std::vector<dml_job_t*>*>(maxptr)) {
  486. for (dml_job_t* job : *handlers_->load()) {
  487. if (job != nullptr) delete job;
  488. }
  489. }
  490. delete active_;
  491. delete cache_;
  492. delete handlers_;
  493. delete incomplete_cache_;
  494. }
  495. }
  496. inline void dsacache::CacheData::Deallocate() {
  497. // although deallocate should only be called from
  498. // a safe context to do so, it can not hurt to
  499. // defensively perform the operation atomically
  500. // and check for incomplete cache if no deallocation
  501. // takes place for the retrieved local cache
  502. uint8_t* cache_local = cache_->exchange(nullptr);
  503. if (cache_local != nullptr && delete_) numa_free(cache_local, size_);
  504. else if (*incomplete_cache_ != nullptr) numa_free(*incomplete_cache_, size_);
  505. else;
  506. }
  507. inline void dsacache::CacheData::WaitOnCompletion() {
  508. // first check if waiting is even neccessary as a valid
  509. // cache pointer signals that no waiting is to be performed
  510. if (cache_->load() != nullptr) {
  511. return;
  512. }
  513. // then check if the handlers are available
  514. handlers_->wait(nullptr);
  515. // exchange the global handlers pointer with nullptr to have a local
  516. // copy - this signals that this thread is the sole owner and therefore
  517. // responsible for waiting for them. we can not set to nullptr here but
  518. // set to maximum of 64-bit in order to prevent deadlocks from the above
  519. // waiting construct
  520. std::vector<dml_job_t*>* local_handlers = handlers_->exchange(reinterpret_cast<std::vector<dml_job_t*>*>(maxptr));
  521. // ensure that no other thread snatched the handlers before us
  522. // and in case one did, wait again and then return
  523. if (local_handlers == nullptr || local_handlers == reinterpret_cast<std::vector<dml_job_t*>*>(maxptr)) {
  524. cache_->wait(nullptr);
  525. return;
  526. }
  527. // at this point we are responsible for waiting for the handlers
  528. // and handling any error that comes through them gracefully
  529. bool error = false;
  530. for (dml_job_t* job : *local_handlers) {
  531. if (job == nullptr) {
  532. std::cerr << "[x] Got nullptr-job!" << std::endl;
  533. // if one of the copy tasks failed we abort the whole task
  534. // after all operations are completed on it
  535. error = true;
  536. }
  537. else {
  538. const dml_status_t status = dml_wait_job(job, DML_WAIT_MODE_UMWAIT);
  539. if (status != DML_STATUS_OK) {
  540. std::cerr << "[x] Operation Failed!" << 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. delete job;
  547. }
  548. // at this point all handlers have been waited for
  549. // and therefore may be decomissioned
  550. delete local_handlers;
  551. // handle errors now by aborting the cache
  552. if (error) {
  553. cache_->store(src_);
  554. numa_free(*incomplete_cache_, size_);
  555. delete_ = false;
  556. *incomplete_cache_ = nullptr;
  557. }
  558. else {
  559. cache_->store(*incomplete_cache_);
  560. }
  561. // notify all waiting threads so they wake up quickly
  562. cache_->notify_all();
  563. handlers_->notify_all();
  564. }
  565. void dsacache::CacheData::SetTaskHandlersAndCache(uint8_t* cache, std::vector<dml_job_t*>* handlers) {
  566. *incomplete_cache_ = cache;
  567. handlers_->store(handlers);
  568. handlers_->notify_one();
  569. }
  570. void dsacache::CacheData::Init() {
  571. cache_->store(nullptr);
  572. delete_ = true;
  573. }