Automatic detection of CPU capabilities

This commit is contained in:
tevador
2019-10-08 23:09:35 +02:00
parent eda3603bf3
commit e0484dfb99
8 changed files with 232 additions and 28 deletions

View File

@@ -93,6 +93,7 @@ void printUsage(const char* executable) {
std::cout << " --seed S seed for cache initialization (default: 0)" << std::endl;
std::cout << " --ssse3 use optimized Argon2 for SSSE3 CPUs" << std::endl;
std::cout << " --avx2 use optimized Argon2 for AVX2 CPUs" << std::endl;
std::cout << " --auto select the best options for the current CPU" << std::endl;
}
struct MemoryException : public std::exception {
@@ -130,7 +131,7 @@ void mine(randomx_vm* vm, std::atomic<uint32_t>& atomicNonce, AtomicHash& result
}
int main(int argc, char** argv) {
bool softAes, miningMode, verificationMode, help, largePages, jit, secure, ssse3, avx2;
bool softAes, miningMode, verificationMode, help, largePages, jit, secure, ssse3, avx2, autoFlags;
int noncesCount, threadCount, initThreadCount;
uint64_t threadAffinity;
int32_t seedValue;
@@ -153,6 +154,7 @@ int main(int argc, char** argv) {
readOption("--secure", argc, argv, secure);
readOption("--ssse3", argc, argv, ssse3);
readOption("--avx2", argc, argv, avx2);
readOption("--auto", argc, argv, autoFlags);
store32(&seed, seedValue);
@@ -169,31 +171,56 @@ int main(int argc, char** argv) {
std::vector<std::thread> threads;
randomx_dataset* dataset;
randomx_cache* cache;
randomx_flags flags = RANDOMX_FLAG_DEFAULT;
randomx_flags flags;
if (ssse3) {
flags = (randomx_flags)(flags | RANDOMX_FLAG_ARGON2_SSSE3);
if (autoFlags) {
initThreadCount = std::thread::hardware_concurrency();
flags = randomx_get_flags();
}
else {
flags = RANDOMX_FLAG_DEFAULT;
if (ssse3) {
flags |= RANDOMX_FLAG_ARGON2_SSSE3;
}
if (avx2) {
flags |= RANDOMX_FLAG_ARGON2_AVX2;
}
if (!softAes) {
flags |= RANDOMX_FLAG_HARD_AES;
}
if (jit) {
flags |= RANDOMX_FLAG_JIT;
}
}
if (largePages) {
flags |= RANDOMX_FLAG_LARGE_PAGES;
}
if (miningMode) {
flags |= RANDOMX_FLAG_FULL_MEM;
}
if (secure) {
flags |= RANDOMX_FLAG_SECURE;
}
if (flags & RANDOMX_FLAG_ARGON2_SSSE3) {
std::cout << " - Argon2 implementation: SSSE3" << std::endl;
}
if (avx2) {
flags = (randomx_flags)(flags | RANDOMX_FLAG_ARGON2_AVX2);
if (flags & RANDOMX_FLAG_ARGON2_AVX2) {
std::cout << " - Argon2 implementation: AVX2" << std::endl;
}
if (miningMode) {
flags = (randomx_flags)(flags | RANDOMX_FLAG_FULL_MEM);
if (flags & RANDOMX_FLAG_FULL_MEM) {
std::cout << " - full memory mode (2080 MiB)" << std::endl;
}
else {
std::cout << " - light memory mode (256 MiB)" << std::endl;
}
if (jit) {
flags = (randomx_flags)(flags | RANDOMX_FLAG_JIT);
if (flags & RANDOMX_FLAG_JIT) {
std::cout << " - JIT compiled mode ";
if (secure) {
flags = (randomx_flags)(flags | RANDOMX_FLAG_SECURE);
if (flags & RANDOMX_FLAG_SECURE) {
std::cout << "(secure)";
}
std::cout << std::endl;
@@ -202,16 +229,14 @@ int main(int argc, char** argv) {
std::cout << " - interpreted mode" << std::endl;
}
if (softAes) {
std::cout << " - software AES mode" << std::endl;
}
else {
flags = (randomx_flags)(flags | RANDOMX_FLAG_HARD_AES);
if (flags & RANDOMX_FLAG_HARD_AES) {
std::cout << " - hardware AES mode" << std::endl;
}
else {
std::cout << " - software AES mode" << std::endl;
}
if (largePages) {
flags = (randomx_flags)(flags | RANDOMX_FLAG_LARGE_PAGES);
if (flags & RANDOMX_FLAG_LARGE_PAGES) {
std::cout << " - large pages mode" << std::endl;
}
else {
@@ -229,10 +254,10 @@ int main(int argc, char** argv) {
try {
randomx::selectArgonImpl(flags); //just to check if flags are valid
if (jit && !RANDOMX_HAVE_COMPILER) {
if ((flags & RANDOMX_FLAG_JIT) && !RANDOMX_HAVE_COMPILER) {
throw std::runtime_error("JIT compilation is not supported on this platform. Try without --jit");
}
if (!jit && RANDOMX_HAVE_COMPILER) {
if (!(flags & RANDOMX_FLAG_JIT) && RANDOMX_HAVE_COMPILER) {
std::cout << "WARNING: You are using the interpreter mode. Use --jit for optimal performance." << std::endl;
}
@@ -273,7 +298,7 @@ int main(int argc, char** argv) {
for (int i = 0; i < threadCount; ++i) {
randomx_vm *vm = randomx_create_vm(flags, cache, dataset);
if (vm == nullptr) {
if (!softAes) {
if ((flags & RANDOMX_FLAG_HARD_AES)) {
throw std::runtime_error("Cannot create VM with the selected options. Try using --softAes");
}
if (largePages) {
@@ -290,10 +315,7 @@ int main(int argc, char** argv) {
int cpuid = -1;
if (threadAffinity)
cpuid = cpuid_from_mask(threadAffinity, i);
if (softAes)
threads.push_back(std::thread(&mine, vms[i], std::ref(atomicNonce), std::ref(result), noncesCount, i, cpuid));
else
threads.push_back(std::thread(&mine, vms[i], std::ref(atomicNonce), std::ref(result), noncesCount, i, cpuid));
threads.push_back(std::thread(&mine, vms[i], std::ref(atomicNonce), std::ref(result), noncesCount, i, cpuid));
}
for (unsigned i = 0; i < threads.size(); ++i) {
threads[i].join();