2 #define _CRT_SECURE_NO_WARNINGS 15 #include "kernels/silentarmy.h" 16 #include "../primitives/block.h" 20 #define CL_MAP_WRITE_INVALIDATE_REGION CL_MAP_WRITE 21 #define CL_MEM_HOST_READ_ONLY 0 41 extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const
char* lpOutputString);
42 static
std::atomic_flag s_logSpin = ATOMIC_FLAG_INIT;
43 #define CL_LOG(_contents) \ 46 std::stringstream ss; \ 48 while (s_logSpin.test_and_set(std::memory_order_acquire)) {} \ 49 OutputDebugStringA(ss.str().c_str()); \ 50 cerr << ss.str() << endl << flush; \ 51 s_logSpin.clear(std::memory_order_release); \ 54 #define CL_LOG(_contents) cout << "[OPENCL]:" << _contents << endl 58 #define CL_QUERIED_DEVICE_TYPES (CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR) 61 : m_openclOnePointOne()
66 std::cout <<
"Error allocating dst_solutions array!" << std::endl;
79 vector<cl::Platform> platforms;
84 catch(cl::Error
const& err)
86 #if defined(CL_PLATFORM_NOT_FOUND_KHR) 87 if (err.err() == CL_PLATFORM_NOT_FOUND_KHR)
88 CL_LOG(
"No OpenCL platforms found");
99 if (platforms.empty())
102 unsigned platform_num = min<unsigned>(_platformId, platforms.size() - 1);
103 vector<cl::Device> devices =
getDevices(platforms, _platformId);
106 CL_LOG(
"No OpenCL devices found.");
111 unsigned device_num = min<unsigned>(_deviceId, devices.size() - 1);
113 string device_version = device.
getInfo<CL_DEVICE_VERSION>();
115 return "{ \"platform\": \"" + platforms[platform_num].getInfo<CL_PLATFORM_NAME>() +
"\", \"device\": \"" + device.getInfo<CL_DEVICE_NAME>() +
"\", \"version\": \"" + device_version +
"\" }";
118 std::vector<cl::Device>
cl_gpuminer::getDevices(std::vector<cl::Platform>
const& _platforms,
unsigned _platformId)
120 vector<cl::Device> devices;
121 unsigned platform_num = min<unsigned>(_platformId, _platforms.size() - 1);
124 _platforms[platform_num].getDevices(
129 catch (cl::Error
const& err)
132 if (err.err() != CL_DEVICE_NOT_FOUND)
141 if (platforms.empty())
143 return platforms.size();
149 if (platforms.empty())
152 vector<cl::Device> devices =
getDevices(platforms, _platformId);
155 CL_LOG(
"No OpenCL devices found.");
158 return devices.size();
163 unsigned _platformId,
164 unsigned _localWorkSize,
165 unsigned _globalWorkSize
175 _device.
getInfo(CL_DEVICE_GLOBAL_MEM_SIZE, &result);
178 "Found suitable OpenCL device [" << _device.
getInfo<CL_DEVICE_NAME>()
179 <<
"] with " << result <<
" bytes of GPU memory" 189 if (platforms.empty())
191 for (
unsigned i = 0; i < platforms.size(); ++i)
201 if (platforms.empty())
203 if (_platformId >= platforms.size())
206 vector<cl::Device> devices =
getDevices(platforms, _platformId);
208 if (_callback(device))
217 if (platforms.empty())
219 for (
unsigned i = 0; i < platforms.size(); ++i)
226 if (platforms.empty())
228 if (_platformId >= platforms.size())
231 vector<cl::Device> devices =
getDevices(platforms, _platformId);
238 string outString =
"\nListing OpenCL devices.\nFORMAT: [deviceID] deviceName\n";
242 outString +=
"[" + to_string(i) +
"] " + _device.
getInfo<CL_DEVICE_NAME>() +
"\n";
243 outString +=
"\tCL_DEVICE_TYPE: ";
244 switch (_device.
getInfo<CL_DEVICE_TYPE>())
246 case CL_DEVICE_TYPE_CPU:
247 outString +=
"CPU\n";
249 case CL_DEVICE_TYPE_GPU:
250 outString +=
"GPU\n";
252 case CL_DEVICE_TYPE_ACCELERATOR:
253 outString +=
"ACCELERATOR\n";
256 outString +=
"DEFAULT\n";
259 outString +=
"\tCL_DEVICE_GLOBAL_MEM_SIZE: " + to_string(_device.
getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>()) +
"\n";
260 outString +=
"\tCL_DEVICE_MAX_MEM_ALLOC_SIZE: " + to_string(_device.
getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>()) +
"\n";
261 outString +=
"\tCL_DEVICE_MAX_WORK_GROUP_SIZE: " + to_string(_device.
getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>()) +
"\n";
277 unsigned _platformId,
279 const std::vector<std::string> _kernels
286 if (platforms.empty())
290 _platformId = min<unsigned>(_platformId, platforms.size() - 1);
291 CL_LOG(
"Using platform: " << platforms[_platformId].getInfo<CL_PLATFORM_NAME>().c_str());
294 vector<cl::Device> devices =
getDevices(platforms, _platformId);
297 CL_LOG(
"No OpenCL devices found.");
302 cl::Device& device = devices[min<unsigned>(_deviceId, devices.size() - 1)];
303 string device_version = device.
getInfo<CL_DEVICE_VERSION>();
304 CL_LOG(
"Using device: " << device.
getInfo<CL_DEVICE_NAME>().c_str() <<
"(" << device_version.c_str() <<
")");
306 if (strncmp(
"OpenCL 1.0", device_version.c_str(), 10) == 0)
308 CL_LOG(
"OpenCL 1.0 is not supported.");
311 if (strncmp(
"OpenCL 1.1", device_version.c_str(), 10) == 0)
333 ifstream kernel_file(
"./libgpuminer/kernels/silentarmy.cl");
334 string code((istreambuf_iterator<char>(kernel_file)), istreambuf_iterator<char>());
341 sources.push_back({ code.c_str(), code.size() });
346 program.
build({ device });
347 CL_LOG(
"Printing program log");
350 catch (cl::Error
const&)
358 for (
auto & _kernel : _kernels)
361 catch (cl::Error
const& err)
363 CL_LOG(
"gpuKERNEL Creation failed: " << err.what() <<
"(" << err.err() <<
"). Bailing.");
379 catch (cl::Error
const& err)
395 uint32_t sol_found = 0;
396 size_t local_ws = 64;
398 unsigned char buf[136] = {0};
407 memcpy( buf + 8, header + 128, header_len - 128);
408 buf[0] = (header_len - 128)/8+1;
441 if (round == PARAM_K - 1)
458 size_t sz =
sizeof(
sols_t)*1;
460 sols = (
sols_t *)malloc(sz);
469 for (
unsigned sol_i = 0; sol_i < sols->
nr; sol_i++)
476 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[]
#define CL_QUERIED_DEVICE_TYPES
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)
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()
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
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.
#define CL_LOG(_contents)
static bool configureGPU(unsigned _platformId, unsigned _localWorkSize, unsigned _globalWorkSize)
static unsigned s_initialGlobalWorkSize
The initial global work size for the searches.