main.cpp 13 KB

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