From a48dff3bee03bb37b2893372aba7563e3b97433c Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 15 Aug 2017 12:24:43 +0200 Subject: [PATCH] PoW init reordering - inited by the worker thread on its own init, instead of when the imports are being evaluated - also got rid of windows-style newlines in OpenCL PoW --- src/class_singleWorker.py | 1 + src/openclpow.py | 216 +++++++++++++++++++------------------- src/proofofwork.py | 9 +- 3 files changed, 114 insertions(+), 112 deletions(-) diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index e7f58f1b..58eb33c6 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -42,6 +42,7 @@ class singleWorker(threading.Thread, StoppableThread): # QThread.__init__(self, parent) threading.Thread.__init__(self, name="singleWorker") self.initStop() + proofofwork.init() def stopThread(self): try: diff --git a/src/openclpow.py b/src/openclpow.py index ca40e634..894a5b77 100644 --- a/src/openclpow.py +++ b/src/openclpow.py @@ -1,111 +1,111 @@ -#!/usr/bin/env python2.7 -from struct import pack, unpack -import time -import hashlib -import random -import os - -from bmconfigparser import BMConfigParser -import paths -from state import shutdown -from debug import logger - -libAvailable = True -ctx = False -queue = False -program = False -gpus = [] -enabledGpus = [] -vendors = [] -hash_dt = None - -try: - import numpy - import pyopencl as cl -except: - libAvailable = False - -def initCL(): +#!/usr/bin/env python2.7 +from struct import pack, unpack +import time +import hashlib +import random +import os + +from bmconfigparser import BMConfigParser +import paths +from state import shutdown +from debug import logger + +libAvailable = True +ctx = False +queue = False +program = False +gpus = [] +enabledGpus = [] +vendors = [] +hash_dt = None + +try: + import numpy + import pyopencl as cl +except: + libAvailable = False + +def initCL(): global ctx, queue, program, hash_dt, libAvailable if libAvailable is False: return - del enabledGpus[:] - del vendors[:] - del gpus[:] - ctx = False - try: - hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)]) - try: - for platform in cl.get_platforms(): - gpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) - if BMConfigParser().safeGet("bitmessagesettings", "opencl") == platform.vendor: - enabledGpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) - if platform.vendor not in vendors: - vendors.append(platform.vendor) - except: - pass - if (len(enabledGpus) > 0): - ctx = cl.Context(devices=enabledGpus) - queue = cl.CommandQueue(ctx) - f = open(os.path.join(paths.codePath(), "bitmsghash", 'bitmsghash.cl'), 'r') - fstr = ''.join(f.readlines()) - program = cl.Program(ctx, fstr).build(options="") - logger.info("Loaded OpenCL kernel") - else: - logger.info("No OpenCL GPUs found") - del enabledGpus[:] - except Exception as e: - logger.error("OpenCL fail: ", exc_info=True) - del enabledGpus[:] - -def openclAvailable(): - return (len(gpus) > 0) - -def openclEnabled(): - return (len(enabledGpus) > 0) - -def do_opencl_pow(hash, target): - output = numpy.zeros(1, dtype=[('v', numpy.uint64, 1)]) - if (len(enabledGpus) == 0): - return output[0][0] - - data = numpy.zeros(1, dtype=hash_dt, order='C') - data[0]['v'] = ("0000000000000000" + hash).decode("hex") - data[0]['target'] = target - - hash_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=data) - dest_buf = cl.Buffer(ctx, cl.mem_flags.WRITE_ONLY, output.nbytes) - - kernel = program.kernel_sha512 - worksize = kernel.get_work_group_info(cl.kernel_work_group_info.WORK_GROUP_SIZE, enabledGpus[0]) - - kernel.set_arg(0, hash_buf) - kernel.set_arg(1, dest_buf) - - start = time.time() - progress = 0 - globamt = worksize*2000 - - while output[0][0] == 0 and shutdown == 0: - kernel.set_arg(2, pack("Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8]) - print "{} - value {} < {}".format(nonce, trialValue, target) - + del enabledGpus[:] + del vendors[:] + del gpus[:] + ctx = False + try: + hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)]) + try: + for platform in cl.get_platforms(): + gpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) + if BMConfigParser().safeGet("bitmessagesettings", "opencl") == platform.vendor: + enabledGpus.extend(platform.get_devices(device_type=cl.device_type.GPU)) + if platform.vendor not in vendors: + vendors.append(platform.vendor) + except: + pass + if (len(enabledGpus) > 0): + ctx = cl.Context(devices=enabledGpus) + queue = cl.CommandQueue(ctx) + f = open(os.path.join(paths.codePath(), "bitmsghash", 'bitmsghash.cl'), 'r') + fstr = ''.join(f.readlines()) + program = cl.Program(ctx, fstr).build(options="") + logger.info("Loaded OpenCL kernel") + else: + logger.info("No OpenCL GPUs found") + del enabledGpus[:] + except Exception as e: + logger.error("OpenCL fail: ", exc_info=True) + del enabledGpus[:] + +def openclAvailable(): + return (len(gpus) > 0) + +def openclEnabled(): + return (len(enabledGpus) > 0) + +def do_opencl_pow(hash, target): + output = numpy.zeros(1, dtype=[('v', numpy.uint64, 1)]) + if (len(enabledGpus) == 0): + return output[0][0] + + data = numpy.zeros(1, dtype=hash_dt, order='C') + data[0]['v'] = ("0000000000000000" + hash).decode("hex") + data[0]['target'] = target + + hash_buf = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=data) + dest_buf = cl.Buffer(ctx, cl.mem_flags.WRITE_ONLY, output.nbytes) + + kernel = program.kernel_sha512 + worksize = kernel.get_work_group_info(cl.kernel_work_group_info.WORK_GROUP_SIZE, enabledGpus[0]) + + kernel.set_arg(0, hash_buf) + kernel.set_arg(1, dest_buf) + + start = time.time() + progress = 0 + globamt = worksize*2000 + + while output[0][0] == 0 and shutdown == 0: + kernel.set_arg(2, pack("Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8]) + print "{} - value {} < {}".format(nonce, trialValue, target) + diff --git a/src/proofofwork.py b/src/proofofwork.py index eb845c25..12d513b6 100644 --- a/src/proofofwork.py +++ b/src/proofofwork.py @@ -237,6 +237,9 @@ def resetPoW(): # init def init(): global bitmsglib, bso, bmpow + + openclpow.initCL() + if "win32" == sys.platform: if ctypes.sizeof(ctypes.c_voidp) == 4: bitmsglib = 'bitmsghash32.dll' @@ -286,7 +289,5 @@ def init(): bmpow = None else: bmpow = None - -init() -if bmpow is None: - buildCPoW() + if bmpow is None: + buildCPoW()