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.

159 lines
5.6 KiB

  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <chrono>
  5. #include <pthread.h>
  6. #include <semaphore.h>
  7. #include <numa.h>
  8. #include <dml/dml.hpp>
  9. #include "statuscode-tostring.hpp"
  10. #include "task-data.hpp"
  11. double avg(const std::vector<double>& v) {
  12. int n = 0;
  13. double mean = 0.0;
  14. for (const auto x : v) {
  15. const double delta = static_cast<double>(x) - mean;
  16. mean += delta / ++n;
  17. }
  18. return mean;
  19. }
  20. #define LOG_CODE_INFO "Location: " << __FILE__ << "@" << __LINE__ << "::" << __FUNCTION__ << std::endl
  21. #define LOG_ERR { pthread_t t = pthread_self(); std::cerr << "--- BEGIN ERROR MSG ---" << std::endl << "Physical: [Node " << args->numa_node << " | Core " << args->core << " | Thread " << t << "]" << std::endl; } std::cerr << LOG_CODE_INFO
  22. #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; }}
  23. template <typename path>
  24. void* thread_function(void* argp) {
  25. TaskData* args = reinterpret_cast<TaskData*>(argp);
  26. std::vector<double> submission_durations;
  27. std::vector<double> completion_durations;
  28. std::vector<double> combined_durations;
  29. // set numa node and core affinity of the current thread
  30. numa_run_on_node(args->numa_node);
  31. cpu_set_t cpuset;
  32. CPU_ZERO(&cpuset);
  33. CPU_SET(args->core, &cpuset);
  34. if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) != 0) {
  35. LOG_ERR << "Error setting affinity for thread" << std::endl;
  36. return nullptr;
  37. }
  38. // allocate memory for the move operation on the requested numa nodes
  39. void* src = numa_alloc_onnode(args->size, args->nnode_src);
  40. void* dst = numa_alloc_onnode(args->size, args->nnode_dst);
  41. dml::data_view srcv = dml::make_view(reinterpret_cast<uint8_t*>(src), args->size);
  42. dml::data_view dstv = dml::make_view(reinterpret_cast<uint8_t*>(dst), args->size);
  43. args->status = dml::status_code::ok;
  44. args->rep_completed = 0;
  45. // wait for specified signal so that all operations start at the same time
  46. sem_wait(args->sig);
  47. for (uint32_t i = 0; i < args->rep_count; i++) {
  48. if (args->batch_submit) {
  49. uint32_t opcount = args->batch_size;
  50. if (args->barrier_after_n_operations > 0) {
  51. opcount += opcount / args->barrier_after_n_operations;
  52. }
  53. const auto st = std::chrono::high_resolution_clock::now();
  54. auto sequence = dml::sequence(opcount, std::allocator<dml::byte_t>());
  55. for (uint32_t j = 0; j < args->batch_size; j++) {
  56. const auto status = sequence.add(dml::mem_copy, srcv, dstv);
  57. if (j % args->barrier_after_n_operations == 0) {
  58. sequence.add(dml::nop);
  59. }
  60. }
  61. auto handler = dml::submit<path>(dml::batch, sequence);
  62. const auto se = std::chrono::high_resolution_clock::now();
  63. auto result = handler.get();
  64. const auto et = std::chrono::high_resolution_clock::now();
  65. submission_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(se - st).count());
  66. completion_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - se).count());
  67. combined_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - st).count());
  68. }
  69. else {
  70. const auto st = std::chrono::high_resolution_clock::now();
  71. // we use the asynchronous submit-routine even though this is not required
  72. // here, however the project later on will only use async operation and
  73. // therefore this behaviour should be benchmarked
  74. auto handler = dml::submit<path>(dml::mem_copy, srcv, dstv);
  75. const auto se = std::chrono::high_resolution_clock::now();
  76. auto result = handler.get();
  77. const auto et = std::chrono::high_resolution_clock::now();
  78. const dml::status_code status = result.status;
  79. CHECK_STATUS(status, "Operation completed with an Error!");
  80. submission_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(se - st).count());
  81. completion_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - se).count());
  82. combined_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - st).count());
  83. }
  84. args->rep_completed++;
  85. }
  86. // free the allocated memory regions on the selected nodes
  87. numa_free(src, args->size);
  88. numa_free(dst, args->size);
  89. args->combined_duration = avg(combined_durations);
  90. args->complete_duration = avg(completion_durations);
  91. args->submit_duration = avg(submission_durations);
  92. args->sig = nullptr;
  93. return nullptr;
  94. }
  95. template <typename path>
  96. void execute_dml_memcpy(std::vector<TaskData>& args) {
  97. sem_t sem;
  98. std::vector<pthread_t> threads;
  99. // initialize semaphore and numactl-library
  100. sem_init(&sem, 0, 0);
  101. numa_available();
  102. // for each submitted task we link the semaphore
  103. // and create the thread, passing the argument
  104. for (auto& arg : args) {
  105. arg.sig = &sem;
  106. threads.emplace_back();
  107. if (pthread_create(&threads.back(), nullptr, thread_function<path>, &arg) != 0) {
  108. std::cerr << "Error creating thread" << std::endl;
  109. exit(1);
  110. }
  111. }
  112. // post will make all waiting threads pass
  113. sem_post(&sem);
  114. for (pthread_t& t : threads) {
  115. pthread_join(t, nullptr);
  116. }
  117. sem_destroy(&sem);
  118. }