def decode(self, server, block_header, target, job_id = None, extranonce2 = None): if block_header: job = Object() binary_data = block_header.decode('hex') data0 = np.zeros(64, np.uint32) data0 = np.insert(data0, [0] * 16, unpack('IIIIIIIIIIIIIIII', binary_data[:64])) job.target = np.array(unpack('IIIIIIII', target.decode('hex')), dtype=np.uint32) job.header = binary_data[:68] job.merkle_end = np.uint32(unpack('I', binary_data[64:68])[0]) job.time = np.uint32(unpack('I', binary_data[68:72])[0]) job.difficulty = np.uint32(unpack('I', binary_data[72:76])[0]) job.state = sha256(STATE, data0) job.f = np.zeros(8, np.uint32) job.state2 = partial(job.state, job.merkle_end, job.time, job.difficulty, job.f) job.targetQ = 2**256 / int(''.join(list(chunks(target, 2))[::-1]), 16) job.job_id = job_id job.extranonce2 = extranonce2 job.server = server calculateF(job.state, job.merkle_end, job.time, job.difficulty, job.f, job.state2) if job.difficulty != self.difficulty: self.set_difficulty(job.difficulty) return job
def mining_thread(self): say_line('started OpenCL miner on platform %d, device %d (%s)', (self.options.platform, self.device_index, self.device_name)) (self.defines, rate_divisor, hashspace) = if_else(self.vectors, ('-DVECTORS', 500, 0x7FFFFFFF), ('', 1000, 0xFFFFFFFF)) self.defines += (' -DOUTPUT_SIZE=' + str(self.output_size)) self.defines += (' -DOUTPUT_MASK=' + str(self.output_size - 1)) self.load_kernel() frame = 1.0 / max(self.frames, 3) unit = self.worksize * 256 global_threads = unit * 10 queue = cl.CommandQueue(self.context) last_rated_pace = last_rated = last_n_time = last_temperature = time() base = last_hash_rate = threads_run_pace = threads_run = 0 output = np.zeros(self.output_size + 1, np.uint32) output_buffer = cl.Buffer(self.context, cl.mem_flags.WRITE_ONLY | cl.mem_flags.USE_HOST_PTR, hostbuf=output) self.kernel.set_arg(20, output_buffer) work = None temperature = 0 while True: if self.should_stop: return sleep(self.frameSleep) if (not work) or (not self.work_queue.empty()): try: work = self.work_queue.get(True, 1) except Empty: continue else: if not work: continue nonces_left = hashspace state = work.state state2 = work.state2 f = work.f self.kernel.set_arg(0, state[0]) self.kernel.set_arg(1, state[1]) self.kernel.set_arg(2, state[2]) self.kernel.set_arg(3, state[3]) self.kernel.set_arg(4, state[4]) self.kernel.set_arg(5, state[5]) self.kernel.set_arg(6, state[6]) self.kernel.set_arg(7, state[7]) self.kernel.set_arg(8, state2[1]) self.kernel.set_arg(9, state2[2]) self.kernel.set_arg(10, state2[3]) self.kernel.set_arg(11, state2[5]) self.kernel.set_arg(12, state2[6]) self.kernel.set_arg(13, state2[7]) self.kernel.set_arg(15, f[0]) self.kernel.set_arg(16, f[1]) self.kernel.set_arg(17, f[2]) self.kernel.set_arg(18, f[3]) self.kernel.set_arg(19, f[4]) if temperature < self.cutoff_temp: self.kernel.set_arg(14, pack('I', base)) cl.enqueue_nd_range_kernel(queue, self.kernel, (global_threads,), (self.worksize,)) nonces_left -= global_threads threads_run_pace += global_threads threads_run += global_threads base = uint32(base + global_threads) else: threads_run_pace = 0 last_rated_pace = time() sleep(self.cutoff_interval) now = time() if self.adapterIndex != None: t = now - last_temperature if temperature >= self.cutoff_temp or t > 1: last_temperature = now with adl_lock: temperature = self.get_temperature() t = now - last_rated_pace if t > 1: rate = (threads_run_pace / t) / rate_divisor last_rated_pace = now; threads_run_pace = 0 r = last_hash_rate / rate if r < 0.9 or r > 1.1: global_threads = max(unit * int((rate * frame * rate_divisor) / unit), unit) last_hash_rate = rate t = now - last_rated if t > self.options.rate: self.update_rate(now, threads_run, t, work.targetQ, rate_divisor) last_rated = now; threads_run = 0 queue.finish() cl.enqueue_read_buffer(queue, output_buffer, output) queue.finish() if output[self.output_size]: result = Object() result.header = work.header result.merkle_end = work.merkle_end result.time = work.time result.difficulty = work.difficulty result.target = work.target result.state = np.array(state) result.nonces = np.array(output) result.job_id = work.job_id result.extranonce2 = work.extranonce2 result.server = work.server result.miner = self self.switch.put(result) output.fill(0) cl.enqueue_write_buffer(queue, output_buffer, output) if not self.switch.update_time: if nonces_left < 3 * global_threads * self.frames: self.update = True nonces_left += 0xFFFFFFFFFFFF elif 0xFFFFFFFFFFF < nonces_left < 0xFFFFFFFFFFFF: say_line('warning: job finished, %s is idle', self.id()) work = None elif now - last_n_time > 1: work.time = bytereverse(bytereverse(work.time) + 1) state2 = partial(state, work.merkle_end, work.time, work.difficulty, f) calculateF(state, work.merkle_end, work.time, work.difficulty, f, state2) self.kernel.set_arg(8, state2[1]) self.kernel.set_arg(9, state2[2]) self.kernel.set_arg(10, state2[3]) self.kernel.set_arg(11, state2[5]) self.kernel.set_arg(12, state2[6]) self.kernel.set_arg(13, state2[7]) self.kernel.set_arg(15, f[0]) self.kernel.set_arg(16, f[1]) self.kernel.set_arg(17, f[2]) self.kernel.set_arg(18, f[3]) self.kernel.set_arg(19, f[4]) last_n_time = now self.update_time_counter += 1 if self.update_time_counter >= self.switch.max_update_time: self.update = True self.update_time_counter = 1