main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. //#define TRACE
  16. #include "InterpretedVirtualMachine.hpp"
  17. #include "CompiledVirtualMachine.hpp"
  18. #include "CompiledLightVirtualMachine.hpp"
  19. #include "AssemblyGeneratorX86.hpp"
  20. #include "Stopwatch.hpp"
  21. #include "blake2/blake2.h"
  22. #include "blake2/endian.h"
  23. #include <fstream>
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <exception>
  27. #include <cstring>
  28. #include "Program.hpp"
  29. #include <string>
  30. #include <thread>
  31. #include <atomic>
  32. #include "dataset.hpp"
  33. #include "Cache.hpp"
  34. #include "hashAes1Rx4.hpp"
  35. const uint8_t seed[32] = { 191, 182, 222, 175, 249, 89, 134, 104, 241, 68, 191, 62, 162, 166, 61, 64, 123, 191, 227, 193, 118, 60, 188, 53, 223, 133, 175, 24, 123, 230, 55, 74 };
  36. const uint8_t blockTemplate__[] = {
  37. 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
  38. 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
  39. 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
  40. 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
  41. };
  42. void dump(const char* buffer, uint64_t count, const char* name) {
  43. std::ofstream fout(name, std::ios::out | std::ios::binary);
  44. fout.write(buffer, count);
  45. fout.close();
  46. }
  47. constexpr char hexmap[] = "0123456789abcdef";
  48. void outputHex(std::ostream& os, const char* data, int length) {
  49. for (int i = 0; i < length; ++i) {
  50. os << hexmap[(data[i] & 0xF0) >> 4];
  51. os << hexmap[data[i] & 0x0F];
  52. }
  53. }
  54. void readOption(const char* option, int argc, char** argv, bool& out) {
  55. for (int i = 0; i < argc; ++i) {
  56. if (strcmp(argv[i], option) == 0) {
  57. out = true;
  58. return;
  59. }
  60. }
  61. out = false;
  62. }
  63. void readIntOption(const char* option, int argc, char** argv, int& out, int defaultValue) {
  64. for (int i = 0; i < argc - 1; ++i) {
  65. if (strcmp(argv[i], option) == 0 && (out = atoi(argv[i + 1])) > 0) {
  66. return;
  67. }
  68. }
  69. out = defaultValue;
  70. }
  71. void readInt(int argc, char** argv, int& out, int defaultValue) {
  72. for (int i = 0; i < argc; ++i) {
  73. if (*argv[i] != '-' && (out = atoi(argv[i])) > 0) {
  74. return;
  75. }
  76. }
  77. out = defaultValue;
  78. }
  79. class AtomicHash {
  80. public:
  81. AtomicHash() {
  82. for (int i = 0; i < 4; ++i)
  83. hash[i].store(0);
  84. }
  85. void xorWith(uint64_t update[4]) {
  86. for (int i = 0; i < 4; ++i)
  87. hash[i].fetch_xor(update[i]);
  88. }
  89. void print(std::ostream& os) {
  90. for (int i = 0; i < 4; ++i)
  91. print(hash[i], os);
  92. os << std::endl;
  93. }
  94. private:
  95. static void print(std::atomic<uint64_t>& hash, std::ostream& os) {
  96. auto h = hash.load();
  97. outputHex(std::cout, (char*)&h, sizeof(h));
  98. }
  99. std::atomic<uint64_t> hash[4];
  100. };
  101. void printUsage(const char* executable) {
  102. std::cout << "Usage: " << executable << " [OPTIONS]" << std::endl;
  103. std::cout << "Supported options:" << std::endl;
  104. std::cout << " --help shows this message" << std::endl;
  105. std::cout << " --mine mining mode: 4 GiB, x86-64 compiled VM" << std::endl;
  106. std::cout << " --verify verification mode: 256 MiB, portable VM" << std::endl;
  107. std::cout << " --largePages use large pages" << std::endl;
  108. std::cout << " --softAes use software AES (default: x86 AES-NI)" << std::endl;
  109. std::cout << " --threads T use T threads (default: 1)" << std::endl;
  110. std::cout << " --init Q initialize dataset with Q threads (default: 1)" << std::endl;
  111. std::cout << " --nonces N run N nonces (default: 1000)" << std::endl;
  112. std::cout << " --genAsm generate x86-64 asm code for nonce N" << std::endl;
  113. std::cout << " --genNative generate RandomX code for nonce N" << std::endl;
  114. }
  115. template<bool softAes>
  116. void generateAsm(uint32_t nonce) {
  117. alignas(16) uint64_t hash[8];
  118. uint8_t blockTemplate[sizeof(blockTemplate__)];
  119. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  120. store32(blockTemplate + 39, nonce);
  121. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  122. uint8_t scratchpad[RANDOMX_SCRATCHPAD_L3];
  123. fillAes1Rx4<softAes>((void*)hash, RANDOMX_SCRATCHPAD_L3, scratchpad);
  124. RandomX::AssemblyGeneratorX86 asmX86;
  125. RandomX::Program p;
  126. fillAes1Rx4<softAes>(hash, sizeof(p), &p);
  127. asmX86.generateProgram(p);
  128. asmX86.printCode(std::cout);
  129. }
  130. template<bool softAes>
  131. void generateNative(uint32_t nonce) {
  132. alignas(16) uint64_t hash[8];
  133. uint8_t blockTemplate[sizeof(blockTemplate__)];
  134. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  135. store32(blockTemplate + 39, nonce);
  136. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  137. uint8_t scratchpad[RANDOMX_SCRATCHPAD_L3];
  138. fillAes1Rx4<softAes>((void*)hash, RANDOMX_SCRATCHPAD_L3, scratchpad);
  139. alignas(16) RandomX::Program prog;
  140. fillAes1Rx4<softAes>((void*)hash, sizeof(prog), &prog);
  141. for (int i = 0; i < RANDOMX_PROGRAM_SIZE; ++i) {
  142. prog(i).dst %= 8;
  143. prog(i).src %= 8;
  144. }
  145. std::cout << prog << std::endl;
  146. }
  147. template<bool softAes>
  148. void mine(RandomX::VirtualMachine* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result, uint32_t noncesCount, int thread, uint8_t* scratchpad) {
  149. alignas(16) uint64_t hash[8];
  150. uint8_t blockTemplate[sizeof(blockTemplate__)];
  151. memcpy(blockTemplate, blockTemplate__, sizeof(blockTemplate));
  152. void* noncePtr = blockTemplate + 39;
  153. auto nonce = atomicNonce.fetch_add(1);
  154. while (nonce < noncesCount) {
  155. //std::cout << "Thread " << thread << " nonce " << nonce << std::endl;
  156. store32(noncePtr, nonce);
  157. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  158. fillAes1Rx4<softAes>((void*)hash, RANDOMX_SCRATCHPAD_L3, scratchpad);
  159. vm->resetRoundingMode();
  160. vm->setScratchpad(scratchpad);
  161. //dump((char*)scratchpad, RandomX::ScratchpadSize, "spad-before.txt");
  162. for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) {
  163. fillAes1Rx4<softAes>((void*)hash, sizeof(RandomX::Program), vm->getProgramBuffer());
  164. vm->initialize();
  165. vm->execute();
  166. vm->getResult<false>(nullptr, 0, hash);
  167. }
  168. fillAes1Rx4<softAes>((void*)hash, sizeof(RandomX::Program), vm->getProgramBuffer());
  169. vm->initialize();
  170. vm->execute();
  171. /*if (RandomX::trace) {
  172. for (int j = 0; j < RandomX::ProgramLength; ++j) {
  173. uint64_t res = *(uint64_t*)(scratchpad + 8 * (RandomX::ProgramLength - 1 - j));
  174. std::cout << std::hex << std::setw(16) << std::setfill('0') << res << std::endl;
  175. }
  176. }*/
  177. vm->getResult<softAes>(scratchpad, RANDOMX_SCRATCHPAD_L3, hash);
  178. result.xorWith(hash);
  179. if (RandomX::trace) {
  180. std::cout << "Nonce: " << nonce << " ";
  181. outputHex(std::cout, (char*)hash, 16);
  182. std::cout << std::endl;
  183. }
  184. nonce = atomicNonce.fetch_add(1);
  185. }
  186. }
  187. int main(int argc, char** argv) {
  188. bool softAes, genAsm, miningMode, verificationMode, help, largePages, async, genNative, jit;
  189. int programCount, threadCount, initThreadCount, epoch;
  190. readOption("--softAes", argc, argv, softAes);
  191. readOption("--genAsm", argc, argv, genAsm);
  192. readOption("--mine", argc, argv, miningMode);
  193. readOption("--verify", argc, argv, verificationMode);
  194. readIntOption("--threads", argc, argv, threadCount, 1);
  195. readIntOption("--nonces", argc, argv, programCount, 1000);
  196. readIntOption("--init", argc, argv, initThreadCount, 1);
  197. readIntOption("--epoch", argc, argv, epoch, 0);
  198. readOption("--largePages", argc, argv, largePages);
  199. readOption("--jit", argc, argv, jit);
  200. readOption("--genNative", argc, argv, genNative);
  201. readOption("--help", argc, argv, help);
  202. if (genAsm) {
  203. if (softAes)
  204. generateAsm<true>(programCount);
  205. else
  206. generateAsm<false>(programCount);
  207. return 0;
  208. }
  209. if (genNative) {
  210. if (softAes)
  211. generateNative<true>(programCount);
  212. else
  213. generateNative<false>(programCount);
  214. return 0;
  215. }
  216. if (help || (!miningMode && !verificationMode)) {
  217. printUsage(argv[0]);
  218. return 0;
  219. }
  220. if (softAes)
  221. std::cout << "Using software AES." << std::endl;
  222. std::atomic<uint32_t> atomicNonce(0);
  223. AtomicHash result;
  224. std::vector<RandomX::VirtualMachine*> vms;
  225. std::vector<std::thread> threads;
  226. RandomX::dataset_t dataset;
  227. const uint64_t cacheSize = (RANDOMX_ARGON_MEMORY + RANDOMX_ARGON_GROWTH * epoch) * RandomX::ArgonBlockSize;
  228. const uint64_t datasetSize = (RANDOMX_DATASET_SIZE + RANDOMX_DS_GROWTH * epoch);
  229. dataset.cache.size = cacheSize;
  230. std::cout << "RandomX - " << (miningMode ? "mining" : "verification") << " mode" << std::endl;
  231. std::cout << "Initializing";
  232. if(miningMode)
  233. std::cout << " (" << initThreadCount << " thread" << (initThreadCount > 1 ? "s)" : ")");
  234. std::cout << " ..." << std::endl;
  235. try {
  236. Stopwatch sw(true);
  237. RandomX::datasetInitCache(seed, dataset, largePages);
  238. if (RandomX::trace) {
  239. std::cout << "Cache: " << std::endl;
  240. outputHex(std::cout, (char*)dataset.cache.memory, sizeof(__m128i));
  241. std::cout << std::endl;
  242. }
  243. if (!miningMode) {
  244. std::cout << "Cache (" << cacheSize << " bytes) initialized in " << sw.getElapsed() << " s" << std::endl;
  245. }
  246. else {
  247. auto cache = dataset.cache;
  248. dataset.dataset.size = datasetSize;
  249. RandomX::datasetAlloc(dataset, largePages);
  250. const uint64_t datasetBlockCount = datasetSize / RandomX::CacheLineSize;
  251. if (initThreadCount > 1) {
  252. auto perThread = datasetBlockCount / initThreadCount;
  253. auto remainder = datasetBlockCount % initThreadCount;
  254. for (int i = 0; i < initThreadCount; ++i) {
  255. auto count = perThread + (i == initThreadCount - 1 ? remainder : 0);
  256. threads.push_back(std::thread(&RandomX::datasetInit, std::ref(cache), std::ref(dataset.dataset), i * perThread, count));
  257. }
  258. for (unsigned i = 0; i < threads.size(); ++i) {
  259. threads[i].join();
  260. }
  261. }
  262. else {
  263. RandomX::datasetInit(cache, dataset.dataset, 0, datasetBlockCount);
  264. }
  265. RandomX::deallocCache(cache, largePages);
  266. threads.clear();
  267. std::cout << "Dataset (" << datasetSize << " bytes) initialized in " << sw.getElapsed() << " s" << std::endl;
  268. }
  269. std::cout << "Initializing " << threadCount << " virtual machine(s) ..." << std::endl;
  270. for (int i = 0; i < threadCount; ++i) {
  271. RandomX::VirtualMachine* vm;
  272. if (miningMode) {
  273. vm = new RandomX::CompiledVirtualMachine();
  274. }
  275. else {
  276. if (jit)
  277. vm = new RandomX::CompiledLightVirtualMachine();
  278. else
  279. vm = new RandomX::InterpretedVirtualMachine(softAes);
  280. }
  281. vm->setDataset(dataset, datasetSize);
  282. vms.push_back(vm);
  283. }
  284. uint8_t* scratchpadMem;
  285. if (largePages) {
  286. scratchpadMem = (uint8_t*)allocLargePagesMemory(threadCount * RANDOMX_SCRATCHPAD_L3);
  287. }
  288. else {
  289. scratchpadMem = (uint8_t*)_mm_malloc(threadCount * RANDOMX_SCRATCHPAD_L3, RandomX::CacheLineSize);
  290. }
  291. std::cout << "Running benchmark (" << programCount << " nonces) ..." << std::endl;
  292. sw.restart();
  293. if (threadCount > 1) {
  294. for (unsigned i = 0; i < vms.size(); ++i) {
  295. if (softAes)
  296. threads.push_back(std::thread(&mine<true>, vms[i], std::ref(atomicNonce), std::ref(result), programCount, i, scratchpadMem + RANDOMX_SCRATCHPAD_L3 * i));
  297. else
  298. threads.push_back(std::thread(&mine<false>, vms[i], std::ref(atomicNonce), std::ref(result), programCount, i, scratchpadMem + RANDOMX_SCRATCHPAD_L3 * i));
  299. }
  300. for (unsigned i = 0; i < threads.size(); ++i) {
  301. threads[i].join();
  302. }
  303. }
  304. else {
  305. if(softAes)
  306. mine<true>(vms[0], std::ref(atomicNonce), std::ref(result), programCount, 0, scratchpadMem);
  307. else
  308. mine<false>(vms[0], std::ref(atomicNonce), std::ref(result), programCount, 0, scratchpadMem);
  309. /*if (miningMode)
  310. std::cout << "Average program size: " << ((RandomX::CompiledVirtualMachine*)vms[0])->getTotalSize() / programCount / RandomX::ChainLength << std::endl;*/
  311. }
  312. double elapsed = sw.getElapsed();
  313. std::cout << "Calculated result: ";
  314. result.print(std::cout);
  315. if(programCount == 1000)
  316. std::cout << "Reference result: 83875c55fb9ff4a75205a744b82926ebbe23219c6291889c9ee91603c845c597" << std::endl;
  317. if (!miningMode) {
  318. std::cout << "Performance: " << 1000 * elapsed / programCount << " ms per hash" << std::endl;
  319. }
  320. else {
  321. std::cout << "Performance: " << programCount / elapsed << " hashes per second" << std::endl;
  322. }
  323. }
  324. catch (std::exception& e) {
  325. std::cout << "ERROR: " << e.what() << std::endl;
  326. return 1;
  327. }
  328. return 0;
  329. }