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.

173 lines
6.9 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 "util/barrier.hpp"
  11. #include "util/dml-helper.hpp"
  12. #include "util/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::nanoseconds>(se - st).count()); args->complete_duration.emplace_back(std::chrono::duration_cast<std::chrono::nanoseconds>(et - se).count()); args->combined_duration.emplace_back(std::chrono::duration_cast<std::chrono::nanoseconds>(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. std::memset(src, 0, args->size);
  28. std::memset(dst, 0, args->size);
  29. args->status = dml::status_code::ok;
  30. args->rep_completed = 0;
  31. std::chrono::time_point<std::chrono::steady_clock> tps;
  32. // we add 5 as the first 5 iterations will not be meassured
  33. // to remove exceptional values encountered during warmup
  34. for (uint32_t i = 0; i < args->rep_count + 5; i++) {
  35. // synchronize the start of each iteration
  36. // using the barrier structure
  37. args->barrier_->wait();
  38. if (args->batch_submit) {
  39. const auto st = std::chrono::steady_clock::now();
  40. auto sequence = dml::sequence(args->batch_size, std::allocator<dml::byte_t>());
  41. for (uint32_t j = 0; j < args->batch_size; j++) {
  42. // block_on_fault() is required to submit the task in a way so that the
  43. // DSA engine can handle page faults itself together with the IOMMU which
  44. // requires the WQ to be configured to allow this too
  45. const auto status = sequence.add(dml::mem_copy.block_on_fault(), srcv, dstv);
  46. CHECK_STATUS(status, "Adding operation to batch failed!");
  47. }
  48. // we use the asynchronous submit-routine even though this is not required
  49. // here, however the project later on will only use async operation and
  50. // therefore this behaviour should be benchmarked
  51. auto handler = dml::submit<path>(dml::batch, sequence);
  52. const auto se = std::chrono::steady_clock::now();
  53. auto result = handler.get();
  54. const auto et = std::chrono::steady_clock::now();
  55. const dml::status_code status = result.status;
  56. CHECK_STATUS(status, "Batch completed with an Error!");
  57. ADD_TIMING_MESSUREMENT;
  58. }
  59. else if (args->batch_size > 1) {
  60. // implementation for non-batched batch submit follows here
  61. // this means we submit a bunch of work as single descriptors
  62. // but then dont wait for the completion immediately
  63. std::vector<dml::handler<dml::mem_copy_operation, std::allocator<uint8_t>>> handlers;
  64. const auto st = std::chrono::steady_clock::now();
  65. for (uint32_t j = 0; j < args->batch_size; j++) {
  66. // block_on_fault() is required to submit the task in a way so that the
  67. // DSA engine can handle page faults itself together with the IOMMU which
  68. // requires the WQ to be configured to allow this too
  69. handlers.emplace_back(dml::submit<path>(dml::mem_copy.block_on_fault(), srcv, dstv));
  70. }
  71. const auto se = std::chrono::steady_clock::now();
  72. for (auto& handler : handlers) {
  73. auto result = handler.get();
  74. const dml::status_code status = result.status;
  75. CHECK_STATUS(status, "Operation completed with an Error!");
  76. }
  77. const auto et = std::chrono::steady_clock::now();
  78. ADD_TIMING_MESSUREMENT;
  79. }
  80. else {
  81. const auto st = std::chrono::steady_clock::now();
  82. // we use the asynchronous submit-routine even though this is not required
  83. // here, however the project later on will only use async operation and
  84. // therefore this behaviour should be benchmarked
  85. // block_on_fault() is required to submit the task in a way so that the
  86. // DSA engine can handle page faults itself together with the IOMMU which
  87. // requires the WQ to be configured to allow this too
  88. auto handler = dml::submit<path>(dml::mem_copy.block_on_fault(), srcv, dstv);
  89. const auto se = std::chrono::steady_clock::now();
  90. auto result = handler.get();
  91. const auto et = std::chrono::steady_clock::now();
  92. const dml::status_code status = result.status;
  93. CHECK_STATUS(status, "Operation completed with an Error!");
  94. ADD_TIMING_MESSUREMENT;
  95. }
  96. // again: we do not count the first 5 repetitions
  97. if (i == 5) tps = std::chrono::steady_clock::now();
  98. if (i >= 5) args->rep_completed++;
  99. }
  100. const auto tpe = std::chrono::steady_clock::now();
  101. args->total_time = std::chrono::duration_cast<std::chrono::nanoseconds>(tpe - tps).count();
  102. // free the allocated memory regions on the selected nodes
  103. numa_free(src, args->size);
  104. numa_free(dst, args->size);
  105. return nullptr;
  106. }
  107. template <typename path>
  108. void execute_dml_memcpy(std::vector<TaskData>& args) {
  109. barrier task_barrier(args.size());
  110. std::vector<pthread_t> threads;
  111. // initialize numa library
  112. numa_available();
  113. // for each submitted task we link the semaphore
  114. // and create the thread, passing the argument
  115. for (auto& arg : args) {
  116. arg.barrier_ = &task_barrier;
  117. threads.emplace_back();
  118. if (pthread_create(&threads.back(), nullptr, thread_function<path>, &arg) != 0) {
  119. std::cerr << "Error creating thread" << std::endl;
  120. exit(1);
  121. }
  122. }
  123. for (pthread_t& t : threads) {
  124. pthread_join(t, nullptr);
  125. }
  126. }