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.

162 lines
5.7 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. const dml::status_code status = result.status;
  66. CHECK_STATUS(status, "Batch completed with an Error!");
  67. submission_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(se - st).count());
  68. completion_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - se).count());
  69. combined_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - st).count());
  70. }
  71. else {
  72. const auto st = std::chrono::high_resolution_clock::now();
  73. // we use the asynchronous submit-routine even though this is not required
  74. // here, however the project later on will only use async operation and
  75. // therefore this behaviour should be benchmarked
  76. auto handler = dml::submit<path>(dml::mem_copy, srcv, dstv);
  77. const auto se = std::chrono::high_resolution_clock::now();
  78. auto result = handler.get();
  79. const auto et = std::chrono::high_resolution_clock::now();
  80. const dml::status_code status = result.status;
  81. CHECK_STATUS(status, "Operation completed with an Error!");
  82. submission_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(se - st).count());
  83. completion_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - se).count());
  84. combined_durations.emplace_back(std::chrono::duration_cast<std::chrono::microseconds>(et - st).count());
  85. }
  86. args->rep_completed++;
  87. }
  88. // free the allocated memory regions on the selected nodes
  89. numa_free(src, args->size);
  90. numa_free(dst, args->size);
  91. args->combined_duration = avg(combined_durations);
  92. args->complete_duration = avg(completion_durations);
  93. args->submit_duration = avg(submission_durations);
  94. args->sig = nullptr;
  95. return nullptr;
  96. }
  97. template <typename path>
  98. void execute_dml_memcpy(std::vector<TaskData>& args) {
  99. sem_t sem;
  100. std::vector<pthread_t> threads;
  101. // initialize semaphore and numactl-library
  102. sem_init(&sem, 0, 0);
  103. numa_available();
  104. // for each submitted task we link the semaphore
  105. // and create the thread, passing the argument
  106. for (auto& arg : args) {
  107. arg.sig = &sem;
  108. threads.emplace_back();
  109. if (pthread_create(&threads.back(), nullptr, thread_function<path>, &arg) != 0) {
  110. std::cerr << "Error creating thread" << std::endl;
  111. exit(1);
  112. }
  113. }
  114. // post will make all waiting threads pass
  115. sem_post(&sem);
  116. for (pthread_t& t : threads) {
  117. pthread_join(t, nullptr);
  118. }
  119. sem_destroy(&sem);
  120. }