Skip to content
Browse files

redent & fix imports

  • Loading branch information...
1 parent e62c0c1 commit 070f79675e2f955ce66a96420572d800078ac302 @forrestv committed Aug 2, 2011
View
6 Bennu/kernels/poclbm/BFIPatcher.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
+# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
# CFSworks <CFSworks@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -27,7 +27,7 @@ class BFIPatcher(object):
"""Patches .ELF files compiled for Evergreen GPUs; changes the microcode
so that any BYTE_ALIGN_INT instructions become BFI_INT.
"""
-
+
def __init__(self, interface):
self.interface = interface
@@ -119,4 +119,4 @@ def readELFSections(self, data):
return sections
except struct.error:
self.interface.debug('A struct.error occurred while reading ELF!')
- raise PatchError()
+ raise PatchError()
View
30 Bennu/kernels/poclbm/Midstate.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
+# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
# CFSworks <CFSworks@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,18 +23,18 @@
# Some SHA-256 constants...
K = [
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
- 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
- 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
- 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
- 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
- 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
- ]
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
+ 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
+ 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
+ 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
+ 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
+]
A0 = 0x6a09e667
B0 = 0xbb67ae85
@@ -94,7 +94,7 @@ def calculateMidstate(data, state=None, rounds=None):
s1 = rotateright(w[14],17) ^ rotateright(w[14],19) ^ (w[14] >> 10)
w.append(addu32(w[0], s0, w[9], s1))
w.pop(0)
-
+
if rounds is None:
a = addu32(a, A0)
b = addu32(b, B0)
@@ -105,4 +105,4 @@ def calculateMidstate(data, state=None, rounds=None):
g = addu32(g, G0)
h = addu32(h, H0)
- return struct.pack('<IIIIIIII', a, b, c, d, e, f, g, h)
+ return struct.pack('<IIIIIIII', a, b, c, d, e, f, g, h)
View
58 Bennu/kernels/poclbm/__init__.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
+# Copyright (C) 2011 by jedi95 <jedi95@gmail.com> and
# CFSworks <CFSworks@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -42,7 +42,7 @@ def _swap(s, l):
class KernelData(object):
- """This class is a container for all the data required for a single kernel
+ """This class is a container for all the data required for a single kernel
execution.
"""
@@ -56,7 +56,7 @@ def __init__(self, nonceRange, core, vectors, aggression):
nonceRange.midstate = calculateMidstate(_swap(nonceRange.data[:64], 4))
#print nonceRange.midstate.encode('hex')
data = np.array(
- unpack('IIII', _swap(nonceRange.unit.data[64:], 4)), dtype=np.uint32)
+ unpack('IIII', _swap(nonceRange.unit.data[64:], 4)), dtype=np.uint32)
# Vectors do twice the work per execution, so calculate accordingly...
rateDivisor = 2 if vectors else 1
@@ -111,8 +111,8 @@ def calculateF(self, data):
rotr(self.state2[5], 13) ^ rotr(self.state2[5], 22)) +
((self.state2[5] & self.state2[6]) | (self.state2[7] &
(self.state2[5] | self.state2[6]))))
-
-
+
+
class Kernel(object):
"""A Phoenix Miner-compatible kernel that uses the poclbm OpenCL kernel."""
'''
@@ -181,12 +181,12 @@ def f(*args, **kwargs):
# We need a QueueReader to efficiently provide our dedicated thread
# with work.
- #self.qr = QueueReader(self.core, lambda nr: self.preprocess(nr),
+ #self.qr = QueueReader(self.core, lambda nr: self.preprocess(nr),
# lambda x,y: self.size * 1 << self.loopExponent)
# The platform selection must be valid to mine.
platforms = cl.get_platforms()
-
+
if self.PLATFORM >= len(platforms) or \
(self.PLATFORM is None and len(platforms) > 1):
self.interface.log(
@@ -202,7 +202,7 @@ def f(*args, **kwargs):
return
elif self.PLATFORM is None:
self.PLATFORM = 0
-
+
devices = platforms[self.PLATFORM].get_devices()
print len(devices), self.DEVICE, self.DEVICE >= len(devices), (self.DEVICE is None and len(devices) > 1)
@@ -216,7 +216,7 @@ def f(*args, **kwargs):
for i,d in enumerate(devices):
self.interface.log(' [%d]\t%s' % (i, d.name), False, False)
-
+
# Since the device selection is invalid, we can't mine.
self.interface.fatal()
return
@@ -239,9 +239,9 @@ def f(*args, **kwargs):
self.output_buf = cl.Buffer(
self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR,
hostbuf=self.output)
-
- self.applyMeta()
+ self.applyMeta()
+
def applyMeta(self):
"""Apply any kernel-specific metadata."""
self.interface.setMeta('kernel', 'poclbm r%s' % self.REVISION)
@@ -296,14 +296,14 @@ def loadKernel(self, device):
# Finally, the actual work of loading the kernel...
try:
binary = open(fileName, 'rb')
- except IOError:
+ except IOError:
binary = None
try:
if binary is None:
self.kernel = cl.Program(
self.context, kernel).build(self.defines)
-
+
#apply BFI_INT if enabled
if self.BFI_INT:
#patch the binary output from the compiler
@@ -325,7 +325,7 @@ def loadKernel(self, device):
binaryData = binary.read()
self.kernel = cl.Program(
self.context, [device], [binaryData]).build(self.defines)
-
+
except cl.LogicError:
self.interface.fatal("Failed to compile OpenCL kernel!")
return
@@ -335,35 +335,35 @@ def loadKernel(self, device):
return
finally:
if binary: binary.close()
-
+
cl.unload_compiler()
# If the user didn't specify their own worksize, use the maxium
# supported by the device.
maxSize = self.kernel.search.get_work_group_info(
- cl.kernel_work_group_info.WORK_GROUP_SIZE, self.device)
+ cl.kernel_work_group_info.WORK_GROUP_SIZE, self.device)
if self.WORKSIZE is None:
self.WORKSIZE = maxSize
else:
if self.WORKSIZE > maxSize:
self.interface.log('Warning: Worksize exceeds the maximum of '
- + str(maxSize) + ', using default.')
+ + str(maxSize) + ', using default.')
if self.WORKSIZE < 1:
self.interface.log('Warning: Invalid worksize, using default.')
self.WORKSIZE = min(self.WORKSIZE, maxSize)
self.WORKSIZE = max(self.WORKSIZE, 1)
#if the worksize is not a power of 2, round down to the nearest one
- if (self.WORKSIZE & (self.WORKSIZE - 1)) != 0:
+ if (self.WORKSIZE & (self.WORKSIZE - 1)) != 0:
self.WORKSIZE = 1 << int(math.floor(math.log(X)/math.log(2)))
-
+
self.interface.setWorkFactor(self.WORKSIZE)
def updateIterations(self):
# Set up the number of internal iterations to run if FASTLOOP enabled
rate = self.core.getRate()
-
+
if not (rate <= 0):
#calculate the number of iterations to run
EXP = max(0, (math.log(rate)/math.log(2)) - (self.AGGRESSION - 8))
@@ -374,9 +374,9 @@ def updateIterations(self):
EXP = round(EXP)
else:
EXP = self.loopExponent
-
+
self.loopExponent = int(max(0, EXP))
-
+
def preprocess(self, nr):
if self.FASTLOOP:
self.updateIterations()
@@ -397,7 +397,7 @@ def postprocess(self, output, nr, got):
# Iterate over only the first OUTPUT_SIZE items. Exclude the last item
# which is a duplicate of the most recently-found nonce.
for i in xrange(self.OUTPUT_SIZE):
- if output[i]:
+ if output[i]:
data = nr.data[:76] + struct.pack(">I", output[i])
hash_ = int(hashlib.sha256(hashlib.sha256(data).digest()).digest()[::-1].encode('hex'), 16)
if hash_ <= nr.target:
@@ -412,15 +412,15 @@ def start(self, work_getter, solution_putter):
@defer.inlineCallbacks
def work():
while flag[0]:
-
+
work = work_getter()
if work is None:
print "Worker starved!"
yield sleep(1)
continue
data, i = work
-
+
self.kernel.search(
self.commandQueue, (data.size, ), (self.WORKSIZE, ),
data.state[0], data.state[1], data.state[2], data.state[3],
@@ -435,20 +435,20 @@ def work():
self.commandQueue, self.output_buf, self.output)
#self.commandQueue.finish()
yield threads.deferToThread(self.commandQueue.finish)
-
+
# The OpenCL code will flag the last item in the output buffer
# when it finds a valid nonce. If that's the case, send it to
# the main thread for postprocessing and clean the buffer
# for the next pass.
if self.output[self.OUTPUT_SIZE]:
self.postprocess(self.output.copy(), data.nr, solution_putter)
-
+
self.output.fill(0)
cl.enqueue_write_buffer(
self.commandQueue, self.output_buf, self.output)
#for data in res:
# solution_putter(data)
-
+
work()
return lambda: flag.__setitem__(0, False)
-
+
View
32 Bennu/main.py
@@ -1,9 +1,7 @@
import sys
-from twisted.internet import defer, reactor, task
-from twisted.python import log
+from twisted.internet import reactor, task
-from util import jsonrpc, deferral
from . import kernels, transports
def parse_args():
@@ -19,11 +17,11 @@ def parse_args():
return
miner_args2 = {}
for a in miner_args:
- k, v = a.split('=', 1)
- if k in miner_args2:
- print 'Duplicate argument %r, exiting!' % (k,)
- return
- miner_args2[k] = v
+ k, v = a.split('=', 1)
+ if k in miner_args2:
+ print 'Duplicate argument %r, exiting!' % (k,)
+ return
+ miner_args2[k] = v
kernel_name = miner_args2.pop('KERNEL', 'python')
return kernel_name, miner_args2, urls
@@ -39,16 +37,16 @@ def run():
work_done = [0, 0]
frames_done = [-1]
def get_work():
- for tp in tps:
- work = tp.get_work()
- if work is not None:
- work_done[0] += work_done[1]
- frames_done[0] += 1
- work_done[1] = work.size
- return work
+ for tp in tps:
+ work = tp.get_work()
+ if work is not None:
+ work_done[0] += work_done[1]
+ frames_done[0] += 1
+ work_done[1] = work.size
+ return work
def got_solution(data):
- for tp in tps: # XXX
- tp.send_solution(data)
+ for tp in tps: # XXX
+ tp.send_solution(data)
start_time = reactor.seconds()
kernel.start(get_work, got_solution)
View
2 Bennu/transports/http/__init__.py
@@ -59,7 +59,7 @@ def handle_response(self, r):
self.seen_identifiers.add(w.identifier)
self.current_identifier = w.identifier
self.work.add(self.preprocessor(w))
-
+
@defer.inlineCallbacks
def work_filler(self):
while True:
View
5 Bennu/transports/http/jsonrpc.py
@@ -1,10 +1,9 @@
from __future__ import division
-import base64
import json
from twisted.internet import defer
-from twisted.python import log, util
+from twisted.python import util
from twisted.web import client, error
def getPageAndHeaders(url, contextFactory=None, *args, **kwargs):
@@ -41,7 +40,7 @@ def __init__(self, url, timeout=5, headers={}):
def callRemote(self, method, *params, **kwargs):
if not set(['headers', 'receive_headers']).issuperset(kwargs.iterkeys()):
raise ValueError()
-
+
headers = util.InsensitiveDict({
'Content-Type': 'application/json',
})
View
161 Bennu/util/deferral.py
@@ -1,161 +0,0 @@
-from __future__ import division
-
-import random
-
-from twisted.internet import defer, reactor
-from twisted.python import failure, log
-
-def sleep(t):
- d = defer.Deferred()
- reactor.callLater(t, d.callback, None)
- return d
-
-def retry(message, delay):
- '''
- @retry('Error getting block:', 1)
- @defer.inlineCallbacks
- def get_block(hash):
- ...
- '''
-
- def retry2(func):
- @defer.inlineCallbacks
- def f(*args, **kwargs):
- while True:
- try:
- result = yield func(*args, **kwargs)
- except:
- log.err(None, message)
- yield sleep(delay)
- else:
- defer.returnValue(result)
- return f
- return retry2
-
-class ReplyMatcher(object):
- '''
- Converts request/got response interface to deferred interface
- '''
-
- def __init__(self, func, timeout=5):
- self.func = func
- self.timeout = timeout
- self.map = {}
-
- def __call__(self, id):
- self.func(id)
- uniq = random.randrange(2**256)
- df = defer.Deferred()
- def timeout():
- df, timer = self.map[id].pop(uniq)
- df.errback(failure.Failure(defer.TimeoutError()))
- if not self.map[id]:
- del self.map[id]
- self.map.setdefault(id, {})[uniq] = (df, reactor.callLater(self.timeout, timeout))
- return df
-
- def got_response(self, id, resp):
- if id not in self.map:
- return
- for df, timer in self.map.pop(id).itervalues():
- timer.cancel()
- df.callback(resp)
-
-class GenericDeferrer(object):
- '''
- Converts query with identifier/got response interface to deferred interface
- '''
-
- def __init__(self, max_id, func, timeout=5):
- self.max_id = max_id
- self.func = func
- self.timeout = timeout
- self.map = {}
-
- def __call__(self, *args, **kwargs):
- while True:
- id = random.randrange(self.max_id)
- if id not in self.map:
- break
- df = defer.Deferred()
- def timeout():
- self.map.pop(id)
- df.errback(failure.Failure(defer.TimeoutError()))
- timer = reactor.callLater(self.timeout, timeout)
- self.func(id, *args, **kwargs)
- self.map[id] = df, timer
- return df
-
- def got_response(self, id, resp):
- if id not in self.map:
- return
- df, timer = self.map.pop(id)
- timer.cancel()
- df.callback(resp)
-
-class NotNowError(Exception):
- pass
-
-class DeferredCacher(object):
- '''
- like memoize, but for functions that return Deferreds
-
- @DeferredCacher
- def f(x):
- ...
- return df
-
- @DeferredCacher.with_backing(bsddb.hashopen(...))
- def f(x):
- ...
- return df
- '''
-
- @classmethod
- def with_backing(cls, backing):
- return lambda func: cls(func, backing)
-
- def __init__(self, func, backing=None):
- if backing is None:
- backing = {}
-
- self.func = func
- self.backing = backing
- self.waiting = {}
-
- @defer.inlineCallbacks
- def __call__(self, key):
- if key in self.waiting:
- yield self.waiting[key]
-
- if key in self.backing:
- defer.returnValue(self.backing[key])
- else:
- self.waiting[key] = defer.Deferred()
- try:
- value = yield self.func(key)
- finally:
- self.waiting.pop(key).callback(None)
-
- self.backing[key] = value
- defer.returnValue(value)
-
- def call_now(self, key):
- if key in self.waiting:
- raise NotNowError()
-
- if key in self.backing:
- return self.backing[key]
- else:
- self.waiting[key] = defer.Deferred()
- def cb(value):
- self.backing[key] = value
- self.waiting.pop(key).callback(None)
- def eb(fail):
- self.waiting.pop(key).callback(None)
- print
- print 'Error when requesting noncached value:'
- fail.printTraceback()
- print
- self.func(key).addCallback(cb).addErrback(eb)
- raise NotNowError()

0 comments on commit 070f796

Please sign in to comment.
Something went wrong with that request. Please try again.