Skip to content

Commit

Permalink
2.10.4
Browse files Browse the repository at this point in the history
  • Loading branch information
IndeedMiners committed Apr 27, 2019
1 parent dc6643f commit b50f3f9
Show file tree
Hide file tree
Showing 106 changed files with 6,976 additions and 6,409 deletions.
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
IndentWidth: 4
TabWidth: 4
ColumnLimit: 0
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
SpaceBeforeParens: Never
UseTab: Always
AlignAfterOpenBracket: DontAlign
PointerBindsToType: true
BreakConstructorInitializers: AfterColon
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
165 changes: 83 additions & 82 deletions xmrstak/backend/amd/OclCryptonightR_gen.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#include <string>
#include <sstream>
#include <mutex>
#include <cstring>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>


#include "xmrstak/backend/amd/OclCryptonightR_gen.hpp"
#include "xmrstak/backend/cpu/crypto/variant4_random_math.h"
#include "xmrstak/misc/console.hpp"
#include "xmrstak/cpputil/read_write_lock.h"
#include "xmrstak/misc/console.hpp"

#include <chrono>
#include <thread>
#include <iostream>
#include <regex>

#include <thread>

namespace xmrstak
{
Expand All @@ -23,16 +21,16 @@ namespace amd

static std::string get_code(const V4_Instruction* code, int code_size)
{
std::stringstream s;
std::stringstream s;

for (int i = 0; i < code_size; ++i)
for(int i = 0; i < code_size; ++i)
{
const V4_Instruction inst = code[i];

const uint32_t a = inst.dst_index;
const uint32_t b = inst.src_index;

switch (inst.opcode)
switch(inst.opcode)
{
case MUL:
s << 'r' << a << "*=r" << b << ';';
Expand All @@ -59,37 +57,39 @@ static std::string get_code(const V4_Instruction* code, int code_size)
s << '\n';
}

return s.str();
return s.str();
}

struct CacheEntry
{
CacheEntry(xmrstak_algo algo, uint64_t height_offset, size_t deviceIdx, cl_program program) :
algo(algo),
height_offset(height_offset),
deviceIdx(deviceIdx),
program(program)
{}

xmrstak_algo algo;
uint64_t height_offset;
size_t deviceIdx;
cl_program program;
CacheEntry(xmrstak_algo algo, uint64_t height_offset, size_t deviceIdx, cl_program program) :
algo(algo),
height_offset(height_offset),
deviceIdx(deviceIdx),
program(program)
{
}

xmrstak_algo algo;
uint64_t height_offset;
size_t deviceIdx;
cl_program program;
};

struct BackgroundTaskBase
{
virtual ~BackgroundTaskBase() {}
virtual void exec() = 0;
virtual ~BackgroundTaskBase() {}
virtual void exec() = 0;
};

template<typename T>
template <typename T>
struct BackgroundTask : public BackgroundTaskBase
{
BackgroundTask(T&& func) : m_func(std::move(func)) {}
void exec() override { m_func(); }
BackgroundTask(T&& func) :
m_func(std::move(func)) {}
void exec() override { m_func(); }

T m_func;
T m_func;
};

static ::cpputil::RWLock CryptonightR_cache_mutex;
Expand All @@ -101,19 +101,18 @@ static std::vector<BackgroundTaskBase*> background_tasks;
static std::thread* background_thread = nullptr;

static cl_program search_program(
const GpuContext* ctx,
xmrstak_algo algo,
const GpuContext* ctx,
xmrstak_algo algo,
uint64_t height_offset,
bool lock_cache = true
)
bool lock_cache = true)
{
if(lock_cache)
CryptonightR_cache_mutex.ReadLock();

// Check if the cache has this program
for (const CacheEntry& entry : CryptonightR_cache)
for(const CacheEntry& entry : CryptonightR_cache)
{
if ((entry.algo == algo) && (entry.height_offset == height_offset) && (entry.deviceIdx == ctx->deviceIdx))
if((entry.algo == algo) && (entry.height_offset == height_offset) && (entry.deviceIdx == ctx->deviceIdx))
{
printer::inst()->print_msg(LDEBUG, "CryptonightR: program for height_offset %llu found in cache", height_offset);
auto result = entry.program;
Expand All @@ -130,43 +129,46 @@ static cl_program search_program(

static void background_thread_proc()
{
std::vector<BackgroundTaskBase*> tasks;
for (;;) {
tasks.clear();
{
std::lock_guard<std::mutex> g(background_tasks_mutex);
background_tasks.swap(tasks);
}

for (BackgroundTaskBase* task : tasks) {
task->exec();
delete task;
}
std::vector<BackgroundTaskBase*> tasks;
for(;;)
{
tasks.clear();
{
std::lock_guard<std::mutex> g(background_tasks_mutex);
background_tasks.swap(tasks);
}

for(BackgroundTaskBase* task : tasks)
{
task->exec();
delete task;
}

std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
}

template<typename T>
template <typename T>
static void background_exec(T&& func)
{
BackgroundTaskBase* task = new BackgroundTask<T>(std::move(func));
BackgroundTaskBase* task = new BackgroundTask<T>(std::move(func));

std::lock_guard<std::mutex> g(background_tasks_mutex);
background_tasks.push_back(task);
if (!background_thread) {
background_thread = new std::thread(background_thread_proc);
}
std::lock_guard<std::mutex> g(background_tasks_mutex);
background_tasks.push_back(task);
if(!background_thread)
{
background_thread = new std::thread(background_thread_proc);
}
}

static cl_program CryptonightR_build_program(
const GpuContext* ctx,
xmrstak_algo algo,
uint64_t height_offset,
const GpuContext* ctx,
xmrstak_algo algo,
uint64_t height_offset,
uint64_t height_chunk_size,
uint32_t precompile_count,
std::string source_code,
std::string options)
uint32_t precompile_count,
std::string source_code,
std::string options)
{
std::vector<cl_program> old_programs;
old_programs.reserve(32);
Expand All @@ -177,7 +179,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_offset + (2 + precompile_count) * height_chunk_size < height_offset))
if((entry.algo == algo) && (entry.height_offset + (2 + precompile_count) * height_chunk_size < height_offset))
{
printer::inst()->print_msg(LDEBUG, "CryptonightR: program for height_offset %llu released (old program)", entry.height_offset);
old_programs.push_back(entry.program);
Expand All @@ -201,7 +203,8 @@ static cl_program CryptonightR_build_program(

cl_program program = search_program(ctx, algo, height_offset);

if(program) {
if(program)
{
return program;
}

Expand All @@ -211,19 +214,19 @@ static cl_program CryptonightR_build_program(
program = clCreateProgramWithSource(ctx->opencl_ctx, 1, (const char**)&source, NULL, &ret);
if(ret != CL_SUCCESS)
{
printer::inst()->print_msg(L0,"Error %s when calling clCreateProgramWithSource on the OpenCL miner code", err_to_str(ret));
printer::inst()->print_msg(L0, "Error %s when calling clCreateProgramWithSource on the OpenCL miner code", err_to_str(ret));
return program;
}

ret = clBuildProgram(program, 1, &ctx->DeviceID, options.c_str(), NULL, NULL);
if(ret != CL_SUCCESS)
{
size_t len;
printer::inst()->print_msg(L0,"Error %s when calling clBuildProgram.", err_to_str(ret));
printer::inst()->print_msg(L0, "Error %s when calling clBuildProgram.", err_to_str(ret));

if((ret = clGetProgramBuildInfo(program, ctx->DeviceID, CL_PROGRAM_BUILD_LOG, 0, NULL, &len)) != CL_SUCCESS)
{
printer::inst()->print_msg(L0,"Error %s when calling clGetProgramBuildInfo for length of build log output.", err_to_str(ret));
printer::inst()->print_msg(L0, "Error %s when calling clGetProgramBuildInfo for length of build log output.", err_to_str(ret));
return program;
}

Expand All @@ -233,12 +236,12 @@ static cl_program CryptonightR_build_program(
if((ret = clGetProgramBuildInfo(program, ctx->DeviceID, CL_PROGRAM_BUILD_LOG, len, BuildLog, NULL)) != CL_SUCCESS)
{
free(BuildLog);
printer::inst()->print_msg(L0,"Error %s when calling clGetProgramBuildInfo for build log.", err_to_str(ret));
printer::inst()->print_msg(L0, "Error %s when calling clGetProgramBuildInfo for build log.", err_to_str(ret));
return program;
}

printer::inst()->print_str("Build log:\n");
std::cerr<<BuildLog<<std::endl;
std::cerr << BuildLog << std::endl;

free(BuildLog);
return program;
Expand All @@ -249,12 +252,11 @@ static cl_program CryptonightR_build_program(
{
if((ret = clGetProgramBuildInfo(program, ctx->DeviceID, CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &status, NULL)) != CL_SUCCESS)
{
printer::inst()->print_msg(L0,"Error %s when calling clGetProgramBuildInfo for status of build.", err_to_str(ret));
printer::inst()->print_msg(L0, "Error %s when calling clGetProgramBuildInfo for status of build.", err_to_str(ret));
return program;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
while(status == CL_BUILD_IN_PROGRESS);
} while(status == CL_BUILD_IN_PROGRESS);

CryptonightR_cache_mutex.WriteLock();
auto cached_program = search_program(ctx, algo, height_offset, false);
Expand All @@ -277,9 +279,9 @@ static cl_program CryptonightR_build_program(

cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t height_offset, uint64_t height_chunk_size, uint32_t precompile_count, bool background)
{
if (background)
if(background)
{
background_exec([=](){ CryptonightR_get_program(ctx, algo, height_offset, height_chunk_size, precompile_count, false); });
background_exec([=]() { CryptonightR_get_program(ctx, algo, height_offset, height_chunk_size, precompile_count, false); });
return nullptr;
}

Expand All @@ -288,19 +290,19 @@ cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t
if(program != nullptr)
return program;

printer::inst()->print_msg(LDEBUG, "CryptonightR: create code for block %llu to %llu",height_offset, height_offset + height_chunk_size);
printer::inst()->print_msg(LDEBUG, "CryptonightR: create code for block %llu to %llu", height_offset, height_offset + height_chunk_size);

const char* source_code_definitions=
#include "amd_gpu/opencl/wolf-aes.cl"
#include "amd_gpu/opencl/cryptonight_r_def.rtcl"
;
const char* source_code_definitions =
#include "amd_gpu/opencl/cryptonight_r_def.rtcl"
#include "amd_gpu/opencl/wolf-aes.cl"
;

const char* source_code_template =
#include "amd_gpu/opencl/cryptonight_r.rtcl"
;
#include "amd_gpu/opencl/cryptonight_r.rtcl"
;
const char include_name[] = "XMRSTAK_INCLUDE_RANDOM_MATH";
const char* offset = strstr(source_code_template, include_name);
if (!offset)
if(!offset)
{
printer::inst()->print_msg(LDEBUG, "CryptonightR_get_program: XMRSTAK_INCLUDE_RANDOM_MATH not found in cryptonight_r.cl", algo);
return nullptr;
Expand All @@ -312,7 +314,7 @@ cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t
{
V4_Instruction code[256];
int code_size;
switch (algo.Id())
switch(algo.Id())
{
case cryptonight_r_wow:
code_size = v4_random_math_init<cryptonight_r_wow>(code, height_offset + c);
Expand Down Expand Up @@ -370,7 +372,6 @@ cl_program CryptonightR_get_program(GpuContext* ctx, xmrstak_algo algo, uint64_t
if(algo == cryptonight_gpu)
options += " -cl-fp32-correctly-rounded-divide-sqrt";


program = search_program(ctx, algo, height_offset);

if(program != nullptr)
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 @@ -3,8 +3,8 @@
#include "xmrstak/backend/cryptonight.hpp"

#include <stdint.h>
#include <vector>
#include <string>
#include <vector>

#if defined(__APPLE__)
#include <OpenCL/cl.h>
Expand Down
Loading

0 comments on commit b50f3f9

Please sign in to comment.