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.

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