Skip to content

Commit

Permalink
fix opencl and cuda cryptonight_r caching
Browse files Browse the repository at this point in the history
The original implementation of the cache release and create always new
kernel, this can lead into performance issues and my crashes.
  • Loading branch information
psychocrypt committed Mar 10, 2019
1 parent f0c223b commit 3ebf66a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
11 changes: 7 additions & 4 deletions xmrstak/backend/amd/OclCryptonightR_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ static cl_program CryptonightR_build_program(
const GpuContext* ctx,
xmrstak_algo algo,
uint64_t height,
uint32_t precompile_count,
cl_kernel old_kernel,
std::string source_code,
std::string options)
Expand All @@ -151,7 +152,7 @@ static cl_program CryptonightR_build_program(
for(size_t i = 0; i < CryptonightR_cache.size();)
{
const CacheEntry& entry = CryptonightR_cache[i];
if ((entry.algo == algo) && (entry.height + 2 < height))
if ((entry.algo == algo) && (entry.height + 2 + precompile_count < height))
{
printer::inst()->print_msg(LDEBUG, "CryptonightR: program for height %llu released (old program)", entry.height);
old_programs.push_back(entry.program);
Expand Down Expand Up @@ -252,10 +253,12 @@ static cl_program CryptonightR_build_program(
return program;
}

cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t height, bool background, cl_kernel old_kernel)
cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t height, uint32_t precompile_count, bool background, cl_kernel old_kernel)
{
printer::inst()->print_msg(LDEBUG, "CryptonightR: start %llu released",height);

if (background) {
background_exec([=](){ CryptonightR_get_program(ctx, algo, height, false, old_kernel); });
background_exec([=](){ CryptonightR_get_program(ctx, algo, height, precompile_count, false, old_kernel); });
return nullptr;
}

Expand Down Expand Up @@ -347,7 +350,7 @@ cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t

}

return CryptonightR_build_program(ctx, algo, height, old_kernel, source, options);
return CryptonightR_build_program(ctx, algo, height, precompile_count, old_kernel, source, options);
}

} // namespace amd
Expand Down
2 changes: 1 addition & 1 deletion xmrstak/backend/amd/OclCryptonightR_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace amd
{

cl_program CryptonightR_get_program(GpuContext* ctx, const xmrstak_algo algo,
uint64_t height, bool background = false, cl_kernel old_kernel = nullptr);
uint64_t height, uint32_t precompile_count, bool background = false, cl_kernel old_kernel = nullptr);

} // namespace amd
} // namespace xmrstak
10 changes: 5 additions & 5 deletions xmrstak/backend/amd/amd_gpu/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,10 @@ size_t XMRSetJob(GpuContext* ctx, uint8_t* input, size_t input_len, uint64_t tar

if ((miner_algo == cryptonight_r) || (miner_algo == cryptonight_r_wow)) {

uint32_t PRECOMPILATION_DEPTH = 4;

// Get new kernel
cl_program program = xmrstak::amd::CryptonightR_get_program(ctx, miner_algo, height);
cl_program program = xmrstak::amd::CryptonightR_get_program(ctx, miner_algo, height, PRECOMPILATION_DEPTH);

if (program != ctx->ProgramCryptonightR) {
cl_int ret;
Expand All @@ -969,12 +971,10 @@ size_t XMRSetJob(GpuContext* ctx, uint8_t* input, size_t input_len, uint64_t tar
}
ctx->ProgramCryptonightR = program;

uint32_t PRECOMPILATION_DEPTH = 4;

// Precompile next program in background
xmrstak::amd::CryptonightR_get_program(ctx, miner_algo, height + 1, true, old_kernel);
xmrstak::amd::CryptonightR_get_program(ctx, miner_algo, height + 1, PRECOMPILATION_DEPTH, true, old_kernel);
for (int i = 2; i <= PRECOMPILATION_DEPTH; ++i)
xmrstak::amd::CryptonightR_get_program(ctx, miner_algo, height + i, true, nullptr);
xmrstak::amd::CryptonightR_get_program(ctx, miner_algo, height + i, PRECOMPILATION_DEPTH, true, nullptr);

printer::inst()->print_msg(LDEBUG, "Thread #%zu updated CryptonightR", ctx->deviceIdx);
}
Expand Down
9 changes: 5 additions & 4 deletions xmrstak/backend/nvidia/CudaCryptonightR_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ static void CryptonightR_build_program(
std::string& lowered_name,
const xmrstak_algo& algo,
uint64_t height,
uint32_t precompile_count,
int arch_major,
int arch_minor,
std::string source)
Expand All @@ -164,7 +165,7 @@ static void CryptonightR_build_program(
for (size_t i = 0; i < CryptonightR_cache.size();)
{
const CacheEntry& entry = CryptonightR_cache[i];
if ((entry.algo == algo) && (entry.height + 2 < height))
if ((entry.algo == algo) && (entry.height + 2 + precompile_count < height))
{
printer::inst()->print_msg(LDEBUG, "CryptonightR: program for height %llu released (old program)", entry.height);
CryptonightR_cache[i] = std::move(CryptonightR_cache.back());
Expand Down Expand Up @@ -273,10 +274,10 @@ static void CryptonightR_build_program(
CryptonightR_cache_mutex.UnLock();
}

void CryptonightR_get_program(std::vector<char>& ptx, std::string& lowered_name, const xmrstak_algo algo, uint64_t height, int arch_major, int arch_minor, bool background)
void CryptonightR_get_program(std::vector<char>& ptx, std::string& lowered_name, const xmrstak_algo algo, uint64_t height, uint32_t precompile_count, int arch_major, int arch_minor, bool background)
{
if (background) {
background_exec([=]() { std::vector<char> tmp; std::string s; CryptonightR_get_program(tmp, s, algo, height, arch_major, arch_minor, false); });
background_exec([=]() { std::vector<char> tmp; std::string s; CryptonightR_get_program(tmp, s, algo, height, precompile_count, arch_major, arch_minor, false); });
return;
}

Expand Down Expand Up @@ -329,7 +330,7 @@ void CryptonightR_get_program(std::vector<char>& ptx, std::string& lowered_name,
CryptonightR_cache_mutex.UnLock();
}

CryptonightR_build_program(ptx, lowered_name, algo, height, arch_major, arch_minor, source_code);
CryptonightR_build_program(ptx, lowered_name, algo, height, precompile_count, arch_major, arch_minor, source_code);
}

} // namespace xmrstak
Expand Down
2 changes: 1 addition & 1 deletion xmrstak/backend/nvidia/CudaCryptonightR_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace nvidia
{

void CryptonightR_get_program(std::vector<char>& ptx, std::string& lowered_name,
const xmrstak_algo algo, uint64_t height, int arch_major, int arch_minor, bool background = false);
const xmrstak_algo algo, uint64_t height, uint32_t precompile_count, int arch_major, int arch_minor, bool background = false);


} // namespace xmrstak
Expand Down
8 changes: 6 additions & 2 deletions xmrstak/backend/nvidia/nvcc_code/cuda_core.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1033,17 +1033,21 @@ void cryptonight_core_cpu_hash(nvid_ctx* ctx, const xmrstak_algo& miner_algo, ui
if(ctx->module)
cuModuleUnload(ctx->module);

uint32_t PRECOMPILATION_DEPTH = 4;

std::vector<char> ptx;
std::string lowered_name;
xmrstak::nvidia::CryptonightR_get_program(ptx, lowered_name, miner_algo, chain_height, ctx->device_arch[0], ctx->device_arch[1]);
xmrstak::nvidia::CryptonightR_get_program(ptx, lowered_name, miner_algo, chain_height, PRECOMPILATION_DEPTH, ctx->device_arch[0], ctx->device_arch[1]);

CU_CHECK(ctx->device_id, cuModuleLoadDataEx(&ctx->module, ptx.data(), 0, 0, 0));
CU_CHECK(ctx->device_id, cuModuleGetFunction(&ctx->kernel, ctx->module, lowered_name.c_str()));

ctx->kernel_height = chain_height;
ctx->cached_algo = miner_algo;

xmrstak::nvidia::CryptonightR_get_program(ptx, lowered_name, miner_algo, chain_height + 1, ctx->device_arch[0], ctx->device_arch[1], true);
for (int i = 1; i <= PRECOMPILATION_DEPTH; ++i)
xmrstak::nvidia::CryptonightR_get_program(ptx, lowered_name, miner_algo,
chain_height + i, PRECOMPILATION_DEPTH, ctx->device_arch[0], ctx->device_arch[1], true);
}
}

Expand Down

0 comments on commit 3ebf66a

Please sign in to comment.