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.

163 lines
6.5 KiB

  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <chrono>
  5. #include <numeric>
  6. #include <pthread.h>
  7. #include <semaphore.h>
  8. #include <numa.h>
  9. #include <dml/dml.hpp>
  10. #include "barrier.hpp"
  11. #include "statuscode-tostring.hpp"
  12. #include "task-data.hpp"
  13. #define LOG_CODE_INFO "Location: " << __FILE__ << "@" << __LINE__ << "::" << __FUNCTION__ << std::endl
  14. #define LOG_ERR { pthread_t t = pthread_self(); std::cerr << "--- BEGIN ERROR MSG ---" << std::endl << "Physical: [Node " << args->numa_node << " | Thread " << t << "]" << std::endl; } std::cerr << LOG_CODE_INFO
  15. #define CHECK_STATUS(status,msg) { if (status != dml::status_code::ok) { LOG_ERR << "Status Code: " << StatusCodeToString(status) << std::endl << msg << std::endl; args->status = status; return nullptr; }}
  16. #define ADD_TIMING_MESSUREMENT { if (i >= 5) { args->submit_duration.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(se - st).count()); args->complete_duration.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - se).count()); args->combined_duration.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - st).count());}}
  17. template <typename path>
  18. void* thread_function(void* argp) {
  19. TaskData* args = reinterpret_cast<TaskData*>(argp);
  20. // set numa node and core affinity of the current thread
  21. numa_run_on_node(args->numa_node);
  22. // allocate memory for the move operation on the requested numa nodes
  23. void* src = numa_alloc_onnode(args->size, args->nnode_src);
  24. void* dst = numa_alloc_onnode(args->size, args->nnode_dst);
  25. dml::data_view srcv = dml::make_view(reinterpret_cast<uint8_t*>(src), args->size);
  26. dml::data_view dstv = dml::make_view(reinterpret_cast<uint8_t*>(dst), args->size);
  27. args->status = dml::status_code::ok;
  28. args->rep_completed = 0;
  29. // we add 5 as the first 5 iterations will not be meassured
  30. // to remove exceptional values encountered during warmup
  31. for (uint32_t i = 0; i < args->rep_count + 5; i++) {
  32. // synchronize the start of each iteration
  33. // using the barrier structure
  34. args->barrier_->wait();
  35. if (args->batch_submit) {
  36. const auto st = std::chrono::steady_clock::now();
  37. auto sequence = dml::sequence(args->batch_size, std::allocator<dml::byte_t>());
  38. for (uint32_t j = 0; j < args->batch_size; j++) {
  39. // block_on_fault() is required to submit the task in a way so that the
  40. // DSA engine can handle page faults itself together with the IOMMU which
  41. // requires the WQ to be configured to allow this too
  42. const auto status = sequence.add(dml::mem_copy.block_on_fault(), srcv, dstv);
  43. CHECK_STATUS(status, "Adding operation to batch failed!");
  44. }
  45. // we use the asynchronous submit-routine even though this is not required
  46. // here, however the project later on will only use async operation and
  47. // therefore this behaviour should be benchmarked
  48. auto handler = dml::submit<path>(dml::batch, sequence);
  49. const auto se = std::chrono::steady_clock::now();
  50. auto result = handler.get();
  51. const auto et = std::chrono::steady_clock::now();
  52. const dml::status_code status = result.status;
  53. CHECK_STATUS(status, "Batch completed with an Error!");
  54. ADD_TIMING_MESSUREMENT;
  55. }
  56. else if (args->batch_size > 1) {
  57. // implementation for non-batched batch submit follows here
  58. // this means we submit a bunch of work as single descriptors
  59. // but then dont wait for the completion immediately
  60. std::vector<dml::handler<dml::mem_copy_operation, std::allocator<uint8_t>>> handlers;
  61. const auto st = std::chrono::steady_clock::now();
  62. for (uint32_t j = 0; j < args->batch_size; j++) {
  63. // block_on_fault() is required to submit the task in a way so that the
  64. // DSA engine can handle page faults itself together with the IOMMU which
  65. // requires the WQ to be configured to allow this too
  66. handlers.emplace_back(dml::submit<path>(dml::mem_copy.block_on_fault(), srcv, dstv));
  67. }
  68. const auto se = std::chrono::steady_clock::now();
  69. for (auto& handler : handlers) {
  70. auto result = handler.get();
  71. const dml::status_code status = result.status;
  72. CHECK_STATUS(status, "Operation completed with an Error!");
  73. }
  74. const auto et = std::chrono::steady_clock::now();
  75. ADD_TIMING_MESSUREMENT;
  76. }
  77. else {
  78. const auto st = std::chrono::steady_clock::now();
  79. // we use the asynchronous submit-routine even though this is not required
  80. // here, however the project later on will only use async operation and
  81. // therefore this behaviour should be benchmarked
  82. // block_on_fault() is required to submit the task in a way so that the
  83. // DSA engine can handle page faults itself together with the IOMMU which
  84. // requires the WQ to be configured to allow this too
  85. auto handler = dml::submit<path>(dml::mem_copy.block_on_fault(), srcv, dstv);
  86. const auto se = std::chrono::steady_clock::now();
  87. auto result = handler.get();
  88. const auto et = std::chrono::steady_clock::now();
  89. const dml::status_code status = result.status;
  90. CHECK_STATUS(status, "Operation completed with an Error!");
  91. ADD_TIMING_MESSUREMENT;
  92. }
  93. // again: we do not count the first 5 repetitions
  94. if (i >= 5) args->rep_completed++;
  95. }
  96. // free the allocated memory regions on the selected nodes
  97. numa_free(src, args->size);
  98. numa_free(dst, args->size);
  99. return nullptr;
  100. }
  101. template <typename path>
  102. void execute_dml_memcpy(std::vector<TaskData>& args) {
  103. barrier task_barrier(args.size());
  104. std::vector<pthread_t> threads;
  105. // initialize numa library
  106. numa_available();
  107. // for each submitted task we link the semaphore
  108. // and create the thread, passing the argument
  109. for (auto& arg : args) {
  110. arg.barrier_ = &task_barrier;
  111. threads.emplace_back();
  112. if (pthread_create(&threads.back(), nullptr, thread_function<path>, &arg) != 0) {
  113. std::cerr << "Error creating thread" << std::endl;
  114. exit(1);
  115. }
  116. }
  117. for (pthread_t& t : threads) {
  118. pthread_join(t, nullptr);
  119. }
  120. }