From 4ab255d43034458a6d59f71ca3b91847e6a147d9 Mon Sep 17 00:00:00 2001 From: mailchuck Date: Fri, 2 Oct 2015 15:04:16 +0200 Subject: [PATCH] OpenCL Fixes Still not fully working and hardcoded device number --- src/openclpow.py | 33 +++++++++++++++------------------ src/proofofwork.py | 9 +++++++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/openclpow.py b/src/openclpow.py index 65449b85..7ab4dba8 100644 --- a/src/openclpow.py +++ b/src/openclpow.py @@ -1,31 +1,28 @@ -import numpy +#!/usr/bin/env python2.7 from struct import pack, unpack import time import hashlib import random -import pyopencl as cl +import os -hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)]) ctx = False queue = False program = False try: - if (len(cl.get_platforms()) > 0): - ctx = cl.create_some_context() - queue = cl.CommandQueue(ctx) - - #f = open('/usr/src/PyBitmessage/src/kernel.cl', 'r') - import os - print "working directory: " + os.getcwd() -# time.sleep(5) - f = open('kernel.cl', 'r') - fstr = ''.join(f.readlines()) - program = cl.Program(ctx, fstr).build() + import numpy + import pyopencl as cl + hash_dt = numpy.dtype([('target', numpy.uint64), ('v', numpy.str_, 73)]) + if (len(cl.get_platforms()) > 0): + ctx = cl.create_some_context() + queue = cl.CommandQueue(ctx) + full_path = os.path.dirname(os.path.realpath(__file__)) + f = open(os.path.join(full_path, 'kernel.cl'), 'r') + fstr = ''.join(f.readlines()) + program = cl.Program(ctx, fstr).build(options="") except Exception as e: - print "opencl fail:" + str(e) -# time.sleep(5) - ctx = False + print "opencl fail:" + str(e) + ctx = False def has_opencl(): return (ctx != False) @@ -43,7 +40,7 @@ def do_opencl_pow(hash, target): 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, cl.get_platforms()[0].get_devices()[0]) + worksize = kernel.get_work_group_info(cl.kernel_work_group_info.WORK_GROUP_SIZE, cl.get_platforms()[0].get_devices()[1]) kernel.set_arg(0, hash_buf) kernel.set_arg(1, dest_buf) diff --git a/src/proofofwork.py b/src/proofofwork.py index 85f79430..81f7e3df 100644 --- a/src/proofofwork.py +++ b/src/proofofwork.py @@ -73,7 +73,7 @@ def _doFastPoW(target, initialHash): return result[0], result[1] time.sleep(0.2) -def _doGPUPow(target, initialHash): +def _doGPUPoW(target, initialHash): print "GPU POW\n" nonce = openclpow.do_opencl_pow(initialHash.encode("hex"), target) trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8]) @@ -83,7 +83,12 @@ def _doGPUPow(target, initialHash): def run(target, initialHash): target = int(target) if shared.safeConfigGetBoolean('bitmessagesettings', 'opencl') and openclpow.has_opencl(): - return _doGPUPow(target, initialHash) +# trialvalue1, nonce1 = _doGPUPoW(target, initialHash) +# trialvalue, nonce = _doFastPoW(target, initialHash) +# print "GPU: %s, %s" % (trialvalue1, nonce1) +# print "Fast: %s, %s" % (trialvalue, nonce) +# return [trialvalue, nonce] + return _doGPUPoW(target, initialHash) elif frozen == "macosx_app" or not frozen: return _doFastPoW(target, initialHash) else: