dataset.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "common.hpp"
  20. #include "dataset.hpp"
  21. #include "Cache.hpp"
  22. #include "virtualMemory.hpp"
  23. #include "softAes.h"
  24. #include "squareHash.h"
  25. #include "blake2/endian.h"
  26. #if defined(__SSE2__)
  27. #include <wmmintrin.h>
  28. #define PREFETCHNTA(x) _mm_prefetch((const char *)(x), _MM_HINT_NTA)
  29. #else
  30. #define PREFETCH(memory)
  31. #endif
  32. namespace RandomX {
  33. void initBlock(const uint8_t* cache, uint8_t* out, uint32_t blockNumber, const KeysContainer& keys) {
  34. uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
  35. r0 = 4ULL * blockNumber;
  36. r1 = r2 = r3 = r4 = r5 = r6 = r7 = 0;
  37. constexpr uint32_t mask = (CacheSize - 1) & CacheLineAlignMask;
  38. for (auto i = 0; i < DatasetIterations; ++i) {
  39. const uint8_t* mixBlock = cache + (r0 & mask);
  40. PREFETCHNTA(mixBlock);
  41. r0 = squareHash(r0);
  42. r0 ^= load64(mixBlock + 0);
  43. r1 ^= load64(mixBlock + 8);
  44. r2 ^= load64(mixBlock + 16);
  45. r3 ^= load64(mixBlock + 24);
  46. r4 ^= load64(mixBlock + 32);
  47. r5 ^= load64(mixBlock + 40);
  48. r6 ^= load64(mixBlock + 48);
  49. r7 ^= load64(mixBlock + 56);
  50. }
  51. store64(out + 0, r0);
  52. store64(out + 8, r1);
  53. store64(out + 16, r2);
  54. store64(out + 24, r3);
  55. store64(out + 32, r4);
  56. store64(out + 40, r5);
  57. store64(out + 48, r6);
  58. store64(out + 56, r7);
  59. }
  60. void datasetRead(addr_t addr, MemoryRegisters& memory, RegisterFile& reg) {
  61. uint64_t* datasetLine = (uint64_t*)(memory.ds.dataset + memory.ma);
  62. memory.mx ^= addr;
  63. memory.mx &= -64; //align to cache line
  64. std::swap(memory.mx, memory.ma);
  65. PREFETCHNTA(memory.ds.dataset + memory.ma);
  66. for (int i = 0; i < RegistersCount; ++i)
  67. reg.r[i] ^= datasetLine[i];
  68. }
  69. void datasetReadLight(addr_t addr, MemoryRegisters& memory, int_reg_t (&reg)[RegistersCount]) {
  70. memory.mx ^= addr;
  71. memory.mx &= CacheLineAlignMask; //align to cache line
  72. Cache* cache = memory.ds.cache;
  73. uint64_t datasetLine[CacheLineSize / sizeof(uint64_t)];
  74. initBlock(cache->getCache(), (uint8_t*)datasetLine, memory.ma / CacheLineSize, cache->getKeys());
  75. for (int i = 0; i < RegistersCount; ++i)
  76. reg[i] ^= datasetLine[i];
  77. std::swap(memory.mx, memory.ma);
  78. }
  79. void datasetReadLightAsync(addr_t addr, MemoryRegisters& memory, int_reg_t(&reg)[RegistersCount]) {
  80. ILightClientAsyncWorker* aw = memory.ds.asyncWorker;
  81. const uint64_t* datasetLine = aw->getBlock(memory.ma);
  82. for (int i = 0; i < RegistersCount; ++i)
  83. reg[i] ^= datasetLine[i];
  84. memory.mx ^= addr;
  85. memory.mx &= CacheLineAlignMask; //align to cache line
  86. std::swap(memory.mx, memory.ma);
  87. aw->prepareBlock(memory.ma);
  88. }
  89. void datasetAlloc(dataset_t& ds, bool largePages) {
  90. if (sizeof(size_t) <= 4)
  91. throw std::runtime_error("Platform doesn't support enough memory for the dataset");
  92. if (largePages) {
  93. ds.dataset = (uint8_t*)allocLargePagesMemory(DatasetSize);
  94. }
  95. else {
  96. ds.dataset = (uint8_t*)_mm_malloc(DatasetSize, 64);
  97. if (ds.dataset == nullptr) {
  98. throw std::runtime_error("Dataset memory allocation failed. >4 GiB of free virtual memory is needed.");
  99. }
  100. }
  101. }
  102. void datasetInit(Cache* cache, dataset_t ds, uint32_t startBlock, uint32_t blockCount) {
  103. for (uint32_t i = startBlock; i < startBlock + blockCount; ++i) {
  104. initBlock(cache->getCache(), ds.dataset + i * CacheLineSize, i, cache->getKeys());
  105. }
  106. }
  107. template<bool softAes>
  108. void datasetInitCache(const void* seed, dataset_t& ds, bool largePages) {
  109. ds.cache = new(Cache::alloc(largePages)) Cache();
  110. ds.cache->initialize<softAes>(seed, SeedSize);
  111. }
  112. template
  113. void datasetInitCache<false>(const void*, dataset_t&, bool);
  114. template
  115. void datasetInitCache<true>(const void*, dataset_t&, bool);
  116. template<bool softAes>
  117. void aesBench(uint32_t blockCount) {
  118. alignas(16) KeysContainer keys;
  119. alignas(16) uint8_t buffer[CacheLineSize];
  120. for (uint32_t block = 0; block < blockCount; ++block) {
  121. initBlock(buffer, buffer, 0, keys);
  122. }
  123. }
  124. template void aesBench<false>(uint32_t blockCount);
  125. template void aesBench<true>(uint32_t blockCount);
  126. }