dataset.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include <new>
  16. #include <algorithm>
  17. #include <stdexcept>
  18. #include <cstring>
  19. #include <limits>
  20. #include "common.hpp"
  21. #include "dataset.hpp"
  22. #include "Cache.hpp"
  23. #include "virtualMemory.hpp"
  24. #include "softAes.h"
  25. #include "squareHash.h"
  26. #include "blake2/endian.h"
  27. #if defined(__SSE2__)
  28. #include <wmmintrin.h>
  29. #define PREFETCHNTA(x) _mm_prefetch((const char *)(x), _MM_HINT_NTA)
  30. #else
  31. #define PREFETCH(memory)
  32. #endif
  33. namespace RandomX {
  34. #if true //RANDOMX_ARGON_GROWTH != 0 || (!defined(_M_X64) && !defined(__x86_64__))
  35. static FORCE_INLINE uint8_t* selectMixBlock(const Cache& cache, uint64_t& currentIndex, uint64_t& nextIndex) {
  36. uint8_t* mixBlock;
  37. if (RANDOMX_ARGON_GROWTH == 0) {
  38. constexpr uint32_t mask = (RANDOMX_ARGON_MEMORY * ArgonBlockSize / CacheLineSize - 1);
  39. mixBlock = cache.memory + (currentIndex & mask) * CacheLineSize;
  40. }
  41. else {
  42. const uint32_t modulus = cache.size / CacheLineSize;
  43. mixBlock = cache.memory + (currentIndex % modulus) * CacheLineSize;
  44. }
  45. PREFETCHNTA(mixBlock);
  46. nextIndex = squareHash(currentIndex + nextIndex);
  47. return mixBlock;
  48. }
  49. static FORCE_INLINE void mixCache(uint8_t* mixBlock, uint64_t& c0, uint64_t& c1, uint64_t& c2, uint64_t& c3, uint64_t& c4, uint64_t& c5, uint64_t& c6, uint64_t& c7) {
  50. c0 ^= load64(mixBlock + 0);
  51. c1 ^= load64(mixBlock + 8);
  52. c2 ^= load64(mixBlock + 16);
  53. c3 ^= load64(mixBlock + 24);
  54. c4 ^= load64(mixBlock + 32);
  55. c5 ^= load64(mixBlock + 40);
  56. c6 ^= load64(mixBlock + 48);
  57. c7 ^= load64(mixBlock + 56);
  58. }
  59. void initBlock(const Cache& cache, uint8_t* out, uint64_t blockNumber, unsigned iterations) {
  60. uint64_t c0, c1, c2, c3, c4, c5, c6, c7;
  61. c0 = blockNumber;
  62. c1 = c2 = c3 = c4 = c5 = c6 = c7 = 0;
  63. uint8_t* mixBlock;
  64. for (auto i = 0; i < iterations; ++i) {
  65. mixBlock = selectMixBlock(cache, c0, c1);
  66. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  67. mixBlock = selectMixBlock(cache, c1, c2);
  68. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  69. mixBlock = selectMixBlock(cache, c2, c3);
  70. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  71. mixBlock = selectMixBlock(cache, c3, c4);
  72. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  73. mixBlock = selectMixBlock(cache, c4, c5);
  74. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  75. mixBlock = selectMixBlock(cache, c5, c6);
  76. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  77. mixBlock = selectMixBlock(cache, c6, c7);
  78. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  79. mixBlock = selectMixBlock(cache, c7, c0);
  80. mixCache(mixBlock, c0, c1, c2, c3, c4, c5, c6, c7);
  81. }
  82. store64(out + 0, c0);
  83. store64(out + 8, c1);
  84. store64(out + 16, c2);
  85. store64(out + 24, c3);
  86. store64(out + 32, c4);
  87. store64(out + 40, c5);
  88. store64(out + 48, c6);
  89. store64(out + 56, c7);
  90. }
  91. #endif
  92. void datasetRead(addr_t addr, MemoryRegisters& memory, RegisterFile& reg) {
  93. uint64_t* datasetLine = (uint64_t*)(memory.ds.dataset.memory + memory.ma);
  94. memory.mx ^= addr;
  95. memory.mx &= -64; //align to cache line
  96. std::swap(memory.mx, memory.ma);
  97. PREFETCHNTA(memory.ds.dataset.memory + memory.ma);
  98. for (int i = 0; i < RegistersCount; ++i)
  99. reg.r[i] ^= datasetLine[i];
  100. }
  101. void datasetReadLight(addr_t addr, MemoryRegisters& memory, int_reg_t (&reg)[RegistersCount]) {
  102. memory.mx ^= addr;
  103. memory.mx &= CacheLineAlignMask; //align to cache line
  104. Cache& cache = memory.ds.cache;
  105. uint64_t datasetLine[CacheLineSize / sizeof(uint64_t)];
  106. initBlock(cache, (uint8_t*)datasetLine, memory.ma / CacheLineSize, RANDOMX_CACHE_ACCESSES / 8);
  107. for (int i = 0; i < RegistersCount; ++i)
  108. reg[i] ^= datasetLine[i];
  109. std::swap(memory.mx, memory.ma);
  110. }
  111. void datasetReadLightAsync(addr_t addr, MemoryRegisters& memory, int_reg_t(&reg)[RegistersCount]) {
  112. ILightClientAsyncWorker* aw = memory.ds.asyncWorker;
  113. const uint64_t* datasetLine = aw->getBlock(memory.ma);
  114. for (int i = 0; i < RegistersCount; ++i)
  115. reg[i] ^= datasetLine[i];
  116. memory.mx ^= addr;
  117. memory.mx &= CacheLineAlignMask; //align to cache line
  118. std::swap(memory.mx, memory.ma);
  119. aw->prepareBlock(memory.ma);
  120. }
  121. void datasetAlloc(dataset_t& ds, bool largePages) {
  122. if (std::numeric_limits<size_t>::max() < RANDOMX_DATASET_SIZE)
  123. throw std::runtime_error("Platform doesn't support enough memory for the dataset");
  124. if (largePages) {
  125. ds.dataset.memory = (uint8_t*)allocLargePagesMemory(ds.dataset.size);
  126. }
  127. else {
  128. ds.dataset.memory = (uint8_t*)_mm_malloc(ds.dataset.size, 64);
  129. if (ds.dataset.memory == nullptr) {
  130. throw std::runtime_error("Dataset memory allocation failed. >4 GiB of free virtual memory is needed.");
  131. }
  132. }
  133. }
  134. void datasetInit(Cache& cache, Dataset& ds, uint32_t startBlock, uint32_t blockCount) {
  135. for (uint64_t i = startBlock; i < startBlock + blockCount; ++i) {
  136. initBlock(cache, ds.memory + i * CacheLineSize, i, RANDOMX_CACHE_ACCESSES / 8);
  137. }
  138. }
  139. void datasetInitCache(const void* seed, dataset_t& ds, bool largePages) {
  140. ds.cache.memory = allocCache(ds.cache.size, largePages);
  141. argonFill(ds.cache, seed, SeedSize);
  142. }
  143. }