Xisepe

lab8_evm

Dec 18th, 2022
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. class TestConfig {
  5. public:
  6.     constexpr static uint32_t ITERATIONS = 5;
  7.     constexpr static size_t CACHE_SIZE_BYTE = 11 * 1024 * 1024;
  8.     constexpr static size_t OFFSET_BYTE = 16 * 1024 * 1024;
  9.     constexpr static size_t TESTER_ARRAY_SIZE = 200000000;
  10. };
  11.  
  12. class Tester {
  13. public:
  14.     static void runTest(uint32_t minFragments, uint32_t maxFragments) {
  15.         auto array = new uint32_t[TestConfig::TESTER_ARRAY_SIZE];
  16.         for (uint32_t i = minFragments; i < maxFragments; ++i) {
  17.             initArray(array, i, TestConfig::OFFSET_BYTE / sizeof(uint32_t), TestConfig::CACHE_SIZE_BYTE / i);
  18.             std::cout << "Fragments: " << i << " "
  19.                       << " Ticks: " << run(array) / (TestConfig::ITERATIONS * TestConfig::TESTER_ARRAY_SIZE) << std::endl;
  20.         }
  21.         delete[] array;
  22.     }
  23.  
  24. private:
  25.     static inline uint64_t rdtsc() {
  26.         unsigned int lo, hi;
  27.         asm volatile("rdtsc\n"
  28.                      : "=a"(lo), "=d"(hi)::"memory");
  29.         return ((uint64_t) hi << 32) | lo;
  30.     }
  31.  
  32.     static void initArray(uint32_t *array, uint32_t fragments, size_t offset, size_t size) {
  33.         for (size_t i = 0; i < size; ++i) {
  34.             for (size_t j = 0; j <= fragments; ++j) {
  35.                 array[i + ((j - 1) % fragments) * offset] = i + (j % fragments) * offset;
  36.             }
  37.         }
  38.     }
  39.  
  40.     static uint64_t run(uint32_t *array) {
  41.         volatile uint64_t t1 = 0;
  42.         volatile uint64_t t2 = 0;
  43.         volatile uint32_t k = 0;
  44.  
  45.         t1 = rdtsc();
  46.         for (size_t i = 0; i < TestConfig::ITERATIONS * TestConfig::TESTER_ARRAY_SIZE; ++i) {
  47.             k = array[k];
  48.         }
  49.         t2 = rdtsc();
  50.  
  51.         return t2 - t1;
  52.     }
  53. };
  54.  
  55. int main() {
  56.     Tester::runTest(1, 33);
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment