2 #define _CRT_SECURE_NO_WARNINGS 15 #include "kernels/silentarmy.h" 17 #include "../primitives/block.h" 21 #define CL_MAP_WRITE_INVALIDATE_REGION CL_MAP_WRITE 22 #define CL_MEM_HOST_READ_ONLY 0 42 extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const
char* lpOutputString);
43 static
std::atomic_flag s_logSpin = ATOMIC_FLAG_INIT;
44 #define CL_LOG(_contents) \ 47 std::stringstream ss; \ 49 while (s_logSpin.test_and_set(std::memory_order_acquire)) {} \ 50 OutputDebugStringA(ss.str().c_str()); \ 51 cerr << ss.str() << endl << flush; \ 52 s_logSpin.clear(std::memory_order_release); \ 55 #define CL_LOG(_contents) cout << "[OPENCL]:" << _contents << endl 59 #define CL_QUERIED_DEVICE_TYPES (CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR) 62 : m_openclOnePointOne()
74 std::cout <<
"Error allocating dst_solutions array!" << std::endl;
87 vector<cl::Platform> platforms;
92 catch(cl::Error
const& err)
94 #if defined(CL_PLATFORM_NOT_FOUND_KHR) 95 if (err.err() == CL_PLATFORM_NOT_FOUND_KHR)
96 CL_LOG(
"No OpenCL platforms found");
107 if (platforms.empty())
110 unsigned platform_num = min<unsigned>(_platformId, platforms.size() - 1);
111 vector<cl::Device> devices =
getDevices(platforms, _platformId);
114 CL_LOG(
"No OpenCL devices found.");
119 unsigned device_num = min<unsigned>(_deviceId, devices.size() - 1);
121 string device_version = device.
getInfo<CL_DEVICE_VERSION>();
123 return "{ \"platform\": \"" + platforms[platform_num].getInfo<CL_PLATFORM_NAME>() +
"\", \"device\": \"" + device.getInfo<CL_DEVICE_NAME>() +
"\", \"version\": \"" + device_version +
"\" }";
128 vector<cl::Device> devices;
129 unsigned platform_num = min<unsigned>(_platformId, _platforms.size() - 1);
132 _platforms[platform_num].getDevices(
137 catch (cl::Error
const& err)
140 if (err.err() != CL_DEVICE_NOT_FOUND)
149 if (platforms.empty())
151 return platforms.size();
157 if (platforms.empty())
160 vector<cl::Device> devices =
getDevices(platforms, _platformId);
163 CL_LOG(
"No OpenCL devices found.");
166 return devices.size();
171 unsigned _platformId,
172 unsigned _localWorkSize,
173 unsigned _globalWorkSize
183 _device.
getInfo(CL_DEVICE_GLOBAL_MEM_SIZE, &result);
186 "Found suitable OpenCL device [" << _device.
getInfo<CL_DEVICE_NAME>()
187 <<
"] with " << result <<
" bytes of GPU memory" 197 if (platforms.empty())
199 for (
unsigned i = 0; i < platforms.size(); ++i)
209 if (platforms.empty())
211 if (_platformId >= platforms.size())
214 vector<cl::Device> devices =
getDevices(platforms, _platformId);
216 if (_callback(device))
225 if (platforms.empty())
227 for (
unsigned i = 0; i < platforms.size(); ++i)
234 if (platforms.empty())
236 if (_platformId >= platforms.size())
239 vector<cl::Device> devices =
getDevices(platforms, _platformId);
246 string outString =
"\nListing OpenCL devices.\nFORMAT: [deviceID] deviceName\n";
250 outString +=
"[" + to_string(i) +
"] " + _device.
getInfo<CL_DEVICE_NAME>() +
"\n";
251 outString +=
"\tCL_DEVICE_TYPE: ";
252 switch (_device.
getInfo<CL_DEVICE_TYPE>())
254 case CL_DEVICE_TYPE_CPU:
255 outString +=
"CPU\n";
257 case CL_DEVICE_TYPE_GPU:
258 outString +=
"GPU\n";
260 case CL_DEVICE_TYPE_ACCELERATOR:
261 outString +=
"ACCELERATOR\n";
264 outString +=
"DEFAULT\n";
267 outString +=
"\tCL_DEVICE_GLOBAL_MEM_SIZE: " + to_string(_device.
getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>()) +
"\n";
268 outString +=
"\tCL_DEVICE_MAX_MEM_ALLOC_SIZE: " + to_string(_device.
getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>()) +
"\n";
269 outString +=
"\tCL_DEVICE_MAX_WORK_GROUP_SIZE: " + to_string(_device.
getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>()) +
"\n";
285 unsigned _platformId,
287 const std::vector<std::string> _kernels
294 if (platforms.empty())
298 _platformId = min<unsigned>(_platformId, platforms.size() - 1);
299 CL_LOG(
"Using platform: " << platforms[_platformId].getInfo<CL_PLATFORM_NAME>().c_str());
302 vector<cl::Device> devices =
getDevices(platforms, _platformId);
305 CL_LOG(
"No OpenCL devices found.");
310 cl::Device& device = devices[min<unsigned>(_deviceId, devices.size() - 1)];
311 string device_version = device.
getInfo<CL_DEVICE_VERSION>();
312 CL_LOG(
"Using device: " << device.
getInfo<CL_DEVICE_NAME>().c_str() <<
"(" << device_version.c_str() <<
")");
314 if (strncmp(
"OpenCL 1.0", device_version.c_str(), 10) == 0)
316 CL_LOG(
"OpenCL 1.0 is not supported.");
319 if (strncmp(
"OpenCL 1.1", device_version.c_str(), 10) == 0)
341 ifstream kernel_file(
"./libgpuminer/kernels/silentarmy.cl");
342 string code((istreambuf_iterator<char>(kernel_file)), istreambuf_iterator<char>());
345 unsigned char *pcodestr =
nullptr;
362 string code(pcodestr, pcodestr+ codesize);
366 sources.push_back({ code.c_str(), code.size() });
371 program.
build({ device });
372 CL_LOG(
"Printing program log");
375 catch (cl::Error
const&)
383 for (
auto & _kernel : _kernels)
386 catch (cl::Error
const& err)
388 CL_LOG(
"gpuKERNEL Creation failed: " << err.what() <<
"(" << err.err() <<
"). Bailing.");
404 catch (cl::Error
const& err)
420 uint32_t sol_found = 0;
421 size_t local_ws = 64;
423 unsigned char buf[136] = {0};
433 memcpy( buf + 8, header + 128, header_len - 128);
434 buf[0] = (header_len - 128)/8+1;
443 for (round = 0; round <
PARAM_K; round++)
467 if (round == PARAM_K - 1)
484 size_t sz =
sizeof(
sols_t)*1;
486 sols = (
sols_t *)malloc(sz);
495 for (
unsigned sol_i = 0; sol_i < sols->
nr; sol_i++)
502 catch (cl::Error
const& err)
static std::string platform_info(unsigned _platformId=0, unsigned _deviceId=0)
unsigned int m_stepWorkSizeAdjust
The step used in the work size adjustment.
const unsigned char CL_MINER_KERNEL[]
cl_int getBuildInfo(const Device &device, cl_program_build_info name, T *param) const
static unsigned const c_defaultMSPerBatch
Default value of the milliseconds per global work size (per batch)
unsigned m_globalWorkSize
void zcash_blake2b_init(blake2b_state_t *st, uint8_t hash_len, uint32_t n, uint32_t k)
static unsigned s_msPerBatch
The target milliseconds per batch for the search. If 0, then no adjustment will happen.
void run(uint8_t *header, size_t header_len, uint256 nonce, sols_t *indices, uint32_t *n_sol, uint256 *ptr)
unsigned int FABCOIN_HASH_LEN()
cl_int enqueueWriteBuffer(const Buffer &buffer, cl_bool blocking,::size_t offset,::size_t size, const void *ptr, const VECTOR_CLASS< Event > *events=NULL, Event *event=NULL) const
std::hash for asio::adress
assert(len-trim+(2 *lenIndices)<=WIDTH)
static unsigned s_extraRequiredGPUMem
GPU memory required for other things, like window rendering e.t.c.
static std::vector< cl::Platform > getPlatforms()
static unsigned const c_defaultLocalWorkSize
Default value of the local work size. Also known as workgroup size.
#define FABCOIN_NONCE_LEN
static bool searchForAllDevices(unsigned _platformId, std::function< bool(cl::Device const &)> _callback)
#define CL_LOG(_contents)
static unsigned getNumDevices(unsigned _platformId=0)
static bool s_allowCPU
Allow CPU to appear as an OpenCL device or not. Default is false.
bool init(unsigned _platformId, unsigned _deviceId, std::vector< std::string > _kernels)
cl_int enqueueFillBuffer(const Buffer &buffer, const void *ptr,::size_t pattern_size,::size_t offset,::size_t size, const VECTOR_CLASS< Event > *events=NULL, Event *event=NULL) const
std::vector< cl::Kernel > m_gpuKernels
const char * get_error_string(cl_int error)
cl_int enqueueNDRangeKernel(const Kernel &kernel, const NDRange &offset, const NDRange &global, const NDRange &local, const VECTOR_CLASS< Event > *events=NULL, Event *event=NULL) const
VECTOR_CLASS< std::pair< const char *,::size_t > > Sources
void zcash_blake2b_update(blake2b_state_t *st, const uint8_t *_msg, uint32_t msg_len, uint32_t is_final)
static void listDevices()
unsigned int ROWS_PER_UINT()
#define CL_QUERIED_DEVICE_TYPES
const size_t CL_MINER_KERNEL_SIZE184
CommandQueue interface for cl_command_queue.
const size_t CL_MINER_KERNEL_SIZE
void * memcpy(void *a, const void *b, size_t c)
cl_int build(const VECTOR_CLASS< Device > &devices, const char *options=NULL, void(CL_CALLBACK *notifyFptr)(cl_program, void *)=NULL, void *data=NULL) const
#define round(a, b, c, x, mul)
cl_int getInfo(cl_device_info name, T *param) const
const unsigned char CL_MINER_KERNEL184[]
cl_int enqueueReadBuffer(const Buffer &buffer, cl_bool blocking,::size_t offset,::size_t size, void *ptr, const VECTOR_CLASS< Event > *events=NULL, Event *event=NULL) const
size_t select_work_size_blake(void)
uint32_t verify_sol(sols_t *sols, unsigned sol_i)
Kernel interface that implements cl_kernel.
static void doForAllDevices(unsigned _platformId, std::function< void(cl::Device const &)> _callback)
static unsigned getNumPlatforms()
static unsigned s_workgroupSize
The local work size for the search.
cl::Buffer rowCounters[2]
Device interface for cl_device_id.
Program interface that implements cl_program.
static std::vector< cl::Device > getDevices(std::vector< cl::Platform > const &_platforms, unsigned _platformId)
static unsigned const c_defaultGlobalWorkSizeMultiplier
Default value of the global work size as a multiplier of the local work size.
static bool configureGPU(unsigned _platformId, unsigned _localWorkSize, unsigned _globalWorkSize)
static unsigned s_initialGlobalWorkSize
The initial global work size for the searches.