main.cpp 12 KB

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